2026-03-28 12:36:06 +00:00
|
|
|
package linter
|
|
|
|
|
|
2026-03-28 13:16:31 +00:00
|
|
|
import "strings"
|
2026-03-28 12:46:04 +00:00
|
|
|
|
2026-03-28 16:24:26 +00:00
|
|
|
// DetectPrimaryLanguage returns a clean language name without slashes or special chars.
|
2026-03-28 12:46:04 +00:00
|
|
|
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)
|
2026-03-28 16:24:26 +00:00
|
|
|
// Clean the name: turn "C/C++" into "c"
|
|
|
|
|
if name == "c/c++" || name == "c++" {
|
|
|
|
|
name = "c"
|
|
|
|
|
}
|
2026-03-28 12:46:04 +00:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 16:24:26 +00:00
|
|
|
// Bias toward Go in mixed repos, otherwise prefer "c" for C files
|
2026-03-28 12:46:04 +00:00
|
|
|
if counts["go"] > 0 && counts["go"] >= maxCount/2 {
|
|
|
|
|
return "go"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bestLang
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 13:16:31 +00:00
|
|
|
// SupportedLanguages returns all known languages (for future use or error messages).
|
2026-03-28 12:46:04 +00:00
|
|
|
func SupportedLanguages() []string {
|
|
|
|
|
var langs []string
|
|
|
|
|
for _, l := range languages {
|
|
|
|
|
langs = append(langs, strings.ToLower(l.Name))
|
|
|
|
|
}
|
|
|
|
|
return langs
|
|
|
|
|
}
|