Skip to content

Commit f09b6c5

Browse files
danieledsmarijnh
authored andcommitted
Added wordsOnly option. Updated documentation.
1 parent 640fbb2 commit f09b6c5

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

addon/search/match-highlighter.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
// document.
99
//
1010
// The option can be set to true to simply enable it, or to a
11-
// {minChars, style, showToken} object to explicitly configure it.
12-
// minChars is the minimum amount of characters that should be
11+
// {minChars, style, wordsOnly, showToken, delay} object to explicitly
12+
// configure it. minChars is the minimum amount of characters that should be
1313
// selected for the behavior to occur, and style is the token style to
1414
// apply to the matches. This will be prefixed by "cm-" to create an
15-
// actual CSS class name. showToken, when enabled, will cause the
16-
// current token to be highlighted when nothing is selected.
15+
// actual CSS class name. If wordsOnly is enabled, the matches will be
16+
// highlighted only if the selected text is a word. showToken, when enabled,
17+
// will cause the current token to be highlighted when nothing is selected.
18+
// delay is used to specify how much time to wait, in milliseconds, before
19+
// highlighting the matches.
1720

1821
(function(mod) {
1922
if (typeof exports == "object" && typeof module == "object") // CommonJS
@@ -28,17 +31,20 @@
2831
var DEFAULT_MIN_CHARS = 2;
2932
var DEFAULT_TOKEN_STYLE = "matchhighlight";
3033
var DEFAULT_DELAY = 100;
34+
var DEFAULT_WORDS_ONLY = false;
3135

3236
function State(options) {
3337
if (typeof options == "object") {
3438
this.minChars = options.minChars;
3539
this.style = options.style;
3640
this.showToken = options.showToken;
3741
this.delay = options.delay;
42+
this.wordsOnly = options.wordsOnly;
3843
}
3944
if (this.style == null) this.style = DEFAULT_TOKEN_STYLE;
4045
if (this.minChars == null) this.minChars = DEFAULT_MIN_CHARS;
4146
if (this.delay == null) this.delay = DEFAULT_DELAY;
47+
if (this.wordsOnly == null) this.wordsOnly = DEFAULT_WORDS_ONLY;
4248
this.overlay = this.timeout = null;
4349
}
4450

@@ -81,12 +87,30 @@
8187
}
8288
var from = cm.getCursor("from"), to = cm.getCursor("to");
8389
if (from.line != to.line) return;
90+
if (state.wordsOnly && !isWord(cm, from, to)) return;
8491
var selection = cm.getRange(from, to).replace(/^\s+|\s+$/g, "");
8592
if (selection.length >= state.minChars)
8693
cm.addOverlay(state.overlay = makeOverlay(selection, false, state.style));
8794
});
8895
}
8996

97+
function isWord(cm, from, to) {
98+
var str = cm.getRange(from, to);
99+
if (str.match(/^\w+$/) !== null) {
100+
if (from.ch > 0) {
101+
var pos = {line: from.line, ch: from.ch - 1};
102+
var chr = cm.getRange(pos, from);
103+
if (chr.match(/\W/) === null) return false;
104+
}
105+
if (to.ch < cm.getLine(from.line).length) {
106+
var pos = {line: to.line, ch: to.ch + 1};
107+
var chr = cm.getRange(to, pos);
108+
if (chr.match(/\W/) === null) return false;
109+
}
110+
return true;
111+
} else return false;
112+
}
113+
90114
function boundariesAround(stream, re) {
91115
return (!stream.start || !re.test(stream.string.charAt(stream.start - 1))) &&
92116
(stream.pos == stream.string.length || !re.test(stream.string.charAt(stream.pos)));

0 commit comments

Comments
 (0)