You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Have an extension with a command that uses the TextEditorEdit to replace a line of text with a longer line of text.
For eg: the below replaces the first line with a long text.
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('hello', () => {
vscode.window.activeTextEditor.edit(editBuilder => {
editBuilder.replace(new vscode.Range(0,0,1,0), 'This is a long text meant to replace something that was shorter.');
})
}));
}
Now place the cursor in the end of the first line of any file and run the above command
Say the first line had text that was 10 chars long.
After running the above command text from the 11th char to the end of the line will be selected.
For extensions that use the TextEditorEdit this would result in text deletion if the user was to press enter after the edits were made
The text was updated successfully, but these errors were encountered:
The cursor selection is a tracked range sitting on the text that gets changed via the edit API. There is an internal hidden flag, forceMoveMarkers which can be influenced by the methods being called on the text editor edit builder.
There are two options:
1a. set the position immediately after sending off the edit request:
the caveat is that this is slightly incorrect, as if the edit fails the cursor selection will be changed
vscode.window.activeTextEditor.edit(editBuilder => {
editBuilder.replace(new vscode.Range(0,0,1,0), 'This is a long text meant to replace something that was shorter.');
});
vscode.window.activeTextEditor.position = ...
1b. set the position once the edits are applied:
this can also be slightly incorrect, and might suffer from timing issues, though it is IMHO a bit better than 1a.
vscode.window.activeTextEditor.edit(editBuilder => {
editBuilder.replace(new vscode.Range(0,0,1,0), 'This is a long text meant to replace something that was shorter.');
}).then(function() {
vscode.window.activeTextEditor.position = ...
});
use delete + insert instead of replace to get forceMoveMarkers to be true.
vscode.window.activeTextEditor.edit(editBuilder => {
editBuilder.delete(new vscode.Range(0,0,0,123415));
editBuilder.insert(new vscode.Position(0,123415), 'This is a long text meant to replace something that was shorter.');
});
I would try 2, if that fails, then 1b, if that fails, then 1a.
If nothing works for your use-case we might consider adding a new API method on TextEditorEdit to precisely define the resulting selection(s), although 2 is IMHO preferable as the same file can be opened in multiple editors and it would be preferable for all the cursors (not just the one of the current active editor) to behave the same way.
Have an extension with a command that uses the
TextEditorEdit
to replace a line of text with a longer line of text.For eg: the below replaces the first line with a long text.
Now place the cursor in the end of the first line of any file and run the above command
Say the first line had text that was 10 chars long.
After running the above command text from the 11th char to the end of the line will be selected.
For extensions that use the
TextEditorEdit
this would result in text deletion if the user was to press enter after the edits were madeThe text was updated successfully, but these errors were encountered: