Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update C queries and grammar #3789

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions book/src/languages.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ These configuration keys are available:
| `config` | Language Server configuration |
| `grammar` | The tree-sitter grammar to use (defaults to the value of `name`) |
| `formatter` | The formatter for the language, it will take precedence over the lsp when defined. The formatter must be able to take the original file as input from stdin and write the formatted file to stdout |
| `append-error-node` | Whether to append a `(ERROR) @error` capture to the language query. (defaults to `true`) |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the option name, I think highlight-errors or something similar would be more intuitive.

I'm also curious whether this should be

  • language-specific vs editor-wide
  • on/off by default

IMO the error highlighting is pretty noisy when you're typing out a block but I'm not sure how others feel.

Copy link
Member Author

@kirawi kirawi Sep 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There doesn't seem to be a straightforward way to use the editor config. I could either pass it a parameter (requires changing a lot of calls), or find a way to reference the editor config within the highlighter config.

Edit: The latter wouldn't be possible because the editor config is defined in helix-view while this is in helix-core.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yeah good point. Using language config is ok then I think 👍


### Language Server configuration

Expand Down
15 changes: 12 additions & 3 deletions helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ fn default_timeout() -> u64 {
20
}

fn default_append_error_node() -> bool {
true
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Configuration {
pub language: Vec<LanguageConfiguration>,
Expand Down Expand Up @@ -115,6 +119,9 @@ pub struct LanguageConfiguration {
pub auto_pairs: Option<AutoPairs>,

pub rulers: Option<Vec<u16>>, // if set, override editor's rulers

#[serde(default = "default_append_error_node")]
pub append_error_node: bool,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -355,9 +362,11 @@ impl LanguageConfiguration {
fn initialize_highlight(&self, scopes: &[String]) -> Option<Arc<HighlightConfiguration>> {
let language = self.language_id.to_ascii_lowercase();

let highlights_query = read_query(&language, "highlights.scm");
// always highlight syntax errors
// highlights_query += "\n(ERROR) @error";
let mut highlights_query = read_query(&language, "highlights.scm");

if self.append_error_node {
highlights_query += "\n(ERROR) @error";
}

let injections_query = read_query(&language, "injections.scm");
let locals_query = read_query(&language, "locals.scm");
Expand Down
2 changes: 1 addition & 1 deletion languages.toml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ args = { console = "internalConsole", attachCommands = [ "platform select remote

[[grammar]]
name = "c"
source = { git = "https://github.com/tree-sitter/tree-sitter-c", rev = "f05e279aedde06a25801c3f2b2cc8ac17fac52ae" }
source = { git = "https://github.com/tree-sitter/tree-sitter-c", rev = "7175a6dd5fc1cee660dce6fe23f6043d75af424a" }

[[language]]
name = "cpp"
Expand Down
225 changes: 156 additions & 69 deletions runtime/queries/c/highlights.scm
Original file line number Diff line number Diff line change
@@ -1,72 +1,147 @@
(storage_class_specifier) @keyword.storage

"goto" @keyword
"register" @keyword
"break" @keyword
"case" @keyword
"continue" @keyword
"default" @keyword
"do" @keyword
"else" @keyword
"enum" @keyword
"extern" @keyword
"for" @keyword
"if" @keyword
"inline" @keyword
"return" @keyword
"sizeof" @keyword
"struct" @keyword
"switch" @keyword
"typedef" @keyword
"union" @keyword
"volatile" @keyword
"while" @keyword
"const" @keyword
[
"const"
"default"
"enum"
"extern"
"inline"
"static"
"struct"
"typedef"
"union"
"volatile"
"goto"
"register"
] @keyword

"sizeof" @keyword.operator
"return" @keyword.control.return

[
"while"
"for"
"do"
"continue"
"break"
] @keyword.control.repeat

[
"if"
"else"
"case"
"switch"
] @keyword.control.conditional
(conditional_expression [ "?" ":" ] @keyword.control.conditional)

[
"#define"
"#elif"
"#else"
"#endif"
"#if"
"#ifdef"
"#ifndef"
"#include"
(preproc_directive)
"#define"
"#include"
"#if"
"#ifdef"
"#ifndef"
"#else"
"#elif"
"#endif"
(preproc_directive)
] @keyword.directive

"--" @operator
"-" @operator
"-=" @operator
"->" @operator
"=" @operator
"!=" @operator
"*" @operator
"&" @operator
"&&" @operator
"+" @operator
"++" @operator
"+=" @operator
"<" @operator
"==" @operator
">" @operator
"||" @operator
">=" @operator
"<=" @operator

"." @punctuation.delimiter
";" @punctuation.delimiter

[(true) (false)] @constant.builtin.boolean

(enumerator) @type.enum.variant
[ ";" ":" "," ] @punctuation.delimiter
[ "(" ")" "[" "]" "{" "}"] @punctuation.bracket
"..." @punctuation.special

[
"="

"-"
"*"
"/"
"+"
"%"

"~"
"|"
"&"
"^"
"<<"
">>"

"->"
"."

"<"
"<="
">="
">"
"=="
"!="

"!"
"&&"
"||"

"-="
"+="
"*="
"/="
"%="
"|="
"&="
"^="
">>="
"<<="
"--"
"++"
] @operator

;; Make sure the comma operator is given a highlight group after the comma
;; punctuator so the operator is highlighted properly.
(comma_expression [ "," ] @operator)

[
(true)
(false)
] @constant.builtin.boolean

(string_literal) @string
(system_lib_string) @string
(system_lib_string) @string.special.path

(null) @constant
(number_literal) @constant.numeric.integer
(escape_sequence) @constant.character.escape
(char_literal) @constant.character
(number_literal) @constant.numeric
(null) @constant.builtin

[
(preproc_arg)
(preproc_defined)
] @function.macro

(statement_identifier) @label

[
(type_identifier)
(sized_type_specifier)
(type_descriptor)
] @type
(primitive_type) @type.builtin

(sizeof_expression value: (parenthesized_expression (identifier) @type))
(enumerator
name: (identifier) @type.enum.variant)

((identifier) @constant
(#match? @constant "^[A-Z][A-Z\\d_]*$"))

(case_statement
value: (identifier) @constant)

;; Preproc def / undef
(preproc_def
name: (_) @constant)
(preproc_call
directive: (preproc_directive) @_u
argument: (_) @constant
(#eq? @_u "#undef"))

(call_expression
function: (identifier) @function)
Expand All @@ -76,17 +151,29 @@
(function_declarator
declarator: (identifier) @function)
(preproc_function_def
name: (identifier) @function.special)
name: (identifier) @function.macro)

(field_identifier) @variable.other.member
(statement_identifier) @label
(type_identifier) @type
(primitive_type) @type
(sized_type_specifier) @type
(comment) @comment

((identifier) @constant
(#match? @constant "^[A-Z][A-Z\\d_]*$"))
;; Parameters
(parameter_declaration
declarator: (identifier) @variable.parameter)
(parameter_declaration
declarator: (pointer_declarator) @variable.parameter)
(preproc_params (identifier) @variable.parameter)

(identifier) @variable
[
"__attribute__"
"__cdecl"
"__clrcall"
"__stdcall"
"__fastcall"
"__thiscall"
"__vectorcall"
"_unaligned"
"__unaligned"
"__declspec"
(attribute_declaration)
] @attribute

(comment) @comment
(field_identifier) @variable.other.member
1 change: 0 additions & 1 deletion runtime/queries/cpon/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

(string) @string
(escape_sequence) @constant.character.escape
(ERROR) @error

"," @punctuation.delimiter
[
Expand Down
1 change: 0 additions & 1 deletion runtime/queries/dart/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,4 @@
(#match? @variable.builtin "^(abstract|as|covariant|deferred|dynamic|export|external|factory|Function|get|implements|import|interface|library|operator|mixin|part|set|static|typedef)$"))

; Error
(ERROR) @error

1 change: 0 additions & 1 deletion runtime/queries/dot/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,5 @@
(preproc)
] @comment

(ERROR) @error

(identifier) @variable
1 change: 0 additions & 1 deletion runtime/queries/fish/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,3 @@

;; Error

(ERROR) @error
1 change: 0 additions & 1 deletion runtime/queries/git-rebase/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@
"#" @punctuation.delimiter
(comment) @comment

(ERROR) @error
2 changes: 0 additions & 2 deletions runtime/queries/godot-resource/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,3 @@
] @punctuation.bracket

"=" @operator

(ERROR) @error
1 change: 0 additions & 1 deletion runtime/queries/gotmpl/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,3 @@
(nil) @constant.builtin

(comment) @comment
(ERROR) @error
1 change: 0 additions & 1 deletion runtime/queries/json/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

(string) @string
(escape_sequence) @constant.character.escape
(ERROR) @error

"," @punctuation.delimiter
[
Expand Down
1 change: 0 additions & 1 deletion runtime/queries/latex/highlights.scm
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
;; General syntax
(ERROR) @error

(command_name) @function
(caption
Expand Down
1 change: 0 additions & 1 deletion runtime/queries/lean/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@
(sorry) @error

;; Error
(ERROR) @error

; Variables
(identifier) @variable
1 change: 0 additions & 1 deletion runtime/queries/llvm-mir/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,3 @@
(float_keyword)
] @keyword

(ERROR) @error
1 change: 0 additions & 1 deletion runtime/queries/llvm/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,3 @@
"zeroinitializer"
] @constant.builtin

(ERROR) @error
1 change: 0 additions & 1 deletion runtime/queries/lua/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,3 @@
(identifier) @variable

;; Error
(ERROR) @error
1 change: 0 additions & 1 deletion runtime/queries/ocaml/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@

[(comment) (line_number_directive) (directive) (shebang)] @comment

(ERROR) @error

; Blanket highlights
; ------------------
Expand Down
1 change: 0 additions & 1 deletion runtime/queries/python/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,3 @@
(#match? @type.builtin
"^(BaseException|Exception|ArithmeticError|BufferError|LookupError|AssertionError|AttributeError|EOFError|FloatingPointError|GeneratorExit|ImportError|ModuleNotFoundError|IndexError|KeyError|KeyboardInterrupt|MemoryError|NameError|NotImplementedError|OSError|OverflowError|RecursionError|ReferenceError|RuntimeError|StopIteration|StopAsyncIteration|SyntaxError|IndentationError|TabError|SystemError|SystemExit|TypeError|UnboundLocalError|UnicodeError|UnicodeEncodeError|UnicodeDecodeError|UnicodeTranslateError|ValueError|ZeroDivisionError|EnvironmentError|IOError|WindowsError|BlockingIOError|ChildProcessError|ConnectionError|BrokenPipeError|ConnectionAbortedError|ConnectionRefusedError|ConnectionResetError|FileExistsError|FileNotFoundError|InterruptedError|IsADirectoryError|NotADirectoryError|PermissionError|ProcessLookupError|TimeoutError|Warning|UserWarning|DeprecationWarning|PendingDeprecationWarning|SyntaxWarning|RuntimeWarning|FutureWarning|ImportWarning|UnicodeWarning|BytesWarning|ResourceWarning)$"))

(ERROR) @error
1 change: 0 additions & 1 deletion runtime/queries/r/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,3 @@
(identifier) @variable

; Error
(ERROR) @error
Loading