From 0990c2868d1a7b25f6614fe00ba5d379945a328e Mon Sep 17 00:00:00 2001 From: Alfredo Beaumont Date: Tue, 8 Aug 2017 11:58:02 +0200 Subject: [PATCH 1/2] common: Add filename parameter to GetLanguageByContent function Internal code needs the filename to select a matcher, so not passing any filename means no language will ever be found otherwise. --- common.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common.go b/common.go index 937285a..0799d1e 100644 --- a/common.go +++ b/common.go @@ -85,8 +85,8 @@ func GetLanguageByExtension(filename string) (language string, safe bool) { // GetLanguageByContent returns detected language. If there are more than one possibles languages // it returns the first language by alphabetically order and safe to false. -func GetLanguageByContent(content []byte) (language string, safe bool) { - return getLanguageByStrategy(GetLanguagesByContent, "", content, nil) +func GetLanguageByContent(filename string, content []byte) (language string, safe bool) { + return getLanguageByStrategy(GetLanguagesByContent, filename, content, nil) } // GetLanguageByClassifier returns the most probably language detected for the given content. It uses From c9122aad9fbe37ba919fad2eb4e91e26f2a3b13f Mon Sep 17 00:00:00 2001 From: Alfredo Beaumont Date: Tue, 8 Aug 2017 14:58:01 +0200 Subject: [PATCH 2/2] common: Use underscore as parameter name for unused parameters --- common.go | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/common.go b/common.go index 0799d1e..2e2013b 100644 --- a/common.go +++ b/common.go @@ -136,9 +136,9 @@ func GetLanguages(filename string, content []byte) []string { return languages } -// GetLanguagesByModeline returns a slice of possible languages for the given content, filename will be ignored. +// GetLanguagesByModeline returns a slice of possible languages for the given content. // It complies with the signature to be a Strategy type. -func GetLanguagesByModeline(filename string, content []byte, candidates []string) []string { +func GetLanguagesByModeline(_ string, content []byte, candidates []string) []string { headFoot := getHeaderAndFooter(content) var languages []string for _, getLang := range modelinesFunc { @@ -196,9 +196,9 @@ var ( reVimLang = regexp.MustCompile(`(?i:filetype|ft|syntax)\s*=(\w+)(?:\s|:|$)`) ) -// GetLanguagesByEmacsModeline returns a slice of possible languages for the given content, filename and candidates -// will be ignored. It complies with the signature to be a Strategy type. -func GetLanguagesByEmacsModeline(filename string, content []byte, candidates []string) []string { +// GetLanguagesByEmacsModeline returns a slice of possible languages for the given content. +// It complies with the signature to be a Strategy type. +func GetLanguagesByEmacsModeline(_ string, content []byte, _ []string) []string { matched := reEmacsModeline.FindAllSubmatch(content, -1) if matched == nil { return nil @@ -222,9 +222,9 @@ func GetLanguagesByEmacsModeline(filename string, content []byte, candidates []s return []string{language} } -// GetLanguagesByVimModeline returns a slice of possible languages for the given content, filename and candidates -// will be ignored. It complies with the signature to be a Strategy type. -func GetLanguagesByVimModeline(filename string, content []byte, candidates []string) []string { +// GetLanguagesByVimModeline returns a slice of possible languages for the given content. +// It complies with the signature to be a Strategy type. +func GetLanguagesByVimModeline(_ string, content []byte, _ []string) []string { matched := reVimModeline.FindAllSubmatch(content, -1) if matched == nil { return nil @@ -258,15 +258,15 @@ func GetLanguagesByVimModeline(filename string, content []byte, candidates []str return []string{language} } -// GetLanguagesByFilename returns a slice of possible languages for the given filename, content and candidates -// will be ignored. It complies with the signature to be a Strategy type. -func GetLanguagesByFilename(filename string, content []byte, candidates []string) []string { +// GetLanguagesByFilename returns a slice of possible languages for the given filename. +// It complies with the signature to be a Strategy type. +func GetLanguagesByFilename(filename string, _ []byte, _ []string) []string { return data.LanguagesByFilename[filepath.Base(filename)] } -// GetLanguagesByShebang returns a slice of possible languages for the given content, filename and candidates -// will be ignored. It complies with the signature to be a Strategy type. -func GetLanguagesByShebang(filename string, content []byte, candidates []string) (languages []string) { +// GetLanguagesByShebang returns a slice of possible languages for the given content. +// It complies with the signature to be a Strategy type. +func GetLanguagesByShebang(_ string, content []byte, _ []string) (languages []string) { interpreter := getInterpreter(content) return data.LanguagesByInterpreter[interpreter] } @@ -346,9 +346,9 @@ func lookForMultilineExec(data []byte) string { return interpreter } -// GetLanguagesByExtension returns a slice of possible languages for the given filename, content and candidates -// will be ignored. It complies with the signature to be a Strategy type. -func GetLanguagesByExtension(filename string, content []byte, candidates []string) []string { +// GetLanguagesByExtension returns a slice of possible languages for the given filename. +// It complies with the signature to be a Strategy type. +func GetLanguagesByExtension(filename string, _ []byte, _ []string) []string { if !strings.Contains(filename, ".") { return nil } @@ -377,9 +377,9 @@ func getDotIndexes(filename string) []int { return dots } -// GetLanguagesByContent returns a slice of possible languages for the given content, filename and candidates -// will be ignored. It complies with the signature to be a Strategy type. -func GetLanguagesByContent(filename string, content []byte, candidates []string) []string { +// GetLanguagesByContent returns a slice of possible languages for the given content. +// It complies with the signature to be a Strategy type. +func GetLanguagesByContent(filename string, content []byte, _ []string) []string { ext := strings.ToLower(filepath.Ext(filename)) fnMatcher, ok := data.ContentMatchers[ext] if !ok {