-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Magik and PKl languages that are not handled by Lingu…
…ist (#790) Add fallbacks for languages not supported yet by linguist or go-enry
- Loading branch information
Showing
5 changed files
with
169 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// This file wraps the logic of go-enry (https://github.com/go-enry/go-enry) to support additional languages. | ||
// go-enry is based off of a package called Linguist (https://github.com/github/linguist) | ||
// and sometimes programming languages may not be supported by Linguist | ||
// or may take a while to get merged in and make it into go-enry. This wrapper | ||
// gives us flexibility to support languages in those cases. We list additional languages | ||
// in this file and remove them once they make it into Linguist and go-enry. | ||
// This logic is similar to what we have in the sourcegraph/sourcegraph repo, in the future | ||
// we plan to refactor both into a common library to share between the two repos. | ||
package languages | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/go-enry/go-enry/v2" | ||
) | ||
|
||
var unsupportedByLinguistAliasMap = map[string]string{ | ||
// Pkl Configuration Language (https://pkl-lang.org/) | ||
// Add to linguist on 6/7/24 | ||
// can remove once go-enry package updates | ||
// to that linguist version | ||
"pkl": "Pkl", | ||
// Magik Language | ||
"magik": "Magik", | ||
} | ||
|
||
var unsupportedByLinguistExtensionToNameMap = map[string]string{ | ||
// Pkl Configuration Language (https://pkl-lang.org/) | ||
".pkl": "Pkl", | ||
// Magik Language | ||
".magik": "Magik", | ||
} | ||
|
||
// getLanguagesByAlias is a replacement for enry.GetLanguagesByAlias | ||
// It supports languages that are missing in linguist | ||
func GetLanguageByAlias(alias string) (language string, ok bool) { | ||
language, ok = enry.GetLanguageByAlias(alias) | ||
if !ok { | ||
normalizedAlias := strings.ToLower(alias) | ||
language, ok = unsupportedByLinguistAliasMap[normalizedAlias] | ||
} | ||
|
||
return | ||
} | ||
|
||
// GetLanguage is a replacement for enry.GetLanguage | ||
// to find out the most probable language to return but includes support | ||
// for languages missing from linguist | ||
func GetLanguage(filename string, content []byte) (language string) { | ||
language = enry.GetLanguage(filename, content) | ||
|
||
// If go-enry failed to find language, fall back on our | ||
// internal check for languages missing in linguist | ||
if language == "" { | ||
ext := filepath.Ext(filename) | ||
normalizedExt := strings.ToLower(ext) | ||
if ext == "" { | ||
return | ||
} | ||
if lang, ok := unsupportedByLinguistExtensionToNameMap[normalizedExt]; ok { | ||
language = lang | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package languages | ||
|
||
import "testing" | ||
|
||
func TestGetLanguageByAlias(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
alias string | ||
want string | ||
wantOk bool | ||
}{ | ||
{ | ||
name: "empty alias", | ||
alias: "", | ||
want: "", | ||
wantOk: false, | ||
}, | ||
{ | ||
name: "unknown alias", | ||
alias: "unknown", | ||
want: "", | ||
wantOk: false, | ||
}, | ||
{ | ||
name: "supported alias", | ||
alias: "go", | ||
want: "Go", | ||
wantOk: true, | ||
}, | ||
{ | ||
name: "unsupported by linguist alias", | ||
alias: "magik", | ||
want: "Magik", | ||
wantOk: true, | ||
}, | ||
{ | ||
name: "unsupported by linguist alias normalized", | ||
alias: "mAgIk", | ||
want: "Magik", | ||
wantOk: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, ok := GetLanguageByAlias(tt.alias) | ||
if got != tt.want || ok != tt.wantOk { | ||
t.Errorf("GetLanguageByAlias(%q) = %q, %t, want %q, %t", tt.alias, got, ok, tt.want, tt.wantOk) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetLanguage(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
filename string | ||
content []byte | ||
want string | ||
}{ | ||
{ | ||
name: "empty filename", | ||
filename: "", | ||
content: []byte(""), | ||
want: "", | ||
}, | ||
{ | ||
name: "unknown extension", | ||
filename: "file.unknown", | ||
content: []byte(""), | ||
want: "", | ||
}, | ||
{ | ||
name: "supported extension", | ||
filename: "file.go", | ||
content: []byte("package main"), | ||
want: "Go", | ||
}, | ||
{ | ||
name: "unsupported by linguist extension", | ||
filename: "file.magik", | ||
content: []byte(""), | ||
want: "Magik", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := GetLanguage(tt.filename, tt.content) | ||
if got != tt.want { | ||
t.Errorf("GetLanguage(%q, %q) = %q, want %q", tt.filename, tt.content, got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters