add a text replacement feature to the find_widget

This commit is contained in:
SiriusXT
2024-11-11 18:11:31 +08:00
parent 5116bddc5f
commit 12b71961ae
3 changed files with 79 additions and 24 deletions

View File

@@ -20,6 +20,7 @@ export default class FindInText {
// Clear
const findAndReplaceEditing = textEditor.plugins.get('FindAndReplaceEditing');
findAndReplaceEditing.state.clear(model);
this.editingState = findAndReplaceEditing.state;
findAndReplaceEditing.stop();
if (searchTerm !== "") {
// Parameters are callback/text, options.matchCase=false, options.wholeWords=false
@@ -29,7 +30,7 @@ export default class FindInText {
// let re = new RegExp(searchTerm, 'gi');
// let m = text.match(re);
// totalFound = m ? m.length : 0;
const options = { "matchCase" : matchCase, "wholeWords" : wholeWord };
const options = { "matchCase": matchCase, "wholeWords": wholeWord };
findResult = textEditor.execute('find', searchTerm, options);
totalFound = findResult.results.length;
// Find the result beyond the cursor
@@ -102,4 +103,18 @@ export default class FindInText {
textEditor.focus();
}
async replace(replaceText) {
if (this.editingState.highlightedResult !== null) {
const textEditor = await this.getTextEditor();
textEditor.execute('replace', replaceText, this.editingState.highlightedResult);
}
}
async replaceAll(replaceText) {
if (this.editingState.results.length > 0) {
const textEditor = await this.getTextEditor();
textEditor.execute('replaceAll', replaceText, this.editingState.results);
}
}
}