Skip to content

Commit

Permalink
Merge pull request #3055 from tomotg/fix-substitute-with-gc-flag
Browse files Browse the repository at this point in the history
Fix substitute with gc flag
  • Loading branch information
jpoon authored Sep 28, 2018
2 parents bec2758 + afc2ca4 commit 13d05e7
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 6 deletions.
122 changes: 122 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@
"@types/lodash": "4.14.116",
"@types/mocha": "5.2.5",
"@types/node": "9.6.32",
"@types/sinon": "5.0.2",
"gulp": "4.0.0",
"gulp-bump": "3.1.1",
"gulp-git": "2.8.0",
Expand All @@ -698,6 +699,7 @@
"mocha": "5.2.0",
"plugin-error": "1.0.1",
"prettier": "1.14.3",
"sinon": "6.3.4",
"tslint": "5.11.0",
"typescript": "3.0.3",
"vscode": "1.1.21"
Expand Down
12 changes: 6 additions & 6 deletions src/cmd_line/commands/substitute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ export class SubstituteCommand extends node.CommandBase {
!(this._arguments.flags & SubstituteFlags.ConfirmEach) ||
(await this.confirmReplacement(regex.source, line, vimState, match, matchPos))
) {
newContent = newContent.replace(nonGlobalRegex, this._arguments.replace);
await TextEditor.replace(
new vscode.Range(line, 0, line, originalContent.length),
newContent
);
const rangeEnd = newContent.length;
newContent =
newContent.slice(0, matchPos) +
newContent.slice(matchPos).replace(nonGlobalRegex, this._arguments.replace);
await TextEditor.replace(new vscode.Range(line, 0, line, rangeEnd), newContent);

vimState.globalState.jumpTracker.recordJump(
new Jump({
Expand All @@ -137,7 +137,7 @@ export class SubstituteCommand extends node.CommandBase {
Jump.fromStateNow(vimState)
);
}
matchPos += match.length;
matchPos += this._arguments.replace.length;
}
} else {
await TextEditor.replace(
Expand Down
31 changes: 31 additions & 0 deletions test/cmd_line/substitute.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as sinon from 'sinon';
import { SubstituteCommand } from '../../src/cmd_line/commands/substitute';
import { getAndUpdateModeHandler } from '../../extension';
import { commandLine } from '../../src/cmd_line/commandLine';
import { Globals } from '../../src/globals';
Expand Down Expand Up @@ -33,6 +35,24 @@ suite('Basic substitute', () => {
assertEqualLines(['dbd']);
});

test('Replace with `c` flag', async () => {
const confirmStub = sinon.stub(SubstituteCommand.prototype, 'confirmReplacement').returns(true);
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<Esc>']);
await commandLine.Run('%s/a/d/c', modeHandler.vimState);

assertEqualLines(['dba']);
confirmStub.restore();
});

test('Replace with `gc` flag', async () => {
const confirmStub = sinon.stub(SubstituteCommand.prototype, 'confirmReplacement').returns(true);
await modeHandler.handleMultipleKeyEvents(['i', 'f', 'f', 'b', 'a', 'r', 'f', '<Esc>']);
await commandLine.Run('%s/f/foo/gc', modeHandler.vimState);

assertEqualLines(['foofoobarfoo']);
confirmStub.restore();
});

test('Replace multiple lines', async () => {
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<Esc>', 'o', 'a', 'b']);
await commandLine.Run('%s/a/d/g', modeHandler.vimState);
Expand Down Expand Up @@ -129,6 +149,17 @@ suite('Basic substitute', () => {
assertEqualLines(['dba']);
});

test('Replace with `c` flag inverts global flag', async () => {
const confirmStub = sinon
.stub(SubstituteCommand.prototype, 'confirmReplacement')
.returns(true);
await modeHandler.handleMultipleKeyEvents(['i', 'f', 'f', 'b', 'a', 'r', 'f', '<Esc>']);
await commandLine.Run('%s/f/foo/c', modeHandler.vimState);

assertEqualLines(['foofoobarfoo']);
confirmStub.restore();
});

test('Replace multiple lines', async () => {
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<Esc>', 'o', 'a', 'b']);
await commandLine.Run('%s/a/d/', modeHandler.vimState);
Expand Down

0 comments on commit 13d05e7

Please sign in to comment.