match-highlighter.js: override step 2, highlight only multiple matches

This commit is contained in:
tophf 2017-03-29 01:54:31 +03:00
parent 3fb41a17e3
commit 6265951d01

View File

@ -19,6 +19,12 @@
// highlighting the matches. If annotateScrollbar is enabled, the occurences // highlighting the matches. If annotateScrollbar is enabled, the occurences
// will be highlighted on the scrollbar via the matchesonscrollbar addon. // will be highlighted on the scrollbar via the matchesonscrollbar addon.
/* STYLUS: hack start (part 1) */
/* eslint curly: 1, brace-style:1, strict: 0, quotes: 0, semi: 1, indent: 1 */
/* eslint no-var: 0, block-scoped-var: 0, no-redeclare: 0, no-unused-expressions: 1 */
/* global CodeMirror, require, define */
/* STYLUS: hack end (part 1) */
(function(mod) { (function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"), require("./matchesonscrollbar")); mod(require("../../lib/codemirror"), require("./matchesonscrollbar"));
@ -88,7 +94,9 @@
function addOverlay(cm, query, hasBoundary, style) { function addOverlay(cm, query, hasBoundary, style) {
var state = cm.state.matchHighlighter; var state = cm.state.matchHighlighter;
cm.addOverlay(state.overlay = makeOverlay(query, hasBoundary, style)); /* STYLUS: hack start (part 2) */
cm.addOverlay(state.overlay = makeOverlay(cm, query, hasBoundary, style));
/* STYLUS: hack end (part 2) */
if (state.options.annotateScrollbar && cm.showMatchesOnScrollbar) { if (state.options.annotateScrollbar && cm.showMatchesOnScrollbar) {
var searchFor = hasBoundary ? new RegExp("\\b" + query + "\\b") : query; var searchFor = hasBoundary ? new RegExp("\\b" + query + "\\b") : query;
state.matchesonscroll = cm.showMatchesOnScrollbar(searchFor, false, state.matchesonscroll = cm.showMatchesOnScrollbar(searchFor, false,
@ -153,11 +161,28 @@
(stream.pos == stream.string.length || !re.test(stream.string.charAt(stream.pos))); (stream.pos == stream.string.length || !re.test(stream.string.charAt(stream.pos)));
} }
function makeOverlay(query, hasBoundary, style) { function makeOverlay(cm, query, hasBoundary, style) {
/* STYLUS: hack start (part 3) */
const approvedClassName = `cm-${style}-approved`;
let timer;
let occurrences = 0;
return {token: function(stream) { return {token: function(stream) {
clearTimeout(timer);
timer = setTimeout(() => {
occurrences = 0;
timer = null;
});
if (stream.match(query) && if (stream.match(query) &&
(!hasBoundary || boundariesAround(stream, hasBoundary))) (!hasBoundary || boundariesAround(stream, hasBoundary))) {
occurrences++;
if (occurrences == 1) {
cm.display.wrapper.classList.remove(approvedClassName);
} else if (occurrences == 2) {
cm.display.wrapper.classList.add(approvedClassName);
}
return style; return style;
}
/* STYLUS: hack end (part 3) */
stream.next(); stream.next();
stream.skipTo(query.charAt(0)) || stream.skipToEnd(); stream.skipTo(query.charAt(0)) || stream.skipToEnd();
}}; }};