From bf1153cedd33b0e8f02ffcb8a270a9c26b584b9b Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 3 Mar 2022 12:12:18 -0800 Subject: [PATCH] Add syntax highlighting/auto-indent/outlines for JSON files Co-Authored-By: Nathan Sobo --- Cargo.lock | 11 +++++++++++ crates/zed/Cargo.toml | 1 + crates/zed/languages/c/brackets.scm | 3 +++ crates/zed/languages/json/brackets.scm | 3 +++ crates/zed/languages/json/config.toml | 7 +++++++ crates/zed/languages/json/highlights.scm | 12 ++++++++++++ crates/zed/languages/json/indents.scm | 2 ++ crates/zed/languages/json/outline.scm | 2 ++ crates/zed/src/language.rs | 17 +++++++++++++++++ 9 files changed, 58 insertions(+) create mode 100644 crates/zed/languages/c/brackets.scm create mode 100644 crates/zed/languages/json/brackets.scm create mode 100644 crates/zed/languages/json/config.toml create mode 100644 crates/zed/languages/json/highlights.scm create mode 100644 crates/zed/languages/json/indents.scm create mode 100644 crates/zed/languages/json/outline.scm diff --git a/Cargo.lock b/Cargo.lock index 8214dec7a4..92867463c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/crates/zed/Cargo.toml b/crates/zed/Cargo.toml index 2298963757..7b8f79380b 100644 --- a/crates/zed/Cargo.toml +++ b/crates/zed/Cargo.toml @@ -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" diff --git a/crates/zed/languages/c/brackets.scm b/crates/zed/languages/c/brackets.scm new file mode 100644 index 0000000000..9e8c9cd93c --- /dev/null +++ b/crates/zed/languages/c/brackets.scm @@ -0,0 +1,3 @@ +("[" @open "]" @close) +("{" @open "}" @close) +("\"" @open "\"" @close) diff --git a/crates/zed/languages/json/brackets.scm b/crates/zed/languages/json/brackets.scm new file mode 100644 index 0000000000..9e8c9cd93c --- /dev/null +++ b/crates/zed/languages/json/brackets.scm @@ -0,0 +1,3 @@ +("[" @open "]" @close) +("{" @open "}" @close) +("\"" @open "\"" @close) diff --git a/crates/zed/languages/json/config.toml b/crates/zed/languages/json/config.toml new file mode 100644 index 0000000000..782e818676 --- /dev/null +++ b/crates/zed/languages/json/config.toml @@ -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 }, +] diff --git a/crates/zed/languages/json/highlights.scm b/crates/zed/languages/json/highlights.scm new file mode 100644 index 0000000000..7788c8aecc --- /dev/null +++ b/crates/zed/languages/json/highlights.scm @@ -0,0 +1,12 @@ +(string) @string + +(pair + key: (string) @property) + +(number) @number + +[ + (true) + (false) + (null) +] @constant \ No newline at end of file diff --git a/crates/zed/languages/json/indents.scm b/crates/zed/languages/json/indents.scm new file mode 100644 index 0000000000..b7b2a2e767 --- /dev/null +++ b/crates/zed/languages/json/indents.scm @@ -0,0 +1,2 @@ +(array "]" @end) @indent +(object "}" @end) @indent diff --git a/crates/zed/languages/json/outline.scm b/crates/zed/languages/json/outline.scm new file mode 100644 index 0000000000..43e2743478 --- /dev/null +++ b/crates/zed/languages/json/outline.scm @@ -0,0 +1,2 @@ +(pair + key: (string (string_content) @name)) @item diff --git a/crates/zed/src/language.rs b/crates/zed/src/language.rs index f0311730d4..4285f5f311 100644 --- a/crates/zed/src/language.rs +++ b/crates/zed/src/language.rs @@ -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();