Skip to content

Commit

Permalink
Refactor Position and PositionDiff
Browse files Browse the repository at this point in the history
The biggest change is in how PositionDiffs are constructed - now with a object parameter, so it's much easier to understand what the intention is.
BOL diffs no longer accept a character - they bring you to the beginning of the line.
And a few other small tweaks. This probably should've been split across a couple commits, but too late for that now.
  • Loading branch information
J-Fields committed Dec 30, 2019
1 parent ea28879 commit dd61bda
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 176 deletions.
81 changes: 52 additions & 29 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { FileCommand } from './../../cmd_line/commands/file';
import { OnlyCommand } from './../../cmd_line/commands/only';
import { QuitCommand } from './../../cmd_line/commands/quit';
import { Tab, TabCommand } from './../../cmd_line/commands/tab';
import { Position, PositionDiff } from './../../common/motion/position';
import { Position, PositionDiff, PositionDiffType } from './../../common/motion/position';
import { Range } from './../../common/motion/range';
import { NumericString } from './../../common/number/numericString';
import { configuration } from './../../configuration/configuration';
Expand Down Expand Up @@ -639,7 +639,7 @@ class CommandEscReplaceMode extends BaseCommand {
type: 'insertText',
text: textToAdd,
position: position,
diff: new PositionDiff(0, -1),
diff: new PositionDiff({ character: -1 }),
});

await vimState.setCurrentMode(Mode.Normal);
Expand Down Expand Up @@ -892,7 +892,7 @@ class CommandReplaceInReplaceMode extends BaseCommand {
text: replaceState.originalChars[position.character - 1],
start: position.getLeft(),
end: position,
diff: new PositionDiff(0, -1),
diff: new PositionDiff({ character: -1 }),
});
}

