Start with Objective-C

This commit is contained in:
Ted Nyman
2013-12-15 20:15:19 -08:00
parent b3c6c85387
commit 0626def699
2 changed files with 9 additions and 18 deletions

View File

@@ -6,33 +6,19 @@ module Linguist
# Public: Given an array of String language names, a # Public: Given an array of String language names, a
# apply all heuristics against the given data and return an array # apply all heuristics against the given data and return an array
# of matching languages, or nil. # of matching languages, or nil.
#
# data - Array of tokens or String data to analyze. # data - Array of tokens or String data to analyze.
# languages - Array of language name Strings to restrict to. # languages - Array of language name Strings to restrict to.
#
# Returns an array of language name Strings, or [] # Returns an array of language name Strings, or []
def self.find_by_heuristics(data, languages) def self.find_by_heuristics(data, languages)
if languages.all? { |l| ["pod", "perl"].include?(l.downcase) } if languages.all? { |l| ["Objective-C", "C++"].include?(l) }
disambiguate_pod(data, languages)
elsif languages.all? { |l| ["objective-c", "c++"].include?(l.downcase) }
disambiguate_h(data, languages) disambiguate_h(data, languages)
end end
end end
# Internal: Initialize a Heuristics class
def initialize
end
# .pod extensions are ambigious between perl and pod.
#
# Returns an array of still-possible languages, or nil
def self.disambiguate_pod(data, languages)
matches = []
matches << Language["Perl"] if data.include?("my $")
matches
end
# .h extensions are ambigious between C, C++, and Objective-C. # .h extensions are ambigious between C, C++, and Objective-C.
# We want to look for Objective-C. # We want to shortcut look for Objective-C.
def self.disambiguate_h(data, languages) def self.disambiguate_h(data, languages)
matches = [] matches = []
matches << Language["Objective-C"] if data.include?("@interface") matches << Language["Objective-C"] if data.include?("@interface")

View File

@@ -20,4 +20,9 @@ class TestHeuristcs < Test::Unit::TestCase
results = Heuristics.find_by_heuristics(fixture("Objective-C/StyleViewController.h"), languages) results = Heuristics.find_by_heuristics(fixture("Objective-C/StyleViewController.h"), languages)
assert_equal Language["Objective-C"], results.first assert_equal Language["Objective-C"], results.first
end end
def test_detect_still_works_if_nothing_matches
match = Language.detect("Hello.m", fixture("Objective-C/hello.m"))
assert_equal Language["Objective-C"], match
end
end end