Move BaseCommand from actions.ts to base.ts

That's surely where it belongs, and in general, I'd like to split actions.ts up.
This commit is contained in:
Jason Fields 2020-08-12 20:56:45 -04:00
parent 50edf6422a
commit 1b39f67081
8 changed files with 110 additions and 114 deletions

View File

@ -1,7 +1,10 @@
import { Position } from '../common/motion/position';
import { Range } from '../common/motion/range';
import { Notation } from '../configuration/notation';
import { isTextTransformation } from '../transformations/transformations';
import { configuration } from './../configuration/configuration';
import { Mode } from './../mode/mode';
import { VimState } from './../state/vimState';
import { Notation } from '../configuration/notation';
export class BaseAction {
/**
@ -159,6 +162,104 @@ export class BaseAction {
}
}
/**
* A command is something like <Esc>, :, v, i, etc.
*/
export abstract class BaseCommand extends BaseAction {
/**
* If isCompleteAction is true, then triggering this command is a complete action -
* that means that we'll go and try to run it.
*/
isCompleteAction = true;
/**
* If isJump is true, then the cursor position will be added to the jump list on completion.
*/
isJump = false;
multicursorIndex: number | undefined = undefined;
/**
* In multi-cursor mode, do we run this command for every cursor, or just once?
*/
public runsOnceForEveryCursor(): boolean {
return true;
}
/**
* If true, exec() will get called N times where N is the count.
*
* If false, exec() will only be called once, and you are expected to
* handle count prefixes (e.g. the 3 in 3w) yourself.
*/
runsOnceForEachCountPrefix = false;
canBeRepeatedWithDot = false;
/**
* Run the command a single time.
*/
public async exec(position: Position, vimState: VimState): Promise<VimState> {
throw new Error('Not implemented!');
}
/**
* Run the command the number of times VimState wants us to.
*/
public async execCount(position: Position, vimState: VimState): Promise<VimState> {
let timesToRepeat = this.runsOnceForEachCountPrefix ? vimState.recordedState.count || 1 : 1;
if (!this.runsOnceForEveryCursor()) {
for (let i = 0; i < timesToRepeat; i++) {
vimState = await this.exec(position, vimState);
}
for (const transformation of vimState.recordedState.transformations) {
if (isTextTransformation(transformation) && transformation.cursorIndex === undefined) {
transformation.cursorIndex = 0;
}
}
return vimState;
}
let resultingCursors: Range[] = [];
const cursorsToIterateOver = vimState.cursors
.map((x) => new Range(x.start, x.stop))
.sort((a, b) =>
a.start.line > b.start.line ||
(a.start.line === b.start.line && a.start.character > b.start.character)
? 1
: -1
);
let cursorIndex = 0;
for (const { start, stop } of cursorsToIterateOver) {
this.multicursorIndex = cursorIndex++;
vimState.cursorStopPosition = stop;
vimState.cursorStartPosition = start;
for (let j = 0; j < timesToRepeat; j++) {
vimState = await this.exec(stop, vimState);
}
resultingCursors.push(new Range(vimState.cursorStartPosition, vimState.cursorStopPosition));
for (const transformation of vimState.recordedState.transformations) {
if (isTextTransformation(transformation) && transformation.cursorIndex === undefined) {
transformation.cursorIndex = this.multicursorIndex;
}
}
}
vimState.cursors = resultingCursors;
return vimState;
}
}
export enum KeypressState {
WaitingOnKeys,
NoPossibleMatch,

View File

@ -30,7 +30,7 @@ import { Register, RegisterMode } from './../../register/register';
import { SearchDirection, SearchState } from './../../state/searchState';
import { EditorScrollByUnit, EditorScrollDirection, TextEditor } from './../../textEditor';
import { isTextTransformation, Transformation } from './../../transformations/transformations';
import { RegisterAction } from './../base';
import { RegisterAction, BaseCommand } from './../base';
import { BaseAction } from './../base';
import { commandLine } from './../../cmd_line/commandLine';
import * as operator from './../operator';
@ -176,106 +176,6 @@ export class DocumentContentChangeAction extends BaseAction {
}
}
/**
* A command is something like <Esc>, :, v, i, etc.
*/
export abstract class BaseCommand extends BaseAction {
/**
* If isCompleteAction is true, then triggering this command is a complete action -
* that means that we'll go and try to run it.
*/
isCompleteAction = true;
/**
* If isJump is true, then the cursor position will be added to the jump list on completion.
*/
isJump = false;
multicursorIndex: number | undefined = undefined;
/**
* In multi-cursor mode, do we run this command for every cursor, or just once?
*/
public runsOnceForEveryCursor(): boolean {
return true;
}
/**
* If true, exec() will get called N times where N is the count.
*
* If false, exec() will only be called once, and you are expected to
* handle count prefixes (e.g. the 3 in 3w) yourself.
*/
runsOnceForEachCountPrefix = false;
canBeRepeatedWithDot = false;
/**
* Run the command a single time.
*/
public async exec(position: Position, vimState: VimState): Promise<VimState> {
throw new Error('Not implemented!');
}
/**
* Run the command the number of times VimState wants us to.
*/
public async execCount(position: Position, vimState: VimState): Promise<VimState> {
let timesToRepeat = this.runsOnceForEachCountPrefix ? vimState.recordedState.count || 1 : 1;
if (!this.runsOnceForEveryCursor()) {
for (let i = 0; i < timesToRepeat; i++) {
vimState = await this.exec(position, vimState);
}
for (const transformation of vimState.recordedState.transformations) {
if (isTextTransformation(transformation) && transformation.cursorIndex === undefined) {
transformation.cursorIndex = 0;
}
}
return vimState;
}
let resultingCursors: Range[] = [];
const cursorsToIterateOver = vimState.cursors
.map((x) => new Range(x.start, x.stop))
.sort((a, b) =>
a.start.line > b.start.line ||
(a.start.line === b.start.line && a.start.character > b.start.character)
? 1
: -1
);
let cursorIndex = 0;
for (const { start, stop } of cursorsToIterateOver) {
this.multicursorIndex = cursorIndex++;
vimState.cursorStopPosition = stop;
vimState.cursorStartPosition = start;
for (let j = 0; j < timesToRepeat; j++) {
vimState = await this.exec(stop, vimState);
}
resultingCursors.push(new Range(vimState.cursorStartPosition, vimState.cursorStopPosition));
for (const transformation of vimState.recordedState.transformations) {
if (isTextTransformation(transformation) && transformation.cursorIndex === undefined) {
transformation.cursorIndex = this.multicursorIndex;
}
}
}
vimState.cursors = resultingCursors;
return vimState;
}
}
// begin actions
@RegisterAction
class DisableExtension extends BaseCommand {
modes = [

View File

@ -1,7 +1,7 @@
import * as vscode from 'vscode';
import { RegisterAction } from '../base';
import { BaseCommand, CommandShowCommandHistory, CommandShowSearchHistory } from './actions';
import { RegisterAction, BaseCommand } from '../base';
import { CommandShowCommandHistory, CommandShowSearchHistory } from './actions';
import { Mode } from '../../mode/mode';
import { VimState } from '../../state/vimState';
import { commandLine } from '../../cmd_line/commandLine';

View File

@ -11,10 +11,9 @@ import { configuration } from './../../configuration/configuration';
import { Mode } from './../../mode/mode';
import { Register, RegisterMode } from './../../register/register';
import { TextEditor } from './../../textEditor';
import { RegisterAction } from './../base';
import { RegisterAction, BaseCommand } from './../base';
import { ArrowsInInsertMode } from './../motion';
import {
BaseCommand,
CommandInsertAfterCursor,
CommandInsertAtCursor,
CommandInsertAtFirstCharacter,

View File

@ -2,8 +2,7 @@ import { VimState } from '../../../state/vimState';
import { Position } from './../../../common/motion/position';
import { configuration } from './../../../configuration/configuration';
import { Mode, isVisualMode } from './../../../mode/mode';
import { RegisterAction } from './../../base';
import { BaseCommand } from './../../commands/actions';
import { RegisterAction, BaseCommand } from './../../base';
import { EasyMotion } from './easymotion';
import {
EasyMotionCharMoveOpions,

View File

@ -5,8 +5,7 @@ import { Range } from './../../common/motion/range';
import { configuration } from './../../configuration/configuration';
import { Mode } from './../../mode/mode';
import { TextEditor } from './../../textEditor';
import { RegisterAction } from './../base';
import { BaseCommand } from './../commands/actions';
import { RegisterAction, BaseCommand } from './../base';
import { BaseMovement, IMovement } from '../baseMotion';
import {
MoveABacktick,

View File

@ -1,6 +1,6 @@
import * as vscode from 'vscode';
import { Actions, BaseAction, KeypressState } from './../actions/base';
import { Actions, BaseAction, KeypressState, BaseCommand } from './../actions/base';
import { BaseMovement } from '../actions/baseMotion';
import { CommandInsertInInsertMode, CommandInsertPreviousText } from './../actions/commands/insert';
import { Jump } from '../jumps/jump';
@ -22,7 +22,6 @@ import { configuration } from '../configuration/configuration';
import { decoration } from '../configuration/decoration';
import { scrollView } from '../util/util';
import {
BaseCommand,
CommandQuitRecordMacro,
DocumentContentChangeAction,
ActionOverrideCmdD,

View File

@ -1,7 +1,6 @@
import { configuration } from '../configuration/configuration';
import { Mode } from '../mode/mode';
import { BaseAction } from './../actions/base';
import { BaseCommand } from './../actions/commands/actions';
import { BaseAction, BaseCommand } from './../actions/base';
import { BaseOperator } from './../actions/operator';
import { PositionDiff } from './../common/motion/position';
import { Transformation } from './../transformations/transformations';