Skip to content

Commit

Permalink
Substitute with marks
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Aug 8, 2016
1 parent d790dbd commit 044fab9
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,12 @@ class CommandShowCommandLine extends BaseCommand {
public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.commandAction = VimSpecialCommands.ShowCommandLine;

if (vimState.currentMode === ModeName.Normal) {
vimState.commandInitialText = "";
} else {
vimState.commandInitialText = "'<,'>";
}

return vimState;
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/cmd_line/commands/substitute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,9 @@ export class SubstituteCommand extends node.CommandBase {
async execute(): Promise<void> {
const regex = this.getRegex(this._arguments);
const selection = vscode.window.activeTextEditor.selection;
const line = selection.start.isBefore(selection.end) ? selection.start.line : selection.end.line;

if (selection.start.isEqual(selection.end)) {
await this.replaceTextAtLine(selection.start.line, regex);
} else {
const originalContent = TextEditor.getText(selection);
const newContent = originalContent.replace(regex, this._arguments.replace);
await TextEditor.replace(selection, newContent);
}
await this.replaceTextAtLine(line, regex);
}

async executeWithRange(modeHandler : ModeHandler, range: node.LineRange) {
Expand Down
24 changes: 24 additions & 0 deletions src/cmd_line/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ module LexerFunctions {
case "-":
tokens.push(emitToken(TokenType.Minus, state)!);
continue;
case "'":
return lexMark;
default:
return lexCommand;
}
Expand All @@ -76,6 +78,28 @@ module LexerFunctions {
return null;
}

function lexMark(state: Scanner, tokens: Token[]): ILexFunction | null {
// The first token has already been lexed.
while (true) {
if (state.isAtEof) {
return null;
}

var c = state.next();
switch (c) {
case '<':
tokens.push(emitToken(TokenType.SelectionFirstLine, state)!);
break;
case '>':
tokens.push(emitToken(TokenType.SelectionLastLine, state)!);
break;
default:
state.backup();
return lexRange;
}
}
}

function lexLineRef(state : Scanner, tokens: Token[]): ILexFunction | null {
// The first digit has already been lexed.
while (true) {
Expand Down
6 changes: 6 additions & 0 deletions src/cmd_line/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ export class LineRange {
line = Math.max(0, line - 1);
line = Math.min(doc.document.lineCount, line);
return new vscode.Position(line, 0);
case token.TokenType.SelectionFirstLine:
let start = doc.selection.start.isBeforeOrEqual(doc.selection.end) ? doc.selection.start : doc.selection.end;
return new vscode.Position(start.line, 0);
case token.TokenType.SelectionLastLine:
let end = doc.selection.start.isAfter(doc.selection.end) ? doc.selection.start : doc.selection.end;
return new vscode.Position(end.line, 0);
default:
throw new Error("not implemented");
}
Expand Down
2 changes: 2 additions & 0 deletions src/cmd_line/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ function parseLineRange(state: ParserState, commandLine: node.CommandLine): IPar
case token.TokenType.Percent:
case token.TokenType.Comma:
case token.TokenType.LineNumber:
case token.TokenType.SelectionFirstLine:
case token.TokenType.SelectionLastLine:
commandLine.range.addToken(tok);
continue;
case token.TokenType.CommandName:
Expand Down
8 changes: 7 additions & 1 deletion src/cmd_line/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ export enum TokenType {
CommandArgs,
ForwardSearch,
ReverseSearch,
Offset
Offset,
/**
* Marks
*
*/
SelectionFirstLine,
SelectionLastLine
}

export class Token {
Expand Down
4 changes: 3 additions & 1 deletion src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ export class VimState {
*/
public commandAction = VimSpecialCommands.Nothing;

public commandInitialText = "";

public recordedState = new RecordedState();

/**
Expand Down Expand Up @@ -805,7 +807,7 @@ export class ModeHandler implements vscode.Disposable {

switch (command) {
case VimSpecialCommands.ShowCommandLine:
await showCmdLine("", this);
await showCmdLine(vimState.commandInitialText, this);
break;
case VimSpecialCommands.Dot:
if (!vimState.previousFullAction) {
Expand Down

0 comments on commit 044fab9

Please sign in to comment.