Skip to content

Commit

Permalink
clear history when content from disk is changed (#703)
Browse files Browse the repository at this point in the history
* clear history when content from disk is changed

* cleanup

* fix typo

* more cleanup
  • Loading branch information
aminroosta authored and johnfn committed Sep 3, 2016
1 parent 259ce18 commit 660df9a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
21 changes: 21 additions & 0 deletions extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -217,6 +231,13 @@ async function handleKeyEvent(key: string): Promise<void> {
});
}

function handleContentChangedFromDisk(document : vscode.TextDocument) : void {
_.filter(modeHandlerToEditorIdentity, modeHandler => modeHandler.fileName === document.fileName)
.forEach(modeHandler => {
modeHandler.vimState.historyTracker.clear();
});
}

async function handleActiveEditorChange(): Promise<void> {

// Don't run this event handler during testing
Expand Down
10 changes: 10 additions & 0 deletions src/history/historyTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down

3 comments on commit 660df9a

@xconverge
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aminroosta I think this broke undo

the undo tests pass but if you open an editor and type something then press "u" it doesn't work. I did a git reset --hard to the commit before this and undo works.

@aminroosta
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xconverge Thanks for finding this bug. I will investigate what's going on and i will send a PR.

at mention @johnfn :-)

@aminroosta
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xconverge It works fine on my machine. can you please check once more :-)

Please sign in to comment.