2017-11-23 02:25:59 +00:00
|
|
|
/* global CodeMirror */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
(() => {
|
2017-12-01 15:01:19 +00:00
|
|
|
/*
|
|
|
|
The original match-highlighter addon always recreates the highlight overlay
|
|
|
|
even if the token under cursor hasn't changed, which is terribly ineffective
|
|
|
|
(the entire view is re-rendered) and makes our animated token highlight effect
|
|
|
|
restart on every cursor movement.
|
|
|
|
|
|
|
|
Invocation sequence of our hooks:
|
|
|
|
|
|
|
|
1. removeOverlayForHighlighter()
|
|
|
|
The original addon removes the overlay unconditionally
|
|
|
|
so this hook saves the state if the token hasn't changed.
|
|
|
|
|
|
|
|
2. addOverlayForHighlighter()
|
|
|
|
Restores the saved state instead of creating a new overlay,
|
|
|
|
installs a hook to count occurrences.
|
|
|
|
|
|
|
|
3. matchesOnScrollbar()
|
|
|
|
Saves the query regexp passed from the original addon in our helper object,
|
|
|
|
and in case removeOverlayForHighlighter() decided to keep the overlay
|
|
|
|
only rewrites the regexp without invoking the original constructor.
|
|
|
|
*/
|
|
|
|
|
2017-11-23 02:25:59 +00:00
|
|
|
const HL_APPROVED = 'cm-matchhighlight-approved';
|
2017-12-17 18:56:09 +00:00
|
|
|
const SEARCH_MATCH_TOKEN_NAME = 'searching';
|
2017-12-01 15:01:19 +00:00
|
|
|
|
2017-11-23 02:25:59 +00:00
|
|
|
const originalAddOverlay = CodeMirror.prototype.addOverlay;
|
|
|
|
const originalRemoveOverlay = CodeMirror.prototype.removeOverlay;
|
2017-12-01 02:14:52 +00:00
|
|
|
const originalMatchesOnScrollbar = CodeMirror.prototype.showMatchesOnScrollbar;
|
2017-12-05 04:40:28 +00:00
|
|
|
const originalSetOption = CodeMirror.prototype.setOption;
|
2017-12-01 15:01:19 +00:00
|
|
|
let originalGetOption;
|
|
|
|
|
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-12-05 04:40:28 +00:00
|
|
|
CodeMirror.prototype.setOption = setOption;
|
|
|
|
|
|
|
|
let enabled = Boolean(prefs.get('editor.matchHighlight'));
|
2017-12-01 15:01:19 +00:00
|
|
|
|
2017-11-23 02:25:59 +00:00
|
|
|
return;
|
|
|
|
|
2017-12-05 04:40:28 +00:00
|
|
|
function setOption(option, value) {
|
|
|
|
enabled = option === 'highlightSelectionMatches' ? value : enabled;
|
|
|
|
return originalSetOption.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
2017-11-23 02:25:59 +00:00
|
|
|
function shouldIntercept(overlay) {
|
|
|
|
const hlState = this.state.matchHighlighter || {};
|
|
|
|
return overlay === hlState.overlay && (hlState.options || {}).showToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addOverlay() {
|
2017-12-05 04:40:28 +00:00
|
|
|
return enabled && shouldIntercept.apply(this, arguments) &&
|
2017-11-23 02:25:59 +00:00
|
|
|
addOverlayForHighlighter.apply(this, arguments) ||
|
|
|
|
originalAddOverlay.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeOverlay() {
|
2017-12-05 04:40:28 +00:00
|
|
|
return enabled && shouldIntercept.apply(this, arguments) &&
|
2017-11-23 02:25:59 +00:00
|
|
|
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 || {};
|
|
|
|
|
2017-12-01 15:01:19 +00:00
|
|
|
helper.rewriteScrollbarQuery = true;
|
|
|
|
|
|
|
|
// since we're here the original addon decided there's something to highlight,
|
|
|
|
// so we cancel removeOverlayIfExpired() scheduled in our removeOverlay hook
|
2017-11-24 10:16:59 +00:00
|
|
|
clearTimeout(helper.hookTimer);
|
|
|
|
|
2017-12-01 15:01:19 +00:00
|
|
|
// the original addon just removed its overlays, which was intercepted by removeOverlayForHighlighter,
|
|
|
|
// which decided to restore it and saved the previous overlays in our helper object,
|
|
|
|
// so here we are now, restoring them
|
|
|
|
if (helper.skipMatchesOnScrollbar) {
|
2017-11-23 02:25:59 +00:00
|
|
|
state.matchesonscroll = helper.matchesonscroll;
|
|
|
|
state.overlay = helper.overlay;
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-24 10:16:59 +00:00
|
|
|
|
2017-12-01 15:01:19 +00:00
|
|
|
// hook the newly created overlay's token() to count the occurrences
|
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
|
|
|
|
2017-12-01 15:01:19 +00:00
|
|
|
// speed up rendering of scrollbar marks 4 times: we don't need ultimate precision there
|
|
|
|
// so for the duration of this event loop cycle we spoof the "lineWrapping" option
|
|
|
|
// and restore it in the next event loop cycle
|
|
|
|
if (this.options.lineWrapping && CodeMirror.prototype.getOption !== spoofLineWrappingOption) {
|
|
|
|
originalGetOption = CodeMirror.prototype.getOption;
|
|
|
|
CodeMirror.prototype.getOption = spoofLineWrappingOption;
|
|
|
|
setTimeout(() => (CodeMirror.prototype.getOption = originalGetOption));
|
2017-12-01 02:14:52 +00:00
|
|
|
}
|
2017-11-23 02:25:59 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 15:01:19 +00:00
|
|
|
function spoofLineWrappingOption(option) {
|
|
|
|
return option !== 'lineWrapping' && originalGetOption.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
2017-11-23 02:25:59 +00:00
|
|
|
function tokenHook(stream) {
|
2017-12-01 15:01:19 +00:00
|
|
|
// we don't highlight a single match in case 'editor.matchHighlight' option is 'token'
|
|
|
|
// so this hook counts the occurrences and toggles HL_APPROVED class on CM's wrapper element
|
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-12-17 18:56:09 +00:00
|
|
|
|
|
|
|
const tokens = stream.lineOracle.baseTokens;
|
|
|
|
const tokenIndex = tokens.indexOf(stream.pos, 1);
|
|
|
|
if (tokenIndex > 0) {
|
|
|
|
const tokenStart = tokenIndex > 2 ? tokens[tokenIndex - 2] : 0;
|
|
|
|
const token = tokenStart === stream.start && tokens[tokenIndex + 1];
|
|
|
|
const index = token && token.indexOf(SEARCH_MATCH_TOKEN_NAME);
|
|
|
|
if (token && index >= 0 &&
|
|
|
|
(token[index - 1] || ' ') === ' ' &&
|
|
|
|
(token[index + SEARCH_MATCH_TOKEN_NAME.length] || ' ') === ' ') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-12-01 15:01:19 +00:00
|
|
|
const helper = state.highlightHelper;
|
|
|
|
const {query, originalToken} = helper || state.matchesonscroll || {};
|
|
|
|
// no current query means nothing to preserve => remove the overlay
|
|
|
|
if (!query || !originalToken) {
|
2017-11-23 02:25:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-11-24 10:16:59 +00:00
|
|
|
const sel = this.getSelection();
|
2017-12-01 15:01:19 +00:00
|
|
|
// current query differs from the selected text => remove the overlay
|
2017-12-17 18:56:35 +00:00
|
|
|
if (sel && sel.toLowerCase() !== originalToken.toLowerCase()) {
|
2017-12-05 02:40:33 +00:00
|
|
|
helper.query = helper.originalToken = sel;
|
2017-11-24 10:16:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-12-01 15:01:19 +00:00
|
|
|
// if token under cursor has changed => remove the overlay
|
2017-11-24 10:16:59 +00:00
|
|
|
if (!sel) {
|
|
|
|
const {line, ch} = this.getCursor();
|
2017-12-01 15:01:19 +00:00
|
|
|
const queryLen = originalToken.length;
|
2017-11-24 10:16:59 +00:00
|
|
|
const start = Math.max(0, ch - queryLen + 1);
|
|
|
|
const end = ch + queryLen;
|
2017-12-01 15:01:19 +00:00
|
|
|
const string = this.getLine(line);
|
|
|
|
const area = string.slice(start, end);
|
2017-12-17 18:56:35 +00:00
|
|
|
const i = area.indexOf(query);
|
|
|
|
const startInArea = i < 0 ? NaN : i;
|
2017-12-01 15:01:19 +00:00
|
|
|
if (isNaN(startInArea) || start + startInArea > ch ||
|
|
|
|
state.options.showToken.test(string[start + startInArea - 1] || '') ||
|
|
|
|
state.options.showToken.test(string[start + startInArea + queryLen] || '')) {
|
|
|
|
// pass the displayed instance back to the original code to remove it
|
|
|
|
state.matchesonscroll = state.matchesonscroll || helper && helper.matchesonscroll;
|
2017-11-24 10:16:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-11-23 02:25:59 +00:00
|
|
|
}
|
2017-12-01 15:01:19 +00:00
|
|
|
// since the same token is under cursor we prevent the highlighter from rerunning
|
|
|
|
// by saving current overlays in a helper object so that it's restored in addOverlayForHighlighter()
|
2017-11-24 10:16:59 +00:00
|
|
|
state.highlightHelper = {
|
|
|
|
overlay: state.overlay,
|
2017-12-01 15:01:19 +00:00
|
|
|
matchesonscroll: state.matchesonscroll || (helper || {}).matchesonscroll,
|
|
|
|
// instruct our matchesOnScrollbar hook to preserve current scrollbar annotations
|
|
|
|
skipMatchesOnScrollbar: true,
|
|
|
|
// in case the original addon won't highlight anything we need to actually remove the overlays
|
|
|
|
// by setting a timer that runs in the next event loop cycle and can be canceled in this cycle
|
2017-11-24 10:16:59 +00:00
|
|
|
hookTimer: setTimeout(removeOverlayIfExpired, 0, this, state),
|
2017-12-05 02:40:33 +00:00
|
|
|
originalToken,
|
|
|
|
query,
|
2017-11-24 10:16:59 +00:00
|
|
|
};
|
2017-12-01 15:01:19 +00:00
|
|
|
// fool the original addon so it won't invoke state.matchesonscroll.clear()
|
2017-11-24 10:16:59 +00:00
|
|
|
state.matchesonscroll = null;
|
|
|
|
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
|
|
|
state.highlightHelper = null;
|
|
|
|
}
|
|
|
|
|
2017-12-01 02:14:52 +00:00
|
|
|
function matchesOnScrollbar(query, ...args) {
|
2017-12-05 04:40:28 +00:00
|
|
|
if (!enabled) {
|
|
|
|
return originalMatchesOnScrollbar.call(this, query, ...args);
|
|
|
|
}
|
2017-12-01 15:01:19 +00:00
|
|
|
const state = this.state.matchHighlighter;
|
|
|
|
const helper = state.highlightHelper = state.highlightHelper || {};
|
|
|
|
// rewrite the \btoken\b regexp so it matches .token and #token and --token
|
|
|
|
if (helper.rewriteScrollbarQuery && /^\\b.*?\\b$/.test(query.source)) {
|
|
|
|
helper.rewriteScrollbarQuery = undefined;
|
|
|
|
helper.originalToken = query.source.slice(2, -2);
|
|
|
|
const notToken = '(?!' + state.options.showToken.source + ').';
|
|
|
|
query = new RegExp(`(^|${notToken})` + helper.originalToken + `(${notToken}|$)`);
|
|
|
|
}
|
|
|
|
// save the query for future use in removeOverlayForHighlighter
|
|
|
|
helper.query = query;
|
|
|
|
// if removeOverlayForHighlighter() decided to keep the overlay
|
|
|
|
if (helper.skipMatchesOnScrollbar) {
|
|
|
|
helper.skipMatchesOnScrollbar = undefined;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
return originalMatchesOnScrollbar.call(this, query, ...args);
|
2017-12-01 13:16:24 +00:00
|
|
|
}
|
2017-12-01 02:14:52 +00:00
|
|
|
}
|
2017-11-23 02:25:59 +00:00
|
|
|
})();
|