Skip to content

Commit 8d0e59e

Browse files
authored
Merge pull request #1151 from davidbmx/master
Add matches search into code mirror fixes #936
2 parents a155e76 + 6030b35 commit 8d0e59e

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

client/utils/codemirror-search.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ export default function(CodeMirror) {
7373
CodeMirror.on(searchField, "keyup", function (e) {
7474
if (e.keyCode !== 13 && searchField.value.length > 1) { // not enter and more than 1 character to search
7575
startSearch(cm, getSearchState(cm), searchField.value);
76+
} else if (searchField.value.length <= 1) {
77+
cm.display.wrapper.querySelector('.CodeMirror-search-results').innerText = '';
7678
}
7779
});
7880

@@ -260,6 +262,7 @@ export default function(CodeMirror) {
260262
</button>
261263
</div>
262264
<div class="CodeMirror-search-nav">
265+
<button class="CodeMirror-search-results"></button>
263266
<button
264267
title="Previous"
265268
aria-label="Previous"
@@ -292,6 +295,9 @@ export default function(CodeMirror) {
292295
if (state.annotate) { state.annotate.clear(); state.annotate = null; }
293296
state.annotate = cm.showMatchesOnScrollbar(state.query, state.caseInsensitive);
294297
}
298+
if (originalQuery) {
299+
return findNext(cm, false);
300+
}
295301
}
296302

297303
function doSearch(cm, rev, persistent, immediate, ignoreQuery) {
@@ -350,11 +356,19 @@ export default function(CodeMirror) {
350356
var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
351357
if (!cursor.find(rev)) {
352358
cursor = getSearchCursor(cm, state.query, rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0));
353-
if (!cursor.find(rev)) return;
359+
if (!cursor.find(rev)) {
360+
cm.display.wrapper.querySelector('.CodeMirror-search-results').innerText = '';
361+
return;
362+
}
354363
}
355364
cm.setSelection(cursor.from(), cursor.to());
356365
cm.scrollIntoView({from: cursor.from(), to: cursor.to()}, 60);
357366
state.posFrom = cursor.from(); state.posTo = cursor.to();
367+
var num_match = cm.state.search.annotate.matches.length;
368+
var next = cm.state.search.annotate.matches
369+
.findIndex(s => s.from.ch === cursor.from().ch && s.from.line === cursor.from().line) + 1;
370+
var text_match = next + '/' + num_match;
371+
cm.display.wrapper.querySelector('.CodeMirror-search-results').innerText = text_match;
358372
if (callback) callback(cursor.from(), cursor.to())
359373
});}
360374

0 commit comments

Comments
 (0)