nvim-treesitter-textobjects
nvim-treesitter-textobjects copied to clipboard
Treatment of whitespace is inconsistent/inconvenient
Consider a python snippet like the following with the cursor placed at the caret:
print( "a" , "b" , "c" )
^
If you try to select parameter.inner or parameter.outer, nothing will happen.
If the cursor is in a different location:
print( "a" , "b" , "c" )
^
Now parameter.outer will select something (the "b" parameter up to its comma, but not its leading space),
but parameter.inner still does nothing.
This is probably technically correct according to the way the syntax tree maps to tokens, but it's not very convenient for editing. You'll get similar behavior if you happen to be in whitespace for other text objects, e.g.:
def foo(bar):
print(bar)
^
Here function.outer will select the entire function and body as expected, but function.inner selects nothing.
When the cursor is on whitespace, it should probably "snap" to the next non-whitespace token before looking for the surrounding text object.
The behavior is even weirder when selecting function.inner in the beginning whitespace of a nested function: function.outer selects the inner function, whereas function.inner selects the outer function.
At the moment we have no treatment of whitespaces at at all in this plugin. I always wanted to do this finally but I've never come to point to actually handle it. But I hope that at least some weirdness regard parameter.{inner/outer} got fixed by https://github.com/nvim-treesitter/nvim-treesitter-textobjects/pull/87. For all other white space issues I would really recommend to submit a whitespace handling strategy to this plugin because at the moment we don't have any.
I just tried to fix some whitespace stuff here for parameter.inner and outer, (#87). The problem basically is that treesitter "smartly" ignores whitespace. The parameter node in the syntax tree doesn't include the white space. The only way to capture whitespace (that i know) is to make a range from two nodes, which will capture the whitespace between those. This is why outer sometimes works in the cases you mentioned but inner generally won't
If you know a way to capture pre or post whitespace (without capturing the prev/next token) then we could fix a lot of these problems. I'd be happy to do the work if someone finds a technique
This is already fixed, you need to set the lookahead option to true https://github.com/nvim-treesitter/nvim-treesitter-textobjects/blob/f51c3feb5abe1dea556c9076b8a8928d3bf56790/doc/nvim-treesitter-textobjects.txt#L21.
Look ahead doesn't work. In my second example, it would actually have to look backwards to find the parameter.
@stsewd can you reopen this, please?
@bkoropoff we do support the lookbehind option too https://github.com/nvim-treesitter/nvim-treesitter-textobjects/blob/99600641de75419f7729698acf9dc3d79117ff70/lua/nvim-treesitter-textobjects.lua#L33, those options are currently global, I have opened this issue to make the settings per-mapping https://github.com/nvim-treesitter/nvim-treesitter-textobjects/issues/156.