Notes/src/public/app/widgets/find_in_code.js

214 lines
8.1 KiB
JavaScript
Raw Normal View History

2022-05-16 23:56:43 +02:00
// ck-find-result and ck-find-result_selected are the styles ck-editor
// uses for highlighting matches, use the same one on CodeMirror
// for consistency
2022-05-25 23:38:06 +02:00
import utils from "../services/utils.js";
2022-05-16 23:56:43 +02:00
const FIND_RESULT_SELECTED_CSS_CLASSNAME = "ck-find-result_selected";
const FIND_RESULT_CSS_CLASSNAME = "ck-find-result";
export default class FindInCode {
constructor(parent) {
2022-05-25 23:38:06 +02:00
/** @property {FindWidget} */
this.parent = parent;
}
async getCodeEditor() {
return this.parent.noteContext.getCodeEditor();
}
2022-05-16 23:56:43 +02:00
async performFind(searchTerm, matchCase, wholeWord) {
let findResult = null;
let totalFound = 0;
let currentFound = -1;
// See https://codemirror.net/addon/search/searchcursor.js for tips
const codeEditor = await this.getCodeEditor();
2022-05-16 23:56:43 +02:00
const doc = codeEditor.doc;
const text = doc.getValue();
// Clear all markers
if (this.findResult != null) {
codeEditor.operation(() => {
for (let i = 0; i < this.findResult.length; ++i) {
const marker = this.findResult[i];
marker.clear();
}
});
}
if (searchTerm !== "") {
2022-05-25 23:38:06 +02:00
searchTerm = utils.escapeRegExp(searchTerm);
2022-05-16 23:56:43 +02:00
// Find and highlight matches
// Find and highlight matches
// XXX Using \\b and not using the unicode flag probably doesn't
2023-06-29 23:32:19 +02:00
// work with non-ASCII alphabets, findAndReplace uses a more
2022-05-16 23:56:43 +02:00
// complicated regexp, see
// https://github.com/ckeditor/ckeditor5/blob/b95e2faf817262ac0e1e21993d9c0bde3f1be594/packages/ckeditor5-find-and-replace/src/utils.js#L145
const wholeWordChar = wholeWord ? "\\b" : "";
2025-01-09 18:07:02 +02:00
const re = new RegExp(wholeWordChar + searchTerm + wholeWordChar, "g" + (matchCase ? "" : "i"));
2022-05-16 23:56:43 +02:00
let curLine = 0;
let curChar = 0;
let curMatch = null;
findResult = [];
2023-05-05 23:41:11 +02:00
// All those markText take several seconds on e.g., this ~500-line
// script, batch them inside an operation, so they become
2022-05-16 23:56:43 +02:00
// unnoticeable. Alternatively, an overlay could be used, see
// https://codemirror.net/addon/search/match-highlighter.js ?
codeEditor.operation(() => {
for (let i = 0; i < text.length; ++i) {
2023-05-05 23:41:11 +02:00
// Fetch the next match if it's the first time or if past the current match start
2025-01-09 18:07:02 +02:00
if (curMatch == null || curMatch.index < i) {
2022-05-16 23:56:43 +02:00
curMatch = re.exec(text);
if (curMatch == null) {
// No more matches
break;
}
}
// Create a non-selected highlight marker for the match, the
// selected marker highlight will be done later
if (i === curMatch.index) {
2025-01-09 18:07:02 +02:00
let fromPos = { line: curLine, ch: curChar };
2023-05-05 23:41:11 +02:00
// If multiline is supported, this needs to recalculate curLine since the match may span lines
2025-01-09 18:07:02 +02:00
let toPos = { line: curLine, ch: curChar + curMatch[0].length };
2023-05-05 23:41:11 +02:00
// or css = "color: #f3"
2025-01-09 18:07:02 +02:00
let marker = doc.markText(fromPos, toPos, { className: FIND_RESULT_CSS_CLASSNAME });
2022-05-16 23:56:43 +02:00
findResult.push(marker);
2023-05-05 23:41:11 +02:00
// Set the first match beyond the cursor as the current match
2022-05-16 23:56:43 +02:00
if (currentFound === -1) {
const cursorPos = codeEditor.getCursor();
2025-01-09 18:07:02 +02:00
if (fromPos.line > cursorPos.line || (fromPos.line === cursorPos.line && fromPos.ch >= cursorPos.ch)) {
2022-05-16 23:56:43 +02:00
currentFound = totalFound;
}
}
totalFound++;
}
// Do line and char position tracking
if (text[i] === "\n") {
curLine++;
curChar = 0;
} else {
curChar++;
}
}
});
}
this.findResult = findResult;
// Calculate curfound if not already, highlight it as selected
if (totalFound > 0) {
2025-01-09 18:07:02 +02:00
currentFound = Math.max(0, currentFound);
2022-05-16 23:56:43 +02:00
let marker = findResult[currentFound];
let pos = marker.find();
codeEditor.scrollIntoView(pos.to);
marker.clear();
2025-01-09 18:07:02 +02:00
findResult[currentFound] = doc.markText(pos.from, pos.to, { className: FIND_RESULT_SELECTED_CSS_CLASSNAME });
2022-05-16 23:56:43 +02:00
}
return {
totalFound,
2023-09-05 21:00:24 +02:00
currentFound: Math.min(currentFound + 1, totalFound)
2022-05-16 23:56:43 +02:00
};
}
async findNext(direction, currentFound, nextFound) {
const codeEditor = await this.getCodeEditor();
2022-05-16 23:56:43 +02:00
const doc = codeEditor.doc;
//
// Dehighlight current, highlight & scrollIntoView next
//
let marker = this.findResult[currentFound];
let pos = marker.find();
marker.clear();
2025-01-09 18:07:02 +02:00
marker = doc.markText(pos.from, pos.to, { className: FIND_RESULT_CSS_CLASSNAME });
2022-05-16 23:56:43 +02:00
this.findResult[currentFound] = marker;
marker = this.findResult[nextFound];
pos = marker.find();
marker.clear();
2025-01-09 18:07:02 +02:00
marker = doc.markText(pos.from, pos.to, { className: FIND_RESULT_SELECTED_CSS_CLASSNAME });
2022-05-16 23:56:43 +02:00
this.findResult[nextFound] = marker;
codeEditor.scrollIntoView(pos.from);
}
2022-05-26 16:29:54 +02:00
async findBoxClosed(totalFound, currentFound) {
const codeEditor = await this.getCodeEditor();
2022-05-16 23:56:43 +02:00
if (totalFound > 0) {
const doc = codeEditor.doc;
const pos = this.findResult[currentFound].find();
// Note setting the selection sets the cursor to
// the end of the selection and scrolls it into
// view
doc.setSelection(pos.from, pos.to);
// Clear all markers
codeEditor.operation(() => {
for (let i = 0; i < this.findResult.length; ++i) {
let marker = this.findResult[i];
marker.clear();
}
});
}
this.findResult = null;
codeEditor.focus();
}
2024-11-11 22:57:24 +08:00
async replace(replaceText) {
2024-11-12 10:56:54 +08:00
// this.findResult may be undefined and null
2025-01-09 18:07:02 +02:00
if (!this.findResult || this.findResult.length === 0) {
2024-11-12 10:56:54 +08:00
return;
}
2024-11-11 22:57:24 +08:00
let currentFound = -1;
this.findResult.forEach((marker, index) => {
const pos = marker.find();
if (pos) {
if (marker.className === FIND_RESULT_SELECTED_CSS_CLASSNAME) {
currentFound = index;
return;
}
}
});
if (currentFound >= 0) {
let marker = this.findResult[currentFound];
let pos = marker.find();
const codeEditor = await this.getCodeEditor();
const doc = codeEditor.doc;
doc.replaceRange(replaceText, pos.from, pos.to);
marker.clear();
let nextFound;
if (currentFound === this.findResult.length - 1) {
nextFound = 0;
} else {
nextFound = currentFound;
}
this.findResult.splice(currentFound, 1);
if (this.findResult.length > 0) {
this.findNext(0, nextFound, nextFound);
}
}
}
async replaceAll(replaceText) {
2025-01-09 18:07:02 +02:00
if (!this.findResult || this.findResult.length === 0) {
2024-11-12 10:56:54 +08:00
return;
}
2024-11-11 22:57:24 +08:00
const codeEditor = await this.getCodeEditor();
const doc = codeEditor.doc;
codeEditor.operation(() => {
for (let currentFound = 0; currentFound < this.findResult.length; currentFound++) {
let marker = this.findResult[currentFound];
let pos = marker.find();
doc.replaceRange(replaceText, pos.from, pos.to);
marker.clear();
}
});
this.findResult = [];
}
2022-05-16 23:56:43 +02:00
}