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

TextEditorDecoration applied to incorrect range if racing with user edits #74094

Open
Colengms opened this issue May 22, 2019 · 1 comment · May be fixed by #172672
Open

TextEditorDecoration applied to incorrect range if racing with user edits #74094

Colengms opened this issue May 22, 2019 · 1 comment · May be fixed by #172672
Assignees
Labels
editor-core Editor basic functionality feature-request Request for new features or functionality semantic-tokens Semantic tokens issues
Milestone

Comments

@Colengms
Copy link
Contributor

Version: 1.33.1 (user setup)
Commit: 51b0b28
Date: 2019-04-11T08:27:14.102Z
Electron: 3.1.6
Chrome: 66.0.3359.181
Node.js: 10.2.0
V8: 6.6.346.32
OS: Windows_NT x64 10.0.18362

The wrong text gets decorated if the user is typing when the decoration is applied.

Steps to Reproduce:

  1. Add the following to a didChangeTextDocument handler in an extension:
        let decoration: vscode.TextEditorDecorationType = vscode.window.createTextEditorDecorationType({
            color: "#FF00FF"
        });
        let txt1: string = textDocumentChangeEvent.document.getText();
        let editors: vscode.TextEditor[] = vscode.window.visibleTextEditors.filter(e => e.document.uri === textDocumentChangeEvent.document.uri);
        for (let e of editors) {
            e.setDecorations(decoration, [new vscode.Range(1, 1, 1,10)]);
        }
        let txt2: string = textDocumentChangeEvent.document.getText();
  1. Prepare a file with some text i.e.:
XXXXXXXXXXXX
XXXXXXXXXXXX
  1. Put a breakpoint immediately after the first call to getText().
  2. When you hit that breakpoint, inspect txt1. You will see the contents of the file.
  3. While remaining at the breakpoint, paste a couple lines to the start of the file. For example, to make the file look like so:
YYYYYYYYYYYY
YYYYYYYYYYYY
XXXXXXXXXXXX
XXXXXXXXXXXX
  1. Step past the second call to getText(). You will see it retains the original content. This is good (better then changing the state of the document out from under us, before delivering didChangeTextDocument with the new edits). The intention here is to set a decoration within the second line of X's, and the apparent state of the document seems to imply this is what to expect.
  2. Continue, allowing the didChangeTextDocument handler to return.

Seeing: The decoration is applied to the wrong text.

Capture

Expected: The decoration range(s) should get adjusted based on the pending edits.

@jrieken jrieken assigned alexdima and unassigned jrieken May 22, 2019
@alexdima alexdima added this to the On Deck milestone Jul 23, 2019
@alexdima alexdima added the feature-request Request for new features or functionality label Jul 23, 2019
@alexdima alexdima added the semantic-tokens Semantic tokens issues label Aug 7, 2019
@hediet hediet added the editor-core Editor basic functionality label Oct 21, 2021
@pokey
Copy link
Contributor

pokey commented Jan 26, 2023

Maybe you could add a version parameter to editor.setDecorations() where the caller can supply the most recent known document version, and the function would throw an error if the document version has increased.

It would be kind of similar to the way things work with TextEditor.edit(), where the extension host passes a version number to the main thread, which rejects the edit if it is on an old document version.

The difference here would be that the caller would be responsible for capturing the version number, instead of it being captured automatically.

Do you think that would make sense @alexdima? I'd be happy to contribute a PR

edit: I just threw together a PR with an alternate approach where the version number is automatically captured #172672

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
editor-core Editor basic functionality feature-request Request for new features or functionality semantic-tokens Semantic tokens issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants