I am working on vscode TextEditorEdit extension. Here I need to put the cursor on a specified line number after opening the file as an editor, based on an api response content. How can I achieve that? or what class or interface provides the functionality to do this?
Below are the codes I tried to achieve this. but both didn't work. Not even raising any exception or error.
Although this is opening and showing the file on the editor properly. But not make any effect when I try to change the cursor position.
- THENing the
vscode.window.showTextDocument()
and assigning theeditor.selection
with custom selection. This makes no effect on editor.
vscode.workspace.openTextDocument(openPath).then(async doc => {
let pos1 = new vscode.Position(57, 40)
let pos2 = new vscode.Position(57, 42)
let sel = new vscode.Selection(pos1,pos2)
let rng = new vscode.Range(pos1, pos2)
vscode.window.showTextDocument(doc, vscode.ViewColumn.One).then((editor: vscode.TextEditor) => {
editor.selection = sel
//editor.revealRange(rng, vscode.TextEditorRevealType.InCenter)
});
});
I also tried revealing the range with editor.revealRange(rng, vscode.TextEditorRevealType.InCenter)
but no effect of this also.
- Adding the
options?:
parameter invscode.window.showTextDocument()
. But this too doesn't do anything. Please see the below code-
vscode.workspace.openTextDocument(openPath).then(async doc => {
let pos1 = new vscode.Position(57, 40)
let pos2 = new vscode.Position(57, 42)
let rng = new vscode.Range(pos1, pos2)
vscode.window.showTextDocument(doc, {selection: rng, viewColumn: vscode.ViewColumn.One})
});
I guess, the problem is with opening the file first and showing the text document into the editor, and then changing the cursor location.