Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange cursor behavior after workspace.applyEdit(); #11841

Closed
Jakobud opened this Issue Sep 11, 2016 · 3 comments

Comments

Projects
None yet
2 participants
@Jakobud
Copy link

Jakobud commented Sep 11, 2016

  • VSCode Version: 1.5.1
  • OS Version: Windows 7 x64

I am working on an extension that will inject some text into your document at the current cursor (or all cursors if there are multiple). I'm seeing some weird behavior with the cursor after inserting the text.

Steps to Reproduce:

  1. Type some random sentence of text.
  2. Place your cursor somewhere mid sentence.
  3. Run an extension that inserts "HELLO WORLD" at your cursor position:
let textEditor = vscode.window.activeTextEditor;
// Get the active text document's uri
let uri = textEditor.document.uri;

// Create a new TextEdit for each selection
let edits = [];
for (let selection of textEditor.selections) {
  edits.push(vscode.TextEdit.insert(selection.active, "HELLO WORLD");
}

// New WorkspaceEdit
let edit = new vscode.WorkspaceEdit();

// Add the TextEdit's to your WorkspaceEdit
edit.set(uri, edits);

// Applying the WorkspaceEdit
vscode.workspace.applyEdit(edit);
  1. You will see your sentence with "HELLO WORLD" inserted into the middle of it. Your cursor will be immediately following the D in HELLO WORLD.
  2. Press LEFT on your keyboard, your cursor will jump to the beginning of HELLO WORLD, before the H.

That seems like weird behavior right? I would expect the cursor to move one space to the left, but instead of jumped to the beginning of the inserted text, where your cursor was before inserting the text. Is this a bug or intended behavior?

@Jakobud

This comment has been minimized.

Copy link
Author

Jakobud commented Sep 11, 2016

Oh I think it's because the inserted text becomes a selection. Nevermind on this.

@Jakobud Jakobud closed this Sep 11, 2016

@weakish

This comment has been minimized.

Copy link

weakish commented Sep 13, 2016

the inserted text becomes a selection

@Jakobud
Can you please explain more?
A selection of inserted text is added? Or the original selection is replaced?

On my machine, after inserted (multiline, i.e. with \n) text,
textEditor.selections.length does not change.
So the original selection is replaced? But ... (see below)

Press LEFT on your keyboard,
your cursor will jump to the beginning of HELLO WORLD,
before the H.

How to go to a position inside HELLO WORLD?
I tried create a new Selection of current line (textEditor.selection.start.line) and its range.end,
the cursor will be at the end of inserted string, a.k.a. D in your example.
Then I tried to go backward, creating a new Selection of current line
(new textEditor.selection.start.line) and its range.end - n,
then the range.end will still be the original end (before insert),
thus it will go backward n characters from the original end,
i.e. n + 1 characters before H in your example.

@Jakobud

This comment has been minimized.

Copy link
Author

Jakobud commented Sep 13, 2016

I didn't investigate this too much farther. It appeared that when you insert text into a TextDocument that the inserted text becomes selected. I didn't look at textEditor.selections. To go to a position inside your selected text I'm not sure.

@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 18, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.