Skip to content

Commit 4a68108

Browse files
Arnab Bosemarijnh
authored andcommitted
[match-highlighter addon] Add annotateScrollbar option
When enabled, and the annotatescrollbar addon is loaded, matches will be shown on the scrollbar.
1 parent d40f5e2 commit 4a68108

File tree

3 files changed

+94
-15
lines changed

3 files changed

+94
-15
lines changed

AUTHORS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Anthony Gégo
5959
Anthony Grimes
6060
Anton Kovalyov
6161
AQNOUCH Mohammed
62+
Arnab Bose
6263
areos
6364
as3boyan
6465
AtomicPages LLC
@@ -183,6 +184,7 @@ Gergely Hegykozi
183184
Giovanni Calò
184185
Glenn Jorde
185186
Glenn Ruehle
187+
Google Inc.
186188
Golevka
187189
Gordon Smith
188190
Grant Skinner

addon/search/match-highlighter.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
// highlighted only if the selected text is a word. showToken, when enabled,
1717
// will cause the current token to be highlighted when nothing is selected.
1818
// delay is used to specify how much time to wait, in milliseconds, before
19-
// highlighting the matches.
19+
// highlighting the matches. If annotateScrollbar is enabled, the occurances
20+
// will be highlighted on the scrollbar via the matchesonscrollbar addon.
2021

2122
(function(mod) {
2223
if (typeof exports == "object" && typeof module == "object") // CommonJS
23-
mod(require("../../lib/codemirror"));
24+
mod(require("../../lib/codemirror"), require("./matchesonscrollbar"));
2425
else if (typeof define == "function" && define.amd) // AMD
25-
define(["../../lib/codemirror"], mod);
26+
define(["../../lib/codemirror", "./matchesonscrollbar"], mod);
2627
else // Plain browser env
2728
mod(CodeMirror);
2829
})(function(CodeMirror) {
@@ -40,18 +41,19 @@
4041
this.showToken = options.showToken;
4142
this.delay = options.delay;
4243
this.wordsOnly = options.wordsOnly;
44+
this.annotateScrollbar = options.annotateScrollbar;
4345
}
4446
if (this.style == null) this.style = DEFAULT_TOKEN_STYLE;
4547
if (this.minChars == null) this.minChars = DEFAULT_MIN_CHARS;
4648
if (this.delay == null) this.delay = DEFAULT_DELAY;
4749
if (this.wordsOnly == null) this.wordsOnly = DEFAULT_WORDS_ONLY;
4850
this.overlay = this.timeout = null;
51+
this.matchesonscroll = null;
4952
}
5053

