Skip to content

Commit

Permalink
split GetLanguage into GetLanguage and GetLanguages
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarmonaa committed Jun 15, 2017
1 parent beda5b7 commit bea1bc3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
1 change: 0 additions & 1 deletion classifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package enry

import (
"math"

"sort"

"gopkg.in/src-d/enry.v1/internal/tokenizer"
Expand Down
59 changes: 33 additions & 26 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,8 @@ var DefaultStrategies = []Strategy{

// GetLanguage applies a sequence of strategies based on the given filename and content
// to find out the most probably language to return.
func GetLanguage(filename string, content []byte) string {
var languages []string
candidates := []string{}
for _, strategy := range DefaultStrategies {
languages = strategy(filename, content, candidates)
if len(languages) == 1 {
return languages[0]
}

if len(languages) > 0 {
candidates = append(candidates, languages...)
}
}

func GetLanguage(filename string, content []byte) (language string) {
languages := GetLanguages(filename, content)
return firstLanguage(languages)
}

Expand All @@ -51,17 +39,6 @@ func firstLanguage(languages []string) string {
return languages[0]
}

func getLanguageByStrategy(strategy Strategy, filename string, content []byte, candidates []string) (string, bool) {
languages := strategy(filename, content, candidates)
return getFirstLanguageAndSafe(languages)
}

func getFirstLanguageAndSafe(languages []string) (language string, safe bool) {
language = firstLanguage(languages)
safe = len(languages) == 1
return
}

// GetLanguageByModeline returns detected language. If there are more than one possibles languages
// it returns the first language by alphabetically order and safe to false.
func GetLanguageByModeline(content []byte) (language string, safe bool) {
Expand Down Expand Up @@ -110,13 +87,43 @@ func GetLanguageByClassifier(content []byte, candidates []string) (language stri
return getLanguageByStrategy(GetLanguagesByClassifier, "", content, candidates)
}

func getLanguageByStrategy(strategy Strategy, filename string, content []byte, candidates []string) (string, bool) {
languages := strategy(filename, content, candidates)
return getFirstLanguageAndSafe(languages)
}

func getFirstLanguageAndSafe(languages []string) (language string, safe bool) {
language = firstLanguage(languages)
safe = len(languages) == 1
return
}

// GetLanguageBySpecificClassifier returns the most probably language for the given content using
// classifier to detect language.
func GetLanguageBySpecificClassifier(content []byte, candidates []string, classifier Classifier) (language string, safe bool) {
languages := GetLanguagesBySpecificClassifier(content, candidates, classifier)
return getFirstLanguageAndSafe(languages)
}

// GetLanguages applies a sequence of strategies based on the given filename and content
// to find out the most probably languages to return.
func GetLanguages(filename string, content []byte) []string {
var languages []string
candidates := []string{}
for _, strategy := range DefaultStrategies {
languages = strategy(filename, content, candidates)
if len(languages) == 1 {
return languages
}

if len(languages) > 0 {
candidates = append(candidates, languages...)
}
}

return languages
}

// GetLanguagesByModeline returns a slice of possible languages for the given content, filename will be ignored.
// It is comply with the signature to be a Strategy type.
func GetLanguagesByModeline(filename string, content []byte, candidates []string) []string {
Expand Down Expand Up @@ -242,7 +249,7 @@ func GetLanguagesByVimModeline(filename string, content []byte, candidates []str
// GetLanguagesByFilename returns a slice of possible languages for the given filename, content and candidates
// will be ignored. It is comply with the signature to be a Strategy type.
func GetLanguagesByFilename(filename string, content []byte, candidates []string) []string {
return languagesByFilename[filename]
return languagesByFilename[filepath.Base(filename)]
}

// GetLanguagesByShebang returns a slice of possible languages for the given content, filename and candidates
Expand Down
1 change: 1 addition & 0 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (s *SimpleLinguistTestSuite) TestGetLanguage() {
filename string
content []byte
expected string
safe bool
}{
{name: "TestGetLanguage_1", filename: "foo.py", content: []byte{}, expected: "Python"},
{name: "TestGetLanguage_2", filename: "foo.m", content: []byte(":- module"), expected: "Mercury"},
Expand Down

0 comments on commit bea1bc3

Please sign in to comment.