structural editing when cursor is right outside the structure
See original GitHub issueThe problem
Consider something like
def test():
print("a")
and suppose the cursor is at the end of the print statement, after the )
.
Currently take funk
does not select the function when the cursor is at that position, though it does work if the cursor is anywhere before the final )
.
edit (@pokey):
Why does this happen?
Cursorless relies on the Parse tree extension to get the current node, and then walks up from there. The problem is that this function just passes a position to web-tree-sitter’s rootNode.descendantForPosition
, which will always look to the right, so if there is whitespace to the right, it will go way up the tree to find a node that contains the given whitespace.
The solution
We should probably fix this one inside the parse tree extension. In getNodeAtLocation
, we will proceed by trying to find an appropriate position to pass to web-tree-sitter, rather than just directly using the given position:
- If the position is at the end of the file, subtract one from the position, unless the file is empty, in which case just use the position
- Otherwise, inspect the character to the right of the position
- If it is whitespace, subtract one from the position
- Otherwise, use position
We then take the position and pass it to rootNode.descendantForPosition
, like we do today
Alternate solution
If the above doesn’t work for some reason, we could instead always instead first ask for the node from position like today, but then subtract one from position and ask again, and if they overlap, take the smaller one
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top GitHub Comments
I would start by only applying to
containingScope
, as I don’t think it buys us anything for other modifiers, at the expense of added complexity / unpredictabilityJust a note - #629 should fix and close this.