Skip to content

Commit

Permalink
Remove word in command line with <C-w>
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenguh committed Sep 2, 2019
1 parent 92c3359 commit fc8889a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
25 changes: 25 additions & 0 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2165,6 +2165,31 @@ class CommandEscInCommandline extends BaseCommand {
}
}

@RegisterAction
class CommandRemoveWordCommandline extends BaseCommand {
modes = [ModeName.CommandlineInProgress];
keys = ['<C-w>'];
runsOnceForEveryCursor() {
return this.keysPressed[0] === '\n';
}

public async exec(position: Position, vimState: VimState): Promise<VimState> {
const key = this.keysPressed[0];
const characterAt = Position.getWordLeft(
vimState.currentCommandlineText,
vimState.statusBarCursorCharacterPos
);

vimState.currentCommandlineText = characterAt
? vimState.currentCommandlineText.substring(0, characterAt)
: '';
vimState.statusBarCursorCharacterPos = vimState.currentCommandlineText.length;

commandLine.lastKeyPressed = key;
return vimState;
}
}

@RegisterAction
class CommandCtrlVInCommandline extends BaseCommand {
modes = [ModeName.CommandlineInProgress];
Expand Down
50 changes: 37 additions & 13 deletions src/common/motion/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,22 @@ export class Position extends vscode.Position {
);
}

/**
* Get the position of the word counting from the position specified.
* @param text The string to search from.
* @param pos The position of text to search from.
* @param inclusive true if we consider the pos a valid result, false otherwise.
* @returns The character position of the word to the left relative to the text and the pos.
* undefined if there is no word to the left of the postion.
*/
public static getWordLeft(
text: string,
pos: number,
inclusive: boolean = false
): number | undefined {
return Position.getWordLeftWtihRegex(text, pos, Position._nonWordCharRegex, inclusive);
}

/**
* Inclusive is true if we consider the current position a valid result, false otherwise.
*/
Expand Down Expand Up @@ -1030,7 +1046,7 @@ export class Position extends vscode.Position {
return regexp;
}

private getAllPositions(line: string, regex: RegExp): number[] {
private static getAllPositions(line: string, regex: RegExp): number[] {
let positions: number[] = [];
let result = regex.exec(line);

Expand Down Expand Up @@ -1068,21 +1084,29 @@ export class Position extends vscode.Position {
return positions;
}

private static getWordLeftWtihRegex(
text: string,
pos: number,
regex: RegExp,
forceFirst: boolean = false,
inclusive: boolean = false
): number | undefined {
const positions = Position.getAllPositions(text, regex);
return positions
.reverse()
.find(index => (index < pos && !inclusive) || (index <= pos && inclusive) || forceFirst);
}

/**
* Inclusive is true if we consider the current position a valid result, false otherwise.
*/
private getWordLeftWithRegex(regex: RegExp, inclusive: boolean = false): Position {
for (let currentLine = this.line; currentLine >= 0; currentLine--) {
let positions = this.getAllPositions(
const newCharacter = Position.getWordLeftWtihRegex(
TextEditor.getLineAt(new vscode.Position(currentLine, 0)).text,
regex
);
let newCharacter = _.find(
positions.reverse(),
index =>
(index < this.character && !inclusive) ||
(index <= this.character && inclusive) ||
currentLine !== this.line
this.character,
regex,
currentLine !== this.line
);

if (newCharacter !== undefined) {
Expand All @@ -1098,7 +1122,7 @@ export class Position extends vscode.Position {
*/
private getWordRightWithRegex(regex: RegExp, inclusive: boolean = false): Position {
for (let currentLine = this.line; currentLine < TextEditor.getLineCount(); currentLine++) {
let positions = this.getAllPositions(
let positions = Position.getAllPositions(
TextEditor.getLineAt(new vscode.Position(currentLine, 0)).text,
regex
);
Expand Down Expand Up @@ -1228,7 +1252,7 @@ export class Position extends vscode.Position {
private getCurrentSentenceEndWithRegex(regex: RegExp, inclusive: boolean): Position {
let paragraphEnd = this.getCurrentParagraphEnd();
for (let currentLine = this.line; currentLine <= paragraphEnd.line; currentLine++) {
let allPositions = this.getAllPositions(
let allPositions = Position.getAllPositions(
TextEditor.getLineAt(new vscode.Position(currentLine, 0)).text,
regex
);
Expand All @@ -1255,7 +1279,7 @@ export class Position extends vscode.Position {
return paragraphEnd;
} else {
for (let currentLine = this.line; currentLine <= paragraphEnd.line; currentLine++) {
let nonWhitePositions = this.getAllPositions(
let nonWhitePositions = Position.getAllPositions(
TextEditor.getLineAt(new vscode.Position(currentLine, 0)).text,
/\S/g
);
Expand Down

0 comments on commit fc8889a

Please sign in to comment.