From 6265951d01ebd28904458933962a1d3393cfeae5 Mon Sep 17 00:00:00 2001 From: tophf Date: Wed, 29 Mar 2017 01:54:31 +0300 Subject: [PATCH] match-highlighter.js: override step 2, highlight only multiple matches --- .../addon/search/match-highlighter.js | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/codemirror-overwrites/addon/search/match-highlighter.js b/codemirror-overwrites/addon/search/match-highlighter.js index 73ba0e05..a3de4c03 100644 --- a/codemirror-overwrites/addon/search/match-highlighter.js +++ b/codemirror-overwrites/addon/search/match-highlighter.js @@ -19,6 +19,12 @@ // highlighting the matches. If annotateScrollbar is enabled, the occurences // 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) { if (typeof exports == "object" && typeof module == "object") // CommonJS mod(require("../../lib/codemirror"), require("./matchesonscrollbar")); @@ -88,7 +94,9 @@ function addOverlay(cm, query, hasBoundary, style) { 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) { var searchFor = hasBoundary ? new RegExp("\\b" + query + "\\b") : query; state.matchesonscroll = cm.showMatchesOnScrollbar(searchFor, false, @@ -153,11 +161,28 @@ (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) { + clearTimeout(timer); + timer = setTimeout(() => { + occurrences = 0; + timer = null; + }); 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; + } + /* STYLUS: hack end (part 3) */ stream.next(); stream.skipTo(query.charAt(0)) || stream.skipToEnd(); }};