···11+// This file wraps the logic of go-enry (https://github.com/go-enry/go-enry) to support additional languages.
22+// go-enry is based off of a package called Linguist (https://github.com/github/linguist)
33+// and sometimes programming languages may not be supported by Linguist
44+// or may take a while to get merged in and make it into go-enry. This wrapper
55+// gives us flexibility to support languages in those cases. We list additional languages
66+// in this file and remove them once they make it into Linguist and go-enry.
77+// This logic is similar to what we have in the sourcegraph/sourcegraph repo, in the future
88+// we plan to refactor both into a common library to share between the two repos.
99+package languages
1010+1111+import (
1212+ "path/filepath"
1313+ "strings"
1414+1515+ "github.com/go-enry/go-enry/v2"
1616+)
1717+1818+var unsupportedByLinguistAliasMap = map[string]string{
1919+ // Pkl Configuration Language (https://pkl-lang.org/)
2020+ // Add to linguist on 6/7/24
2121+ // can remove once go-enry package updates
2222+ // to that linguist version
2323+ "pkl": "Pkl",
2424+ // Magik Language
2525+ "magik": "Magik",
2626+}
2727+2828+var unsupportedByLinguistExtensionToNameMap = map[string]string{
2929+ // Pkl Configuration Language (https://pkl-lang.org/)
3030+ ".pkl": "Pkl",
3131+ // Magik Language
3232+ ".magik": "Magik",
3333+}
3434+3535+// getLanguagesByAlias is a replacement for enry.GetLanguagesByAlias
3636+// It supports languages that are missing in linguist
3737+func GetLanguageByAlias(alias string) (language string, ok bool) {
3838+ language, ok = enry.GetLanguageByAlias(alias)
3939+ if !ok {
4040+ normalizedAlias := strings.ToLower(alias)
4141+ language, ok = unsupportedByLinguistAliasMap[normalizedAlias]
4242+ }
4343+4444+ return
4545+}
4646+4747+// GetLanguage is a replacement for enry.GetLanguage
4848+// to find out the most probable language to return but includes support
4949+// for languages missing from linguist
5050+func GetLanguage(filename string, content []byte) (language string) {
5151+ language = enry.GetLanguage(filename, content)
5252+5353+ // If go-enry failed to find language, fall back on our
5454+ // internal check for languages missing in linguist
5555+ if language == "" {
5656+ ext := filepath.Ext(filename)
5757+ normalizedExt := strings.ToLower(ext)
5858+ if ext == "" {
5959+ return
6060+ }
6161+ if lang, ok := unsupportedByLinguistExtensionToNameMap[normalizedExt]; ok {
6262+ language = lang
6363+ }
6464+ }
6565+ return
6666+}