feat: Add jsonnet highlighting support (#36134)

Use new sourcegraph tree-sitter parser for jsonnet and use it to do
highlighting when configured with tree-sitter.
This commit is contained in:
TJ DeVries 2022-06-06 21:54:21 -04:00 committed by GitHub
parent 5560dc9699
commit 3fefbdf487
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 82 additions and 4 deletions

View File

@ -73,7 +73,8 @@ var engineConfig = syntaxEngineConfig{
var baseEngineConfig = syntaxEngineConfig{
Default: EngineSyntect,
Overrides: map[string]EngineType{
"c#": EngineTreeSitter,
"c#": EngineTreeSitter,
"jsonnet": EngineTreeSitter,
},
}
@ -107,6 +108,8 @@ func init() {
go func() {
conf.Watch(func() {
// Populate effective configuration with base configuration
// We have to add here to make sure that even if there is no config,
// we still update to use the defaults
engineConfig.Default = baseEngineConfig.Default
for name, engine := range baseEngineConfig.Overrides {
engineConfig.Overrides[name] = engine
@ -126,7 +129,16 @@ func init() {
}
// Set overrides from configuration
//
// We populate the confuration with base again, because we need to
// create a brand new map to not take any values that were
// previously in the table from the last configuration.
//
// After that, we set the values from the new configuration
engineConfig.Overrides = map[string]EngineType{}
for name, engine := range baseEngineConfig.Overrides {
engineConfig.Overrides[name] = engine
}
for name, engine := range config.SyntaxHighlighting.Engine.Overrides {
if overrideEngine, ok := engineNameToEngineType(engine); ok {
engineConfig.Overrides[strings.ToLower(name)] = overrideEngine
@ -180,9 +192,9 @@ func getLanguageFromConfig(config syntaxHighlightConfig, path string) (string, b
// getLanguage will return the name of the language and default back to enry if
// no language could be found.
func getLanguage(path string, contents string) (string, bool) {
ft, found := getLanguageFromConfig(highlightConfig, path)
lang, found := getLanguageFromConfig(highlightConfig, path)
if found {
return ft, true
return lang, true
}
return enry.GetLanguage(path, []byte(contents)), false

View File

@ -1698,6 +1698,7 @@ dependencies = [
"tree-sitter-c-sharp",
"tree-sitter-go",
"tree-sitter-highlight",
"tree-sitter-jsonnet",
"tree-sitter-sql",
]
@ -2206,6 +2207,15 @@ dependencies = [
"tree-sitter",
]
[[package]]
name = "tree-sitter-jsonnet"
version = "0.0.1"
source = "git+https://github.com/sourcegraph/tree-sitter-jsonnet#009e6f06266f46ae07077dd6c8026ded56ab7dd8"
dependencies = [
"cc",
"tree-sitter",
]
[[package]]
name = "tree-sitter-sql"
version = "0.0.2"

View File

@ -37,5 +37,7 @@ tree-sitter-sql = { git = "https://github.com/sourcegraph/tree-sitter-sql" }
# is picked by tree-sitter-c-sharp, whereas tree-sitter-go picks 0.20.x
tree-sitter-c-sharp = { git = "https://github.com/tree-sitter/tree-sitter-c-sharp" }
tree-sitter-jsonnet = { git = "https://github.com/sourcegraph/tree-sitter-jsonnet" }
[dev-dependencies]
insta = "1.11.0"

View File

@ -0,0 +1,53 @@
(id) @variable
(param identifier: (id) @variable.parameter)
(bind function: (id) @function)
(fieldname) @string.special
[
"["
"]"
"{"
"}"
] @punctuation.bracket
[ "error" "assert" ] @identifier.function
; keyword style
[
"if"
"then"
"else"
] @conditional
[
(local)
(super)
"function"
"for"
"in"
"import"
"importstr"
] @keyword
; Language basics
(comment) @comment
(number) @number
[ (true) (false) ] @boolean
[ (self) (dollar) ] @constant.builtin
(binaryop) @operator
(unaryop) @operator
; It's possible for us to give a "special" highlight to
; the triple ||| to start a string if we wanted with this query
;; ((string_start) @comment
;; (#eq? @comment "|||"))
[
(string_start)
(string_end)
] @string.special
(string_content) @string
; Imports
(import) @variable.module

View File

@ -100,7 +100,7 @@ macro_rules! create_configurations {
lazy_static::lazy_static! {
static ref CONFIGURATIONS: HashMap<&'static str, HighlightConfiguration> = {
create_configurations!( go, sql, c_sharp )
create_configurations!( go, sql, c_sharp, jsonnet )
};
}

View File

@ -110,6 +110,7 @@ var enryLanguageMappings = map[string]string{
var supportedFiletypes = map[string]struct{}{
"go": {},
"c_sharp": {},
"jsonnet": {},
}
// Client represents a client connection to a syntect_server.