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 // Extensions for the Apex programming language
20 // See https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dev_guide.htm
21 "apex": "Apex",
22 // Pkl Configuration Language (https://pkl-lang.org/)
23 // Add to linguist on 6/7/24
24 // can remove once go-enry package updates
25 // to that linguist version
26 "pkl": "Pkl",
27 // Magik Language
28 "magik": "Magik",
29}
30
31var unsupportedByLinguistExtensionToNameMap = map[string]string{
32 ".apex": "Apex",
33 ".apxt": "Apex",
34 ".apxc": "Apex",
35 ".cls": "Apex",
36 ".trigger": "Apex",
37 // Pkl Configuration Language (https://pkl-lang.org/)
38 ".pkl": "Pkl",
39 // Magik Language
40 ".magik": "Magik",
41}
42
43// getLanguagesByAlias is a replacement for enry.GetLanguagesByAlias
44// It supports languages that are missing in linguist
45func GetLanguageByAlias(alias string) (language string, ok bool) {
46 language, ok = enry.GetLanguageByAlias(alias)
47 if !ok {
48 normalizedAlias := strings.ToLower(alias)
49 language, ok = unsupportedByLinguistAliasMap[normalizedAlias]
50 }
51
52 return
53}
54
55// GetLanguage is a replacement for enry.GetLanguage
56// to find out the most probable language to return but includes support
57// for languages missing from linguist
58func GetLanguage(filename string, content []byte) (language string) {
59 language = enry.GetLanguage(filename, content)
60
61 // If go-enry failed to find language, fall back on our
62 // internal check for languages missing in linguist
63 if language == "" {
64 ext := filepath.Ext(filename)
65 normalizedExt := strings.ToLower(ext)
66 if ext == "" {
67 return
68 }
69 if lang, ok := unsupportedByLinguistExtensionToNameMap[normalizedExt]; ok {
70 language = lang
71 }
72 }
73 return
74}