Skip to content

Fixed function highlighting #65

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

Merged
Merged
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
48 changes: 38 additions & 10 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

; Methods
; --------------------
;; TODO: does not work
;(function_type
;name: (identifier) @method)
(super) @function

; TODO: add method/call_expression to grammar and
; distinguish method call from variable access
(function_expression_body
(identifier) @function)

; NOTE: This query is a bit of a work around for the fact that the dart grammar doesn't
; specifically identify a node as a function call
(((identifier) @function (#match? @function "^_?[a-z]"))
. (selector . (argument_part))) @function
Comment on lines +14 to +15
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the pattern both nvim and zed are utilizing to delineate between constructors and function calls


; Annotations
; --------------------
(annotation
Expand Down Expand Up @@ -87,13 +94,16 @@
(scoped_identifier
scope: (identifier) @type)
(function_signature
name: (identifier) @method)
name: (identifier) @function)
(getter_signature
(identifier) @method)
(identifier) @function)
(setter_signature
name: (identifier) @function)
(enum_declaration
name: (identifier) @type)
(enum_constant
name: (identifier) @method)
(type_identifier) @type
(void_type) @type

((scoped_identifier
scope: (identifier) @type
Expand All @@ -115,19 +125,36 @@
(inferred_type) @keyword

((identifier) @type
(#match? @type "^_?[A-Z]"))
(#match? @type "^_?[A-Z].*[a-z]"))

("Function" @type)
(void_type) @type

(this) @variable.builtin

; properties
; TODO: add method/call_expression to grammar and
; distinguish method call from variable access
(expression_statement
(selector
(unconditional_assignable_selector
(identifier) @function))
Copy link
Contributor Author

@matthewnitschke matthewnitschke Apr 6, 2024

Choose a reason for hiding this comment

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

For now, I'm not worrying about annotating the differences between @function and @function.method

probably will look at this later, if it's considered beneficial

(selector (argument_part (arguments)))
)
(expression_statement
(cascade_section
(cascade_selector (identifier) @function)
(argument_part (arguments))
)
)

(unconditional_assignable_selector
(identifier) @property)

(conditional_assignable_selector
(identifier) @property)

(cascade_section
(cascade_selector
(identifier) @property))

; assignments
(assignment_expression
left: (assignable_expression) @variable)
Expand Down Expand Up @@ -170,6 +197,7 @@
(const_builtin)
(part_of_builtin)
(rethrow_builtin)
(void_type)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

void is not really a type in dart, and most existing highlighting treats it as a keyword (thinking mostly the LSP syntax highlighting the vscode implements)

"abstract"
"as"
"async"
Expand Down
38 changes: 38 additions & 0 deletions test/highlight/functions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class SomeClass {
final str = '';
int get getter => 12;
void set setter(int value) {}
void method() => print('asdf');
// ^ function
}

String topLevelFn() => 'str';
// ^ function

extension SomeExtension on SomeClass {
void extensionMethod() => print('extension');
// ^ function
}

void main() {
final instance = SomeClass();
instance.str;
// ^ property
instance.getter;
// ^ property
instance.setter = 12;
// ^ property
instance.method();
// ^ function
topLevelFn();
// <- function
instance.extensionMethod();
// ^ function
instance
..method()
// ^ function
..str
// ^ property
..getter;
// ^ property
}
4 changes: 2 additions & 2 deletions test/highlight/keywords.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Other extends Foo {
final int b = 2;

void foo(covariant String test) {}
// <- type
// <- keyword
// ^ keyword
factory Other.something() => Other();
// <- keyword
Expand Down Expand Up @@ -121,7 +121,7 @@ void main() {
}

void foo() async {
// <- type
// <- keyword
// ^ keyword
await other('');
// <- keyword
Expand Down
3 changes: 1 addition & 2 deletions test/highlight/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class Person {

String getName() {
// <- type
// ^ method
// The above used to be 'function.method', not 'method'. Fix it?
// ^ function
return this.name;

return Material.DENIM;
Expand Down