-
-
Notifications
You must be signed in to change notification settings - Fork 139
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
Tree-sitter rolling fixes (February) #906
Changes from all commits
9829178
32c464b
69fb61e
3131464
5628d08
10ce3b1
cb7d4aa
e332953
c188a26
cc20d70
89686ce
82eac22
9b1e8e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
*.ts | ||
vendor |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
; PREPROCESSOR | ||
; ============ | ||
|
||
|
@@ -16,21 +17,48 @@ | |
(["#if" "#ifdef" "#ifndef" "#endif" "#elif" "#else" "#define" "#include"] @punctuation.definition.directive.c | ||
(#set! adjust.endAfterFirstMatchOf "^#")) | ||
|
||
|
||
; This will match if the more specific rules above haven't matched. The | ||
; anonymous nodes will match under ideal conditions, but might not be present | ||
; if the parser is flummoxed. | ||
; `preproc_directive` will be used when the parser doesn't recognize the | ||
; directive as one of the above. It's permissive; `#afdfafsdfdfad` would be | ||
; parsed as a `preproc_directive`. | ||
; | ||
; Hence this rule will match if the more specific rules above haven't matched. | ||
; The anonymous nodes will match under ideal conditions, but might not be | ||
; present even when they ought to be _if_ the parser is flummoxed; so this'll | ||
; sometimes catch `#ifdef` and others. | ||
((preproc_directive) @keyword.control.directive.c | ||
(#set! capture.shy true)) | ||
|
||
((preproc_ifdef | ||
(identifier) @entity.name.function.preprocessor.c | ||
(#match? @entity.name.function.preprocessor.c "[a-zA-Z_$][\\w$]*"))) | ||
((preproc_directive) @punctuation.definition.directive.c | ||
(#set! capture.shy true) | ||
(#set! adjust.endAfterFirstMatchOf "^#")) | ||
|
||
; Macro functions are definitely entities. | ||
(preproc_function_def | ||
(identifier) @entity.name.function.preprocessor.c | ||
(#set! capture.final true)) | ||
|
||
; Identifiers in macro definitions are definitely constants. | ||
((preproc_def | ||
name: (identifier) @constant.preprocessor.c)) | ||
|
||
; We can also safely treat identifiers as constants in `#ifdef`… | ||
((preproc_ifdef | ||
(identifier) @constant.preprocessor.c)) | ||
|
||
; …and `#if` and `#elif`… | ||
(preproc_if | ||
(binary_expression | ||
(identifier) @constant.preprocessor.c)) | ||
(preproc_elif | ||
(binary_expression | ||
(identifier) @constant.preprocessor.c)) | ||
|
||
; …and `#undef`. | ||
((preproc_call | ||
directive: (preproc_directive) @_IGNORE_ | ||
argument: (preproc_arg) @constant.preprocessor.c) | ||
(#eq? @_IGNORE_ "#undef")) | ||
|
||
(system_lib_string) @string.quoted.other.lt-gt.include.c | ||
((system_lib_string) @punctuation.definition.string.begin.c | ||
(#set! adjust.endAfterFirstMatchOf "^<")) | ||
|
@@ -48,6 +76,15 @@ | |
(#set! capture.final true)) | ||
|
||
(primitive_type) @support.storage.type.builtin.c | ||
|
||
; When the user has typed `#define FOO`, the macro injection thinks that `FOO` | ||
; is a type declaration (for some reason). This node structure seems to exist | ||
; only in that unusual and incorrect scenario, so we'll stop it from happening | ||
; so that it doesn't override the underlying `constant.other.c` scope. | ||
(translation_unit | ||
(type_identifier) @_IGNORE_ | ||
(#set! capture.final)) | ||
|
||
(type_identifier) @support.other.storage.type.c | ||
|
||
; These types are all reserved words; if we see an identifier with this name, | ||
|
@@ -133,27 +170,31 @@ | |
|
||
; The "x" in `int x;` | ||
(declaration | ||
declarator: (identifier) @variable.declaration.c) | ||
declarator: (identifier) @variable.other.declaration.c) | ||
Comment on lines
-136
to
+173
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we marking so many of these as I can guess it helps make sure rules in stylesheets actually apply, by adding this class on to the rule's selector to increase its specificity as needed, without resorting to |
||
|
||
; The "x" in `int x = y;` | ||
(init_declarator | ||
declarator: (identifier) @variable.declaration.c) | ||
declarator: (identifier) @variable.other.declaration.c) | ||
|
||
; The "x" in `SomeType *x;` | ||
; (Should work no matter how many pointers deep we are.) | ||
(pointer_declarator | ||
declarator: [(identifier) (field_identifier)] @variable.declaration.pointer.c | ||
declarator: [(identifier) (field_identifier)] @variable.other.declaration.pointer.c | ||
(#is? test.descendantOfType "declaration field_declaration")) | ||
|
||
; An array declarator: the "table" in `int table[4];` | ||
(array_declarator | ||
declarator: (identifier) @variable.other.declaration.c) | ||
|
||
; A member of a struct. | ||
(field_declaration | ||
(field_identifier) @variable.declaration.member.c) | ||
(field_identifier) @variable.other.declaration.member.c) | ||
|
||
; An attribute in a C99 struct designated initializer: | ||
; the "foo" in `MY_TYPE a = { .foo = true }; | ||
(initializer_pair | ||
(field_designator | ||
(field_identifier) @variable.declaration.member.c)) | ||
(field_identifier) @variable.other.declaration.member.c)) | ||
|
||
; (and the associated ".") | ||
(initializer_pair | ||
|
@@ -162,15 +203,15 @@ | |
|
||
(field_declaration | ||
(pointer_declarator | ||
(field_identifier) @variable.declaration.member.c)) | ||
(field_identifier) @variable.other.declaration.member.c)) | ||
|
||
(field_declaration | ||
(array_declarator | ||
(field_identifier) @variable.declaration.member.c)) | ||
(field_identifier) @variable.other.declaration.member.c)) | ||
|
||
(init_declarator | ||
(pointer_declarator | ||
(identifier) @variable.declaration.member.c)) | ||
(identifier) @variable.other.declaration.member.c)) | ||
|
||
; The "x" in `x = y;` | ||
(assignment_expression | ||
|
@@ -253,8 +294,19 @@ | |
(false) | ||
] @constant.language._TYPE_.c | ||
|
||
((identifier) @constant.c | ||
(#match? @constant.c "[_A-Z][_A-Z0-9]*$")) | ||
; Don't try to scope (e.g.) `int FOO = 1` as a constant when the user types `=` | ||
; but has not typed the value yet. | ||
(ERROR | ||
(identifier) @_IGNORE_ | ||
(#set! capture.final)) | ||
|
||
; In most languages we wouldn't be making the assumption that an all-caps | ||
; identifier should be treated as a constant. But those languages don't have | ||
; macro preprocessors. The convention is decently strong in C/C++ that all-caps | ||
; identifiers will refer to `#define`d things. | ||
((identifier) @constant.other.c | ||
(#match? @constant.other.c "^[_A-Z][_A-Z0-9]*$") | ||
(#set! capture.shy)) | ||
|
||
|
||
; COMMENTS | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Knowing the issue this is addressing, these comments are great and very readable, IMO. Even though I can't read |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I like the amount of comments in this, I sort of almost can follow along with this. (One of these days I need to read what I believe I heard you've written about how to write .scm files, so I know what's specifically going on in these. But I can sense the comments will come very much in handy then.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably because of how not-at-home I feel inside C/C++ as opposed to most of the other languages for which I've written
highlights.scm
files.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was gonna say, at risk of being mildly off-topic or tangenty: C seems really tricky based on reading through this (plus some stuff I'm dealing with outside of Pulsar lately that's in C...)