Skip to content

Commit

Permalink
More refactor (primarily of Position)
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Fields committed Mar 29, 2020
1 parent 192a9fd commit 38db172
Show file tree
Hide file tree
Showing 15 changed files with 334 additions and 403 deletions.
98 changes: 46 additions & 52 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ import { Position, PositionDiff, PositionDiffType } from './../../common/motion/
import { Range } from './../../common/motion/range';
import { NumericString } from './../../common/number/numericString';
import { configuration } from './../../configuration/configuration';
import {
Mode,
visualBlockGetTopLeftPosition,
visualBlockGetBottomRightPosition,
isVisualMode,
} from './../../mode/mode';
import { Mode, visualBlockGetTopLeftPosition, isVisualMode } from './../../mode/mode';
import { Register, RegisterMode } from './../../register/register';
import { SearchDirection, SearchState } from './../../state/searchState';
import { EditorScrollByUnit, EditorScrollDirection, TextEditor } from './../../textEditor';
Expand Down Expand Up @@ -603,11 +598,11 @@ abstract class CommandEditorScroll extends BaseCommand {
vimState.cursorStopPosition.line - visibleRange.start.line - timesToRepeat;
if (this.to === 'up' && scrolloff > linesAboveCursor) {
vimState.cursorStopPosition = vimState.cursorStopPosition
.getUpByCount(scrolloff - linesAboveCursor)
.getUp(scrolloff - linesAboveCursor)
.withColumn(vimState.desiredColumn);
} else if (this.to === 'down' && scrolloff > linesBelowCursor) {
vimState.cursorStopPosition = vimState.cursorStopPosition
.getDownByCount(scrolloff - linesBelowCursor)
.getDown(scrolloff - linesBelowCursor)
.withColumn(vimState.desiredColumn);
}

Expand Down Expand Up @@ -668,11 +663,11 @@ abstract class CommandScrollAndMoveCursor extends BaseCommand {
by: 'line',
value: scrollLines,
revealCursor: smoothScrolling,
select: [Mode.Visual, Mode.VisualBlock, Mode.VisualLine].includes(vimState.currentMode),
select: isVisualMode(vimState.currentMode),
});

const newPositionLine = clamp(
position.line + (this.to === 'down' ? 1 : -1) * scrollLines,
position.line + (this.to === 'down' ? scrollLines : -scrollLines),
0,
vimState.editor.document.lineCount - 1
);
Expand Down Expand Up @@ -862,8 +857,7 @@ class CommandOverrideCopy extends BaseCommand {
if (vimState.currentMode === Mode.Visual) {
text = vimState.cursors
.map((range) => {
const start = Position.EarlierOf(range.start, range.stop);
const stop = Position.LaterOf(range.start, range.stop);
const [start, stop] = Position.sorted(range.start, range.stop);
return vimState.editor.document.getText(new vscode.Range(start, stop.getRight()));
})
.join('\n');
Expand All @@ -872,14 +866,14 @@ class CommandOverrideCopy extends BaseCommand {
.map((range) => {
return vimState.editor.document.getText(
new vscode.Range(
Position.EarlierOf(range.start.getLineBegin(), range.stop.getLineBegin()),
Position.LaterOf(range.start.getLineEnd(), range.stop.getLineEnd())
Position.earlierOf(range.start.getLineBegin(), range.stop.getLineBegin()),
Position.laterOf(range.start.getLineEnd(), range.stop.getLineEnd())
)
);
})
.join('\n');
} else if (vimState.currentMode === Mode.VisualBlock) {
for (const { line } of Position.IterateLinesInBlock(vimState)) {
for (const { line } of TextEditor.iterateLinesInBlock(vimState)) {
text += line + '\n';
}
} else if (vimState.currentMode === Mode.Insert || vimState.currentMode === Mode.Normal) {
Expand Down Expand Up @@ -1498,8 +1492,7 @@ export class PutWithIndentCommand extends BaseCommand {
canBeRepeatedWithDot = true;

public async exec(position: Position, vimState: VimState): Promise<VimState> {
const result = await new PutCommand().exec(position, vimState, false, true);
return result;
return new PutCommand().exec(position, vimState, false, true);
}

public async execCount(position: Position, vimState: VimState): Promise<VimState> {
Expand Down Expand Up @@ -1590,9 +1583,7 @@ export class PutBeforeCommand extends BaseCommand {
const command = new PutCommand();
command.multicursorIndex = this.multicursorIndex;

const result = await command.exec(position, vimState, true);

return result;
return command.exec(position, vimState, true);
}
}

Expand Down Expand Up @@ -1642,9 +1633,9 @@ export class PutBeforeWithIndentCommand extends BaseCommand {
const result = await new PutCommand().exec(position, vimState, true, true);

if (vimState.effectiveRegisterMode === RegisterMode.LineWise) {
result.cursorStopPosition = result.cursorStopPosition
.getPreviousLineBegin()
.getFirstLineNonBlankChar();
result.cursorStopPosition = TextEditor.getFirstNonWhitespaceCharOnLine(
result.cursorStopPosition.getUp().line
);
}

return result;
Expand Down Expand Up @@ -1908,7 +1899,9 @@ class CommandCenterScrollFirstChar extends BaseCommand {
);

// Move cursor to first char of line
vimState.cursorStopPosition = vimState.cursorStopPosition.getFirstLineNonBlankChar();
vimState.cursorStopPosition = TextEditor.getFirstNonWhitespaceCharOnLine(
vimState.cursorStopPosition.line
);

return vimState;
}
Expand Down Expand Up @@ -1966,7 +1959,9 @@ class CommandTopScrollFirstChar extends BaseCommand {
});

// Move cursor to first char of line
vimState.cursorStopPosition = vimState.cursorStopPosition.getFirstLineNonBlankChar();
vimState.cursorStopPosition = TextEditor.getFirstNonWhitespaceCharOnLine(
vimState.cursorStopPosition.line
);

return vimState;
}
Expand Down Expand Up @@ -2024,7 +2019,9 @@ class CommandBottomScrollFirstChar extends BaseCommand {
});

// Move cursor to first char of line
vimState.cursorStopPosition = vimState.cursorStopPosition.getFirstLineNonBlankChar();
vimState.cursorStopPosition = TextEditor.getFirstNonWhitespaceCharOnLine(
vimState.cursorStopPosition.line
);

return vimState;
}
Expand Down Expand Up @@ -2129,7 +2126,7 @@ class CommandDeleteToLineEnd extends BaseCommand {

const linesDown = (vimState.recordedState.count || 1) - 1;
const start = position;
const end = position.getDownByCount(linesDown).getLineEnd().getLeftThroughLineBreaks();
const end = position.getDown(linesDown).getLineEnd().getLeftThroughLineBreaks();

return new operator.DeleteOperator(this.multicursorIndex).run(vimState, start, end);
}
Expand Down Expand Up @@ -2164,7 +2161,7 @@ class CommandChangeToLineEnd extends BaseCommand {
vimState,
position,
position
.getDownByCount(Math.max(0, count - 1))
.getDown(Math.max(0, count - 1))
.getLineEnd()
.getLeft()
);
Expand Down Expand Up @@ -2354,7 +2351,7 @@ class CommandVisualLineMode extends BaseCommand {
await vimState.setCurrentMode(Mode.VisualLine);

if (vimState.recordedState.count > 1) {
vimState.cursorStopPosition = vimState.cursorStopPosition.getDownByCount(
vimState.cursorStopPosition = vimState.cursorStopPosition.getDown(
vimState.recordedState.count - 1
);
}
Expand Down Expand Up @@ -2561,7 +2558,7 @@ export class CommandInsertAtFirstCharacter extends BaseCommand {

public async exec(position: Position, vimState: VimState): Promise<VimState> {
await vimState.setCurrentMode(Mode.Insert);
vimState.cursorStopPosition = position.getFirstLineNonBlankChar();
vimState.cursorStopPosition = TextEditor.getFirstNonWhitespaceCharOnLine(position.line);

return vimState;
}
Expand Down Expand Up @@ -2989,7 +2986,7 @@ export class ActionDeleteChar extends BaseCommand {
const state = await new operator.DeleteOperator(this.multicursorIndex).run(
vimState,
position,
position.getRightByCount(timesToRepeat - 1)
position.getRight(timesToRepeat - 1)
);

await state.setCurrentMode(Mode.Normal);
Expand Down Expand Up @@ -3035,7 +3032,7 @@ export class ActionDeleteLastChar extends BaseCommand {

const state = await new operator.DeleteOperator(this.multicursorIndex).run(
vimState,
position.getLeftByCount(timesToRepeat),
position.getLeft(timesToRepeat),
position.getLeft()
);

Expand Down Expand Up @@ -3246,8 +3243,7 @@ class ActionJoinVisualBlockMode extends BaseCommand {
}

vimState.currentRegisterMode = RegisterMode.CharacterWise;
vimState = await new ActionJoin().execJoinLines(start, end, vimState, 1);
return vimState;
return new ActionJoin().execJoinLines(start, end, vimState, 1);
}
}

Expand All @@ -3268,7 +3264,9 @@ class ActionJoinNoWhitespace extends BaseCommand {
let lineOne = TextEditor.getLineAt(position).text;
let lineTwo = TextEditor.getLineAt(position.getNextLineBegin()).text;

lineTwo = lineTwo.substring(position.getNextLineBegin().getFirstLineNonBlankChar().character);
lineTwo = lineTwo.substring(
TextEditor.getFirstNonWhitespaceCharOnLine(position.getDown().line).character
);

let resultLine = lineOne + lineTwo;

Expand Down Expand Up @@ -3415,9 +3413,7 @@ class ActionReplaceCharacterVisual extends BaseCommand {
let end = vimState.cursorStopPosition;

// If selection is reversed, reorganize it so that the text replace logic always works
if (end.isBeforeOrEqual(start)) {
[start, end] = [end, start];
}
[start, end] = Position.sorted(start, end);

// Limit to not replace EOL
const textLength = TextEditor.getLineAt(end).text.length;
Expand Down Expand Up @@ -3493,7 +3489,7 @@ class ActionReplaceCharacterVisualBlock extends BaseCommand {
toInsert = TextEditor.getTabCharacter(vimState.editor);
}

for (const { start, end } of Position.IterateLinesInBlock(vimState)) {
for (const { start, end } of TextEditor.iterateLinesInBlock(vimState)) {
if (end.isBeforeOrEqual(start)) {
continue;
}
Expand Down Expand Up @@ -3530,7 +3526,7 @@ class ActionDeleteVisualBlock extends BaseCommand {
public async exec(position: Position, vimState: VimState): Promise<VimState> {
const lines: string[] = [];

for (const { line, start, end } of Position.IterateLinesInBlock(vimState)) {
for (const { line, start, end } of TextEditor.iterateLinesInBlock(vimState)) {
lines.push(line);
vimState.recordedState.transformations.push({
type: 'deleteRange',
Expand Down Expand Up @@ -3565,7 +3561,7 @@ class ActionShiftDVisualBlock extends BaseCommand {
}

public async exec(position: Position, vimState: VimState): Promise<VimState> {
for (const { start } of Position.IterateLinesInBlock(vimState)) {
for (const { start } of TextEditor.iterateLinesInBlock(vimState)) {
vimState.recordedState.transformations.push({
type: 'deleteRange',
range: new Range(start, start.getLineEnd()),
Expand Down Expand Up @@ -3598,7 +3594,7 @@ class ActionGoToInsertVisualBlockMode extends BaseCommand {
vimState.isMultiCursor = true;
vimState.isFakeMultiCursor = true;

for (const { line, start } of Position.IterateLinesInBlock(vimState)) {
for (const { line, start } of TextEditor.iterateLinesInBlock(vimState)) {
if (line === '' && start.character !== 0) {
continue;
}
Expand All @@ -3618,7 +3614,7 @@ class ActionChangeInVisualBlockMode extends BaseCommand {
}

public async exec(position: Position, vimState: VimState): Promise<VimState> {
for (const { start, end } of Position.IterateLinesInBlock(vimState)) {
for (const { start, end } of TextEditor.iterateLinesInBlock(vimState)) {
vimState.recordedState.transformations.push({
type: 'deleteRange',
range: new Range(start, end),
Expand All @@ -3630,7 +3626,7 @@ class ActionChangeInVisualBlockMode extends BaseCommand {
vimState.isMultiCursor = true;
vimState.isFakeMultiCursor = true;

for (const { start } of Position.IterateLinesInBlock(vimState)) {
for (const { start } of TextEditor.iterateLinesInBlock(vimState)) {
vimState.cursors.push(new Range(start, start));
}
vimState.cursors = vimState.cursors.slice(1);
Expand All @@ -3648,7 +3644,7 @@ class ActionChangeToEOLInVisualBlockMode extends BaseCommand {
}

public async exec(position: Position, vimState: VimState): Promise<VimState> {
for (const { start } of Position.IterateLinesInBlock(vimState)) {
for (const { start } of TextEditor.iterateLinesInBlock(vimState)) {
vimState.recordedState.transformations.push({
type: 'deleteRange',
range: new Range(start, start.getLineEnd()),
Expand All @@ -3660,7 +3656,7 @@ class ActionChangeToEOLInVisualBlockMode extends BaseCommand {
vimState.isMultiCursor = true;
vimState.isFakeMultiCursor = true;

for (const { end } of Position.IterateLinesInBlock(vimState)) {
for (const { end } of TextEditor.iterateLinesInBlock(vimState)) {
vimState.cursors.push(new Range(end, end));
}
vimState.cursors = vimState.cursors.slice(1);
Expand Down Expand Up @@ -3782,7 +3778,7 @@ class ActionGoToInsertVisualBlockModeAppend extends BaseCommand {
vimState.isMultiCursor = true;
vimState.isFakeMultiCursor = true;

for (const { line, end } of Position.IterateLinesInBlock(vimState)) {
for (const { line, end } of TextEditor.iterateLinesInBlock(vimState)) {
if (line.trim() === '') {
vimState.recordedState.transformations.push({
type: 'replaceText',
Expand Down Expand Up @@ -3919,7 +3915,7 @@ abstract class IncrementDecrementNumberAction extends BaseCommand {
? position
: position.getWordLeft(true);

for (let { start, end, word } of Position.IterateWords(whereToStart)) {
for (let { start, end, word } of TextEditor.iterateWords(whereToStart)) {
// '-' doesn't count as a word, but is important to include in parsing
// the number, as long as it is not just part of the word (-foo2 for example)
if (text[start.character - 1] === '-' && /\d/.test(text[start.character])) {
Expand All @@ -3942,9 +3938,7 @@ abstract class IncrementDecrementNumberAction extends BaseCommand {
start,
end
);
vimState.cursorStopPosition = vimState.cursorStopPosition.getLeftByCount(
num.suffix.length
);
vimState.cursorStopPosition = vimState.cursorStopPosition.getLeft(num.suffix.length);
return vimState;
} else {
word = word.slice(num.prefix.length + num.value.toString().length);
Expand Down Expand Up @@ -4096,7 +4090,7 @@ class ActionOverrideCmdDInsert extends BaseCommand {
const matchWordLength =
matchWordPos.getLeft().getCurrentWordEnd(true).getRight().character -
matchWordPos.getWordLeft(false).character;
const wordBegin = curPos.getLeftByCount(matchWordLength);
const wordBegin = curPos.getLeft(matchWordLength);
return new vscode.Selection(wordBegin, curPos);
}
}
Expand Down
20 changes: 8 additions & 12 deletions src/actions/commands/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,16 @@ class CommandEscInsertMode extends BaseCommand {
const changeAction = vimState.recordedState.actionsRun[
vimState.recordedState.actionsRun.length - 2
] as DocumentContentChangeAction;
const changesArray = changeAction.contentChanges;
let docChanges: vscode.TextDocumentContentChangeEvent[] = [];

for (const change of changesArray) {
docChanges.push(change.textDiff);
}
const docChanges = changeAction.contentChanges.map((change) => change.textDiff);

let positionDiff = new PositionDiff();
// Add count amount of inserts in the case of 4i=<esc>
for (let i = 0; i < vimState.recordedState.count - 1; i++) {
// If this is the last transform, move cursor back one character
if (i === vimState.recordedState.count - 2) {
positionDiff = new PositionDiff({ character: -1 });
}
const positionDiff =
i === vimState.recordedState.count - 2
? new PositionDiff({ character: -1 })
: new PositionDiff();

// Add a transform containing the change
vimState.recordedState.transformations.push({
Expand Down Expand Up @@ -171,7 +167,7 @@ class CommandInsertBelowChar extends BaseCommand {
return vimState;
}

const charBelowCursorPosition = position.getDownByCount(1);
const charBelowCursorPosition = position.getDown();

if (charBelowCursorPosition.isLineEnd()) {
return vimState;
Expand Down Expand Up @@ -450,7 +446,7 @@ class CommandCtrlW extends BaseCommand {
keys = ['<C-w>'];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
let wordBegin;
let wordBegin: Position;
if (position.isInLeadingWhitespace()) {
wordBegin = position.getLineBegin();
} else if (position.isLineBeginning()) {
Expand Down Expand Up @@ -514,7 +510,7 @@ class CommandInsertAboveChar extends BaseCommand {
return vimState;
}

const charAboveCursorPosition = position.getUpByCount(1);
const charAboveCursorPosition = position.getUp(1);

if (charAboveCursorPosition.isLineEnd()) {
return vimState;
Expand Down
Loading

0 comments on commit 38db172

Please sign in to comment.