1
0

matchesonscrollbar.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"), require("./searchcursor"), require("../scroll/annotatescrollbar"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror", "./searchcursor", "../scroll/annotatescrollbar"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineExtension("showMatchesOnScrollbar", function(query, caseFold, className) {
  13. return new SearchAnnotation(this, query, caseFold, className);
  14. });
  15. function SearchAnnotation(cm, query, caseFold, className) {
  16. this.cm = cm;
  17. this.annotation = cm.annotateScrollbar(className || "CodeMirror-search-match");
  18. this.query = query;
  19. this.caseFold = caseFold;
  20. this.gap = {from: cm.firstLine(), to: cm.lastLine() + 1};
  21. this.matches = [];
  22. this.update = null;
  23. this.findMatches();
  24. this.annotation.update(this.matches);
  25. var self = this;
  26. cm.on("change", this.changeHandler = function(_cm, change) { self.onChange(change); });
  27. }
  28. var MAX_MATCHES = 1000;
  29. SearchAnnotation.prototype.findMatches = function() {
  30. if (!this.gap) return;
  31. for (var i = 0; i < this.matches.length; i++) {
  32. var match = this.matches[i];
  33. if (match.from.line >= this.gap.to) break;
  34. if (match.to.line >= this.gap.from) this.matches.splice(i--, 1);
  35. }
  36. var cursor = this.cm.getSearchCursor(this.query, CodeMirror.Pos(this.gap.from, 0), this.caseFold);
  37. while (cursor.findNext()) {
  38. var match = {from: cursor.from(), to: cursor.to()};
  39. if (match.from.line >= this.gap.to) break;
  40. this.matches.splice(i++, 0, match);
  41. if (this.matches.length > MAX_MATCHES) break;
  42. }
  43. this.gap = null;
  44. };
  45. function offsetLine(line, changeStart, sizeChange) {
  46. if (line <= changeStart) return line;
  47. return Math.max(changeStart, line + sizeChange);
  48. }
  49. SearchAnnotation.prototype.onChange = function(change) {
  50. var startLine = change.from.line;
  51. var endLine = CodeMirror.changeEnd(change).line;
  52. var sizeChange = endLine - change.to.line;
  53. if (this.gap) {
  54. this.gap.from = Math.min(offsetLine(this.gap.from, startLine, sizeChange), change.from.line);
  55. this.gap.to = Math.max(offsetLine(this.gap.to, startLine, sizeChange), change.from.line);
  56. } else {
  57. this.gap = {from: change.from.line, to: endLine + 1};
  58. }
  59. if (sizeChange) for (var i = 0; i < this.matches.length; i++) {
  60. var match = this.matches[i];
  61. var newFrom = offsetLine(match.from.line, startLine, sizeChange);
  62. if (newFrom != match.from.line) match.from = CodeMirror.Pos(newFrom, match.from.ch);
  63. var newTo = offsetLine(match.to.line, startLine, sizeChange);
  64. if (newTo != match.to.line) match.to = CodeMirror.Pos(newTo, match.to.ch);
  65. }
  66. clearTimeout(this.update);
  67. var self = this;
  68. this.update = setTimeout(function() { self.updateAfterChange(); }, 250);
  69. };
  70. SearchAnnotation.prototype.updateAfterChange = function() {
  71. this.findMatches();
  72. this.annotation.update(this.matches);
  73. };
  74. SearchAnnotation.prototype.clear = function() {
  75. this.cm.off("change", this.changeHandler);
  76. this.annotation.clear();
  77. };
  78. });