Skip to content

Commit

Permalink
Merge pull request #65 from matthewnitschke/fixed_function_highlighting
Browse files Browse the repository at this point in the history
Fixed function highlighting
  • Loading branch information
TimWhiting authored Apr 7, 2024
2 parents adf31d8 + ceedf12 commit 1aef5a6
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 14 deletions.
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

; 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))
(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)
"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

0 comments on commit 1aef5a6

Please sign in to comment.