Skip to content

Enable replacePattern #135444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { editorWidgetBackground, editorWidgetForeground, inputActiveOptionBackgr
import { widgetClose } from 'vs/platform/theme/common/iconRegistry';
import { attachProgressBarStyler } from 'vs/platform/theme/common/styler';
import { IColorTheme, IThemeService, registerThemingParticipant } from 'vs/platform/theme/common/themeService';
import { parseReplaceString, ReplacePattern } from 'vs/editor/contrib/find/replacePattern';

const NLS_FIND_INPUT_LABEL = nls.localize('label.find', "Find");
const NLS_FIND_INPUT_PLACEHOLDER = nls.localize('placeholder.find', "Find");
Expand Down Expand Up @@ -269,6 +270,13 @@ export abstract class SimpleFindReplaceWidget extends Widget {
return this._replaceInput.getValue();
}

protected get replacePattern() {
if (this._state.isRegex) {
return parseReplaceString(this.replaceValue);
}
return ReplacePattern.fromStaticValue(this.replaceValue);
}

public get focusTracker(): dom.IFocusTracker {
return this._focusTracker;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,11 @@ export class NotebookFindWidget extends SimpleFindReplaceWidget implements INote
const { cell, match } = this._findModel.getCurrentMatch();
this._progressBar.infinite().show();

const replacePattern = this.replacePattern;
const replaceString = replacePattern.buildReplaceString(match.matches, this._state.preserveCase);

const viewModel = this._notebookEditor._getViewModel();
viewModel.replaceOne(cell, match.range, this.replaceValue).then(() => {
viewModel.replaceOne(cell, match.range, replaceString).then(() => {
this._progressBar.stop();
});
}
Expand All @@ -130,8 +133,20 @@ export class NotebookFindWidget extends SimpleFindReplaceWidget implements INote

this._progressBar.infinite().show();

const replacePattern = this.replacePattern;

const cellFindMatches = this._findModel.findMatches;
let replaceStrings: string[] = [];
cellFindMatches.forEach(cellFindMatch => {
const findMatches = cellFindMatch.matches;
findMatches.forEach(findMatch => {
const matches = findMatch.matches;
replaceStrings.push(replacePattern.buildReplaceString(matches, this._state.preserveCase));
});
});

const viewModel = this._notebookEditor._getViewModel();
viewModel.replaceAll(this._findModel.findMatches, this.replaceValue).then(() => {
viewModel.replaceAll(this._findModel.findMatches, replaceStrings).then(() => {
this._progressBar.stop();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ export abstract class BaseCellViewModel extends Disposable {
options.regex || false,
options.caseSensitive || false,
options.wholeWord ? options.wordSeparators || null : null,
false);
options.regex || false);
} else {
const lineCount = this.textBuffer.getLineCount();
const fullRange = new Range(1, 1, lineCount, this.textBuffer.getLineLength(lineCount) + 1);
Expand All @@ -543,7 +543,7 @@ export abstract class BaseCellViewModel extends Disposable {
return null;
}

cellMatches = this.textBuffer.findMatchesLineByLine(fullRange, searchData, false, 1000);
cellMatches = this.textBuffer.findMatchesLineByLine(fullRange, searchData, options.regex || false, 1000);
}

return cellMatches;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
});
}

async replaceAll(matches: CellFindMatch[], text: string): Promise<void> {
async replaceAll(matches: CellFindMatch[], texts: string[]): Promise<void> {
if (!matches.length) {
return;
}
Expand All @@ -916,9 +916,9 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
this._lastNotebookEditResource.push(matches[0].cell.uri);

matches.forEach(match => {
match.matches.forEach(singleMatch => {
match.matches.forEach((singleMatch, index) => {
textEdits.push({
edit: { range: singleMatch.range, text: text },
edit: { range: singleMatch.range, text: texts[index] },
resource: match.cell.uri
});
});
Expand Down