MCPcopy Create free account
hub / github.com/OpenEndedGroup/Field2 / doReplace

Function doReplace

lib/web/CodeMirror/keymap/vim.js:5240–5333  ·  view source on GitHub ↗

* @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)

Source from the content-addressed store, hash-verified

5238 * @param {function()} callback A callback for when the replace is done.
5239 */
5240 function doReplace(cm, confirm, global, lineStart, lineEnd, searchCursor, query,
5241 replaceWith, callback) {
5242 // Set up all the functions.
5243 cm.state.vim.exMode = true;
5244 var done = false;
5245 var lastPos = searchCursor.from();
5246 function replaceAll() {
5247 cm.operation(function() {
5248 while (!done) {
5249 replace();
5250 next();
5251 }
5252 stop();
5253 });
5254 }
5255 function replace() {
5256 var text = cm.getRange(searchCursor.from(), searchCursor.to());
5257 var newText = text.replace(query, replaceWith);
5258 searchCursor.replace(newText);
5259 }
5260 function next() {
5261 // The below only loops to skip over multiple occurrences on the same
5262 // line when 'global' is not true.
5263 while(searchCursor.findNext() &&
5264 isInRange(searchCursor.from(), lineStart, lineEnd)) {
5265 if (!global && lastPos && searchCursor.from().line == lastPos.line) {
5266 continue;
5267 }
5268 cm.scrollIntoView(searchCursor.from(), 30);
5269 cm.setSelection(searchCursor.from(), searchCursor.to());
5270 lastPos = searchCursor.from();
5271 done = false;
5272 return;
5273 }
5274 done = true;
5275 }
5276 function stop(close) {
5277 if (close) { close(); }
5278 cm.focus();
5279 if (lastPos) {
5280 cm.setCursor(lastPos);
5281 var vim = cm.state.vim;
5282 vim.exMode = false;
5283 vim.lastHPos = vim.lastHSPos = lastPos.ch;
5284 }
5285 if (callback) { callback(); }
5286 }
5287 function onPromptKeyDown(e, _value, close) {
5288 // Swallow all keys.
5289 CodeMirror.e_stop(e);
5290 var keyName = CodeMirror.keyName(e);
5291 switch (keyName) {
5292 case 'Y':
5293 replace(); next(); break;
5294 case 'N':
5295 next(); break;
5296 case 'A':
5297 // replaceAll contains a call to close of its own. We don't want it

Callers 1

VimFunction · 0.70

Calls 6

showConfirmFunction · 0.85
showPromptFunction · 0.85
fromMethod · 0.80
nextFunction · 0.70
replaceAllFunction · 0.70
callbackFunction · 0.50

Tested by

no test coverage detected