Skip to content

[VS Code] Code suggestion streaming causes Undo to not work afterwards

Checklist

Summary

Note - this has been an issue since the introduction of streaming, around %17.0.

Code suggestion streaming (aka code generation) involves repeatedly triggering no-op edits on the page to cause VS Code to fire off multiple inline suggestion events in succession.

This no-op edit fills up the undo history with multiple edits (that do nothing), leaving the user unable to use the 'Undo' functionality easily. They need to hit undo enough times to go back over all of the no-op edit events - something like 10-20 times for a medium sized code generation result.

Thanks @elwyn-gitlab for the idea during a sync discussion.

Steps to reproduce

  1. Open a file with GitLab Workflow enabled and set up.
  2. Trigger a code generation (e.g. pressing Return after a comment)
  3. Wait for the result to finish streaming.
  4. Do not accept the suggestion (optional, but makes the result clearer).
  5. Press 'Undo' (e.g. "Cmd + Z") multiple times.

What is the current bug behavior?

Nothing happens, until the user presses Undo as many times as there were individual 'chunks' loading in during the streaming process.

What is the expected correct behavior?

The last text change performed by the user is undone.

Relevant logs and/or screenshots

Possible fixes

The code here is responsible:

    // forceTriggerInlineCompletion
    const prevCharPosition = document.positionAt(activeOffset - 1);
    const replaceRange = new vscode.Range(prevCharPosition, activePosition);
    const value = document.getText(replaceRange);

    await editor.edit(edit => edit.replace(replaceRange, value));
Edited by Tristan Read