meain / evil-textobj-tree-sitter

Tree-sitter powered textobjects for evil mode in Emacs
Apache License 2.0
197 stars 14 forks source link

Selecting something that doesn't match at point has surprising results #89

Closed deifactor closed 1 year ago

deifactor commented 1 year ago

If I have a Rust file like the following

fn blah() { 1234 }

fn baz() {
  let x = 5678;
}

and put point inside the definition of blah, then the @argument.inner motion will jump all the way to the definition of baz. This is very surprising; imo if there's no text object matching the query that contains point, it should just fail.

meain commented 1 year ago

This is done so as to mimic how ci" or cib works. That said, I'm not opposed to adding a flag that can switch off the behaviour.

meain commented 1 year ago

I'm also wondering if this should be specific to each textobj, ie things lie arguments could have this limit, but maybe not for functions. Do you have any thoughts around this?

deifactor commented 1 year ago

Hmm, I didn't know that ci" works like that. In that case this behavior makes more sense. A new parameter to evil-textobj-tree-sitter-get-textobj could work, maybe with an option that sets the default.

The reason I ran into this was that I was running into some issues with the queries; for example, some_function(1234) doesn't match the 'outer parameter' query since there's no trailing comma; I saw the cursor zooming off elsewhere and thought there was some weird issue computing the character positions.

IMO outer objects should fall back to inner objects if the inner exists and the outer doesn't, but I'm not sure if you agree. Separate issue anyway.

BTW, thanks for this library; when I've got a bit more free time I'd love to help. :)

meain commented 1 year ago

A new parameter to evil-textobj-tree-sitter-get-textobj could work, maybe with an option that sets the default.

I think that makes sense.

MO outer objects should fall back to inner objects if the inner exists and the outer doesn't, but I'm not sure if you agree. Separate issue anyway.

I don't wanna force it on users, but you can provide multiple capture groups if you would like it to match both inner and outer.

  (define-key evil-outer-text-objects-map "q" (evil-textobj-tree-sitter-get-textobj ("conditional.outer" "conditional.inner")))
meain commented 1 year ago

I gave it some thought and decided to merge a global flag. You now have: (setq evil-textobj-tree-sitter-use-next-if-not-within nil).