From 5ecc4421d7df3dce927d698024a43e0fc1450af0 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 5 Jul 2011 09:34:33 -0500 Subject: [PATCH] Basic Matlab detection Fixes #15 --- lib/linguist/blob_helper.rb | 34 ++++++++++++++++++++++++++++++++++ test/test_blob.rb | 6 ++++++ 2 files changed, 40 insertions(+) diff --git a/lib/linguist/blob_helper.rb b/lib/linguist/blob_helper.rb index 538fe5c6..f18a9204 100644 --- a/lib/linguist/blob_helper.rb +++ b/lib/linguist/blob_helper.rb @@ -278,6 +278,9 @@ module Linguist # If its a header file (.h) try to guess the language header_language || + # If it's a .m file, try to guess the language + m_language || + # If it's a .r file, try to guess the language r_language || @@ -310,6 +313,37 @@ module Linguist end end + # Internal: Guess language of .m files. + # + # Objective-C heuristics: + # * Keywords + # + # Matlab heuristics: + # * Leading function keyword + # * "%" comments + # + # Returns a Language. + def m_language + return unless extname == '.m' + + # Objective-C keywords + if lines.grep(/^#import|@(interface|implementation|property|synthesize|end)/).any? + Language['Objective-C'] + + # File function + elsif lines.first.to_s =~ /^function / + Language['Matlab'] + + # Matlab comment + elsif lines.grep(/^%/).any? + Language['Matlab'] + + # Fallback to Objective-C, don't want any Matlab false positives + else + Language['Objective-C'] + end + end + # Internal: Guess language of .r files. # # Returns a Language. diff --git a/test/test_blob.rb b/test/test_blob.rb index 0a89031e..ff045d9c 100644 --- a/test/test_blob.rb +++ b/test/test_blob.rb @@ -234,6 +234,12 @@ class TestBlob < Test::Unit::TestCase assert_equal Language['R'], blob("hello-r.R").language assert_equal Language['Rebol'], blob("hello-rebol.r").language + # .m disambiguation + assert_equal Language['Objective-C'], blob("Foo.m").language + assert_equal Language['Objective-C'], blob("hello.m").language + assert_equal Language['Matlab'], blob("matlab_function.m").language + # assert_equal Language['Matlab'], blob("matlab_script.m").language + # ML assert_equal Language['OCaml'], blob("Foo.ml").language assert_equal Language['Standard ML'], blob("Foo.sig").language