Skip to content

Commit 55d0484

Browse files
committed
Annotate scrollbar when matches are folded
1 parent 83b9f82 commit 55d0484

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

addon/scroll/annotatescrollbar.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,20 @@
7272
var wrapping = cm.getOption("lineWrapping");
7373
var singleLineH = wrapping && cm.defaultTextHeight() * 1.5;
7474
var curLine = null, curLineObj = null;
75+
76+
function getFoldLineHandle(pos) {
77+
var marks = cm.findMarksAt(pos);
78+
for (var i = 0; i < marks.length; ++i) {
79+
if (marks[i].collapsed)
80+
return marks[i].lines[0];
81+
}
82+
}
83+
7584
function getY(pos, top) {
7685
if (curLine != pos.line) {
7786
curLine = pos.line;
78-
curLineObj = cm.getLineHandle(curLine);
87+
if(!(curLineObj = getFoldLineHandle(pos)))
88+
curLineObj = cm.getLineHandle(curLine);
7989
}
8090
if ((curLineObj.widgets && curLineObj.widgets.length) ||
8191
(wrapping && curLineObj.height > singleLineH))

test/annotatescrollbar.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
namespace = "annotatescrollbar_";
2+
3+
(function () {
4+
function test(name, run, content, query, expected) {
5+
return testCM(name, function (cm) {
6+
var annotation = cm.annotateScrollbar({
7+
listenForChanges: false,
8+
className: "CodeMirror-search-match"
9+
});
10+
var matches = [];
11+
var cursor = cm.getSearchCursor(query, CodeMirror.Pos(0, 0));
12+
while (cursor.findNext()) {
13+
var match = {
14+
from: cursor.from(),
15+
to: cursor.to()
16+
};
17+
matches.push(match)
18+
}
19+
20+
if (run) run(cm);
21+
22+
cm.display.barWidth = 5;
23+
annotation.update(matches);
24+
25+
var annotations = cm.getWrapperElement().getElementsByClassName(annotation.options.className);
26+
eq(annotations.length, expected, "Expected " + expected + " annotations on the scrollbar.")
27+
}, {
28+
value: content,
29+
mode: "javascript",
30+
foldOptions: {
31+
rangeFinder: CodeMirror.fold.brace
32+
}
33+
});
34+
}
35+
36+
function doFold(cm) {
37+
cm.foldCode(cm.getCursor());
38+
}
39+
var simpleProg = "function foo() {\n\n return \"foo\";\n\n}\n\nfoo();\n";
40+
var consecutiveLineMatches = "function foo() {\n return \"foo\";\n}\nfoo();\n";
41+
var singleLineMatches = "function foo() { return \"foo\"; }foo();\n";
42+
43+
// Base case - expect 3 matches and 3 annotations
44+
test("simple", null, simpleProg, "foo", 3);
45+
// Consecutive line matches are combines into a single annotation - expect 3 matches and 2 annotations
46+
test("combineConsecutiveLine", null, consecutiveLineMatches, "foo", 2);
47+
// Matches on a single line get a single annotation - expect 3 matches and 1 annotation
48+
test("combineSingleLine", null, singleLineMatches, "foo", 1);
49+
// Matches within a fold are annotated on the folded line - expect 3 matches and 2 annotations
50+
test("simpleFold", doFold, simpleProg, "foo", 2);
51+
// Combination of combineConsecutiveLine and simpleFold cases - expect 3 matches and 1 annotation
52+
test("foldedMatch", doFold, consecutiveLineMatches, "foo", 1);
53+
// Hidden matches within a fold are annotated on the folded line - expect 1 match and 1 annotation
54+
test("hiddenMatch", doFold, simpleProg, "return", 1);
55+
})();

test/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,14 @@ <h2>Test Suite</h2>
157157
<script src="../addon/fold/foldcode.js"></script>
158158
<script src="../addon/fold/brace-fold.js"></script>
159159
<script src="../addon/fold/xml-fold.js"></script>
160+
<script src="../addon/scroll/annotatescrollbar.js"></script>
160161

161162
<script src="emacs_test.js"></script>
162163
<script src="sql-hint-test.js"></script>
163164
<script src="sublime_test.js"></script>
164165
<script src="vim_test.js"></script>
165166
<script src="html-hint-test.js"></script>
167+
<script src="annotatescrollbar.js"></script>
166168
<script>
167169
window.onload = runHarness;
168170
CodeMirror.on(window, 'hashchange', runHarness);

0 commit comments

Comments
 (0)