diff --git a/extension.ts b/extension.ts index d0051bd20a7..2d4dc0ccc0f 100644 --- a/extension.ts +++ b/extension.ts @@ -8,6 +8,7 @@ import * as vscode from 'vscode'; import * as util from './src/util'; +import * as _ from "lodash"; import { showCmdLine } from './src/cmd_line/main'; import { ModeHandler } from './src/mode/modeHandler'; import { TaskQueue } from './src/taskQueue'; @@ -114,6 +115,19 @@ export async function activate(context: vscode.ExtensionContext) { vscode.window.onDidChangeActiveTextEditor(handleActiveEditorChange, this); + vscode.workspace.onDidChangeTextDocument((event) => { + /** + * Change from vscode editor should set document.isDirty to true but they initially don't! + * There is a timing issue in vscode codebase between when the isDirty flag is set and + * when registered callbacks are fired. https://github.com/Microsoft/vscode/issues/11339 + */ + setTimeout(() => { + if (!event.document.isDirty) { + handleContentChangedFromDisk(event.document); + } + }, 0); + }); + registerCommand(context, 'type', async (args) => { if (!vscode.window.activeTextEditor) { return; @@ -217,6 +231,13 @@ async function handleKeyEvent(key: string): Promise { }); } +function handleContentChangedFromDisk(document : vscode.TextDocument) : void { + _.filter(modeHandlerToEditorIdentity, modeHandler => modeHandler.fileName === document.fileName) + .forEach(modeHandler => { + modeHandler.vimState.historyTracker.clear(); + }); +} + async function handleActiveEditorChange(): Promise { // Don't run this event handler during testing diff --git a/src/history/historyTracker.ts b/src/history/historyTracker.ts index 2265856a64a..655df29d4dc 100644 --- a/src/history/historyTracker.ts +++ b/src/history/historyTracker.ts @@ -177,6 +177,16 @@ export class HistoryTracker { } constructor() { + this._initialize(); + } + + public clear() { + this.historySteps = []; + this.currentHistoryStepIndex = 0; + this._initialize(); + } + + private _initialize() { /** * We add an initial, unrevertable step, which inserts the entire document. */ diff --git a/src/mode/modeHandler.ts b/src/mode/modeHandler.ts index dc3c6e17c46..1e00fa9d971 100644 --- a/src/mode/modeHandler.ts +++ b/src/mode/modeHandler.ts @@ -458,7 +458,7 @@ export class ModeHandler implements vscode.Disposable { /** * Filename associated with this ModeHandler. Only used for debugging. */ - public filename: string; + public fileName: string; private _caretDecoration = vscode.window.createTextEditorDecorationType( { @@ -491,7 +491,7 @@ export class ModeHandler implements vscode.Disposable { constructor(filename = "") { ModeHandler.IsTesting = Globals.isTesting; - this.filename = filename; + this.fileName = filename; this._vimState = new VimState(); this._insertModeRemapper = new InsertModeRemapper(true); @@ -532,7 +532,7 @@ export class ModeHandler implements vscode.Disposable { return; } - if (e.textEditor.document.fileName !== this.filename) { + if (e.textEditor.document.fileName !== this.fileName) { return; }