2017-03-19 03:17:23 +00:00
|
|
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
|
|
// Distributed under an MIT license: http://codemirror.net/LICENSE
|
|
|
|
|
|
|
|
// Highlighting text that matches the selection
|
|
|
|
//
|
|
|
|
// Defines an option highlightSelectionMatches, which, when enabled,
|
|
|
|
// will style strings that match the selection throughout the
|
|
|
|
// document.
|
|
|
|
//
|
|
|
|
// The option can be set to true to simply enable it, or to a
|
|
|
|
// {minChars, style, wordsOnly, showToken, delay} object to explicitly
|
|
|
|
// configure it. minChars is the minimum amount of characters that should be
|
|
|
|
// selected for the behavior to occur, and style is the token style to
|
|
|
|
// apply to the matches. This will be prefixed by "cm-" to create an
|
|
|
|
// actual CSS class name. If wordsOnly is enabled, the matches will be
|
|
|
|
// highlighted only if the selected text is a word. showToken, when enabled,
|
|
|
|
// will cause the current token to be highlighted when nothing is selected.
|
|
|
|
// delay is used to specify how much time to wait, in milliseconds, before
|
|
|
|
// highlighting the matches. If annotateScrollbar is enabled, the occurences
|
|
|
|
// will be highlighted on the scrollbar via the matchesonscrollbar addon.
|
|
|
|
|
2017-03-28 22:54:31 +00:00
|
|
|
/* 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) */
|
|
|
|
|
2017-03-19 03:17:23 +00:00
|
|
|
(function(mod) {
|
|
|
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
|
|
mod(require("../../lib/codemirror"), require("./matchesonscrollbar"));
|
|
|
|
else if (typeof define == "function" && define.amd) // AMD
|
|
|
|
define(["../../lib/codemirror", "./matchesonscrollbar"], mod);
|
|
|
|
else // Plain browser env
|
|
|
|
mod(CodeMirror);
|
|
|
|
})(function(CodeMirror) {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var defaults = {
|
|
|
|
style: "matchhighlight",
|
|
|
|
minChars: 2,
|
|
|
|
delay: 100,
|
|
|
|
wordsOnly: false,
|
|
|
|
annotateScrollbar: false,
|
|
|
|
showToken: false,
|
|
|
|
trim: true
|
|
|
|
}
|
|
|
|
|
|
|
|
function State(options) {
|
|
|
|
this.options = {}
|
|
|
|
for (var name in defaults)
|
|
|
|
this.options[name] = (options && options.hasOwnProperty(name) ? options : defaults)[name]
|
|
|
|
this.overlay = this.timeout = null;
|
|
|
|
this.matchesonscroll = null;
|
|
|
|
this.active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeMirror.defineOption("highlightSelectionMatches", false, function(cm, val, old) {
|
|
|
|
if (old && old != CodeMirror.Init) {
|
|
|
|
removeOverlay(cm);
|
|
|
|
clearTimeout(cm.state.matchHighlighter.timeout);
|
|
|
|
cm.state.matchHighlighter = null;
|
|
|
|
cm.off("cursorActivity", cursorActivity);
|
|
|
|
cm.off("focus", onFocus)
|
|
|
|
}
|
|
|
|
if (val) {
|
|
|
|
var state = cm.state.matchHighlighter = new State(val);
|
|
|
|
if (cm.hasFocus()) {
|
|
|
|
state.active = true
|
|
|
|
highlightMatches(cm)
|
|
|
|
} else {
|
|
|
|
cm.on("focus", onFocus)
|
|
|
|
}
|
|
|
|
cm.on("cursorActivity", cursorActivity);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function cursorActivity(cm) {
|
|
|
|
var state = cm.state.matchHighlighter;
|
|
|
|
if (state.active || cm.hasFocus()) scheduleHighlight(cm, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
function onFocus(cm) {
|
|
|
|
var state = cm.state.matchHighlighter
|
|
|
|
if (!state.active) {
|
|
|
|
state.active = true
|
|
|
|
scheduleHighlight(cm, state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function scheduleHighlight(cm, state) {
|
|
|
|
clearTimeout(state.timeout);
|
|
|
|
state.timeout = setTimeout(function() {highlightMatches(cm);}, state.options.delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addOverlay(cm, query, hasBoundary, style) {
|
|
|
|
var state = cm.state.matchHighlighter;
|
2017-03-28 22:54:31 +00:00
|
|
|
/* STYLUS: hack start (part 2) */
|
|
|
|
cm.addOverlay(state.overlay = makeOverlay(cm, query, hasBoundary, style));
|
|
|
|
/* STYLUS: hack end (part 2) */
|
2017-03-19 03:17:23 +00:00
|
|
|
if (state.options.annotateScrollbar && cm.showMatchesOnScrollbar) {
|
|
|
|
var searchFor = hasBoundary ? new RegExp("\\b" + query + "\\b") : query;
|
|
|
|
state.matchesonscroll = cm.showMatchesOnScrollbar(searchFor, false,
|
|
|
|
{className: "CodeMirror-selection-highlight-scrollbar"});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeOverlay(cm) {
|
|
|
|
var state = cm.state.matchHighlighter;
|
|
|
|
if (state.overlay) {
|
|
|
|
cm.removeOverlay(state.overlay);
|
|
|
|
state.overlay = null;
|
|
|
|
if (state.matchesonscroll) {
|
|
|
|
state.matchesonscroll.clear();
|
|
|
|
state.matchesonscroll = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function highlightMatches(cm) {
|
|
|
|
cm.operation(function() {
|
|
|
|
var state = cm.state.matchHighlighter;
|
|
|
|
if (!cm.somethingSelected() && state.options.showToken) {
|
|
|
|
var re = state.options.showToken === true ? /[\w$]/ : state.options.showToken;
|
|
|
|
var cur = cm.getCursor(), line = cm.getLine(cur.line), start = cur.ch, end = start;
|
|
|
|
while (start && re.test(line.charAt(start - 1))) --start;
|
|
|
|
while (end < line.length && re.test(line.charAt(end))) ++end;
|
2017-04-28 21:54:37 +00:00
|
|
|
/* STYLUS: hack start */
|
|
|
|
const token = line.slice(start, end);
|
|
|
|
if (token !== state.lastToken) {
|
|
|
|
state.lastToken = token;
|
|
|
|
removeOverlay(cm);
|
|
|
|
if (token) {
|
|
|
|
addOverlay(cm, token, re, state.options.style);
|
|
|
|
}
|
|
|
|
}
|
2017-03-19 03:17:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-28 21:54:37 +00:00
|
|
|
removeOverlay(cm);
|
|
|
|
/* STYLUS: hack end */
|
2017-03-19 03:17:23 +00:00
|
|
|
var from = cm.getCursor("from"), to = cm.getCursor("to");
|
|
|
|
if (from.line != to.line) return;
|
|
|
|
if (state.options.wordsOnly && !isWord(cm, from, to)) return;
|
|
|
|
var selection = cm.getRange(from, to)
|
|
|
|
if (state.options.trim) selection = selection.replace(/^\s+|\s+$/g, "")
|
|
|
|
if (selection.length >= state.options.minChars)
|
|
|
|
addOverlay(cm, selection, false, state.options.style);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function isWord(cm, from, to) {
|
|
|
|
var str = cm.getRange(from, to);
|
|
|
|
if (str.match(/^\w+$/) !== null) {
|
|
|
|
if (from.ch > 0) {
|
|
|
|
var pos = {line: from.line, ch: from.ch - 1};
|
|
|
|
var chr = cm.getRange(pos, from);
|
|
|
|
if (chr.match(/\W/) === null) return false;
|
|
|
|
}
|
|
|
|
if (to.ch < cm.getLine(from.line).length) {
|
|
|
|
var pos = {line: to.line, ch: to.ch + 1};
|
|
|
|
var chr = cm.getRange(to, pos);
|
|
|
|
if (chr.match(/\W/) === null) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function boundariesAround(stream, re) {
|
|
|
|
return (!stream.start || !re.test(stream.string.charAt(stream.start - 1))) &&
|
|
|
|
(stream.pos == stream.string.length || !re.test(stream.string.charAt(stream.pos)));
|
|
|
|
}
|
|
|
|
|
2017-03-28 22:54:31 +00:00
|
|
|
function makeOverlay(cm, query, hasBoundary, style) {
|
|
|
|
/* STYLUS: hack start (part 3) */
|
|
|
|
const approvedClassName = `cm-${style}-approved`;
|
|
|
|
let timer;
|
|
|
|
let occurrences = 0;
|
2017-03-19 03:17:23 +00:00
|
|
|
return {token: function(stream) {
|
2017-03-28 22:54:31 +00:00
|
|
|
clearTimeout(timer);
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
occurrences = 0;
|
|
|
|
timer = null;
|
|
|
|
});
|
2017-03-19 03:17:23 +00:00
|
|
|
if (stream.match(query) &&
|
2017-03-28 22:54:31 +00:00
|
|
|
(!hasBoundary || boundariesAround(stream, hasBoundary))) {
|
|
|
|
occurrences++;
|
|
|
|
if (occurrences == 1) {
|
|
|
|
cm.display.wrapper.classList.remove(approvedClassName);
|
|
|
|
} else if (occurrences == 2) {
|
|
|
|
cm.display.wrapper.classList.add(approvedClassName);
|
|
|
|
}
|
2017-03-19 03:17:23 +00:00
|
|
|
return style;
|
2017-03-28 22:54:31 +00:00
|
|
|
}
|
|
|
|
/* STYLUS: hack end (part 3) */
|
2017-03-19 03:17:23 +00:00
|
|
|
stream.next();
|
|
|
|
stream.skipTo(query.charAt(0)) || stream.skipToEnd();
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
});
|