mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Guess .h language
This commit is contained in:
@@ -269,8 +269,12 @@ module Linguist
|
|||||||
# Returns a Language object
|
# Returns a Language object
|
||||||
def language
|
def language
|
||||||
if text?
|
if text?
|
||||||
# First see if there is a Language for the extension
|
# If its a header file (.h) try to guess the language
|
||||||
if Language.find_by_extension(extname)
|
if language = header_language
|
||||||
|
language
|
||||||
|
|
||||||
|
# See if there is a Language for the extension
|
||||||
|
elsif Language.find_by_extension(extname)
|
||||||
pathname.language
|
pathname.language
|
||||||
|
|
||||||
# Try to detect Language from shebang line
|
# Try to detect Language from shebang line
|
||||||
@@ -293,6 +297,21 @@ module Linguist
|
|||||||
language.lexer
|
language.lexer
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Internal: Guess language of header files (.h).
|
||||||
|
#
|
||||||
|
# Returns a Language.
|
||||||
|
def header_language
|
||||||
|
return unless extname == '.h'
|
||||||
|
|
||||||
|
if lines.grep(/^@(interface|property|private|public|end)/).any?
|
||||||
|
Language['Objective-C']
|
||||||
|
elsif lines.grep(/^class |^\s+(public|protected|private):/).any?
|
||||||
|
Language['C++']
|
||||||
|
else
|
||||||
|
Language['C']
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Internal: Extract the script name from the shebang line
|
# Internal: Extract the script name from the shebang line
|
||||||
#
|
#
|
||||||
# Requires Blob#data
|
# Requires Blob#data
|
||||||
|
|||||||
8
test/fixtures/blob/Foo.h
vendored
Normal file
8
test/fixtures/blob/Foo.h
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#import <Foundation/Foundation.h>
|
||||||
|
|
||||||
|
|
||||||
|
@interface Foo : NSObject {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
6
test/fixtures/blob/Foo.m
vendored
Normal file
6
test/fixtures/blob/Foo.m
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#import "Foo.h"
|
||||||
|
|
||||||
|
|
||||||
|
@implementation Foo
|
||||||
|
|
||||||
|
@end
|
||||||
10
test/fixtures/blob/FooAppDelegate.h
vendored
Normal file
10
test/fixtures/blob/FooAppDelegate.h
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#import <Cocoa/Cocoa.h>
|
||||||
|
|
||||||
|
@interface FooAppDelegate : NSObject <NSApplicationDelegate> {
|
||||||
|
@private
|
||||||
|
NSWindow *window;
|
||||||
|
}
|
||||||
|
|
||||||
|
@property (assign) IBOutlet NSWindow *window;
|
||||||
|
|
||||||
|
@end
|
||||||
12
test/fixtures/blob/FooAppDelegate.m
vendored
Normal file
12
test/fixtures/blob/FooAppDelegate.m
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#import "FooAppDelegate.h"
|
||||||
|
|
||||||
|
@implementation FooAppDelegate
|
||||||
|
|
||||||
|
@synthesize window;
|
||||||
|
|
||||||
|
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
|
||||||
|
{
|
||||||
|
// Insert code here to initialize your application
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
10
test/fixtures/blob/bar.h
vendored
Normal file
10
test/fixtures/blob/bar.h
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
class Bar
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
|
||||||
|
char *name;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
void hello();
|
||||||
|
}
|
||||||
10
test/fixtures/blob/bar.hpp
vendored
Normal file
10
test/fixtures/blob/bar.hpp
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
class Bar
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
|
||||||
|
char *name;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
void hello();
|
||||||
|
}
|
||||||
6
test/fixtures/blob/hello.c
vendored
Normal file
6
test/fixtures/blob/hello.c
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
printf("Hello World\n");
|
||||||
|
}
|
||||||
8
test/fixtures/blob/hello.cpp
vendored
Normal file
8
test/fixtures/blob/hello.cpp
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
cout << "Hello World" << endl;
|
||||||
|
}
|
||||||
6
test/fixtures/blob/hello.h
vendored
Normal file
6
test/fixtures/blob/hello.h
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#ifndef HELLO_H
|
||||||
|
#define HELLO_H
|
||||||
|
|
||||||
|
void hello();
|
||||||
|
|
||||||
|
#endif
|
||||||
7
test/fixtures/blob/hello.m
vendored
Normal file
7
test/fixtures/blob/hello.m
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#import <Cocoa/Cocoa.h>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
NSLog(@"Hello, World!\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -180,10 +180,20 @@ class TestBlob < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_language
|
def test_language
|
||||||
assert_equal Language['Ruby'], blob("foo.rb").language
|
assert_equal Language['Ruby'], blob("foo.rb").language
|
||||||
assert_equal Language['Ruby'], blob("script.rb").language
|
assert_equal Language['Ruby'], blob("script.rb").language
|
||||||
assert_equal Language['Text'], blob("octocat.png").language
|
assert_equal Language['Text'], blob("octocat.png").language
|
||||||
assert_equal Language['Ruby'], blob("wrong_shebang.rb").language
|
assert_equal Language['Ruby'], blob("wrong_shebang.rb").language
|
||||||
|
assert_equal Language['C'], blob("hello.c").language
|
||||||
|
assert_equal Language['C'], blob("hello.h").language
|
||||||
|
assert_equal Language['C++'], blob("hello.cpp").language
|
||||||
|
assert_equal Language['C++'], blob("bar.h").language
|
||||||
|
assert_equal Language['C++'], blob("bar.hpp").language
|
||||||
|
assert_equal Language['Objective-C'], blob("hello.m").language
|
||||||
|
assert_equal Language['Objective-C'], blob("Foo.m").language
|
||||||
|
assert_equal Language['Objective-C'], blob("Foo.h").language
|
||||||
|
assert_equal Language['Objective-C'], blob("FooAppDelegate.m").language
|
||||||
|
assert_equal Language['Objective-C'], blob("FooAppDelegate.h").language
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_lexer
|
def test_lexer
|
||||||
|
|||||||
Reference in New Issue
Block a user