How to set the cursor position in an active editor?

Hi all,

Can anyone tell me how I might go about setting the cursor position in an active editor? I don’t want to insert any text, just set the position.

I thought maybe something like this below:

let editor = nova.workspace.activeTextEditor();
let range = new Range(20, 20);

editor.selectedRange = range;

Unfortunately, this doesn’t seem to work.

Thanks,
Jason

If you follow up with editor.scrollToCursorPosition() does it work?

I have this nugget for pushing and popping the cursor location that seems to work reliably:

class JumpStack {
    constructor() {
        this.jumpStack = [];
    }

    push() {
        this.jumpStack.push({
            uri: nova.workspace.activeTextEditor.document.uri,
            range: nova.workspace.activeTextEditor.selectedRange,
        });
    }

    pop() {
        let p = this.jumpStack.pop();
        if (p) {
            nova.workspace
                .openFile(p.uri)
                .then((targetEditor) => {
                    targetEditor.selectedRange = p.range;
                    targetEditor.scrollToCursorPosition();
                })
                .catch((err) => {
                    console.error(`Failed to pop ${err}`);
                });
        } else {
            console.log('jump stack is empty');
        }
    }
}
2 Likes

That seems to be working great. Thanks John!