fork of https://github.com/sourcegraph/zoekt
1// This file wraps the logic of go-enry (https://github.com/go-enry/go-enry) to support additional languages.
2// go-enry is based off of a package called Linguist (https://github.com/github/linguist)
3// and sometimes programming languages may not be supported by Linguist
4// or may take a while to get merged in and make it into go-enry. This wrapper
5// gives us flexibility to support languages in those cases. We list additional languages
6// in this file and remove them once they make it into Linguist and go-enry.
7// This logic is similar to what we have in the sourcegraph/sourcegraph repo, in the future
8// we plan to refactor both into a common library to share between the two repos.
9package languages
10
11import (
12 "path/filepath"
13 "strings"
14
15 "github.com/go-enry/go-enry/v2"
16)
17
18var unsupportedByLinguistAliasMap = map[string]string{
19 // Pkl Configuration Language (https://pkl-lang.org/)
20 // Add to linguist on 6/7/24
21 // can remove once go-enry package updates
22 // to that linguist version
23 "pkl": "Pkl",
24 // Magik Language
25 "magik": "Magik",
26}
27
28var unsupportedByLinguistExtensionToNameMap = map[string]string{
29 // Pkl Configuration Language (https://pkl-lang.org/)
30 ".pkl": "Pkl",
31 // Magik Language
32 ".magik": "Magik",
33}
34
35// getLanguagesByAlias is a replacement for enry.GetLanguagesByAlias
36// It supports languages that are missing in linguist
37func GetLanguageByAlias(alias string) (language string, ok bool) {
38 language, ok = enry.GetLanguageByAlias(alias)
39 if !ok {
40 normalizedAlias := strings.ToLower(alias)
41 language, ok = unsupportedByLinguistAliasMap[normalizedAlias]
42 }
43
44 return
45}
46
47// GetLanguage is a replacement for enry.GetLanguage
48// to find out the most probable language to return but includes support
49// for languages missing from linguist
50func GetLanguage(filename string, content []byte) (language string) {
51 language = enry.GetLanguage(filename, content)
52
53 // If go-enry failed to find language, fall back on our
54 // internal check for languages missing in linguist
55 if language == "" {
56 ext := filepath.Ext(filename)
57 normalizedExt := strings.ToLower(ext)
58 if ext == "" {
59 return
60 }
61 if lang, ok := unsupportedByLinguistExtensionToNameMap[normalizedExt]; ok {
62 language = lang
63 }
64 }
65 return
66}