How to check if a cursor is selecting or not?


I am watching cursors in my editor with editor.observeCursors() and inside that listening to position changes with cursor.onDidChangePosition().
I want to perform some action when the cursor changes position but not when it’s selecting some text. Currently cursors and selections are in no way linked, so what do I do?


You could have your code check to see if there are any selections before continuing.


Thanks for the help! But I ended up checking if cursor.selection.isEmpty(), I didn’t know that cursor and selection were linked like that.


Yeah, there are a few interesting things like that.

It’s possible that some other command or package could create both a selection and a cursor, and then move the cursor around. In this case, your package would still take effect. I don’t know what you want to do, so I don’t know if this is undesirable. The reason I suggested editor.getSelections() was to make sure that no text was selected before the package took action.

The above scenario isn’t able to be accomplished with user inputs by default and isn’t something you need to be worried about. However, it would be trivially easy for me to set up some code that does that.