searchcursor.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. (function(){
  2. function SearchCursor(cm, query, pos, caseFold) {
  3. this.atOccurrence = false; this.cm = cm;
  4. if (caseFold == null && typeof query == "string") caseFold = false;
  5. pos = pos ? cm.clipPos(pos) : {line: 0, ch: 0};
  6. this.pos = {from: pos, to: pos};
  7. // The matches method is filled in based on the type of query.
  8. // It takes a position and a direction, and returns an object
  9. // describing the next occurrence of the query, or null if no
  10. // more matches were found.
  11. if (typeof query != "string") { // Regexp match
  12. if (!query.global) query = new RegExp(query.source, query.ignoreCase ? "ig" : "g");
  13. this.matches = function(reverse, pos) {
  14. if (reverse) {
  15. query.lastIndex = 0;
  16. var line = cm.getLine(pos.line).slice(0, pos.ch), match = query.exec(line), start = 0;
  17. while (match) {
  18. start += match.index + 1;
  19. line = line.slice(start);
  20. query.lastIndex = 0;
  21. var newmatch = query.exec(line);
  22. if (newmatch) match = newmatch;
  23. else break;
  24. }
  25. start--;
  26. } else {
  27. query.lastIndex = pos.ch;
  28. var line = cm.getLine(pos.line), match = query.exec(line),
  29. start = match && match.index;
  30. }
  31. if (match)
  32. return {from: {line: pos.line, ch: start},
  33. to: {line: pos.line, ch: start + match[0].length},
  34. match: match};
  35. };
  36. } else { // String query
  37. if (caseFold) query = query.toLowerCase();
  38. var fold = caseFold ? function(str){return str.toLowerCase();} : function(str){return str;};
  39. var target = query.split("\n");
  40. // Different methods for single-line and multi-line queries
  41. if (target.length == 1)
  42. this.matches = function(reverse, pos) {
  43. var line = fold(cm.getLine(pos.line)), len = query.length, match;
  44. if (reverse ? (pos.ch >= len && (match = line.lastIndexOf(query, pos.ch - len)) != -1)
  45. : (match = line.indexOf(query, pos.ch)) != -1)
  46. return {from: {line: pos.line, ch: match},
  47. to: {line: pos.line, ch: match + len}};
  48. };
  49. else
  50. this.matches = function(reverse, pos) {
  51. var ln = pos.line, idx = (reverse ? target.length - 1 : 0), match = target[idx], line = fold(cm.getLine(ln));
  52. var offsetA = (reverse ? line.indexOf(match) + match.length : line.lastIndexOf(match));
  53. if (reverse ? offsetA >= pos.ch || offsetA != match.length
  54. : offsetA <= pos.ch || offsetA != line.length - match.length)
  55. return;
  56. for (;;) {
  57. if (reverse ? !ln : ln == cm.lineCount() - 1) return;
  58. line = fold(cm.getLine(ln += reverse ? -1 : 1));
  59. match = target[reverse ? --idx : ++idx];
  60. if (idx > 0 && idx < target.length - 1) {
  61. if (line != match) return;
  62. else continue;
  63. }
  64. var offsetB = (reverse ? line.lastIndexOf(match) : line.indexOf(match) + match.length);
  65. if (reverse ? offsetB != line.length - match.length : offsetB != match.length)
  66. return;
  67. var start = {line: pos.line, ch: offsetA}, end = {line: ln, ch: offsetB};
  68. return {from: reverse ? end : start, to: reverse ? start : end};
  69. }
  70. };
  71. }
  72. }
  73. SearchCursor.prototype = {
  74. findNext: function() {return this.find(false);},
  75. findPrevious: function() {return this.find(true);},
  76. find: function(reverse) {
  77. var self = this, pos = this.cm.clipPos(reverse ? this.pos.from : this.pos.to);
  78. function savePosAndFail(line) {
  79. var pos = {line: line, ch: 0};
  80. self.pos = {from: pos, to: pos};
  81. self.atOccurrence = false;
  82. return false;
  83. }
  84. for (;;) {
  85. if (this.pos = this.matches(reverse, pos)) {
  86. this.atOccurrence = true;
  87. return this.pos.match || true;
  88. }
  89. if (reverse) {
  90. if (!pos.line) return savePosAndFail(0);
  91. pos = {line: pos.line-1, ch: this.cm.getLine(pos.line-1).length};
  92. }
  93. else {
  94. var maxLine = this.cm.lineCount();
  95. if (pos.line == maxLine - 1) return savePosAndFail(maxLine);
  96. pos = {line: pos.line+1, ch: 0};
  97. }
  98. }
  99. },
  100. from: function() {if (this.atOccurrence) return this.pos.from;},
  101. to: function() {if (this.atOccurrence) return this.pos.to;},
  102. replace: function(newText) {
  103. var self = this;
  104. if (this.atOccurrence)
  105. self.pos.to = this.cm.replaceRange(newText, self.pos.from, self.pos.to);
  106. }
  107. };
  108. CodeMirror.defineExtension("getSearchCursor", function(query, pos, caseFold) {
  109. return new SearchCursor(this, query, pos, caseFold);
  110. });
  111. })();