searchcursor.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"))
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod)
  8. else // Plain browser env
  9. mod(CodeMirror)
  10. })(function(CodeMirror) {
  11. "use strict"
  12. var Pos = CodeMirror.Pos
  13. function regexpFlags(regexp) {
  14. var flags = regexp.flags
  15. return flags != null ? flags : (regexp.ignoreCase ? "i" : "")
  16. + (regexp.global ? "g" : "")
  17. + (regexp.multiline ? "m" : "")
  18. }
  19. function ensureFlags(regexp, flags) {
  20. var current = regexpFlags(regexp), target = current
  21. for (var i = 0; i < flags.length; i++) if (target.indexOf(flags.charAt(i)) == -1)
  22. target += flags.charAt(i)
  23. return current == target ? regexp : new RegExp(regexp.source, target)
  24. }
  25. function maybeMultiline(regexp) {
  26. return /\\s|\\n|\n|\\W|\\D|\[\^/.test(regexp.source)
  27. }
  28. function searchRegexpForward(doc, regexp, start) {
  29. regexp = ensureFlags(regexp, "g")
  30. for (var line = start.line, ch = start.ch, last = doc.lastLine(); line <= last; line++, ch = 0) {
  31. regexp.lastIndex = ch
  32. var string = doc.getLine(line), match = regexp.exec(string)
  33. if (match)
  34. return {from: Pos(line, match.index),
  35. to: Pos(line, match.index + match[0].length),
  36. match: match}
  37. }
  38. }
  39. function searchRegexpForwardMultiline(doc, regexp, start) {
  40. if (!maybeMultiline(regexp)) return searchRegexpForward(doc, regexp, start)
  41. regexp = ensureFlags(regexp, "gm")
  42. var string, chunk = 1
  43. for (var line = start.line, last = doc.lastLine(); line <= last;) {
  44. // This grows the search buffer in exponentially-sized chunks
  45. // between matches, so that nearby matches are fast and don't
  46. // require concatenating the whole document (in case we're
  47. // searching for something that has tons of matches), but at the
  48. // same time, the amount of retries is limited.
  49. for (var i = 0; i < chunk; i++) {
  50. if (line > last) break
  51. var curLine = doc.getLine(line++)
  52. string = string == null ? curLine : string + "\n" + curLine
  53. }
  54. chunk = chunk * 2
  55. regexp.lastIndex = start.ch
  56. var match = regexp.exec(string)
  57. if (match) {
  58. var before = string.slice(0, match.index).split("\n"), inside = match[0].split("\n")
  59. var startLine = start.line + before.length - 1, startCh = before[before.length - 1].length
  60. return {from: Pos(startLine, startCh),
  61. to: Pos(startLine + inside.length - 1,
  62. inside.length == 1 ? startCh + inside[0].length : inside[inside.length - 1].length),
  63. match: match}
  64. }
  65. }
  66. }
  67. function lastMatchIn(string, regexp) {
  68. var cutOff = 0, match
  69. for (;;) {
  70. regexp.lastIndex = cutOff
  71. var newMatch = regexp.exec(string)
  72. if (!newMatch) return match
  73. match = newMatch
  74. cutOff = match.index + (match[0].length || 1)
  75. if (cutOff == string.length) return match
  76. }
  77. }
  78. function searchRegexpBackward(doc, regexp, start) {
  79. regexp = ensureFlags(regexp, "g")
  80. for (var line = start.line, ch = start.ch, first = doc.firstLine(); line >= first; line--, ch = -1) {
  81. var string = doc.getLine(line)
  82. if (ch > -1) string = string.slice(0, ch)
  83. var match = lastMatchIn(string, regexp)
  84. if (match)
  85. return {from: Pos(line, match.index),
  86. to: Pos(line, match.index + match[0].length),
  87. match: match}
  88. }
  89. }
  90. function searchRegexpBackwardMultiline(doc, regexp, start) {
  91. regexp = ensureFlags(regexp, "gm")
  92. var string, chunk = 1
  93. for (var line = start.line, first = doc.firstLine(); line >= first;) {
  94. for (var i = 0; i < chunk; i++) {
  95. var curLine = doc.getLine(line--)
  96. string = string == null ? curLine.slice(0, start.ch) : curLine + "\n" + string
  97. }
  98. chunk *= 2
  99. var match = lastMatchIn(string, regexp)
  100. if (match) {
  101. var before = string.slice(0, match.index).split("\n"), inside = match[0].split("\n")
  102. var startLine = line + before.length, startCh = before[before.length - 1].length
  103. return {from: Pos(startLine, startCh),
  104. to: Pos(startLine + inside.length - 1,
  105. inside.length == 1 ? startCh + inside[0].length : inside[inside.length - 1].length),
  106. match: match}
  107. }
  108. }
  109. }
  110. var doFold, noFold
  111. if (String.prototype.normalize) {
  112. doFold = function(str) { return str.normalize("NFD").toLowerCase() }
  113. noFold = function(str) { return str.normalize("NFD") }
  114. } else {
  115. doFold = function(str) { return str.toLowerCase() }
  116. noFold = function(str) { return str }
  117. }
  118. // Maps a position in a case-folded line back to a position in the original line
  119. // (compensating for codepoints increasing in number during folding)
  120. function adjustPos(orig, folded, pos, foldFunc) {
  121. if (orig.length == folded.length) return pos
  122. for (var min = 0, max = pos + Math.max(0, orig.length - folded.length);;) {
  123. if (min == max) return min
  124. var mid = (min + max) >> 1
  125. var len = foldFunc(orig.slice(0, mid)).length
  126. if (len == pos) return mid
  127. else if (len > pos) max = mid
  128. else min = mid + 1
  129. }
  130. }
  131. function searchStringForward(doc, query, start, caseFold) {
  132. // Empty string would match anything and never progress, so we
  133. // define it to match nothing instead.
  134. if (!query.length) return null
  135. var fold = caseFold ? doFold : noFold
  136. var lines = fold(query).split(/\r|\n\r?/)
  137. search: for (var line = start.line, ch = start.ch, last = doc.lastLine() + 1 - lines.length; line <= last; line++, ch = 0) {
  138. var orig = doc.getLine(line).slice(ch), string = fold(orig)
  139. if (lines.length == 1) {
  140. var found = string.indexOf(lines[0])
  141. if (found == -1) continue search
  142. var start = adjustPos(orig, string, found, fold) + ch
  143. return {from: Pos(line, adjustPos(orig, string, found, fold) + ch),
  144. to: Pos(line, adjustPos(orig, string, found + lines[0].length, fold) + ch)}
  145. } else {
  146. var cutFrom = string.length - lines[0].length
  147. if (string.slice(cutFrom) != lines[0]) continue search
  148. for (var i = 1; i < lines.length - 1; i++)
  149. if (fold(doc.getLine(line + i)) != lines[i]) continue search
  150. var end = doc.getLine(line + lines.length - 1), endString = fold(end), lastLine = lines[lines.length - 1]
  151. if (endString.slice(0, lastLine.length) != lastLine) continue search
  152. return {from: Pos(line, adjustPos(orig, string, cutFrom, fold) + ch),
  153. to: Pos(line + lines.length - 1, adjustPos(end, endString, lastLine.length, fold))}
  154. }
  155. }
  156. }
  157. function searchStringBackward(doc, query, start, caseFold) {
  158. if (!query.length) return null
  159. var fold = caseFold ? doFold : noFold
  160. var lines = fold(query).split(/\r|\n\r?/)
  161. search: for (var line = start.line, ch = start.ch, first = doc.firstLine() - 1 + lines.length; line >= first; line--, ch = -1) {
  162. var orig = doc.getLine(line)
  163. if (ch > -1) orig = orig.slice(0, ch)
  164. var string = fold(orig)
  165. if (lines.length == 1) {
  166. var found = string.lastIndexOf(lines[0])
  167. if (found == -1) continue search
  168. return {from: Pos(line, adjustPos(orig, string, found, fold)),
  169. to: Pos(line, adjustPos(orig, string, found + lines[0].length, fold))}
  170. } else {
  171. var lastLine = lines[lines.length - 1]
  172. if (string.slice(0, lastLine.length) != lastLine) continue search
  173. for (var i = 1, start = line - lines.length + 1; i < lines.length - 1; i++)
  174. if (fold(doc.getLine(start + i)) != lines[i]) continue search
  175. var top = doc.getLine(line + 1 - lines.length), topString = fold(top)
  176. if (topString.slice(topString.length - lines[0].length) != lines[0]) continue search
  177. return {from: Pos(line + 1 - lines.length, adjustPos(top, topString, top.length - lines[0].length, fold)),
  178. to: Pos(line, adjustPos(orig, string, lastLine.length, fold))}
  179. }
  180. }
  181. }
  182. function SearchCursor(doc, query, pos, options) {
  183. this.atOccurrence = false
  184. this.doc = doc
  185. pos = pos ? doc.clipPos(pos) : Pos(0, 0)
  186. this.pos = {from: pos, to: pos}
  187. var caseFold
  188. if (typeof options == "object") {
  189. caseFold = options.caseFold
  190. } else { // Backwards compat for when caseFold was the 4th argument
  191. caseFold = options
  192. options = null
  193. }
  194. if (typeof query == "string") {
  195. if (caseFold == null) caseFold = false
  196. this.matches = function(reverse, pos) {
  197. return (reverse ? searchStringBackward : searchStringForward)(doc, query, pos, caseFold)
  198. }
  199. } else {
  200. query = ensureFlags(query, "gm")
  201. if (!options || options.multiline !== false)
  202. this.matches = function(reverse, pos) {
  203. return (reverse ? searchRegexpBackwardMultiline : searchRegexpForwardMultiline)(doc, query, pos)
  204. }
  205. else
  206. this.matches = function(reverse, pos) {
  207. return (reverse ? searchRegexpBackward : searchRegexpForward)(doc, query, pos)
  208. }
  209. }
  210. }
  211. SearchCursor.prototype = {
  212. findNext: function() {return this.find(false)},
  213. findPrevious: function() {return this.find(true)},
  214. find: function(reverse) {
  215. var result = this.matches(reverse, this.doc.clipPos(reverse ? this.pos.from : this.pos.to))
  216. // Implements weird auto-growing behavior on null-matches for
  217. // backwards-compatiblity with the vim code (unfortunately)
  218. while (result && CodeMirror.cmpPos(result.from, result.to) == 0) {
  219. if (reverse) {
  220. if (result.from.ch) result.from = Pos(result.from.line, result.from.ch - 1)
  221. else if (result.from.line == this.doc.firstLine()) result = null
  222. else result = this.matches(reverse, this.doc.clipPos(Pos(result.from.line - 1)))
  223. } else {
  224. if (result.to.ch < this.doc.getLine(result.to.line).length) result.to = Pos(result.to.line, result.to.ch + 1)
  225. else if (result.to.line == this.doc.lastLine()) result = null
  226. else result = this.matches(reverse, Pos(result.to.line + 1, 0))
  227. }
  228. }
  229. if (result) {
  230. this.pos = result
  231. this.atOccurrence = true
  232. return this.pos.match || true
  233. } else {
  234. var end = Pos(reverse ? this.doc.firstLine() : this.doc.lastLine() + 1, 0)
  235. this.pos = {from: end, to: end}
  236. return this.atOccurrence = false
  237. }
  238. },
  239. from: function() {if (this.atOccurrence) return this.pos.from},
  240. to: function() {if (this.atOccurrence) return this.pos.to},
  241. replace: function(newText, origin) {
  242. if (!this.atOccurrence) return
  243. var lines = CodeMirror.splitLines(newText)
  244. this.doc.replaceRange(lines, this.pos.from, this.pos.to, origin)
  245. this.pos.to = Pos(this.pos.from.line + lines.length - 1,
  246. lines[lines.length - 1].length + (lines.length == 1 ? this.pos.from.ch : 0))
  247. }
  248. }
  249. CodeMirror.defineExtension("getSearchCursor", function(query, pos, caseFold) {
  250. return new SearchCursor(this.doc, query, pos, caseFold)
  251. })
  252. CodeMirror.defineDocExtension("getSearchCursor", function(query, pos, caseFold) {
  253. return new SearchCursor(this, query, pos, caseFold)
  254. })
  255. CodeMirror.defineExtension("selectMatches", function(query, caseFold) {
  256. var ranges = []
  257. var cur = this.getSearchCursor(query, this.getCursor("from"), caseFold)
  258. while (cur.findNext()) {
  259. if (CodeMirror.cmpPos(cur.to(), this.getCursor("to")) > 0) break
  260. ranges.push({anchor: cur.from(), head: cur.to()})
  261. }
  262. if (ranges.length)
  263. this.setSelections(ranges, 0)
  264. })
  265. });