From 4c8394af15ca61a8ce310c8a899b9647aa112968 Mon Sep 17 00:00:00 2001 From: amin roosta Date: Sat, 3 Sep 2016 02:44:34 +0430 Subject: [PATCH 1/4] clear history when content from disk is changed --- extension.ts | 17 +++++++++++++++++ src/history/historyTracker.ts | 10 ++++++++++ src/mode/modeHandler.ts | 6 +++--- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/extension.ts b/extension.ts index d0051bd20a7..e9e063817b4 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,15 @@ export async function activate(context: vscode.ExtensionContext) { vscode.window.onDidChangeActiveTextEditor(handleActiveEditorChange, this); + vscode.workspace.onDidChangeTextDocument((event) => { + /* TODO: Remove setTimeout (https://github.com/Microsoft/vscode/issues/11339) */ + setTimeout(() => { + if (event.document.isDirty === false) { + handleContentChangedFromDisk(event.document); + } + }, 0); + }); + registerCommand(context, 'type', async (args) => { if (!vscode.window.activeTextEditor) { return; @@ -217,6 +227,13 @@ async function handleKeyEvent(key: string): Promise { }); } +function handleContentChangedFromDisk(document : vscode.TextDocument) : void { + _.filter(modeHandlerToEditorIdentity, { "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 9b8df188ab4..a0009ce5463 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; } From bacea0b92ad6ff399eee0cfdc679e9d9387f5ccb Mon Sep 17 00:00:00 2001 From: amin roosta Date: Sat, 3 Sep 2016 03:23:30 +0430 Subject: [PATCH 2/4] cleanup --- extension.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/extension.ts b/extension.ts index e9e063817b4..9ed31cbc4b0 100644 --- a/extension.ts +++ b/extension.ts @@ -116,9 +116,13 @@ export async function activate(context: vscode.ExtensionContext) { vscode.window.onDidChangeActiveTextEditor(handleActiveEditorChange, this); vscode.workspace.onDidChangeTextDocument((event) => { - /* TODO: Remove setTimeout (https://github.com/Microsoft/vscode/issues/11339) */ + /** + * Change from vscode eidtor 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 === false) { + if (!event.document.isDirty) { handleContentChangedFromDisk(event.document); } }, 0); @@ -228,7 +232,7 @@ async function handleKeyEvent(key: string): Promise { } function handleContentChangedFromDisk(document : vscode.TextDocument) : void { - _.filter(modeHandlerToEditorIdentity, { "fileName" : document.fileName}) + _.filter(modeHandlerToEditorIdentity, (modeHandler) => modeHandler.fileName === document.fileName) .forEach((modeHandler) => { modeHandler.vimState.historyTracker.clear(); }); From 0dcce7666ef5a928eb9c4512e3bae8eb350ddb39 Mon Sep 17 00:00:00 2001 From: amin roosta Date: Sat, 3 Sep 2016 03:26:43 +0430 Subject: [PATCH 3/4] fix typo --- extension.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension.ts b/extension.ts index 9ed31cbc4b0..8695af49e9c 100644 --- a/extension.ts +++ b/extension.ts @@ -117,7 +117,7 @@ export async function activate(context: vscode.ExtensionContext) { vscode.workspace.onDidChangeTextDocument((event) => { /** - * Change from vscode eidtor should set document.isDirty to true but they initially don't! + * 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 */ From 7ad8fb8199f911dc733baf649c396e0680b5817f Mon Sep 17 00:00:00 2001 From: amin roosta Date: Sat, 3 Sep 2016 03:50:56 +0430 Subject: [PATCH 4/4] more cleanup --- extension.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extension.ts b/extension.ts index 8695af49e9c..2d4dc0ccc0f 100644 --- a/extension.ts +++ b/extension.ts @@ -232,8 +232,8 @@ async function handleKeyEvent(key: string): Promise { } function handleContentChangedFromDisk(document : vscode.TextDocument) : void { - _.filter(modeHandlerToEditorIdentity, (modeHandler) => modeHandler.fileName === document.fileName) - .forEach((modeHandler) => { + _.filter(modeHandlerToEditorIdentity, modeHandler => modeHandler.fileName === document.fileName) + .forEach(modeHandler => { modeHandler.vimState.historyTracker.clear(); }); }