* @param {CodeMirror} cm CodeMirror instance we are in. * @param {boolean} confirm Whether to confirm each replace. * @param {Cursor} lineStart Line to start replacing from. * @param {Cursor} lineEnd Line to stop replacing at. * @param {RegExp} query Query for performing matches with
(cm, confirm, global, lineStart, lineEnd, searchCursor, query,
replaceWith, callback)
| 13946 | * @param {function()} callback A callback for when the replace is done. |
| 13947 | */ |
| 13948 | function doReplace(cm, confirm, global, lineStart, lineEnd, searchCursor, query, |
| 13949 | replaceWith, callback) { |
| 13950 | // Set up all the functions. |
| 13951 | cm.state.vim.exMode = true; |
| 13952 | var done = false; |
| 13953 | var lastPos = searchCursor.from(); |
| 13954 | function replaceAll() { |
| 13955 | cm.operation(function() { |
| 13956 | while (!done) { |
| 13957 | replace(); |
| 13958 | next(); |
| 13959 | } |
| 13960 | stop(); |
| 13961 | }); |
| 13962 | } |
| 13963 | function replace() { |
| 13964 | var text = cm.getRange(searchCursor.from(), searchCursor.to()); |
| 13965 | var newText = text.replace(query, replaceWith); |
| 13966 | searchCursor.replace(newText); |
| 13967 | } |
| 13968 | function next() { |
| 13969 | // The below only loops to skip over multiple occurrences on the same |
| 13970 | // line when 'global' is not true. |
| 13971 | while(searchCursor.findNext() && |
| 13972 | isInRange(searchCursor.from(), lineStart, lineEnd)) { |
| 13973 | if (!global && lastPos && searchCursor.from().line == lastPos.line) { |
| 13974 | continue; |
| 13975 | } |
| 13976 | cm.scrollIntoView(searchCursor.from(), 30); |
| 13977 | cm.setSelection(searchCursor.from(), searchCursor.to()); |
| 13978 | lastPos = searchCursor.from(); |
| 13979 | done = false; |
| 13980 | return; |
| 13981 | } |
| 13982 | done = true; |
| 13983 | } |
| 13984 | function stop(close) { |
| 13985 | if (close) { close(); } |
| 13986 | cm.focus(); |
| 13987 | if (lastPos) { |
| 13988 | cm.setCursor(lastPos); |
| 13989 | var vim = cm.state.vim; |
| 13990 | vim.exMode = false; |
| 13991 | vim.lastHPos = vim.lastHSPos = lastPos.ch; |
| 13992 | } |
| 13993 | if (callback) { callback(); } |
| 13994 | } |
| 13995 | function onPromptKeyDown(e, _value, close) { |
| 13996 | // Swallow all keys. |
| 13997 | CodeMirror.e_stop(e); |
| 13998 | var keyName = CodeMirror.keyName(e); |
| 13999 | switch (keyName) { |
| 14000 | case 'Y': |
| 14001 | replace(); next(); break; |
| 14002 | case 'N': |
| 14003 | next(); break; |
| 14004 | case 'A': |
| 14005 | // replaceAll contains a call to close of its own. We don't want it |
no test coverage detected