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

Make parameter.outer delete the preceeding comma in python, dart, rust, c/c++, and some other changes #87

Merged
merged 15 commits into from
Jul 20, 2021
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
10 changes: 8 additions & 2 deletions queries/c/textobjects.scm
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@
(_) @statement.outer)

((parameter_list
(parameter_declaration) @parameter.inner . ","? @_end)
"," @_start . (parameter_declaration) @parameter.inner)
Copy link
Member

Choose a reason for hiding this comment

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

You can write the @parameter.inner in another query, so we don't have duplicates and they also match functions with one parameter. Same for the other queries.

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 possible, although I found another way to fix single parameter lists. If we do extract parameter.inner to a separate query then that would essentially be three queries for every kind of parameter. having 2 already feels unnecessary but I dont know how to do it more elegantly

Copy link
Member

Choose a reason for hiding this comment

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

I think having three queries is more explicit, and we don't have to worry about always trying to capture the inner parameter, and that's one less query for treesitter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We always have to capture the @parameter.inner symbol as part of constructing the @parameter.outer range anwyay. (unless there is a way to do this more directly that I am not familiar with?)
It would just be s/@parameter.inner/@_par and adding a third query for the parameter inner.

If there are performance implications it would be interesting to check, is there anyway to benchmark this?

Single parameter lists are fixed anyway

Copy link
Member

Choose a reason for hiding this comment

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

It would just be s/@parameter.inner/@_par and adding a third query for the parameter inner.

Yeah, that's more explicit and would avoid bugs when changing those queries, like the one that was included.

If there are performance implications it would be interesting to check, is there anyway to benchmark this?

Don't know how to benchmark, but I think these would just generate two matches for parameter.inner instead of just one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i see how it would be more explicit but I don't see how it would avoid bugs? if you wanted to change which nodes are parameters you would have to change 3 places most likely, (right now you have to change two places which is not great either)

Copy link
Member

Choose a reason for hiding this comment

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

if you wanted to change which nodes are parameters you would have to change 3 places most likely

but you won't need to worry about changing or breaking parameter.inner, that one would always work, the other queries will only need to change @_end or @_start for parameter.outer

