mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-30 06:56:29 -05:00
fix #8317
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user