Import tree sitter queries.

This commit is contained in:
Blaž Hrastnik 2021-04-08 23:18:25 +09:00
parent 52da68e49a
commit 71c06c11cb
28 changed files with 357 additions and 37 deletions

3
.gitmodules vendored
View File

@ -82,6 +82,3 @@
path = helix-syntax/languages/tree-sitter-toml
url = https://github.com/ikatyang/tree-sitter-toml
shallow = true
[submodule "helix-syntax/nvim-treesitter"]
path = helix-syntax/nvim-treesitter
url = https://github.com/nvim-treesitter/nvim-treesitter

5
Cargo.lock generated
View File

@ -486,7 +486,6 @@ version = "0.1.0"
dependencies = [
"anyhow",
"helix-syntax",
"log",
"once_cell",
"regex",
"ropey",
@ -1227,9 +1226,9 @@ dependencies = [
[[package]]
name = "tree-sitter"
version = "0.17.1"
version = "0.19.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d18dcb776d3affaba6db04d11d645946d34a69b3172e588af96ce9fecd20faac"
checksum = "1f41201fed3db3b520405a9c01c61773a250d4c3f43e9861c14b2bb232c981ab"
dependencies = [
"cc",
"regex",

View File

@ -16,7 +16,7 @@ tendril = "0.4.2"
unicode-segmentation = "1.6"
unicode-width = "0.1"
# slab = "0.4.2"
tree-sitter = "0.17"
tree-sitter = "0.19"
once_cell = "1.4"
regex = "1"

View File

@ -1,4 +1,4 @@
use crate::{Change, Rope, RopeSlice, Transaction};
use crate::{regex::Regex, Change, Rope, RopeSlice, Transaction};
pub use helix_syntax::{get_language, get_language_name, Lang};
use std::{
@ -59,21 +59,53 @@ pub struct IndentationConfiguration {
pub unit: String,
}
fn read_query(language: &str, filename: &str) -> String {
static INHERITS_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r";+\s*inherits\s*:?\s*([a-z_,()]+)\s*").unwrap());
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let path = root
.join("../runtime/queries")
.join(language)
.join(filename);
let query = std::fs::read_to_string(&path).unwrap_or_default();
// TODO: the collect() is not ideal
let inherits = INHERITS_REGEX
.captures_iter(&query)
.flat_map(|captures| {
captures[1]
.split(',')
.map(str::to_owned)
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
if inherits.is_empty() {
return query;
}
let mut queries = inherits
.iter()
.map(|language| read_query(language, filename))
.collect::<Vec<_>>();
queries.push(query);
queries.concat()
}
impl LanguageConfiguration {
pub fn highlight_config(&self, scopes: &[String]) -> Option<Arc<HighlightConfiguration>> {
self.highlight_config
.get_or_init(|| {
// let name = get_language_name(&self.language_id);
let language = get_language_name(self.language_id).to_ascii_lowercase();
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let highlights_query = read_query(&language, "highlights.scm");
let highlights_query =
std::fs::read_to_string(root.join(&self.path).join("queries/highlights.scm"))
.unwrap_or_default();
let injections_query =
std::fs::read_to_string(root.join(&self.path).join("queries/injections.scm"))
.unwrap_or_default();
let injections_query = read_query(&language, "injections.scm");
let locals_query = "";

View File

@ -7,7 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tree-sitter = "0.17"
tree-sitter = "0.19"
serde = { version = "1.0", features = ["derive"] }
[build-dependencies]

@ -1 +1 @@
Subproject commit 8ece09ca4c0b5e59b124cd19fa92c76b1a9e9dd4
Subproject commit a8eb5cb57c66f74c63ab950de081207cccf52017

@ -1 +1 @@
Subproject commit fa408bc9e77f4b770bd1db984ca00c901ddf95fc
Subproject commit f05e279aedde06a25801c3f2b2cc8ac17fac52ae

@ -1 +1 @@
Subproject commit 21ec3c3deb4365085aa353fadbc6a616d7769f9f
Subproject commit 53a65a908167d6556e1fcdb67f1ee62aac101dda

@ -1 +1 @@
Subproject commit 3bfe046f3967fef92ebb33f8cd58c3ff373d5e56
Subproject commit c61212414a3e95b5f7507f98e83de1d638044adc

@ -1 +1 @@
Subproject commit e882c98b5e62d864f7f9e4d855b19b6050c897a8
Subproject commit 94e10230939e702b4fa3fa2cb5c3bc7173b95d07

@ -1 +1 @@
Subproject commit dadfd9c9aab2630632e61cfce645c13c35aa092f
Subproject commit 2a83dfdd759a632651f852aa4dc0af2525fae5cd

@ -1 +1 @@
Subproject commit 7f442e1c6163d450c69c75c7a621badc3a0ea98f
Subproject commit d93af487cc75120c89257195e6be46c999c6ba18

@ -1 +1 @@
Subproject commit 16c07a726c34c9925b3e28716b2d6d60e3643252
Subproject commit bd6186c24d5eb13b4623efac9d944dcc095c0dad

@ -1 +1 @@
Subproject commit 452151f7192cc26b70fe4a7ec192a37d99854a6a
Subproject commit 4a95461c4761c624f2263725aca79eeaefd36cad

@ -1 +1 @@
Subproject commit d3976b27df8622ed17bef6dd5e358b398e73c676
Subproject commit 65bceef69c3b0f24c0b19ce67d79f57c96e90fcb

@ -1 +1 @@
Subproject commit 6a0863f1ce3fcf6f99dc0addb7886dcbd27c5a48
Subproject commit 0ba7a24b062b671263ae08e707e9e94383b25bb7

@ -1 +1 @@
Subproject commit b0c0367d4b7058921fdc4ba11e257441a64ab809
Subproject commit 0d63eaf94e8d6c0694551b016c802787e61b3fb2

@ -1 +1 @@
Subproject commit 3196e288650992bca2399dda15ac703c342a22bb
Subproject commit d6210ceab11e8d812d4ab59c07c81458ec6e5184

@ -1 +1 @@
Subproject commit 32cd5a04adb4accb0c121f037ab59df3c3488228
Subproject commit dfff673b41df7fadcbb609c6338f38da3cdd018e

@ -1 +1 @@
Subproject commit 1be73c282bb6d9fd2fbb55945b313f9527cad060
Subproject commit a360da0a29a19c281d08295a35ecd0544d2da211

@ -1 +1 @@
Subproject commit 211bb726bb5857f872247b600c7c1808e641a8d4
Subproject commit fb23ed9a99da012d86b7a5059b9d8928607cce29

@ -1 +1 @@
Subproject commit 084da152d85cb8c4bbbe0ab5f3f1f9e5bfb77d3c
Subproject commit 7cff70bbcbbc62001b465603ca1ea88edd668704

@ -1 +1 @@
Subproject commit 543cbe44f16189f7f1b739cf268d70f373d94b87
Subproject commit 3e897ea5925f037cfae2e551f8e6b12eec2a201a

@ -1 +0,0 @@
Subproject commit 1f00ecdfa36ef5e43a4feaf189e8c2c003118c00

View File

@ -0,0 +1,81 @@
"break" @keyword
"case" @keyword
"const" @keyword
"continue" @keyword
"default" @keyword
"do" @keyword
"else" @keyword
"enum" @keyword
"extern" @keyword
"for" @keyword
"if" @keyword
"inline" @keyword
"return" @keyword
"sizeof" @keyword
"static" @keyword
"struct" @keyword
"switch" @keyword
"typedef" @keyword
"union" @keyword
"volatile" @keyword
"while" @keyword
"#define" @keyword
"#elif" @keyword
"#else" @keyword
"#endif" @keyword
"#if" @keyword
"#ifdef" @keyword
"#ifndef" @keyword
"#include" @keyword
(preproc_directive) @keyword
"--" @operator
"-" @operator
"-=" @operator
"->" @operator
"=" @operator
"!=" @operator
"*" @operator
"&" @operator
"&&" @operator
"+" @operator
"++" @operator
"+=" @operator
"<" @operator
"==" @operator
">" @operator
"||" @operator
"." @delimiter
";" @delimiter
(string_literal) @string
(system_lib_string) @string
(null) @constant
(number_literal) @number
(char_literal) @number
(call_expression
function: (identifier) @function)
(call_expression
function: (field_expression
field: (field_identifier) @function))
(function_declarator
declarator: (identifier) @function)
(preproc_function_def
name: (identifier) @function.special)
(field_identifier) @property
(statement_identifier) @label
(type_identifier) @type
(primitive_type) @type
(sized_type_specifier) @type
((identifier) @constant
(#match? @constant "^[A-Z][A-Z\\d_]*$"))
(identifier) @variable
(comment) @comment

View File

@ -0,0 +1,68 @@
; inherits: c
; Functions
(call_expression
function: (scoped_identifier
name: (identifier) @function))
(template_function
name: (identifier) @function)
(template_method
name: (field_identifier) @function)
(template_function
name: (scoped_identifier
name: (identifier) @function))
(function_declarator
declarator: (scoped_identifier
name: (identifier) @function))
(function_declarator
declarator: (scoped_identifier
name: (identifier) @function))
(function_declarator
declarator: (field_identifier) @function)
; Types
((namespace_identifier) @type
(#match? @type "^[A-Z]"))
(auto) @type
; Constants
(this) @variable.builtin
(nullptr) @constant
; Keywords
"catch" @keyword
"class" @keyword
"constexpr" @keyword
"delete" @keyword
"explicit" @keyword
"final" @keyword
"friend" @keyword
"mutable" @keyword
"namespace" @keyword
"noexcept" @keyword
"new" @keyword
"override" @keyword
"private" @keyword
"protected" @keyword
"public" @keyword
"template" @keyword
"throw" @keyword
"try" @keyword
"typename" @keyword
"using" @keyword
"virtual" @keyword
; Strings
(raw_string_literal) @string

View File

@ -0,0 +1,135 @@
; Identifier conventions
; Assume all-caps names are constants
((identifier) @constant
(#match? @constant "^[A-Z][A-Z\\d_]+$'"))
; Assume that uppercase names in paths are types
((scoped_identifier
path: (identifier) @type)
(#match? @type "^[A-Z]"))
((scoped_identifier
path: (scoped_identifier
name: (identifier) @type))
(#match? @type "^[A-Z]"))
; Assume other uppercase names are enum constructors
((identifier) @constructor
(#match? @constructor "^[A-Z]"))
; Function calls
(call_expression
function: (identifier) @function)
(call_expression
function: (field_expression
field: (field_identifier) @function.method))
(call_expression
function: (scoped_identifier
"::"
name: (identifier) @function))
(generic_function
function: (identifier) @function)
(generic_function
function: (scoped_identifier
name: (identifier) @function))
(generic_function
function: (field_expression
field: (field_identifier) @function.method))
(macro_invocation
macro: (identifier) @function.macro
"!" @function.macro)
; Function definitions
(function_item (identifier) @function)
(function_signature_item (identifier) @function)
; Other identifiers
(type_identifier) @type
(primitive_type) @type.builtin
(field_identifier) @property
(line_comment) @comment
(block_comment) @comment
"(" @punctuation.bracket
")" @punctuation.bracket
"[" @punctuation.bracket
"]" @punctuation.bracket
(type_arguments
"<" @punctuation.bracket
">" @punctuation.bracket)
(type_parameters
"<" @punctuation.bracket
">" @punctuation.bracket)
"::" @punctuation.delimiter
"." @punctuation.delimiter
";" @punctuation.delimiter
(parameter (identifier) @variable.parameter)
(lifetime (identifier) @label)
"break" @keyword
"const" @keyword
"continue" @keyword
"default" @keyword
"dyn" @keyword
"else" @keyword
"enum" @keyword
"extern" @keyword
"fn" @keyword
"for" @keyword
"if" @keyword
"impl" @keyword
"in" @keyword
"let" @keyword
"let" @keyword
"loop" @keyword
"macro_rules!" @keyword
"match" @keyword
"mod" @keyword
"move" @keyword
"pub" @keyword
"ref" @keyword
"return" @keyword
"static" @keyword
"struct" @keyword
"trait" @keyword
"type" @keyword
"union" @keyword
"unsafe" @keyword
"use" @keyword
"where" @keyword
"while" @keyword
(mutable_specifier) @keyword
(use_list (self) @keyword)
(scoped_use_list (self) @keyword)
(scoped_identifier (self) @keyword)
(super) @keyword
(self) @variable.builtin
(char_literal) @string
(string_literal) @string
(raw_string_literal) @string
(boolean_literal) @constant.builtin
(integer_literal) @constant.builtin
(float_literal) @constant.builtin
(escape_sequence) @escape
(attribute_item) @attribute
(inner_attribute_item) @attribute
"as" @operator
"*" @operator
"&" @operator
"'" @operator

View File

@ -0,0 +1,9 @@
((macro_invocation
(token_tree) @injection.content)
(#set! injection.language "rust")
(#set! injection.include-children))
((macro_rule
(token_tree) @injection.content)
(#set! injection.language "rust")
(#set! injection.include-children))