Skip to content

structural editing when cursor is right outside the structure #436

Closed
@ym-han

Description

@ym-han

The 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:

  1. 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
  2. Otherwise, inspect the character to the right of the position
  3. If it is whitespace, subtract one from the position
  4. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions