You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
2.1 KiB

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