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.

97 lines
3.8 KiB

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