on master help sign in
Import tree sitter queries.
commit71c06c11cb2f3831326ec10035b66ac6df075c8a authorBlaž Hrastnik <blaz@mxxn.io> dateThu 8 Apr 2021, 23:18 (6 years ago) parent52da68e
28 files changed+357−37
.gitmodules−3 Cargo.lock+2−3 helix-core/Cargo.toml+1−1 helix-core/src/syntax.rs+42−10 helix-syntax/Cargo.toml+1−1 helix-syntax/languages/tree-sitter-bash+1−1 helix-syntax/languages/tree-sitter-c+1−1 helix-syntax/languages/tree-sitter-c-sharp+1−1 helix-syntax/languages/tree-sitter-cpp+1−1 helix-syntax/languages/tree-sitter-css+1−1 helix-syntax/languages/tree-sitter-go+1−1 helix-syntax/languages/tree-sitter-html+1−1 helix-syntax/languages/tree-sitter-java+1−1 helix-syntax/languages/tree-sitter-javascript+1−1 helix-syntax/languages/tree-sitter-json+1−1 helix-syntax/languages/tree-sitter-julia+1−1 helix-syntax/languages/tree-sitter-php+1−1 helix-syntax/languages/tree-sitter-python+1−1 helix-syntax/languages/tree-sitter-ruby+1−1 helix-syntax/languages/tree-sitter-rust+1−1 helix-syntax/languages/tree-sitter-scala+1−1 helix-syntax/languages/tree-sitter-toml+1−1 helix-syntax/languages/tree-sitter-typescript+1−1 helix-syntax/nvim-treesitter−1 runtime/queries/c/highlights.scm+81 runtime/queries/cpp/highlights.scm+68 runtime/queries/rust/highlights.scm+135 runtime/queries/rust/injections.scm+9
.gitmodules−3
@@ -82,6 +82,3 @@
8282 path = helix-syntax/languages/tree-sitter-toml
8383 url = https://github.com/ikatyang/tree-sitter-toml
8484 shallow = true
85−[submodule "helix-syntax/nvim-treesitter"]
86− path = helix-syntax/nvim-treesitter
87− url = https://github.com/nvim-treesitter/nvim-treesitter
Cargo.lock+2−3
@@ -486,7 +486,6 @@ version = "0.1.0"
486486dependencies = [
487487 "anyhow",
488488 "helix-syntax",
489− "log",
490489 "once_cell",
491490 "regex",
492491 "ropey",
@@ -1227,9 +1226,9 @@ dependencies = [
12271226
12281227[[package]]
12291228name = "tree-sitter"
1230−version = "0.17.1"
1229+version = "0.19.3"
12311230source = "registry+https://github.com/rust-lang/crates.io-index"
1232−checksum = "d18dcb776d3affaba6db04d11d645946d34a69b3172e588af96ce9fecd20faac"
1231+checksum = "1f41201fed3db3b520405a9c01c61773a250d4c3f43e9861c14b2bb232c981ab"
12331232dependencies = [
12341233 "cc",
12351234 "regex",
helix-core/Cargo.toml+1−1
@@ -16,7 +16,7 @@ tendril = "0.4.2"
1616unicode-segmentation = "1.6"
1717unicode-width = "0.1"
1818# slab = "0.4.2"
19−tree-sitter = "0.17"
19+tree-sitter = "0.19"
2020once_cell = "1.4"
2121regex = "1"
2222
helix-core/src/syntax.rs+42−10
@@ -1,4 +1,4 @@
1−use crate::{Change, Rope, RopeSlice, Transaction};
1+use crate::{regex::Regex, Change, Rope, RopeSlice, Transaction};
22pub use helix_syntax::{get_language, get_language_name, Lang};
33
44use std::{
@@ -59,21 +59,53 @@ pub struct IndentationConfiguration {
5959 pub unit: String,
6060}
6161
62+fn read_query(language: &str, filename: &str) -> String {
63+ static INHERITS_REGEX: Lazy<Regex> =
64+ Lazy::new(|| Regex::new(r";+\s*inherits\s*:?\s*([a-z_,()]+)\s*").unwrap());
65+
66+ let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
67+
68+ let path = root
69+ .join("../runtime/queries")
70+ .join(language)
71+ .join(filename);
72+
73+ let query = std::fs::read_to_string(&path).unwrap_or_default();
74+
75+ // TODO: the collect() is not ideal
76+ let inherits = INHERITS_REGEX
77+ .captures_iter(&query)
78+ .flat_map(|captures| {
79+ captures[1]
80+ .split(',')
81+ .map(str::to_owned)
82+ .collect::<Vec<_>>()
83+ })
84+ .collect::<Vec<_>>();
85+
86+ if inherits.is_empty() {
87+ return query;
88+ }
89+
90+ let mut queries = inherits
91+ .iter()
92+ .map(|language| read_query(language, filename))
93+ .collect::<Vec<_>>();
94+
95+ queries.push(query);
96+
97+ queries.concat()
98+}
99+
62100impl LanguageConfiguration {
63101 pub fn highlight_config(&self, scopes: &[String]) -> Option<Arc<HighlightConfiguration>> {
64102 self.highlight_config
65103 .get_or_init(|| {
66− // let name = get_language_name(&self.language_id);
67−
68− let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
104+ let language = get_language_name(self.language_id).to_ascii_lowercase();
69105
70− let highlights_query =
71− std::fs::read_to_string(root.join(&self.path).join("queries/highlights.scm"))
72− .unwrap_or_default();
106+ let highlights_query = read_query(&language, "highlights.scm");
73107
74− let injections_query =
75− std::fs::read_to_string(root.join(&self.path).join("queries/injections.scm"))
76− .unwrap_or_default();
108+ let injections_query = read_query(&language, "injections.scm");
77109
78110 let locals_query = "";
79111
helix-syntax/Cargo.toml+1−1
@@ -7,7 +7,7 @@ edition = "2018"
77# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88
99[dependencies]
10−tree-sitter = "0.17"
10+tree-sitter = "0.19"
1111serde = { version = "1.0", features = ["derive"] }
1212
1313[build-dependencies]
helix-syntax/languages/tree-sitter-bash+1−1

Submodule changed.

helix-syntax/languages/tree-sitter-c+1−1

Submodule changed.

helix-syntax/languages/tree-sitter-c-sharp+1−1

Submodule changed.

helix-syntax/languages/tree-sitter-cpp+1−1

Submodule changed.

helix-syntax/languages/tree-sitter-css+1−1

Submodule changed.

helix-syntax/languages/tree-sitter-go+1−1

Submodule changed.

helix-syntax/languages/tree-sitter-html+1−1

Submodule changed.

helix-syntax/languages/tree-sitter-java+1−1

Submodule changed.

helix-syntax/languages/tree-sitter-javascript+1−1

Submodule changed.

helix-syntax/languages/tree-sitter-json+1−1

Submodule changed.

helix-syntax/languages/tree-sitter-julia+1−1

Submodule changed.

helix-syntax/languages/tree-sitter-php+1−1

Submodule changed.

helix-syntax/languages/tree-sitter-python+1−1

Submodule changed.

helix-syntax/languages/tree-sitter-ruby+1−1

Submodule changed.

helix-syntax/languages/tree-sitter-rust+1−1

Submodule changed.

helix-syntax/languages/tree-sitter-scala+1−1

Submodule changed.

helix-syntax/languages/tree-sitter-toml+1−1

Submodule changed.

helix-syntax/languages/tree-sitter-typescript+1−1

Submodule changed.

helix-syntax/nvim-treesitterdeleted−1

Submodule changed.

runtime/queries/c/highlights.scmnew file+81
@@ -0,0 +1,81 @@
1+"break" @keyword
2+"case" @keyword
3+"const" @keyword
4+"continue" @keyword
5+"default" @keyword
6+"do" @keyword
7+"else" @keyword
8+"enum" @keyword
9+"extern" @keyword
10+"for" @keyword
11+"if" @keyword
12+"inline" @keyword
13+"return" @keyword
14+"sizeof" @keyword
15+"static" @keyword
16+"struct" @keyword
17+"switch" @keyword
18+"typedef" @keyword
19+"union" @keyword
20+"volatile" @keyword
21+"while" @keyword
22+
23+"#define" @keyword
24+"#elif" @keyword
25+"#else" @keyword
26+"#endif" @keyword
27+"#if" @keyword
28+"#ifdef" @keyword
29+"#ifndef" @keyword
30+"#include" @keyword
31+(preproc_directive) @keyword
32+
33+"--" @operator
34+"-" @operator
35+"-=" @operator
36+"->" @operator
37+"=" @operator
38+"!=" @operator
39+"*" @operator
40+"&" @operator
41+"&&" @operator
42+"+" @operator
43+"++" @operator
44+"+=" @operator
45+"<" @operator
46+"==" @operator
47+">" @operator
48+"||" @operator
49+
50+"." @delimiter
51+";" @delimiter
52+
53+(string_literal) @string
54+(system_lib_string) @string
55+
56+(null) @constant
57+(number_literal) @number
58+(char_literal) @number
59+
60+(call_expression
61+ function: (identifier) @function)
62+(call_expression
63+ function: (field_expression
64+ field: (field_identifier) @function))
65+(function_declarator
66+ declarator: (identifier) @function)
67+(preproc_function_def
68+ name: (identifier) @function.special)
69+
70+(field_identifier) @property
71+(statement_identifier) @label
72+(type_identifier) @type
73+(primitive_type) @type
74+(sized_type_specifier) @type
75+
76+((identifier) @constant
77+ (#match? @constant "^[A-Z][A-Z\\d_]*$"))
78+
79+(identifier) @variable
80+
81+(comment) @comment
runtime/queries/cpp/highlights.scmnew file+68
@@ -0,0 +1,68 @@
1+; inherits: c
2+
3+; Functions
4+
5+(call_expression
6+ function: (scoped_identifier
7+ name: (identifier) @function))
8+
9+(template_function
10+ name: (identifier) @function)
11+
12+(template_method
13+ name: (field_identifier) @function)
14+
15+(template_function
16+ name: (scoped_identifier
17+ name: (identifier) @function))
18+
19+(function_declarator
20+ declarator: (scoped_identifier
21+ name: (identifier) @function))
22+
23+(function_declarator
24+ declarator: (scoped_identifier
25+ name: (identifier) @function))
26+
27+(function_declarator
28+ declarator: (field_identifier) @function)
29+
30+; Types
31+
32+((namespace_identifier) @type
33+ (#match? @type "^[A-Z]"))
34+
35+(auto) @type
36+
37+; Constants
38+
39+(this) @variable.builtin
40+(nullptr) @constant
41+
42+; Keywords
43+
44+"catch" @keyword
45+"class" @keyword
46+"constexpr" @keyword
47+"delete" @keyword
48+"explicit" @keyword
49+"final" @keyword
50+"friend" @keyword
51+"mutable" @keyword
52+"namespace" @keyword
53+"noexcept" @keyword
54+"new" @keyword
55+"override" @keyword
56+"private" @keyword
57+"protected" @keyword
58+"public" @keyword
59+"template" @keyword
60+"throw" @keyword
61+"try" @keyword
62+"typename" @keyword
63+"using" @keyword
64+"virtual" @keyword
65+
66+; Strings
67+
68+(raw_string_literal) @string
runtime/queries/rust/highlights.scmnew file+135
@@ -0,0 +1,135 @@
1+; Identifier conventions
2+
3+; Assume all-caps names are constants
4+((identifier) @constant
5+ (#match? @constant "^[A-Z][A-Z\\d_]+$'"))
6+
7+; Assume that uppercase names in paths are types
8+((scoped_identifier
9+ path: (identifier) @type)
10+ (#match? @type "^[A-Z]"))
11+((scoped_identifier
12+ path: (scoped_identifier
13+ name: (identifier) @type))
14+ (#match? @type "^[A-Z]"))
15+
16+; Assume other uppercase names are enum constructors
17+((identifier) @constructor
18+ (#match? @constructor "^[A-Z]"))
19+
20+; Function calls
21+
22+(call_expression
23+ function: (identifier) @function)
24+(call_expression
25+ function: (field_expression
26+ field: (field_identifier) @function.method))
27+(call_expression
28+ function: (scoped_identifier
29+ "::"
30+ name: (identifier) @function))
31+
32+(generic_function
33+ function: (identifier) @function)
34+(generic_function
35+ function: (scoped_identifier
36+ name: (identifier) @function))
37+(generic_function
38+ function: (field_expression
39+ field: (field_identifier) @function.method))
40+
41+(macro_invocation
42+ macro: (identifier) @function.macro
43+ "!" @function.macro)
44+
45+; Function definitions
46+
47+(function_item (identifier) @function)
48+(function_signature_item (identifier) @function)
49+
50+; Other identifiers
51+
52+(type_identifier) @type
53+(primitive_type) @type.builtin
54+(field_identifier) @property
55+
56+(line_comment) @comment
57+(block_comment) @comment
58+
59+"(" @punctuation.bracket
60+")" @punctuation.bracket
61+"[" @punctuation.bracket
62+"]" @punctuation.bracket
63+
64+(type_arguments
65+ "<" @punctuation.bracket
66+ ">" @punctuation.bracket)
67+(type_parameters
68+ "<" @punctuation.bracket
69+ ">" @punctuation.bracket)
70+
71+"::" @punctuation.delimiter
72+"." @punctuation.delimiter
73+";" @punctuation.delimiter
74+
75+(parameter (identifier) @variable.parameter)
76+
77+(lifetime (identifier) @label)
78+
79+"break" @keyword
80+"const" @keyword
81+"continue" @keyword
82+"default" @keyword
83+"dyn" @keyword
84+"else" @keyword
85+"enum" @keyword
86+"extern" @keyword
87+"fn" @keyword
88+"for" @keyword
89+"if" @keyword
90+"impl" @keyword
91+"in" @keyword
92+"let" @keyword
93+"let" @keyword
94+"loop" @keyword
95+"macro_rules!" @keyword
96+"match" @keyword
97+"mod" @keyword
98+"move" @keyword
99+"pub" @keyword
100+"ref" @keyword
101+"return" @keyword
102+"static" @keyword
103+"struct" @keyword
104+"trait" @keyword
105+"type" @keyword
106+"union" @keyword
107+"unsafe" @keyword
108+"use" @keyword
109+"where" @keyword
110+"while" @keyword
111+(mutable_specifier) @keyword
112+(use_list (self) @keyword)
113+(scoped_use_list (self) @keyword)
114+(scoped_identifier (self) @keyword)
115+(super) @keyword
116+
117+(self) @variable.builtin
118+
119+(char_literal) @string
120+(string_literal) @string
121+(raw_string_literal) @string
122+
123+(boolean_literal) @constant.builtin
124+(integer_literal) @constant.builtin
125+(float_literal) @constant.builtin
126+
127+(escape_sequence) @escape
128+
129+(attribute_item) @attribute
130+(inner_attribute_item) @attribute
131+
132+"as" @operator
133+"*" @operator
134+"&" @operator
135+"'" @operator
runtime/queries/rust/injections.scmnew file+9
@@ -0,0 +1,9 @@
1+((macro_invocation
2+ (token_tree) @injection.content)
3+ (#set! injection.language "rust")
4+ (#set! injection.include-children))
5+
6+((macro_rule
7+ (token_tree) @injection.content)
8+ (#set! injection.language "rust")
9+ (#set! injection.include-children))