Skip to content

Commit f1cc970

Browse files
committed
Make FindAndReplace newline indicators always show (fixes #175)
1 parent 0fc84be commit f1cc970

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

plugins/find-and-replace.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -626,11 +626,28 @@ codeInput.plugins.FindAndReplace.FindMatchState = class {
626626
}
627627
}
628628

629-
/* Highlight a match from the find functionality given its start and end indexes in the text.
630-
Start from the currentElement as this function is recursive. Use the matchID in the class name
629+
/* Highlight a match from the find functionality given its start and end indexes in the text.
630+
Start from the currentElement. Use the matchID in the class name
631631
of the match so different matches can be identified.
632-
This code is similar to codeInput.plugins.SelectTokenCallbacks.SelectedTokenState.updateSelectedTokens*/
632+
*/
633633
highlightMatch(matchID, currentElement, startIndex, endIndex) {
634+
const lines = currentElement.textContent.substring(startIndex, endIndex).split("\n");
635+
let lineStartIndex = startIndex;
636+
for(let i = 0; i < lines.length; i++) {
637+
if(i == 0) {
638+
this.highlightMatchNewlineOnlyAtStart(matchID, currentElement, lineStartIndex, lineStartIndex+lines[i].length);
639+
} else {
640+
// Include previous newline character too
641+
this.highlightMatchNewlineOnlyAtStart(matchID, currentElement, lineStartIndex-1, lineStartIndex+lines[i].length);
642+
}
643+
644+
lineStartIndex += lines[i].length + 1; // +1 for newline character
645+
}
646+
}
647+
648+
/* Same as highlightMatch, but assumes any newlines in the
649+
match are at the startIndex (for simpler code). */
650+
highlightMatchNewlineOnlyAtStart(matchID, currentElement, startIndex, endIndex) {
634651
for(let i = 0; i < currentElement.childNodes.length; i++) {
635652
let childElement = currentElement.childNodes[i];
636653
let childText = childElement.textContent;
@@ -679,7 +696,7 @@ codeInput.plugins.FindAndReplace.FindMatchState = class {
679696
i++; // An extra element has been added
680697
return;
681698
} else {
682-
this.highlightMatch(matchID, childElement, 0, endIndex);
699+
this.highlightMatchNewlineOnlyAtStart(matchID, childElement, 0, endIndex);
683700
}
684701

685702
// Match ended - nothing to do after backtracking
@@ -738,7 +755,7 @@ codeInput.plugins.FindAndReplace.FindMatchState = class {
738755
i++; // An extra element has been added
739756
}
740757
} else {
741-
this.highlightMatch(matchID, childElement, startIndex, endIndex);
758+
this.highlightMatchNewlineOnlyAtStart(matchID, childElement, startIndex, endIndex);
742759
}
743760

744761
if(childText.length > endIndex) {

0 commit comments

Comments
 (0)