Skip to content

Commit

Permalink
Merge pull request #1629 from Chillee/1503
Browse files Browse the repository at this point in the history
Fixes #1503: Undo history isn't kept when switching tabs
  • Loading branch information
xconverge authored May 4, 2017
2 parents 4abafc9 + f35ac41 commit 31319c9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
44 changes: 21 additions & 23 deletions extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,25 @@ let modeHandlerToEditorIdentity: { [key: string]: ModeHandler } = {};
let previousActiveEditorId: EditorIdentity = new EditorIdentity();

export async function getAndUpdateModeHandler(): Promise<ModeHandler> {
const oldHandler = modeHandlerToEditorIdentity[previousActiveEditorId.toString()];
const prevHandler = modeHandlerToEditorIdentity[previousActiveEditorId.toString()];
const activeEditorId = new EditorIdentity(vscode.window.activeTextEditor);

const oldModeHandler = modeHandlerToEditorIdentity[activeEditorId.toString()];

if (!oldModeHandler ||
oldModeHandler.vimState.editor !== vscode.window.activeTextEditor) {

let curHandler = modeHandlerToEditorIdentity[activeEditorId.toString()];
if (!curHandler) {
const newModeHandler = new ModeHandler();

modeHandlerToEditorIdentity[activeEditorId.toString()] = newModeHandler;
extensionContext.subscriptions.push(newModeHandler);

if (oldModeHandler) {
oldModeHandler.dispose();
}
curHandler = newModeHandler;
}

const handler = modeHandlerToEditorIdentity[activeEditorId.toString()];

handler.vimState.editor = vscode.window.activeTextEditor!;
curHandler.vimState.editor = vscode.window.activeTextEditor!;
if (!prevHandler || curHandler.identity !== prevHandler.identity) {
setTimeout(() => {
curHandler.syncCursors();
}, 0);
}

if (previousActiveEditorId.hasSameBuffer(activeEditorId)) {
if (!previousActiveEditorId.isEqual(activeEditorId)) {
Expand All @@ -103,32 +101,32 @@ export async function getAndUpdateModeHandler(): Promise<ModeHandler> {
} else {
previousActiveEditorId = activeEditorId;

await handler.updateView(handler.vimState, {drawSelection: false, revealRange: false});
await curHandler.updateView(curHandler.vimState, {drawSelection: false, revealRange: false});
}

if (oldHandler && oldHandler.vimState.focusChanged) {
oldHandler.vimState.focusChanged = false;
handler.vimState.focusChanged = true;
if (prevHandler && curHandler.vimState.focusChanged) {
curHandler.vimState.focusChanged = false;
prevHandler.vimState.focusChanged = true;
}

vscode.commands.executeCommand('setContext', 'vim.mode', handler.vimState.currentModeName());
vscode.commands.executeCommand('setContext', 'vim.mode', curHandler.vimState.currentModeName());

// Temporary workaround for vscode bug not changing cursor style properly
// https://github.com/Microsoft/vscode/issues/17472
// https://github.com/Microsoft/vscode/issues/17513
const options = handler.vimState.editor.options;
const options = curHandler.vimState.editor.options;
const desiredStyle = options.cursorStyle;

// Temporarily change to any other cursor style besides the desired type, then change back
if (desiredStyle === vscode.TextEditorCursorStyle.Block) {
handler.vimState.editor.options.cursorStyle = vscode.TextEditorCursorStyle.Line;
handler.vimState.editor.options.cursorStyle = desiredStyle;
curHandler.vimState.editor.options.cursorStyle = vscode.TextEditorCursorStyle.Line;
curHandler.vimState.editor.options.cursorStyle = desiredStyle;
} else {
handler.vimState.editor.options.cursorStyle = vscode.TextEditorCursorStyle.Block;
handler.vimState.editor.options.cursorStyle = desiredStyle;
curHandler.vimState.editor.options.cursorStyle = vscode.TextEditorCursorStyle.Block;
curHandler.vimState.editor.options.cursorStyle = desiredStyle;
}

return handler;
return curHandler;
}

class CompositionState {
Expand Down
19 changes: 12 additions & 7 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,7 @@ export class ModeHandler implements vscode.Disposable {
// For whatever reason, the editor positions aren't updated until after the
// stack clears, which is why this setTimeout is necessary
setTimeout(() => {
if (this._vimState.editor) {
this._vimState.cursorStartPosition = Position.FromVSCodePosition(this._vimState.editor.selection.start);
this._vimState.cursorPosition = Position.FromVSCodePosition(this._vimState.editor.selection.start);
this._vimState.desiredColumn = this._vimState.cursorPosition.character;

this._vimState.whatILastSetTheSelectionTo = this._vimState.editor.selection;
}
this.syncCursors();
}, 0);

// Handle scenarios where mouse used to change current position.
Expand Down Expand Up @@ -1931,4 +1925,15 @@ export class ModeHandler implements vscode.Disposable {
disposable.dispose();
}
}

// Syncs cursors between vscode representation and vim representation
syncCursors() {
if (this._vimState.editor) {
this._vimState.cursorStartPosition = Position.FromVSCodePosition(this._vimState.editor.selection.start);
this._vimState.cursorPosition = Position.FromVSCodePosition(this._vimState.editor.selection.start);
this._vimState.desiredColumn = this._vimState.cursorPosition.character;

this._vimState.whatILastSetTheSelectionTo = this._vimState.editor.selection;
}
}
}

0 comments on commit 31319c9

Please sign in to comment.