Skip to content

Commit

Permalink
Merge pull request #1374 from xconverge/search-replace-remember-search
Browse files Browse the repository at this point in the history
fixes #1373
  • Loading branch information
johnfn authored Mar 9, 2017
2 parents 5312857 + 4c4f885 commit abe53c9
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/cmd_line/commands/substitute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class SubstituteCommand extends node.CommandBase {
return this._arguments;
}

getRegex(args: ISubstituteCommandArguments) {
getRegex(args: ISubstituteCommandArguments, modeHandler: ModeHandler) {
let jsRegexFlags = "";

if (args.flags & SubstituteFlags.ReplaceAll) {
Expand All @@ -70,6 +70,15 @@ export class SubstituteCommand extends node.CommandBase {
jsRegexFlags += "i";
}

// If no pattern is entered, use previous search state
if (args.pattern === "") {
const prevSearchState = modeHandler.vimState.globalState.searchStatePrevious;
if (prevSearchState) {
const prevSearchString = prevSearchState[prevSearchState.length - 1].searchString;
args.pattern = prevSearchString;
}
}

return new RegExp(args.pattern, jsRegexFlags);
}

Expand All @@ -82,8 +91,8 @@ export class SubstituteCommand extends node.CommandBase {
}
}

async execute(): Promise<void> {
const regex = this.getRegex(this._arguments);
async execute(modeHandler : ModeHandler): Promise<void> {
const regex = this.getRegex(this._arguments, modeHandler);
const selection = vscode.window.activeTextEditor.selection;
const line = selection.start.isBefore(selection.end) ? selection.start.line : selection.end.line;

Expand All @@ -110,7 +119,7 @@ export class SubstituteCommand extends node.CommandBase {
// TODO: Global Setting.
// TODO: There are differencies between Vim Regex and JS Regex.

let regex = this.getRegex(this._arguments);
let regex = this.getRegex(this._arguments, modeHandler);
for (let currentLine = startLine.line; currentLine <= endLine.line && currentLine < TextEditor.getLineCount(); currentLine++) {
await this.replaceTextAtLine(currentLine, regex);
}
Expand Down

0 comments on commit abe53c9

Please sign in to comment.