Expand All @@ -904,7 +904,7 @@ class CommandReplaceInReplaceMode extends BaseCommand {
text: char,
start: position,
end: position.getRight(),
diff: new PositionDiff(0, 1),
diff: new PositionDiff({ character: 1 }),
});
} else {
vimState.recordedState.transformations.push({
Expand Down Expand Up @@ -1498,7 +1498,7 @@ export class PutCommand extends BaseCommand {

let textToAdd: string;
let whereToAddText: Position;
let diff = new PositionDiff(0, 0);
let diff = new PositionDiff();

const noPrevLine = vimState.cursorStartPosition.isAtDocumentBegin();
const noNextLine = vimState.cursorStopPosition.isAtDocumentEnd();
Expand Down Expand Up @@ -1585,7 +1585,7 @@ export class PutCommand extends BaseCommand {
register.registerMode === RegisterMode.LineWise
) {
const numNewline = [...text].filter(c => c === '\n').length;
diff = PositionDiff.NewBOLDiff(-numNewline - (noNextLine ? 0 : 1));
diff = PositionDiff.newBOLDiff(-numNewline - (noNextLine ? 0 : 1));
} else if (register.registerMode === RegisterMode.LineWise) {
const check = text.match(/^\s*/);
let numWhitespace = 0;
Expand All @@ -1595,35 +1595,59 @@ export class PutCommand extends BaseCommand {
}

if (after) {
diff = PositionDiff.NewBOLDiff(-numNewlines - 1, numWhitespace);
diff = new PositionDiff({
line: -numNewlines - 1,
character: numWhitespace,
type: PositionDiffType.ExactCharacter,
});
} else {
diff = PositionDiff.NewBOLDiff(currentLineLength > 0 ? 1 : -numNewlines, numWhitespace);
diff = new PositionDiff({
line: currentLineLength > 0 ? 1 : -numNewlines,
character: numWhitespace,
type: PositionDiffType.ExactCharacter,
});
}
} else {
if (!text.includes('\n')) {
if (!position.isLineEnd()) {
if (register.registerMode === RegisterMode.BlockWise) {
if (after) {
diff = new PositionDiff(0, -1 * text.length);
diff = new PositionDiff({
character: -text.length,
});
} else {
diff = new PositionDiff(0, 1);
diff = new PositionDiff({
character: 1,
});
}
} else {
if (after) {
diff = new PositionDiff(0, -1);
diff = new PositionDiff({
character: -1,
});
} else {
diff = new PositionDiff(0, textToAdd.length);
diff = new PositionDiff({
character: textToAdd.length,
});
}
}
}
} else {
if (position.isLineEnd()) {
diff = PositionDiff.NewBOLDiff(-numNewlines, position.character);
diff = new PositionDiff({
line: -numNewlines,
character: position.character,
});
} else {
if (after) {
diff = PositionDiff.NewBOLDiff(-numNewlines, position.character);
diff = new PositionDiff({
line: -numNewlines,
character: position.character,
});
} else {
diff = new PositionDiff(0, 1);
diff = new PositionDiff({
character: 1,
});
}
}
}
Expand Down Expand Up @@ -1696,7 +1720,7 @@ export class PutCommand extends BaseCommand {

result.recordedState.transformations.push({
type: 'moveCursor',
diff: new PositionDiff(-numNewlines + 1, 0),
diff: new PositionDiff({ line: -numNewlines + 1 }),
cursorIndex: this.multicursorIndex,
});

Expand Down Expand Up @@ -1745,7 +1769,7 @@ export class GPutCommand extends BaseCommand {
if (vimState.effectiveRegisterMode === RegisterMode.LineWise) {
result.recordedState.transformations.push({
type: 'moveCursor',
diff: PositionDiff.NewBOLDiff(addedLinesCount, 0),
diff: PositionDiff.newBOLDiff(addedLinesCount),
cursorIndex: this.multicursorIndex,
});
}
Expand Down Expand Up @@ -1888,7 +1912,7 @@ export class GPutBeforeCommand extends BaseCommand {
if (vimState.effectiveRegisterMode === RegisterMode.LineWise) {
result.recordedState.transformations.push({
type: 'moveCursor',
diff: PositionDiff.NewBOLDiff(addedLinesCount, 0),
diff: PositionDiff.newBOLDiff(addedLinesCount),
cursorIndex: this.multicursorIndex,
});
}
Expand Down Expand Up @@ -3646,18 +3670,18 @@ class ActionJoin extends BaseCommand {
startLineNumber = position.line;
startColumn = 0;
endLineNumber = startLineNumber + count;
endColumn = TextEditor.getLineMaxColumn(endLineNumber);
endColumn = TextEditor.getLineLength(endLineNumber);
} else {
startLineNumber = position.line;
startColumn = 0;
endLineNumber = position.line;
endColumn = TextEditor.getLineMaxColumn(endLineNumber);
endColumn = TextEditor.getLineLength(endLineNumber);
}
} else {
startLineNumber = startPosition.line;
startColumn = 0;
endLineNumber = position.line;
endColumn = TextEditor.getLineMaxColumn(endLineNumber);
endColumn = TextEditor.getLineLength(endLineNumber);
}

let trimmedLinesContent = TextEditor.getLineAt(startPosition).text;
Expand Down Expand Up @@ -3715,10 +3739,9 @@ class ActionJoin extends BaseCommand {
text: trimmedLinesContent,
start: deleteStartPosition,
end: deleteEndPosition,
diff: new PositionDiff(
0,
trimmedLinesContent.length - columnDeltaOffset - position.character
),
diff: new PositionDiff({
character: trimmedLinesContent.length - columnDeltaOffset - position.character,
}),
});
} else {
vimState.recordedState.transformations.push({
Expand Down Expand Up @@ -3836,7 +3859,7 @@ class ActionJoinNoWhitespace extends BaseCommand {
type: 'insertText',
text: resultLine,
position: position,
diff: new PositionDiff(0, -lineTwo.length),
diff: new PositionDiff({ character: -lineTwo.length }),
});

newState.cursorStopPosition = new Position(position.line, lineOne.length);
Expand Down Expand Up @@ -3915,7 +3938,7 @@ class ActionReplaceCharacter extends BaseCommand {
vimState.recordedState.transformations.push({
type: 'tab',
cursorIndex: this.multicursorIndex,
diff: new PositionDiff(0, -1),
diff: new PositionDiff({ character: -1 }),
});
} else if (toReplace === '\n') {
// A newline replacement always inserts exactly one newline (regardless
Expand All @@ -3925,15 +3948,15 @@ class ActionReplaceCharacter extends BaseCommand {
text: '\n',
start: position,
end: endPos,
diff: PositionDiff.NewBOLDiff(1),
diff: PositionDiff.newBOLDiff(1),
});
} else {
vimState.recordedState.transformations.push({
type: 'replaceText',
text: toReplace.repeat(timesToRepeat),
start: position,
end: endPos,
diff: new PositionDiff(0, timesToRepeat - 1),
diff: new PositionDiff({ character: timesToRepeat - 1 }),
});
}
return vimState;
Expand Down
6 changes: 3 additions & 3 deletions src/actions/commands/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ class CommandEscInsertMode extends BaseCommand {
docChanges.push(change.textDiff);
}

let positionDiff = new PositionDiff(0, 0);
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(0, -1);
positionDiff = new PositionDiff({ character: -1 });
}

// Add a transform containing the change
Expand Down Expand Up @@ -205,7 +205,7 @@ class CommandInsertIndentInCurrentLine extends BaseCommand {
text: TextEditor.setIndentationLevel(originalText, newIndentationWidth),
start: position.getLineBegin(),
end: position.getLineEnd(),
diff: new PositionDiff(0, newIndentationWidth - indentationWidth),
diff: new PositionDiff({ character: newIndentationWidth - indentationWidth }),
});

return vimState;
Expand Down
2 changes: 1 addition & 1 deletion src/actions/motion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,7 @@ export abstract class MoveInsideCharacter extends ExpandingSelection {
return {
start: startPos,
stop: endPos,
diff: new PositionDiff(0, startPos === position ? 1 : 0),
diff: new PositionDiff({ character: startPos === position ? 1 : 0 }),
};
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/actions/operator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from 'vscode';

import { Position, PositionDiff } from './../common/motion/position';
import { Position, PositionDiff, PositionDiffType } from './../common/motion/position';
import { Range } from './../common/motion/range';
import { configuration } from './../configuration/configuration';
import { Mode, isVisualMode } from './../mode/mode';
Expand Down Expand Up @@ -183,7 +183,7 @@ export class DeleteOperator extends BaseOperator {
Register.put(text, vimState, this.multicursorIndex);
}

let diff = new PositionDiff(0, 0);
let diff = new PositionDiff();
let resultingPosition: Position;

if (currentMode === Mode.Visual) {
Expand All @@ -192,14 +192,16 @@ export class DeleteOperator extends BaseOperator {

if (start.character > TextEditor.getLineAt(start).text.length) {
resultingPosition = start.getLeft();
diff = new PositionDiff(0, -1);
diff = new PositionDiff({ character: -1 });
} else {
resultingPosition = start;
}

if (registerMode === RegisterMode.LineWise) {
resultingPosition = resultingPosition.obeyStartOfLine();
diff = PositionDiff.NewBOLDiff(0, 0, true);
diff = new PositionDiff({
type: PositionDiffType.ObeyStartOfLine,
});
}

vimState.recordedState.transformations.push({
Expand Down Expand Up @@ -598,7 +600,7 @@ export class ChangeOperator extends BaseOperator {
// the line. We do want to run delete if it is a multiline change though ex. c}
vimState.currentRegisterMode = RegisterMode.CharacterWise;
if (
Position.getLineLength(TextEditor.getLineAt(start).lineNumber) !== 0 ||
TextEditor.getLineLength(TextEditor.getLineAt(start).lineNumber) !== 0 ||
end.line !== start.line
) {
if (isLineWise) {
Expand Down Expand Up @@ -655,7 +657,7 @@ export class ChangeOperator extends BaseOperator {
vimState.recordedState.transformations.push({
type: 'reindent',
cursorIndex: this.multicursorIndex,
diff: new PositionDiff(0, 1), // Handle transition from Normal to Insert modes
diff: new PositionDiff({ character: 1 }), // Handle transition from Normal to Insert modes
});
}
}
Expand Down Expand Up @@ -1100,7 +1102,7 @@ class ActionVisualReflowParagraph extends BaseOperator {
start: start,
end: end,
// Move cursor to front of line to realign the view
diff: PositionDiff.NewBOLDiff(0, 0),
diff: PositionDiff.newBOLDiff(),
});

await vimState.setCurrentMode(Mode.Normal);
Expand Down
4 changes: 2 additions & 2 deletions src/actions/textobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,9 @@ abstract class IndentObjectMatch extends TextObjectMovement {
// TextEditor.getLineMaxColumn throws when given line 0, which we don't
// care about here since it just means this text object wouldn't work on a
// single-line document.
let endCharacter;
let endCharacter: number;
if (endLineNumber === TextEditor.getLineCount() - 1 || vimState.currentMode === Mode.Visual) {
endCharacter = TextEditor.getLineMaxColumn(endLineNumber);
endCharacter = TextEditor.getLineLength(endLineNumber);
} else {
endCharacter = 0;
endLineNumber++;
Expand Down
2 changes: 1 addition & 1 deletion src/cmd_line/commandLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class CommandLine {
};
}

public async ShowHistory(initialText: string, vimState: VimState): Promise<string | undefined> {
public async showHistory(initialText: string): Promise<string | undefined> {
if (!vscode.window.activeTextEditor) {
this._logger.debug('No active document.');
return '';
Expand Down
2 changes: 1 addition & 1 deletion src/common/matching/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export class PairMatcher {
let isNextMatch = false;

if (charactersToMatch.includes(deleteText)) {
const matchPosition = currentPosition.add(new PositionDiff(0, 1));
const matchPosition = currentPosition.add(new PositionDiff({ character: 1 }));
matchRange = new vscode.Range(matchPosition, matchPosition.getLeftThroughLineBreaks());
isNextMatch =
vscode.window.activeTextEditor!.document.getText(matchRange) ===
Expand Down

0 comments on commit dd61bda

Please sign in to comment.