From 1b39f67081feb1985176c51479fc2d29a119421a Mon Sep 17 00:00:00 2001 From: Jason Fields Date: Wed, 12 Aug 2020 20:56:45 -0400 Subject: [PATCH] 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. --- src/actions/base.ts | 103 +++++++++++++++++- src/actions/commands/actions.ts | 102 +---------------- src/actions/commands/commandLine.ts | 4 +- src/actions/commands/insert.ts | 3 +- .../plugins/easymotion/easymotion.cmd.ts | 3 +- src/actions/plugins/surround.ts | 3 +- src/mode/modeHandler.ts | 3 +- src/state/recordedState.ts | 3 +- 8 files changed, 110 insertions(+), 114 deletions(-) diff --git a/src/actions/base.ts b/src/actions/base.ts index 37a90ade2..d1e0e8834 100644 --- a/src/actions/base.ts +++ b/src/actions/base.ts @@ -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 , :, 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 { + throw new Error('Not implemented!'); + } + + /** + * Run the command the number of times VimState wants us to. + */ + public async execCount(position: Position, vimState: VimState): Promise { + 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, diff --git a/src/actions/commands/actions.ts b/src/actions/commands/actions.ts index d2bffa544..caae1aec9 100644 --- a/src/actions/commands/actions.ts +++ b/src/actions/commands/actions.ts @@ -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 , :, 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 { - throw new Error('Not implemented!'); - } - - /** - * Run the command the number of times VimState wants us to. - */ - public async execCount(position: Position, vimState: VimState): Promise { - 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 = [ diff --git a/src/actions/commands/commandLine.ts b/src/actions/commands/commandLine.ts index f8922e03d..4020ce564 100644 --- a/src/actions/commands/commandLine.ts +++ b/src/actions/commands/commandLine.ts @@ -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'; diff --git a/src/actions/commands/insert.ts b/src/actions/commands/insert.ts index c965e8ae5..bf72139b2 100644 --- a/src/actions/commands/insert.ts +++ b/src/actions/commands/insert.ts @@ -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, diff --git a/src/actions/plugins/easymotion/easymotion.cmd.ts b/src/actions/plugins/easymotion/easymotion.cmd.ts index 8bdd74782..f35bd7c3e 100644 --- a/src/actions/plugins/easymotion/easymotion.cmd.ts +++ b/src/actions/plugins/easymotion/easymotion.cmd.ts @@ -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, diff --git a/src/actions/plugins/surround.ts b/src/actions/plugins/surround.ts index 86ee9102d..013097ddd 100644 --- a/src/actions/plugins/surround.ts +++ b/src/actions/plugins/surround.ts @@ -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, diff --git a/src/mode/modeHandler.ts b/src/mode/modeHandler.ts index 10d1b92d2..e28d03716 100644 --- a/src/mode/modeHandler.ts +++ b/src/mode/modeHandler.ts @@ -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, diff --git a/src/state/recordedState.ts b/src/state/recordedState.ts index a47496461..bb7ec68f0 100644 --- a/src/state/recordedState.ts +++ b/src/state/recordedState.ts @@ -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';