kylechui / nvim-surround

Add/change/delete surrounding delimiter pairs with ease. Written with :heart: in Lua.
MIT License
2.9k stars 60 forks source link

Treat Rust's macro invocations as function calls #290

Closed tom-anders closed 6 months ago

tom-anders commented 6 months ago

Checklist

Is your feature request related to a problem? Please describe.

In Rust, functions are invoked like this dbg!(x) (note the exclamation mark). Right now, nvim-surround does not detect this as a function call, even though as a Rust user I'd expect dsf to turn this into just x.

Describe the solution you'd like I know that I can customize the pattern for detecting function, but I think the behavior described above should be the default for Rust files (if this is possible)

Additional context Add any other context or screenshots about the feature request here.

kylechui commented 6 months ago

I can't seem to reproduce this behavior. Have you tried updating the plugin to the latest tagged release, and updating your Rust Tree-sitter parser? Do you use nvim-treesitter-textobjects? If so, try updating that plugin as well.

tom-anders commented 6 months ago

Updated both the plugin and nvim-treesitter, the issue still persists - Here's a minimal example:

fn foo() {
   bar();
   dbg!(123); // triggering dsf here deletes the line above
}

Interestingly, when the bar() line is removed, dsf works as expected

kylechui commented 6 months ago

I'm now able to reproduce the issue; funnily enough it doesn't seem to happen if you just paste the code into a non-Rust file.

kylechui commented 6 months ago

Try adding this to ~/.config/nvim/queries/rust/textobjects.scm:

; Defines macro invocations as "function calls"
(macro_invocation) @call.outer
; Copy-pasted from nvim-treesitter-textobject's definition of @call.outer
(call_expression) @call.outer
(call_expression
  arguments: (arguments . "(" . (_) @_start (_)? @_end . ")"
  (#make-range! "call.inner" @_start @_end)))

If this works for you, I'll open a PR upstream and see if they can make the change.

tom-anders commented 6 months ago

Yeah that seems to work, thanks!

kylechui commented 6 months ago

Upstream PR got merged; feel free to re-open if there are still issues!