This commit is contained in:
Sandeep Somavarapu
2016-07-27 23:50:08 +02:00
parent 7fca8ee82d
commit d546a4a6ef
2 changed files with 196 additions and 23 deletions

View File

@@ -6,7 +6,6 @@
import {RunOnceScheduler} from 'vs/base/common/async';
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
import * as strings from 'vs/base/common/strings';
import {ReplacePattern} from 'vs/platform/search/common/replace';
import {ReplaceCommand} from 'vs/editor/common/commands/replaceCommand';
import {Position} from 'vs/editor/common/core/position';
@@ -243,9 +242,18 @@ export class FindModelBoundToEditorModel {
this._moveToPrevMatch(this._editor.getSelection().getStartPosition());
}
public _moveToNextMatch(after:Position, isRecursed:boolean = false): void {
private _moveToNextMatch(nextMatch:Range): void
private _moveToNextMatch(after:Position): void
private _moveToNextMatch(arg: any): void {
let nextMatch = Range.isIRange(arg) ? arg : Position.isIPosition(arg) ? this._getNextMatch(arg) : null;
if (nextMatch) {
this._setCurrentFindMatch(nextMatch);
}
}
private _getNextMatch(after:Position, isRecursed:boolean = false): Range {
if (this._cannotFind()) {
return;
return null;
}
let findScope = this._decorations.getFindScope();
@@ -297,10 +305,10 @@ export class FindModelBoundToEditorModel {
}
if (!isRecursed && !searchRange.containsRange(nextMatch)) {
return this._moveToNextMatch(nextMatch.getEndPosition(), true);
return this._getNextMatch(nextMatch.getEndPosition(), true);
}
this._setCurrentFindMatch(nextMatch);
return nextMatch;
}
public moveToNextMatch(): void {
@@ -312,14 +320,6 @@ export class FindModelBoundToEditorModel {
return replacePattern.getReplaceString(matchedString);
}
private _rangeIsMatch(range:Range): boolean {
let selection = this._editor.getSelection();
let selectionText = this._editor.getModel().getValueInRange(selection);
let regexp = strings.createRegExp(this._state.searchString, this._state.isRegex, this._state.matchCase, this._state.wholeWord, true);
let m = selectionText.match(regexp);
return (m && m[0].length === selectionText.length);
}
public replace(): void {
if (!this._hasMatches()) {
return;
@@ -327,19 +327,22 @@ export class FindModelBoundToEditorModel {
let selection = this._editor.getSelection();
let selectionText = this._editor.getModel().getValueInRange(selection);
if (this._rangeIsMatch(selection)) {
// selection sits on a find match => replace it!
let replaceString = this.getReplaceString(selectionText);
let nextMatch = this._getNextMatch(selection.getStartPosition());
if (nextMatch) {
if (selection.equalsRange(nextMatch)) {
// selection sits on a find match => replace it!
let replaceString = this.getReplaceString(selectionText);
let command = new ReplaceCommand(selection, replaceString);
let command = new ReplaceCommand(selection, replaceString);
this._executeEditorCommand('replace', command);
this._executeEditorCommand('replace', command);
this._decorations.setStartPosition(new Position(selection.startLineNumber, selection.startColumn + replaceString.length));
this.research(true);
} else {
this._decorations.setStartPosition(this._editor.getPosition());
this.moveToNextMatch();
this._decorations.setStartPosition(new Position(selection.startLineNumber, selection.startColumn + replaceString.length));
this.research(true);
} else {
this._decorations.setStartPosition(this._editor.getPosition());
this._moveToNextMatch(nextMatch);
}
}
}

View File

@@ -1622,4 +1622,174 @@ suite('FindModel', () => {
findModel.dispose();
findState.dispose();
});
findTest('replace when search string has look ahed regex', (editor, cursor) => {
let findState = new FindReplaceState();
findState.change({ searchString: 'hello(?=\\sworld)', replaceString: 'hi', isRegex:true }, false);
let findModel = new FindModelBoundToEditorModel(editor, findState);
assertFindState(
editor,
[1, 1, 1, 1],
null,
[
[6, 14, 6, 19],
[7, 14, 7, 19],
[8, 14, 8, 19]
]
);
findModel.replace();
assertFindState(
editor,
[6, 14, 6, 19],
[6, 14, 6, 19],
[
[6, 14, 6, 19],
[7, 14, 7, 19],
[8, 14, 8, 19]
]
);
assert.equal(editor.getModel().getLineContent(6), ' cout << "hello world, Hello!" << endl;');
findModel.replace();
assertFindState(
editor,
[7, 14, 7, 19],
[7, 14, 7, 19],
[
[7, 14, 7, 19],
[8, 14, 8, 19]
]
);
assert.equal(editor.getModel().getLineContent(6), ' cout << "hi world, Hello!" << endl;');
findModel.replace();
assertFindState(
editor,
[8, 14, 8, 19],
[8, 14, 8, 19],
[
[8, 14, 8, 19]
]
);
assert.equal(editor.getModel().getLineContent(7), ' cout << "hi world again" << endl;');
findModel.replace();
assertFindState(
editor,
[8, 16, 8, 16],
null,
[ ]
);
assert.equal(editor.getModel().getLineContent(8), ' cout << "hi world again" << endl;');
findModel.dispose();
findState.dispose();
});
findTest('replace when search string has look ahed regex and cursor is at the last find match', (editor, cursor) => {
let findState = new FindReplaceState();
findState.change({ searchString: 'hello(?=\\sworld)', replaceString: 'hi', isRegex:true }, false);
let findModel = new FindModelBoundToEditorModel(editor, findState);
cursor.trigger('mouse', Handler.MoveTo, {
position: new Position(8, 14)
});
assertFindState(
editor,
[8, 14, 8, 14],
null,
[
[6, 14, 6, 19],
[7, 14, 7, 19],
[8, 14, 8, 19]
]
);
findModel.replace();
assertFindState(
editor,
[8, 14, 8, 19],
[8, 14, 8, 19],
[
[6, 14, 6, 19],
[7, 14, 7, 19],
[8, 14, 8, 19]
]
);
assert.equal(editor.getModel().getLineContent(8), ' cout << "Hello world again" << endl;');
findModel.replace();
assertFindState(
editor,
[6, 14, 6, 19],
[6, 14, 6, 19],
[
[6, 14, 6, 19],
[7, 14, 7, 19],
]
);
assert.equal(editor.getModel().getLineContent(8), ' cout << "hi world again" << endl;');
findModel.replace();
assertFindState(
editor,
[7, 14, 7, 19],
[7, 14, 7, 19],
[
[7, 14, 7, 19]
]
);
assert.equal(editor.getModel().getLineContent(6), ' cout << "hi world, Hello!" << endl;');
findModel.replace();
assertFindState(
editor,
[7, 16, 7, 16],
null,
[ ]
);
assert.equal(editor.getModel().getLineContent(7), ' cout << "hi world again" << endl;');
findModel.dispose();
findState.dispose();
});
findTest('replaceAll when search string has look ahed regex', (editor, cursor) => {
let findState = new FindReplaceState();
findState.change({ searchString: 'hello(?=\\sworld)', replaceString: 'hi', isRegex:true }, false);
let findModel = new FindModelBoundToEditorModel(editor, findState);
assertFindState(
editor,
[1, 1, 1, 1],
null,
[
[6, 14, 6, 19],
[7, 14, 7, 19],
[8, 14, 8, 19]
]
);
findModel.replaceAll();
assert.equal(editor.getModel().getLineContent(6), ' cout << "hi world, Hello!" << endl;');
assert.equal(editor.getModel().getLineContent(7), ' cout << "hi world again" << endl;');
assert.equal(editor.getModel().getLineContent(8), ' cout << "hi world again" << endl;');
assertFindState(
editor,
[1, 1, 1, 1],
null,
[ ]
);
findModel.dispose();
findState.dispose();
});
});