1
0

matchesonscrollbar.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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, options) {
  13. if (typeof options == "string") options = {className: options};
  14. if (!options) options = {};
  15. return new SearchAnnotation(this, query, caseFold, options);
  16. });
  17. function SearchAnnotation(cm, query, caseFold, options) {
  18. this.cm = cm;
  19. var annotateOptions = {listenForChanges: false};
  20. for (var prop in options) annotateOptions[prop] = options[prop];
  21. if (!annotateOptions.className) annotateOptions.className = "CodeMirror-search-match";
  22. this.annotation = cm.annotateScrollbar(annotateOptions);
  23. this.query = query;
  24. this.caseFold = caseFold;
  25. this.gap = {from: cm.firstLine(), to: cm.lastLine() + 1};
  26. this.matches = [];
  27. this.update = null;
  28. this.findMatches();
  29. this.annotation.update(this.matches);
  30. var self = this;
  31. cm.on("change", this.changeHandler = function(_cm, change) { self.onChange(change); });
  32. }
  33. var MAX_MATCHES = 1000;
  34. SearchAnnotation.prototype.findMatches = function() {
  35. if (!this.gap) return;
  36. for (var i = 0; i < this.matches.length; i++) {
  37. var match = this.matches[i];
  38. if (match.from.line >= this.gap.to) break;
  39. if (match.to.line >= this.gap.from) this.matches.splice(i--, 1);
  40. }
  41. var cursor = this.cm.getSearchCursor(this.query, CodeMirror.Pos(this.gap.from, 0), this.caseFold);
  42. while (cursor.findNext()) {
  43. var match = {from: cursor.from(), to: cursor.to()};
  44. if (match.from.line >= this.gap.to) break;
  45. this.matches.splice(i++, 0, match);
  46. if (this.matches.length > MAX_MATCHES) break;
  47. }
  48. this.gap = null;
  49. };
  50. function offsetLine(line, changeStart, sizeChange) {
  51. if (line <= changeStart) return line;
  52. return Math.max(changeStart, line + sizeChange);
  53. }
  54. SearchAnnotation.prototype.onChange = function(change) {
  55. var startLine = change.from.line;
  56. var endLine = CodeMirror.changeEnd(change).line;
  57. var sizeChange = endLine - change.to.line;
  58. if (this.gap) {
  59. this.gap.from = Math.min(offsetLine(this.gap.from, startLine, sizeChange), change.from.line);
  60. this.gap.to = Math.max(offsetLine(this.gap.to, startLine, sizeChange), change.from.line);
  61. } else {
  62. this.gap = {from: change.from.line, to: endLine + 1};
  63. }
  64. if (sizeChange) for (var i = 0; i < this.matches.length; i++) {
  65. var match = this.matches[i];
  66. var newFrom = offsetLine(match.from.line, startLine, sizeChange);
  67. if (newFrom != match.from.line) match.from = CodeMirror.Pos(newFrom, match.from.ch);
  68. var newTo = offsetLine(match.to.line, startLine, sizeChange);
  69. if (newTo != match.to.line) match.to = CodeMirror.Pos(newTo, match.to.ch);
  70. }
  71. clearTimeout(this.update);
  72. var self = this;
  73. this.update = setTimeout(function() { self.updateAfterChange(); }, 250);
  74. };
  75. SearchAnnotation.prototype.updateAfterChange = function() {
  76. this.findMatches();
  77. this.annotation.update(this.matches);
  78. };
  79. SearchAnnotation.prototype.clear = function() {
  80. this.cm.off("change", this.changeHandler);
  81. this.annotation.clear();
  82. };
  83. });