Skip to content

Conversation

@theHamsta
Copy link
Member

@theHamsta theHamsta commented Nov 16, 2020

Addresses: #22

Requires: nvim-treesitter/nvim-treesitter#657

It only works if your cursor is between the parameter or and the end character of the ",". I try to add @stsewd select next textobject soon to make it also work on spaces before the parameter

@theHamsta theHamsta force-pushed the parameter.outer branch 3 times, most recently from 970f75d to 8e07a86 Compare November 16, 2020 16:50
@theHamsta theHamsta changed the title Add @parameter.outer for Python Add @parameter.outer for Python/C/C++ Nov 16, 2020
@theHamsta
Copy link
Member Author

@andreypopp could you test this? and maybe even add the queries for you favorite language?

@andreypopp
Copy link

Wow!

Testing it with python. It works well with argument lists which have the trailing comma but with the following one:

def some(aa, bbb, ccc):
    pass

It cannot extract a range from (no trailing coma after ccc). The result is a traceback:

E5108: Error executing lua .../plugged/nvim-treesitter/lua/nvim-tr
eesitter/tsrange.lua:30: attempt to index local 'end_node' (a nil
value)

I've tried to match on ["," ")"] @_end but it obviously eats the ) in that case...

@theHamsta
Copy link
Member Author

theHamsta commented Nov 16, 2020

But did you apply the patch in the description for nvim-treesitter? This should handle the nil case.

@andreypopp
Copy link

Oops, I apologize, I missed that requirement. It works that way!

I actually tried to patch nvim-treesitter the same way but thought that it's a bit non intuitive behaviour. What if the range's intent is to cover more than two siblings?

a @start b c? @_end

In case c is absent it will result in only [a, a] range.

@theHamsta
Copy link
Member Author

theHamsta commented Nov 16, 2020

Is that bad? If b is not captured the query can not know about it. What you would like to have in that situation would be the ternary (make-range! @a @b @c). If you make @c with ? it is clear that it is optional.

We have a lot of stuff that can be optionally carry a something before or after it.

@andreypopp
Copy link

Is that bad? If b is not captured the query can not know about it.

idk, probably not bad...

Another idea — I think it could be a useful in API to specify an open range.. something like

argument . ["," @_end ")" @_open_end]

To result in:
a. [argument, ","] when comma matches
b. [argument, ")") otherwise, an @_open_end location will be produced by going one char left from the ")".

@andreypopp
Copy link

By the way "open ranges" could with another thing — I've wanted to try to redefine @block.inner not to include braces { and }. As it seems reasonable to me exclude them on cib and so on.

@theHamsta
Copy link
Member Author

Yes, with the new TSRange object you can do many things. We could easily implement the open-range thing. However, it's also possible to use the current approach and just capture the node before the open range. Or to just capture the last node. The last node thing, is however a bit unreliable and it for some reason does not work when you also specify a first node in a query.

You should be able to implement open-range in nvim-treesitter with get_next_node and get_previous_node.

@theHamsta
Copy link
Member Author

theHamsta commented Nov 16, 2020

With this last? predicate:

query.add_predicate('last?', function (match, pattern, bufnr, pred)
  if #pred ~= 2 then error("last? must have exactly one argument!") end
  local node = match[pred[2]]
  if node and node:parent() then
    return node:parent():named_child(node:parent():named_child_count() - 1) == node
  end

  return false
end)
((compound_statement .
  (_) @_start 
  (_) @_end 
 )
 (make-range! "block.inner" @_start @_end)
 (last? @_end)) 

Last is actually only necessary since the . operator does not work at the moment (in tree-sitter).

@theHamsta
Copy link
Member Author

theHamsta commented Nov 16, 2020

I created a bug report regarding the last (tree-sitter/tree-sitter#806). Would be nice if we could specify @block.inner as

(compound_statement .
  (_) @_start 
  (_) @_end .)

Copy link
Member

@stsewd stsewd left a comment

Choose a reason for hiding this comment

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

I try to add @stsewd select next textobject soon to make it also work on spaces before the parameter

Thanks, I said I'd do it, but been busy lately :(

@theHamsta theHamsta requested a review from stsewd November 18, 2020 19:16
@andreypopp
Copy link

@theHamsta don know if you’ve seen but I made a PR to your fork with this text object for JavaScript

@theHamsta theHamsta force-pushed the parameter.outer branch 2 times, most recently from c0915b0 to f119414 Compare November 18, 2020 20:15
@theHamsta theHamsta merged commit b4e12cd into nvim-treesitter:master Nov 18, 2020
@Aerex Aerex mentioned this pull request Dec 7, 2020
@AckslD
Copy link
Contributor

AckslD commented Mar 24, 2021

@theHamsta This is really nice! 🎉 What would it take to be able to also make it work on spaces before the argument? I could try to look into it but just started with treesitter so would be happy to hear any advice :) Similarly it would be great to make it so that when one does e.g. d{@parameter.outer} that whitespace before (or after) gets deleted, as with e.g. daw.

@liujoey
Copy link

liujoey commented Sep 9, 2021

@theHamsta This is really nice! 🎉 What would it take to be able to also make it work on spaces before the argument? I could try to look into it but just started with treesitter so would be happy to hear any advice :) Similarly it would be great to make it so that when one does e.g. d{@parameter.outer} that whitespace before (or after) gets deleted, as with e.g. daw.

If we can add this support then I can completely remove targets.vim from my plugin list. As far as I know Tree Sitter doesn't mark whitespaces as nodes, so one possible approach would be looking for next sibling in the parameter_list and select all the way to that sibling, which effectively deletes the whitespaces.

Edit: typo

@theHamsta
Copy link
Member Author

@liujoey PRs are welcome 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants