2017-11-23 02:25:59 +00:00
|
|
|
/* global CodeMirror */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
(() => {
|
|
|
|
const HL_APPROVED = 'cm-matchhighlight-approved';
|
|
|
|
const originalAddOverlay = CodeMirror.prototype.addOverlay;
|
|
|
|
const originalRemoveOverlay = CodeMirror.prototype.removeOverlay;
|
2017-12-01 02:14:52 +00:00
|
|
|
const originalMatchesOnScrollbar = CodeMirror.prototype.showMatchesOnScrollbar;
|
2017-11-23 02:25:59 +00:00
|
|
|
CodeMirror.prototype.addOverlay = addOverlay;
|
|
|
|
CodeMirror.prototype.removeOverlay = removeOverlay;
|
2017-12-01 02:14:52 +00:00
|
|
|
CodeMirror.prototype.showMatchesOnScrollbar = matchesOnScrollbar;
|
2017-11-23 02:25:59 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
function shouldIntercept(overlay) {
|
|
|
|
const hlState = this.state.matchHighlighter || {};
|
|
|
|
return overlay === hlState.overlay && (hlState.options || {}).showToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addOverlay() {
|
|
|
|
return shouldIntercept.apply(this, arguments) &&
|
|
|
|
addOverlayForHighlighter.apply(this, arguments) ||
|
|
|
|
originalAddOverlay.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeOverlay() {
|
|
|
|
return shouldIntercept.apply(this, arguments) &&
|
|
|
|
removeOverlayForHighlighter.apply(this, arguments) ||
|
|
|
|
originalRemoveOverlay.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addOverlayForHighlighter(overlay) {
|
|
|
|
const state = this.state.matchHighlighter || {};
|
2017-11-24 10:16:59 +00:00
|
|
|
const helper = state.highlightHelper = state.highlightHelper || {};
|
|
|
|
|
|
|
|
clearTimeout(helper.hookTimer);
|
|
|
|
|
2017-11-23 02:25:59 +00:00
|
|
|
if (helper.matchesonscroll) {
|
|
|
|
// restore the original addon's unwanted removeOverlay effects
|
|
|
|
// (in case the token under cursor hasn't changed)
|
|
|
|
state.matchesonscroll = helper.matchesonscroll;
|
|
|
|
state.overlay = helper.overlay;
|
|
|
|
helper.matchesonscroll = null;
|
|
|
|
helper.overlay = null;
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-24 10:16:59 +00:00
|
|
|
|
2017-11-23 02:25:59 +00:00
|
|
|
if (overlay.token !== tokenHook) {
|
2017-11-24 10:16:59 +00:00
|
|
|
overlay.highlightHelper = {
|
2017-11-23 02:25:59 +00:00
|
|
|
token: overlay.token,
|
|
|
|
occurrences: 0,
|
|
|
|
};
|
|
|
|
overlay.token = tokenHook;
|
|
|
|
}
|
2017-12-01 02:14:52 +00:00
|
|
|
|
|
|
|
if (this.options.lineWrapping) {
|
|
|
|
const originalGetOption = CodeMirror.prototype.getOption;
|
|
|
|
CodeMirror.prototype.getOption = function (option) {
|
|
|
|
return option !== 'lineWrapping' && originalGetOption.apply(this, arguments);
|
|
|
|
};
|
|
|
|
setTimeout(() => {
|
|
|
|
CodeMirror.prototype.getOption = originalGetOption;
|
|
|
|
});
|
|
|
|
}
|
2017-11-23 02:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function tokenHook(stream) {
|
2017-11-24 10:16:59 +00:00
|
|
|
const style = this.highlightHelper.token.call(this, stream);
|
2017-11-23 02:25:59 +00:00
|
|
|
if (style !== 'matchhighlight') {
|
|
|
|
return style;
|
|
|
|
}
|
2017-11-24 10:16:59 +00:00
|
|
|
const num = ++this.highlightHelper.occurrences;
|
2017-11-23 02:25:59 +00:00
|
|
|
if (num === 1) {
|
|
|
|
stream.lineOracle.doc.cm.display.wrapper.classList.remove(HL_APPROVED);
|
|
|
|
} else if (num === 2) {
|
|
|
|
stream.lineOracle.doc.cm.display.wrapper.classList.add(HL_APPROVED);
|
|
|
|
}
|
|
|
|
return style;
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeOverlayForHighlighter() {
|
|
|
|
const state = this.state.matchHighlighter || {};
|
2017-11-24 10:16:59 +00:00
|
|
|
const {query} = state.highlightHelper || state.matchesonscroll || {};
|
2017-11-23 02:25:59 +00:00
|
|
|
if (!query) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-23 11:13:07 +00:00
|
|
|
const rx = query instanceof RegExp && query;
|
2017-11-24 10:16:59 +00:00
|
|
|
const sel = this.getSelection();
|
|
|
|
if (sel && (rx && !rx.test(sel) || sel.toLowerCase() !== query)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!sel) {
|
|
|
|
const {line, ch} = this.getCursor();
|
|
|
|
const queryLen = rx ? rx.source.length - 4 : query.length;
|
|
|
|
const start = Math.max(0, ch - queryLen + 1);
|
|
|
|
const end = ch + queryLen;
|
|
|
|
const area = this.getLine(line).substring(start, end);
|
|
|
|
const startInArea = rx ? (area.match(rx) || {}).index :
|
|
|
|
(area.indexOf(query) + 1 || NaN) - 1;
|
|
|
|
if (start + startInArea > ch) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-23 02:25:59 +00:00
|
|
|
}
|
2017-11-24 10:16:59 +00:00
|
|
|
// same token on cursor => prevent the highlighter from rerunning
|
|
|
|
state.highlightHelper = {
|
|
|
|
overlay: state.overlay,
|
|
|
|
matchesonscroll: state.matchesonscroll,
|
|
|
|
showMatchesOnScrollbar: this.showMatchesOnScrollbar,
|
|
|
|
hookTimer: setTimeout(removeOverlayIfExpired, 0, this, state),
|
|
|
|
};
|
|
|
|
state.matchesonscroll = null;
|
|
|
|
this.showMatchesOnScrollbar = scrollbarForHighlighter;
|
|
|
|
return true;
|
2017-11-23 02:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeOverlayIfExpired(self, state) {
|
2017-11-24 10:16:59 +00:00
|
|
|
const {overlay, matchesonscroll} = state.highlightHelper || {};
|
2017-11-23 02:25:59 +00:00
|
|
|
if (overlay) {
|
|
|
|
originalRemoveOverlay.call(self, overlay);
|
|
|
|
}
|
|
|
|
if (matchesonscroll) {
|
|
|
|
matchesonscroll.clear();
|
|
|
|
}
|
2017-11-24 10:16:59 +00:00
|
|
|
self.showMatchesOnScrollbar = state.showMatchesOnScrollbar;
|
|
|
|
state.highlightHelper = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function scrollbarForHighlighter(query) {
|
|
|
|
const helper = this.state.matchHighlighter.highlightHelper;
|
|
|
|
this.showMatchesOnScrollbar = helper.showMatchesOnScrollbar;
|
|
|
|
helper.query = query;
|
2017-11-23 02:25:59 +00:00
|
|
|
}
|
2017-12-01 02:14:52 +00:00
|
|
|
|
|
|
|
function matchesOnScrollbar(query, ...args) {
|
|
|
|
query = new RegExp(/(?:^|[^\w.#\\-])/.source + query.source.slice(2, -2) + /(?:[^\w.#\\-]|$)/.source);
|
|
|
|
return originalMatchesOnScrollbar.call(this, query, ...args);
|
|
|
|
}
|
2017-11-23 02:25:59 +00:00
|
|
|
})();
|