Update DetectPrimaryLanguage to normalize "C/C++" and "C++" to "c" for consistent counting. Revise function comment and internal comments for clarity. Remove redundant comment on finding most common language.
53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package linter
|
|
|
|
import "strings"
|
|
|
|
// DetectPrimaryLanguage returns a clean language name without slashes or special chars.
|
|
func DetectPrimaryLanguage(files []string) string {
|
|
if len(files) == 0 {
|
|
return "unknown"
|
|
}
|
|
|
|
counts := make(map[string]int)
|
|
for _, file := range files {
|
|
lang, err := DetectLanguage(file)
|
|
if err == nil && lang != nil {
|
|
name := strings.ToLower(lang.Name)
|
|
// Clean the name: turn "C/C++" into "c"
|
|
if name == "c/c++" || name == "c++" {
|
|
name = "c"
|
|
}
|
|
counts[name]++
|
|
}
|
|
}
|
|
|
|
if len(counts) == 0 {
|
|
return "unknown"
|
|
}
|
|
|
|
var bestLang string
|
|
maxCount := -1
|
|
for lang, count := range counts {
|
|
if count > maxCount {
|
|
maxCount = count
|
|
bestLang = lang
|
|
}
|
|
}
|
|
|
|
// Bias toward Go in mixed repos, otherwise prefer "c" for C files
|
|
if counts["go"] > 0 && counts["go"] >= maxCount/2 {
|
|
return "go"
|
|
}
|
|
|
|
return bestLang
|
|
}
|
|
|
|
// SupportedLanguages returns all known languages (for future use or error messages).
|
|
func SupportedLanguages() []string {
|
|
var langs []string
|
|
for _, l := range languages {
|
|
langs = append(langs, strings.ToLower(l.Name))
|
|
}
|
|
return langs
|
|
}
|