Skip to content

Commit

Permalink
Merge pull request #313 from alisonatwork/await-mode-changes
Browse files Browse the repository at this point in the history
Await mode changes
  • Loading branch information
alisonatwork authored Nov 13, 2022
2 parents b5bba14 + ac2ced2 commit 3a9c455
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 36 deletions.
8 changes: 3 additions & 5 deletions src/Actions/MoveCursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,20 @@ export class ActionMoveCursor {
}, 100);
}

static updatePreferredColumn(): Thenable<boolean> {
static updatePreferredColumn(): void {
if (ActionMoveCursor.isUpdatePreferredColumnBlocked) {
return Promise.resolve(false);
return;
}

const activeTextEditor = window.activeTextEditor;

if (!activeTextEditor) {
return Promise.resolve(false);
return;
}

ActionMoveCursor.preferredColumnBySelectionIndex = activeTextEditor.selections.map(
(selection) => UtilPosition.getColumn(activeTextEditor, selection.active),
);

return Promise.resolve(true);
}

static async byMotions(args: {
Expand Down
10 changes: 5 additions & 5 deletions src/Dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ export class Dispatcher {
this.disposables.push(
window.onDidChangeTextEditorSelection(() => {
// Ensure this is executed after all pending commands.
setTimeout(() => {
ActionMode.switchByActiveSelections(this._currentMode.id);
setTimeout(async () => {
await ActionMode.switchByActiveSelections(this._currentMode.id);
ActionMoveCursor.updatePreferredColumn();
this._currentMode.onDidChangeTextEditorSelection();
}, 0);
}),
window.onDidChangeActiveTextEditor(() => {
window.onDidChangeActiveTextEditor(async () => {
if (Configuration.defaultModeID === ModeID.INSERT) {
ActionMode.toInsert();
await ActionMode.toInsert();
} else {
// Passing `null` to `currentMode` to force mode switch.
ActionMode.switchByActiveSelections(null);
await ActionMode.switchByActiveSelections(null);
}
ActionMoveCursor.updatePreferredColumn();
}),
Expand Down
14 changes: 5 additions & 9 deletions src/Modes/Insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@ export class ModeInsert extends Mode {
this.processRecord();
}

protected onWillCommandMapMakesChanges(map: CommandMap): Promise<boolean> {
protected onWillCommandMapMakesChanges(map: CommandMap): void {
if (!this.isRecording) {
return Promise.resolve(false);
return;
}

if (map.keys === '\n') {
Expand All @@ -285,27 +285,23 @@ export class ModeInsert extends Mode {
isRepeating: true,
});
}

return Promise.resolve(true);
}

protected onDidCommandMapMakesChanges(map: CommandMap): Promise<boolean> {
protected onDidCommandMapMakesChanges(map: CommandMap): void {
if (!this.isRecording) {
return Promise.resolve(false);
return;
}

if (map.keys === '\n') {
if (!this.textEditor) {
return Promise.resolve(false);
return;
}

this.recordStartPosition = this.textEditor.selection.active;
this.recordStartLineText = this.textEditor.document.lineAt(
this.recordStartPosition.line,
).text;
}

return Promise.resolve(true);
}

onDidChangeTextEditorSelection(): void {
Expand Down
10 changes: 3 additions & 7 deletions src/Modes/Mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,12 @@ export abstract class Mode {
/**
* Override this to do something before command map makes changes.
*/
protected onWillCommandMapMakesChanges(map: CommandMap): Promise<boolean> {
return Promise.resolve(true);
}
protected onWillCommandMapMakesChanges(map: CommandMap): void {}

/**
* Override this to do something after command map made changes.
*/
protected onDidCommandMapMakesChanges(map: CommandMap): Promise<boolean> {
return Promise.resolve(true);
}
protected onDidCommandMapMakesChanges(map: CommandMap): void {}

/**
* Override this to do something after selection changes.
Expand All @@ -132,7 +128,7 @@ export abstract class Mode {
return;
}

let promise: Promise<boolean | undefined> = Promise.resolve(true);
let promise: Promise<boolean | undefined | void> = Promise.resolve(true);

const isAnyActionIsChange = map.actions.some((action) => {
return StaticReflect.getMetadata(SymbolMetadata.Action.isChange, action);
Expand Down
6 changes: 2 additions & 4 deletions src/Modes/Normal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ export class ModeNormal extends Mode {
return this._recordedCommandMaps;
}

protected onWillCommandMapMakesChanges(map: CommandMap): Promise<boolean> {
protected onWillCommandMapMakesChanges(map: CommandMap): void {
if (map.isRepeating) {
return Promise.resolve(false);
return;
}

const actions = map.actions.filter((action) => {
Expand All @@ -386,8 +386,6 @@ export class ModeNormal extends Mode {
isRepeating: true,
},
];

return Promise.resolve(true);
}

onDidRecordFinish(recordedCommandMaps: CommandMap[], lastModeID: ModeID): void {
Expand Down
4 changes: 1 addition & 3 deletions src/Modes/Visual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export class ModeVisual extends Mode {
return this._recordedCommandMaps;
}

protected onWillCommandMapMakesChanges(map: CommandMap): Promise<boolean> {
protected onWillCommandMapMakesChanges(map: CommandMap): void {
const actions = map.actions.filter((action) => {
return (
StaticReflect.getMetadata(SymbolMetadata.Action.shouldSkipOnRepeat, action) !== true
Expand All @@ -243,7 +243,5 @@ export class ModeVisual extends Mode {
isRepeating: true,
},
];

return Promise.resolve(true);
}
}
4 changes: 1 addition & 3 deletions src/Modes/VisualLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export class ModeVisualLine extends Mode {
return this._recordedCommandMaps;
}

protected onWillCommandMapMakesChanges(map: CommandMap): Promise<boolean> {
protected onWillCommandMapMakesChanges(map: CommandMap): void {
const actions = map.actions.filter((action) => {
return (
StaticReflect.getMetadata(SymbolMetadata.Action.shouldSkipOnRepeat, action) !== true
Expand All @@ -240,7 +240,5 @@ export class ModeVisualLine extends Mode {
isRepeating: true,
},
];

return Promise.resolve(true);
}
}

0 comments on commit 3a9c455

Please sign in to comment.