2015-05-04 07:53:29 +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
|
2016-04-20 10:11:40 +00:00
|
|
|
// highlighting the matches. If annotateScrollbar is enabled, the occurences
|
2016-01-17 20:28:04 +00:00
|
|
|
// will be highlighted on the scrollbar via the matchesonscrollbar addon.
|
2015-05-04 07:53:29 +00:00
|
|
|
|
|
|
|
(function(mod) {
|
|
|
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
2016-01-17 20:28:04 +00:00
|
|
|
mod(require("../../lib/codemirror"), require("./matchesonscrollbar"));
|
2015-05-04 07:53:29 +00:00
|
|
|
else if (typeof define == "function" && define.amd) // AMD
|
2016-01-17 20:28:04 +00:00
|
|
|
define(["../../lib/codemirror", "./matchesonscrollbar"], mod);
|
2015-05-04 07:53:29 +00:00
|
|
|
else // Plain browser env
|
|
|
|
mod(CodeMirror);
|
|
|
|
})(function(CodeMirror) {
|
|
|
|
"use strict";
|
|
|
|
|
2016-06-01 06:37:28 +00:00
|
|
|
var defaults = {
|
|
|
|
style: "matchhighlight",
|
|
|
|
minChars: 2,
|
|
|
|
delay: 100,
|
|
|
|
wordsOnly: false,
|
|
|
|
annotateScrollbar: false,
|
|
|
|
showToken: false,
|
|
|
|
trim: true
|
|
|
|
}
|
2015-05-04 07:53:29 +00:00
|
|
|
|
|
|
|
function State(options) {
|
2016-06-01 06:37:28 +00:00
|
|
|
this.options = {}
|
|
|
|
for (var name in defaults)
|
|
|
|
this.options[name] = (options && options.hasOwnProperty(name) ? options : defaults)[name]
|
2015-05-04 07:53:29 +00:00
|
|
|
this.overlay = this.timeout = null;
|
2016-01-17 20:28:04 +00:00
|
|
|
this.matchesonscroll = null;
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CodeMirror.defineOption("highlightSelectionMatches", false, function(cm, val, old) {
|
|
|
|
if (old && old != CodeMirror.Init) {
|
2016-01-17 20:28:04 +00:00
|
|
|
removeOverlay(cm);
|
2015-05-04 07:53:29 +00:00
|
|
|
clearTimeout(cm.state.matchHighlighter.timeout);
|
|
|
|
cm.state.matchHighlighter = null;
|
|
|
|
cm.off("cursorActivity", cursorActivity);
|
|
|
|
}
|
|
|
|
if (val) {
|
|
|
|
cm.state.matchHighlighter = new State(val);
|
|
|
|
highlightMatches(cm);
|
|
|
|
cm.on("cursorActivity", cursorActivity);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function cursorActivity(cm) {
|
|
|
|
var state = cm.state.matchHighlighter;
|
|
|
|
clearTimeout(state.timeout);
|
2016-06-01 06:37:28 +00:00
|
|
|
state.timeout = setTimeout(function() {highlightMatches(cm);}, state.options.delay);
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 20:28:04 +00:00
|
|
|
function addOverlay(cm, query, hasBoundary, style) {
|
|
|
|
var state = cm.state.matchHighlighter;
|
|
|
|
cm.addOverlay(state.overlay = makeOverlay(query, hasBoundary, style));
|
2016-06-01 06:37:28 +00:00
|
|
|
if (state.options.annotateScrollbar && cm.showMatchesOnScrollbar) {
|
2016-01-17 20:28:04 +00:00
|
|
|
var searchFor = hasBoundary ? new RegExp("\\b" + query + "\\b") : query;
|
2016-07-30 04:25:24 +00:00
|
|
|
state.matchesonscroll = cm.showMatchesOnScrollbar(searchFor, false,
|
2016-01-17 20:28:04 +00:00
|
|
|
{className: "CodeMirror-selection-highlight-scrollbar"});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeOverlay(cm) {
|
|
|
|
var state = cm.state.matchHighlighter;
|
|
|
|
if (state.overlay) {
|
|
|
|
cm.removeOverlay(state.overlay);
|
|
|
|
state.overlay = null;
|
2016-06-01 06:37:28 +00:00
|
|
|
if (state.matchesonscroll) {
|
2016-01-17 20:28:04 +00:00
|
|
|
state.matchesonscroll.clear();
|
|
|
|
state.matchesonscroll = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 07:53:29 +00:00
|
|
|
function highlightMatches(cm) {
|
|
|
|
cm.operation(function() {
|
|
|
|
var state = cm.state.matchHighlighter;
|
2016-01-17 20:28:04 +00:00
|
|
|
removeOverlay(cm);
|
2016-06-01 06:37:28 +00:00
|
|
|
if (!cm.somethingSelected() && state.options.showToken) {
|
|
|
|
var re = state.options.showToken === true ? /[\w$]/ : state.options.showToken;
|
2015-05-04 07:53:29 +00:00
|
|
|
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;
|
|
|
|
if (start < end)
|
2016-06-01 06:37:28 +00:00
|
|
|
addOverlay(cm, line.slice(start, end), re, state.options.style);
|
2015-05-04 07:53:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var from = cm.getCursor("from"), to = cm.getCursor("to");
|
|
|
|
if (from.line != to.line) return;
|
2016-06-01 06:37:28 +00:00
|
|
|
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);
|
2015-05-04 07:53:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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)));
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeOverlay(query, hasBoundary, style) {
|
|
|
|
return {token: function(stream) {
|
|
|
|
if (stream.match(query) &&
|
|
|
|
(!hasBoundary || boundariesAround(stream, hasBoundary)))
|
|
|
|
return style;
|
|
|
|
stream.next();
|
|
|
|
stream.skipTo(query.charAt(0)) || stream.skipToEnd();
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
});
|