Add syntax highlighting/auto-indent/outlines for JSON files

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2022-03-03 12:12:18 -08:00
parent 4b0300daea
commit bf1153cedd
9 changed files with 58 additions and 0 deletions

11
Cargo.lock generated
View File

@ -5326,6 +5326,16 @@ dependencies = [
"tree-sitter",
]
[[package]]
name = "tree-sitter-json"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90b04c4e1a92139535eb9fca4ec8fa9666cc96b618005d3ae35f3c957fa92f92"
dependencies = [
"cc",
"tree-sitter",
]
[[package]]
name = "tree-sitter-markdown"
version = "0.0.1"
@ -5935,6 +5945,7 @@ dependencies = [
"toml",
"tree-sitter",
"tree-sitter-c",
"tree-sitter-json",
"tree-sitter-markdown",
"tree-sitter-rust",
"unindent",

View File

@ -92,6 +92,7 @@ tiny_http = "0.8"
toml = "0.5"
tree-sitter = "0.20.4"
tree-sitter-c = "0.20.1"
tree-sitter-json = "0.19.0"
tree-sitter-rust = "0.20.1"
tree-sitter-markdown = { git = "https://github.com/MDeiml/tree-sitter-markdown", rev = "330ecab87a3e3a7211ac69bbadc19eabecdb1cca" }
url = "2.2"

View File

@ -0,0 +1,3 @@
("[" @open "]" @close)
("{" @open "}" @close)
("\"" @open "\"" @close)

View File

@ -0,0 +1,3 @@
("[" @open "]" @close)
("{" @open "}" @close)
("\"" @open "\"" @close)

View File

@ -0,0 +1,7 @@
name = "JSON"
path_suffixes = ["json"]
brackets = [
{ start = "{", end = "}", close = true, newline = true },
{ start = "[", end = "]", close = true, newline = true },
{ start = "\"", end = "\"", close = true, newline = false },
]

View File

@ -0,0 +1,12 @@
(string) @string
(pair
key: (string) @property)
(number) @number
[
(true)
(false)
(null)
] @constant

View File

@ -0,0 +1,2 @@
(array "]" @end) @indent
(object "}" @end) @indent

View File

@ -0,0 +1,2 @@
(pair
key: (string (string_content) @name)) @item

View File

@ -429,6 +429,7 @@ pub fn build_language_registry() -> LanguageRegistry {
.join(".zed"),
);
languages.add(Arc::new(c()));
languages.add(Arc::new(json()));
languages.add(Arc::new(rust()));
languages.add(Arc::new(markdown()));
languages
@ -455,6 +456,8 @@ fn c() -> Language {
Language::new(config, Some(grammar))
.with_highlights_query(load_query("c/highlights.scm").as_ref())
.unwrap()
.with_brackets_query(load_query("c/brackets.scm").as_ref())
.unwrap()
.with_indents_query(load_query("c/indents.scm").as_ref())
.unwrap()
.with_outline_query(load_query("c/outline.scm").as_ref())
@ -462,6 +465,20 @@ fn c() -> Language {
.with_lsp_ext(CLsp)
}
fn json() -> Language {
let grammar = tree_sitter_json::language();
let config = toml::from_slice(&LanguageDir::get("json/config.toml").unwrap().data).unwrap();
Language::new(config, Some(grammar))
.with_highlights_query(load_query("json/highlights.scm").as_ref())
.unwrap()
.with_brackets_query(load_query("json/brackets.scm").as_ref())
.unwrap()
.with_indents_query(load_query("json/indents.scm").as_ref())
.unwrap()
.with_outline_query(load_query("json/outline.scm").as_ref())
.unwrap()
}
fn markdown() -> Language {
let grammar = tree_sitter_markdown::language();
let config = toml::from_slice(&LanguageDir::get("markdown/config.toml").unwrap().data).unwrap();