Get cursor position

It’s possible that I’ve missed something.

But it appears that the workspace API has options to get the selected text, but it doesn’t have an option to get the current cursor position.

This is unfortunate, as some LSP methods can get by with just the current position – the various Goto methods, as well as prepareRename, are quite happy to use a single position and their own AST to determine the closest symbol.

The benefit of this is that if the symbol isn’t going work out (such as trying to rename something that cannot be renamed, like a keyword) then we haven’t changed the selection. Using the selection and the technique of expanding the selection to include the symbol works, kind of, but it’s less ideal.

What would be great is a single TextEditor property for the cursor(s).

texteditor.selectedRange should give you the current cursor position. If there is no range selected, just a cursor, then the start and end index should be the same number, and that’s your position.

use texteditor.selectedRanges if you want to support multiple cursor positions.

1 Like