本站源代码
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.

261 lines
11KB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. // Define search commands. Depends on dialog.js or another
  4. // implementation of the openDialog method.
  5. // Replace works a little oddly -- it will do the replace on the next
  6. // Ctrl-G (or whatever is bound to findNext) press. You prevent a
  7. // replace by making sure the match is no longer selected when hitting
  8. // Ctrl-G.
  9. (function(mod) {
  10. if (typeof exports == "object" && typeof module == "object") // CommonJS
  11. mod(require("../../lib/codemirror"), require("./searchcursor"), require("../dialog/dialog"));
  12. else if (typeof define == "function" && define.amd) // AMD
  13. define(["../../lib/codemirror", "./searchcursor", "../dialog/dialog"], mod);
  14. else // Plain browser env
  15. mod(CodeMirror);
  16. })(function(CodeMirror) {
  17. "use strict";
  18. function searchOverlay(query, caseInsensitive) {
  19. if (typeof query == "string")
  20. query = new RegExp(query.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"), caseInsensitive ? "gi" : "g");
  21. else if (!query.global)
  22. query = new RegExp(query.source, query.ignoreCase ? "gi" : "g");
  23. return {token: function(stream) {
  24. query.lastIndex = stream.pos;
  25. var match = query.exec(stream.string);
  26. if (match && match.index == stream.pos) {
  27. stream.pos += match[0].length || 1;
  28. return "searching";
  29. } else if (match) {
  30. stream.pos = match.index;
  31. } else {
  32. stream.skipToEnd();
  33. }
  34. }};
  35. }
  36. function SearchState() {
  37. this.posFrom = this.posTo = this.lastQuery = this.query = null;
  38. this.overlay = null;
  39. }
  40. function getSearchState(cm) {
  41. return cm.state.search || (cm.state.search = new SearchState());
  42. }
  43. function queryCaseInsensitive(query) {
  44. return typeof query == "string" && query == query.toLowerCase();
  45. }
  46. function getSearchCursor(cm, query, pos) {
  47. // Heuristic: if the query string is all lowercase, do a case insensitive search.
  48. return cm.getSearchCursor(query, pos, {caseFold: queryCaseInsensitive(query), multiline: true});
  49. }
  50. function persistentDialog(cm, text, deflt, onEnter, onKeyDown) {
  51. cm.openDialog(text, onEnter, {
  52. value: deflt,
  53. selectValueOnOpen: true,
  54. closeOnEnter: false,
  55. onClose: function() { clearSearch(cm); },
  56. onKeyDown: onKeyDown
  57. });
  58. }
  59. function dialog(cm, text, shortText, deflt, f) {
  60. if (cm.openDialog) cm.openDialog(text, f, {value: deflt, selectValueOnOpen: true});
  61. else f(prompt(shortText, deflt));
  62. }
  63. function confirmDialog(cm, text, shortText, fs) {
  64. if (cm.openConfirm) cm.openConfirm(text, fs);
  65. else if (confirm(shortText)) fs[0]();
  66. }
  67. function parseString(string) {
  68. return string.replace(/\\([nrt\\])/g, function(match, ch) {
  69. if (ch == "n") return "\n"
  70. if (ch == "r") return "\r"
  71. if (ch == "t") return "\t"
  72. if (ch == "\\") return "\\"
  73. return match
  74. })
  75. }
  76. function parseQuery(query) {
  77. var isRE = query.match(/^\/(.*)\/([a-z]*)$/);
  78. if (isRE) {
  79. try { query = new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i"); }
  80. catch(e) {} // Not a regular expression after all, do a string search
  81. } else {
  82. query = parseString(query)
  83. }
  84. if (typeof query == "string" ? query == "" : query.test(""))
  85. query = /x^/;
  86. return query;
  87. }
  88. function startSearch(cm, state, query) {
  89. state.queryText = query;
  90. state.query = parseQuery(query);
  91. cm.removeOverlay(state.overlay, queryCaseInsensitive(state.query));
  92. state.overlay = searchOverlay(state.query, queryCaseInsensitive(state.query));
  93. cm.addOverlay(state.overlay);
  94. if (cm.showMatchesOnScrollbar) {
  95. if (state.annotate) { state.annotate.clear(); state.annotate = null; }
  96. state.annotate = cm.showMatchesOnScrollbar(state.query, queryCaseInsensitive(state.query));
  97. }
  98. }
  99. function doSearch(cm, rev, persistent, immediate) {
  100. var state = getSearchState(cm);
  101. if (state.query) return findNext(cm, rev);
  102. var q = cm.getSelection() || state.lastQuery;
  103. if (q instanceof RegExp && q.source == "x^") q = null
  104. if (persistent && cm.openDialog) {
  105. var hiding = null
  106. var searchNext = function(query, event) {
  107. CodeMirror.e_stop(event);
  108. if (!query) return;
  109. if (query != state.queryText) {
  110. startSearch(cm, state, query);
  111. state.posFrom = state.posTo = cm.getCursor();
  112. }
  113. if (hiding) hiding.style.opacity = 1
  114. findNext(cm, event.shiftKey, function(_, to) {
  115. var dialog
  116. if (to.line < 3 && document.querySelector &&
  117. (dialog = cm.display.wrapper.querySelector(".CodeMirror-dialog")) &&
  118. dialog.getBoundingClientRect().bottom - 4 > cm.cursorCoords(to, "window").top)
  119. (hiding = dialog).style.opacity = .4
  120. })
  121. };
  122. persistentDialog(cm, getQueryDialog(cm), q, searchNext, function(event, query) {
  123. var keyName = CodeMirror.keyName(event)
  124. var extra = cm.getOption('extraKeys'), cmd = (extra && extra[keyName]) || CodeMirror.keyMap[cm.getOption("keyMap")][keyName]
  125. if (cmd == "findNext" || cmd == "findPrev" ||
  126. cmd == "findPersistentNext" || cmd == "findPersistentPrev") {
  127. CodeMirror.e_stop(event);
  128. startSearch(cm, getSearchState(cm), query);
  129. cm.execCommand(cmd);
  130. } else if (cmd == "find" || cmd == "findPersistent") {
  131. CodeMirror.e_stop(event);
  132. searchNext(query, event);
  133. }
  134. });
  135. if (immediate && q) {
  136. startSearch(cm, state, q);
  137. findNext(cm, rev);
  138. }
  139. } else {
  140. dialog(cm, getQueryDialog(cm), "Search for:", q, function(query) {
  141. if (query && !state.query) cm.operation(function() {
  142. startSearch(cm, state, query);
  143. state.posFrom = state.posTo = cm.getCursor();
  144. findNext(cm, rev);
  145. });
  146. });
  147. }
  148. }
  149. function findNext(cm, rev, callback) {cm.operation(function() {
  150. var state = getSearchState(cm);
  151. var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
  152. if (!cursor.find(rev)) {
  153. cursor = getSearchCursor(cm, state.query, rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0));
  154. if (!cursor.find(rev)) return;
  155. }
  156. cm.setSelection(cursor.from(), cursor.to());
  157. cm.scrollIntoView({from: cursor.from(), to: cursor.to()}, 20);
  158. state.posFrom = cursor.from(); state.posTo = cursor.to();
  159. if (callback) callback(cursor.from(), cursor.to())
  160. });}
  161. function clearSearch(cm) {cm.operation(function() {
  162. var state = getSearchState(cm);
  163. state.lastQuery = state.query;
  164. if (!state.query) return;
  165. state.query = state.queryText = null;
  166. cm.removeOverlay(state.overlay);
  167. if (state.annotate) { state.annotate.clear(); state.annotate = null; }
  168. });}
  169. function getQueryDialog(cm) {
  170. return '<span class="CodeMirror-search-label">' + cm.phrase("Search:") + '</span> <input type="text" style="width: 10em" class="CodeMirror-search-field"/> <span style="color: #888" class="CodeMirror-search-hint">' + cm.phrase("(Use /re/ syntax for regexp search)") + '</span>';
  171. }
  172. function getReplaceQueryDialog(cm) {
  173. return ' <input type="text" style="width: 10em" class="CodeMirror-search-field"/> <span style="color: #888" class="CodeMirror-search-hint">' + cm.phrase("(Use /re/ syntax for regexp search)") + '</span>';
  174. }
  175. function getReplacementQueryDialog(cm) {
  176. return '<span class="CodeMirror-search-label">' + cm.phrase("With:") + '</span> <input type="text" style="width: 10em" class="CodeMirror-search-field"/>';
  177. }
  178. function getDoReplaceConfirm(cm) {
  179. return '<span class="CodeMirror-search-label">' + cm.phrase("Replace?") + '</span> <button>' + cm.phrase("Yes") + '</button> <button>' + cm.phrase("No") + '</button> <button>' + cm.phrase("All") + '</button> <button>' + cm.phrase("Stop") + '</button> ';
  180. }
  181. function replaceAll(cm, query, text) {
  182. cm.operation(function() {
  183. for (var cursor = getSearchCursor(cm, query); cursor.findNext();) {
  184. if (typeof query != "string") {
  185. var match = cm.getRange(cursor.from(), cursor.to()).match(query);
  186. cursor.replace(text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
  187. } else cursor.replace(text);
  188. }
  189. });
  190. }
  191. function replace(cm, all) {
  192. if (cm.getOption("readOnly")) return;
  193. var query = cm.getSelection() || getSearchState(cm).lastQuery;
  194. var dialogText = '<span class="CodeMirror-search-label">' + (all ? cm.phrase("Replace all:") : cm.phrase("Replace:")) + '</span>';
  195. dialog(cm, dialogText + getReplaceQueryDialog(cm), dialogText, query, function(query) {
  196. if (!query) return;
  197. query = parseQuery(query);
  198. dialog(cm, getReplacementQueryDialog(cm), cm.phrase("Replace with:"), "", function(text) {
  199. text = parseString(text)
  200. if (all) {
  201. replaceAll(cm, query, text)
  202. } else {
  203. clearSearch(cm);
  204. var cursor = getSearchCursor(cm, query, cm.getCursor("from"));
  205. var advance = function() {
  206. var start = cursor.from(), match;
  207. if (!(match = cursor.findNext())) {
  208. cursor = getSearchCursor(cm, query);
  209. if (!(match = cursor.findNext()) ||
  210. (start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return;
  211. }
  212. cm.setSelection(cursor.from(), cursor.to());
  213. cm.scrollIntoView({from: cursor.from(), to: cursor.to()});
  214. confirmDialog(cm, getDoReplaceConfirm(cm), cm.phrase("Replace?"),
  215. [function() {doReplace(match);}, advance,
  216. function() {replaceAll(cm, query, text)}]);
  217. };
  218. var doReplace = function(match) {
  219. cursor.replace(typeof query == "string" ? text :
  220. text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
  221. advance();
  222. };
  223. advance();
  224. }
  225. });
  226. });
  227. }
  228. CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);};
  229. CodeMirror.commands.findPersistent = function(cm) {clearSearch(cm); doSearch(cm, false, true);};
  230. CodeMirror.commands.findPersistentNext = function(cm) {doSearch(cm, false, true, true);};
  231. CodeMirror.commands.findPersistentPrev = function(cm) {doSearch(cm, true, true, true);};
  232. CodeMirror.commands.findNext = doSearch;
  233. CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);};
  234. CodeMirror.commands.clearSearch = clearSearch;
  235. CodeMirror.commands.replace = replace;
  236. CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);};
  237. });
上海开阖软件有限公司 沪ICP备12045867号-1