Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotate scrollbar when matches are folded #6388

Merged
merged 1 commit into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion addon/scroll/annotatescrollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,20 @@
var wrapping = cm.getOption("lineWrapping");
var singleLineH = wrapping && cm.defaultTextHeight() * 1.5;
var curLine = null, curLineObj = null;

function getFoldLineHandle(pos) {
var marks = cm.findMarksAt(pos);
for (var i = 0; i < marks.length; ++i) {
if (marks[i].collapsed)
return marks[i].lines[0];
}
}

function getY(pos, top) {
if (curLine != pos.line) {
curLine = pos.line;
curLineObj = cm.getLineHandle(curLine);
if(!(curLineObj = getFoldLineHandle(pos)))
curLineObj = cm.getLineHandle(curLine);
}
if ((curLineObj.widgets && curLineObj.widgets.length) ||
(wrapping && curLineObj.height > singleLineH))
Expand Down
55 changes: 55 additions & 0 deletions test/annotatescrollbar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace = "annotatescrollbar_";

(function () {
function test(name, run, content, query, expected) {
return testCM(name, function (cm) {
var annotation = cm.annotateScrollbar({
listenForChanges: false,
className: "CodeMirror-search-match"
});
var matches = [];
var cursor = cm.getSearchCursor(query, CodeMirror.Pos(0, 0));
while (cursor.findNext()) {
var match = {
from: cursor.from(),
to: cursor.to()
};
matches.push(match)
}

if (run) run(cm);

cm.display.barWidth = 5;
annotation.update(matches);

var annotations = cm.getWrapperElement().getElementsByClassName(annotation.options.className);
eq(annotations.length, expected, "Expected " + expected + " annotations on the scrollbar.")
}, {
value: content,
mode: "javascript",
foldOptions: {
rangeFinder: CodeMirror.fold.brace
}
});
}

function doFold(cm) {
cm.foldCode(cm.getCursor());
}
var simpleProg = "function foo() {\n\n return \"foo\";\n\n}\n\nfoo();\n";
var consecutiveLineMatches = "function foo() {\n return \"foo\";\n}\nfoo();\n";
var singleLineMatches = "function foo() { return \"foo\"; }foo();\n";

// Base case - expect 3 matches and 3 annotations
test("simple", null, simpleProg, "foo", 3);
// Consecutive line matches are combines into a single annotation - expect 3 matches and 2 annotations
test("combineConsecutiveLine", null, consecutiveLineMatches, "foo", 2);
// Matches on a single line get a single annotation - expect 3 matches and 1 annotation
test("combineSingleLine", null, singleLineMatches, "foo", 1);
// Matches within a fold are annotated on the folded line - expect 3 matches and 2 annotations
test("simpleFold", doFold, simpleProg, "foo", 2);
// Combination of combineConsecutiveLine and simpleFold cases - expect 3 matches and 1 annotation
test("foldedMatch", doFold, consecutiveLineMatches, "foo", 1);
// Hidden matches within a fold are annotated on the folded line - expect 1 match and 1 annotation
test("hiddenMatch", doFold, simpleProg, "return", 1);
})();
2 changes: 2 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,14 @@ <h2>Test Suite</h2>
<script src="../addon/fold/foldcode.js"></script>
<script src="../addon/fold/brace-fold.js"></script>
<script src="../addon/fold/xml-fold.js"></script>
<script src="../addon/scroll/annotatescrollbar.js"></script>

<script src="emacs_test.js"></script>
<script src="sql-hint-test.js"></script>
<script src="sublime_test.js"></script>
<script src="vim_test.js"></script>
<script src="html-hint-test.js"></script>
<script src="annotatescrollbar.js"></script>
<script>
window.onload = runHarness;
CodeMirror.on(window, 'hashchange', runHarness);
Expand Down