5154
CodeMirror.defineOption("highlightSelectionMatches", false, function(cm, val, old) {
5255
if (old && old != CodeMirror.Init) {
53-
var over = cm.state.matchHighlighter.overlay;
54-
if (over) cm.removeOverlay(over);
56+
removeOverlay(cm);
5557
clearTimeout(cm.state.matchHighlighter.timeout);
5658
cm.state.matchHighlighter = null;
5759
cm.off("cursorActivity", cursorActivity);
@@ -69,28 +71,47 @@
6971
state.timeout = setTimeout(function() {highlightMatches(cm);}, state.delay);
7072
}
7173

74+
function addOverlay(cm, query, hasBoundary, style) {
75+
var state = cm.state.matchHighlighter;
76+
cm.addOverlay(state.overlay = makeOverlay(query, hasBoundary, style));
77+
if (state.annotateScrollbar) {
78+
var searchFor = hasBoundary ? new RegExp("\\b" + query + "\\b") : query;
79+
state.matchesonscroll = cm.showMatchesOnScrollbar(searchFor, true,
80+
{className: "CodeMirror-selection-highlight-scrollbar"});
81+
}
82+
}
83+
84+
function removeOverlay(cm) {
85+
var state = cm.state.matchHighlighter;
86+
if (state.overlay) {
87+
cm.removeOverlay(state.overlay);
88+
state.overlay = null;
89+
if (state.annotateScrollbar) {
90+
state.matchesonscroll.clear();
91+
state.matchesonscroll = null;
92+
}
93+
}
94+
}
95+
7296
function highlightMatches(cm) {
7397
cm.operation(function() {
7498
var state = cm.state.matchHighlighter;
75-
if (state.overlay) {
76-
cm.removeOverlay(state.overlay);
77-
state.overlay = null;
78-
}
99+
removeOverlay(cm);
79100
if (!cm.somethingSelected() && state.showToken) {
80101
var re = state.showToken === true ? /[\w$]/ : state.showToken;
81102
var cur = cm.getCursor(), line = cm.getLine(cur.line), start = cur.ch, end = start;
82103
while (start && re.test(line.charAt(start - 1))) --start;
83104
while (end < line.length && re.test(line.charAt(end))) ++end;
84105
if (start < end)
85-
cm.addOverlay(state.overlay = makeOverlay(line.slice(start, end), re, state.style));
106+
addOverlay(cm, line.slice(start, end), re, state.style);
86107
return;
87108
}
88109
var from = cm.getCursor("from"), to = cm.getCursor("to");
89110
if (from.line != to.line) return;
90111
if (state.wordsOnly && !isWord(cm, from, to)) return;
91112
var selection = cm.getRange(from, to).replace(/^\s+|\s+$/g, "");
92113
if (selection.length >= state.minChars)
93-
cm.addOverlay(state.overlay = makeOverlay(selection, false, state.style));
114+
addOverlay(cm, selection, false, state.style);
94115
});
95116
}
96117

demo/matchhighlighter.html

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
<link rel="stylesheet" href="../lib/codemirror.css">
88
<script src="../lib/codemirror.js"></script>
9+
<script src="../addon/scroll/annotatescrollbar.js"></script>
10+
<script src="../addon/search/matchesonscrollbar.js"></script>
911
<script src="../addon/search/searchcursor.js"></script>
1012
<script src="../addon/search/match-highlighter.js"></script>
1113
<style type="text/css">
@@ -15,6 +17,8 @@
1517
background-position: bottom;
1618
background-repeat: repeat-x;
1719
}
20+
.cm-matchhighlight {background-color: lightgreen}
21+
.CodeMirror-selection-highlight-scrollbar {background-color: green}
1822
</style>
1923
<div id=nav>
2024
<a href="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../doc/logo.png"></a>
@@ -31,14 +35,66 @@
3135

3236
<article>
3337
<h2>Match Highlighter Demo</h2>
34-
<form><textarea id="code" name="code">Select this text: hardToSpotVar
35-
And everywhere else in your code where hardToSpotVar appears will automatically illuminate.
36-
Give it a try! No more hardToSpotVars.</textarea></form>
38+
<form><textarea id="code" name="code">Select this text: hardtospot
39+
And everywhere else in your code where hardtospot appears will
40+
automatically illuminate. Give it a try! No more hard to spot
41+
variables - stay in context of your code all the time.
42+
43+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut pharetra
44+
interdum dui eu pulvinar. Mauris maximus ligula venenatis tempus
45+
interdum. Cras hendrerit, ipsum sed ultrices pharetra, ligula diam
46+
porttitor lacus, ac tempor eros est a massa. Nam orci elit, vulputate
47+
in tristique quis, consectetur vitae metus. Pellentesque et enim
48+
elementum, lobortis augue in, lacinia sapien. Morbi eu nunc semper,
49+
sagittis felis a, pellentesque mauris. Lorem ipsum dolor sit amet,
50+
consectetur adipiscing elit. Aenean quis diam turpis.
51+
52+
Fusce lobortis nisl quis aliquet euismod. Aenean vitae nulla non ipsum
53+
efficitur scelerisque. Curabitur auctor, lorem non rhoncus porttitor,
54+
augue ligula lacinia dolor, et vehicula magna lorem imperdiet velit.
55+
Fusce risus sem, hardtospot commodo eleifend hendrerit vitae, mollis
56+
quis risus. Cras tincidunt, justo vitae hendrerit venenatis, urna
57+
dolor placerat tortor, eu lobortis lectus dolor in ligula. Nullam non
58+
erat non nisl vulputate ultrices sit amet vestibulum dolor. Quisque in
59+
tortor porta, pellentesque odio nec, malesuada nibh.
60+
61+
In a dui feugiat, ullamcorper urna in, accumsan magna. Donec egestas
62+
sem nec eros rhoncus, vel gravida purus ornare. Nulla orci mauris,
63+
porta nec pharetra sed, ornare et lorem. Donec luctus turpis nunc,
64+
eget dictum felis mollis et. Sed sodales hardtospot nunc vitae leo
65+
rhoncus imperdiet. Donec elementum malesuada velit quis placerat.
66+
Proin accumsan lorem id nisi volutpat ullamcorper. Vivamus laoreet
67+
dolor ac sem malesuada, ac scelerisque ex efficitur. Aliquam tempus
68+
libero velit, vel tristique augue vulputate nec.
69+
70+
Mauris ultrices leo felis, sit amet congue augue aliquam condimentum.
71+
Vivamus purus leo, mattis vitae dignissim vel, ultricies ac ex. Mauris
72+
eu dolor eu purus ultricies ultrices. Sed euismod feugiat ex et
73+
mattis. Morbi cursus laoreet pharetra. Donec eu dolor sodales,
74+
ultricies nisi et, malesuada urna. Praesent sit amet fringilla felis.
75+
Nam rhoncus, est blandit auctor auctor, lorem ipsum laoreet ipsum,
76+
quis sodales libero odio in lorem. Phasellus odio dolor, elementum
77+
sagittis nibh non, fermentum semper libero. Mauris hendrerit
78+
hardtospot lectus sit amet commodo eleifend. Morbi pulvinar eget nisl
79+
at eleifend. Fusce eget porta erat, vitae lobortis libero.
80+
81+
Phasellus sit amet massa in massa pharetra malesuada. Vestibulum at
82+
quam vel libero aliquam volutpat at ut dui. Praesent scelerisque vel
83+
mauris sit amet vehicula. Phasellus at mi nec ligula cursus interdum
84+
sit amet non quam. Aliquam tempus sollicitudin euismod. Nulla euismod
85+
mollis enim tincidunt placerat. Proin ac scelerisque enim, quis
86+
sollicitudin metus. Pellentesque congue nec sapien ut rhoncus. Sed
87+
eget ornare diam, ut consectetur ante. Aenean eleifend mauris quis
88+
ornare accumsan. In hac habitasse hardtospot platea dictumst.
89+
90+
</textarea></form>
3791

3892
<script>
3993
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
4094
lineNumbers: true,
41-
highlightSelectionMatches: {showToken: /\w/}
95+
// To highlight on scrollbars as well, pass annotateScrollbar in options
96+
// as below.
97+
highlightSelectionMatches: {showToken: /\w/, annotateScrollbar: true}
4298
});
4399
</script>
44100

0 commit comments

Comments
 (0)