(#make-range! "parameter.outer" @_start @parameter.inner))
((parameter_list
. (parameter_declaration) @parameter.inner . ","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))

((argument_list
(_) @parameter.inner . ","? @_end)
"," @_start . (_) @parameter.inner)
(#make-range! "parameter.outer" @_start @parameter.inner))
((argument_list
. (_) @parameter.inner . ","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))
31 changes: 29 additions & 2 deletions queries/cpp/textobjects.scm
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,39 @@
(template_declaration
(class_specifier) @class.outer) @class.outer.start

((lambda_capture_specifier
"," @_start . (_) @parameter.inner)
(#make-range! "parameter.outer" @_start @parameter.inner))
((lambda_capture_specifier
. (_) @parameter.inner . ","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))

((template_argument_list
"," @_start . (_) @parameter.inner)
(#make-range! "parameter.outer" @_start @parameter.inner))
((template_argument_list
. (_) @parameter.inner . ","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))

((template_parameter_list
"," @_start . (_) @parameter.inner)
(#make-range! "parameter.outer" @_start @parameter.inner))
((template_parameter_list
. (_) @parameter.inner . ","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))

((parameter_list
(optional_parameter_declaration) @parameter.inner . ","? @_end)
"," @_start . (optional_parameter_declaration) @parameter.inner)
(#make-range! "parameter.outer" @_start @parameter.inner))
((parameter_list
. (optional_parameter_declaration) @parameter.inner . ","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))

((initializer_list
(_) @parameter.inner . ","? @_end)
"," @_start . (_) @parameter.inner @_end)
(#make-range! "parameter.outer" @_start @parameter.inner))
((initializer_list
. (_) @parameter.inner . ","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))

(new_expression
Expand Down
25 changes: 21 additions & 4 deletions queries/dart/textobjects.scm
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,33 @@
(type_alias (function_type)? @function.inner) @function.outer

; parameter
([
[
(formal_parameter)
(normal_parameter_type)
(type_parameter)
] @parameter.inner . ","? @_end
(#make-range! "parameter.outer" @parameter.inner @_end))
] @parameter.inner
(
"," @_start . [
(formal_parameter)
(normal_parameter_type)
(type_parameter)
] @_par
(#make-range! "parameter.outer" @_start @_par))
(
[
(formal_parameter)
(normal_parameter_type)
(type_parameter)
] @_par . "," @_end
(#make-range! "parameter.outer" @_par @_end))

;; TODO: (_)* not supported yet -> for now this works correctly only with simple arguments
((arguments
(_) @parameter.inner . ","? @_end)
. (_) @parameter.inner . ","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))
((arguments
"," @_start . (_) @parameter.inner)
(#make-range! "parameter.outer" @_start @parameter.inner))

; call
(expression_statement
Expand Down
156 changes: 137 additions & 19 deletions queries/python/textobjects.scm
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,146 @@
(call (_) @call.inner)

;; Parameters

((parameters
([(identifier)
(tuple)
(typed_parameter)
(default_parameter)
(typed_default_parameter)
(list_splat)
(dictionary_splat)] @parameter.inner)
. ","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))
"," @_start .
[
(identifier)
(tuple)
(typed_parameter)
(default_parameter)
(typed_default_parameter)
(dictionary_splat_pattern)
(list_splat_pattern)
] @parameter.inner
)
(#make-range! "parameter.outer" @_start @parameter.inner))

((parameters
. [
(identifier)
(tuple)
(typed_parameter)
(default_parameter)
(typed_default_parameter)
(dictionary_splat_pattern)
(list_splat_pattern)
] @parameter.inner
. ","? @_end
)
(#make-range! "parameter.outer" @parameter.inner @_end)
)

((lambda_parameters
[(identifier)
(tuple)
(typed_parameter)
(default_parameter)
(typed_default_parameter)
(list_splat)
(dictionary_splat)] @parameter.inner
. ","? @_end)
"," @_start .
[
(identifier)
(tuple)
(typed_parameter)
(default_parameter)
(typed_default_parameter)
(dictionary_splat_pattern)
(list_splat_pattern)
] @parameter.inner
)
(#make-range! "parameter.outer" @_start @parameter.inner))

((lambda_parameters
. [
(identifier)
(tuple)
(typed_parameter)
(default_parameter)
(typed_default_parameter)
(dictionary_splat_pattern)
(list_splat_pattern)
] @parameter.inner
. ","? @_end
)
(#make-range! "parameter.outer" @parameter.inner @_end))

((tuple
"," @_start .
[
(identifier)
(tuple)
(typed_parameter)
(default_parameter)
(typed_default_parameter)
(dictionary_splat_pattern)
(list_splat_pattern)
] @parameter.inner
)
(#make-range! "parameter.outer" @_start @parameter.inner)
)

((tuple
"(" .
[
(identifier)
(tuple)
(typed_parameter)
(default_parameter)
(typed_default_parameter)
(dictionary_splat_pattern)
(list_splat_pattern)
] @parameter.inner
. ","? @_end
)
(#make-range! "parameter.outer" @parameter.inner @_end)
)

((list
"," @_start .
[
(identifier)
(tuple)
(typed_parameter)
(default_parameter)
(typed_default_parameter)
(dictionary_splat_pattern)
(list_splat_pattern)
] @parameter.inner
)
(#make-range! "parameter.outer" @_start @parameter.inner)
)

((list
. [
(identifier)
(tuple)
(typed_parameter)
(default_parameter)
(typed_default_parameter)
(dictionary_splat_pattern)
(list_splat_pattern)
] @parameter.inner
. ","? @_end
)
(#make-range! "parameter.outer" @parameter.inner @_end))

((dictionary
. (pair) @parameter.inner
. ","? @_end
)
(#make-range! "parameter.outer" @parameter.inner @_end))

((dictionary
"," @_start .
(pair) @parameter.inner
)
(#make-range! "parameter.outer" @_start @parameter.inner))

((argument_list
. (_) @parameter.inner
. ","? @_end
)
(#make-range! "parameter.outer" @parameter.inner @_end))

((argument_list
"," @_start .
(_) @parameter.inner
)
(#make-range! "parameter.outer" @_start @parameter.inner))

; TODO: exclude comments using the future negate syntax from tree-sitter
((argument_list (_) @parameter.inner . ","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))
49 changes: 42 additions & 7 deletions queries/rust/textobjects.scm
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,60 @@
(block (_) @statement.outer)

;; parameter
(((parameter) @parameter.inner . ","? @_end)

((parameters
"," @_start . (parameter) @parameter.inner)
(#make-range! "parameter.outer" @_start @parameter.inner))
((parameters
. (parameter) @parameter.inner . ","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))

((type_parameters
"," @_start . (_) @parameter.inner)
(#make-range! "parameter.outer" @_start @parameter.inner))
((type_parameters
. (_) @parameter.inner . ","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))

((tuple_pattern
(identifier) @parameter.inner . ","? @_end)
"," @_start . (identifier) @parameter.inner)
(#make-range! "parameter.outer" @_start @parameter.inner))
((tuple_pattern
. (identifier) @parameter.inner . ","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))

((tuple_struct_pattern
(identifier) @parameter.inner . ","? @_end)
"," @_start . (identifier) @parameter.inner)
(#make-range! "parameter.outer" @_start @parameter.inner))
((tuple_struct_pattern
. (identifier) @parameter.inner . ","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))

((closure_parameters
(_) @parameter.inner . ","? @_end)
"," @_start . (_) @parameter.inner)
(#make-range! "parameter.outer" @_start @parameter.inner))
((closure_parameters
. (_) @parameter.inner . ","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))

(((arguments
(_) @parameter.inner . ","? @_end))
((arguments
"," @_start . (_) @parameter.inner)
(#make-range! "parameter.outer" @_start @parameter.inner))
((arguments
. (_) @parameter.inner . ","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))

((type_arguments
"," @_start . (_) @parameter.inner)
(#make-range! "parameter.outer" @_start @parameter.inner))
((type_arguments
. (_) @parameter.inner . ","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))

((meta_arguments
"," @_start . (_) @parameter.inner)
(#make-range! "parameter.outer" @_start @parameter.inner))
((meta_arguments
(_) @parameter.inner . ","? @_end)
. (_) @parameter.inner . ","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))