Class: Linguist::Heuristics

Inherits:
Object
  • Object
show all
Defined in:
lib/linguist/heuristics.rb

Overview

A collection of simple heuristics that can be used to better analyze languages.

Constant Summary collapse

ACTIVE =
false

Class Method Summary collapse

Class Method Details

.active?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/linguist/heuristics.rb', line 34

def self.active?
  !!ACTIVE
end

.disambiguate_h(data, languages) ⇒ Object

.h extensions are ambigious between C, C++, and Objective-C. We want to shortcut look for Objective-C and now C++ too!

Returns an array of Languages or [] TODO rename this method as we’re not strictly disambiguating between .h files here.



27
28
29
30
31
32
# File 'lib/linguist/heuristics.rb', line 27

def self.disambiguate_h(data, languages)
  matches = []
  matches << Language["Objective-C"] if data.include?("@interface")
  matches << Language["C++"] if data.include?("#include <cstdint>")
  matches
end

.find_by_heuristics(data, languages) ⇒ Object

Public: Given an array of String language names, apply heuristics against the given data and return an array of matching languages, or nil.

data - Array of tokens or String data to analyze. languages - Array of language name Strings to restrict to.

Returns an array of Languages or []



14
15
16
17
18
19
20
# File 'lib/linguist/heuristics.rb', line 14

def self.find_by_heuristics(data, languages)
  if active?
    if languages.all? { |l| ["Objective-C", "C++"].include?(l) }
      disambiguate_h(data, languages)
    end
  end
end