Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | 1x 1x 1x 1x 1x 1x 1x 11x 16x 9x 7x 2x 2x 20x 29x 20x 20x 20x 20x 20x 20x 29x 29x 22x 17x 17x 7x 20x 20x 24x 20x 20x 20x 20x 20x 24x 20x 20x 29x 29x 22x 22x 22x 7x 4x 3x 11x 11x 11x 11x 11x 11x 11x 11x 11x 7x 7x 7x 7x 7x 7x 7x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 22x 22x 17x 17x 17x 22x 17x 17x 17x 20x 20x 29x 22x 22x 22x 27x 5x 22x 22x 5x 5x 20x 29x 29x 22x 20x 7x 7x 7x 7x 7x 62x 62x | import {warn, poscmp, srcRangeIncludes, changeEnd, logResults} from '../utils'; import {prettyPrintingWidth} from '../ast'; import SHARED from '../shared'; import {commitChanges} from './commitChanges'; import {speculateChanges} from './speculateChanges'; import {FakeAstInsertion, FakeAstReplacement, cloneNode} from './fakeAstEdits'; import {store} from '../store'; // edit_insert : String, ASTNode, String, Pos -> Edit // // Construct an edit to insert `text` in the list `parent.field` at the given `pos`. export function edit_insert(text, parent, field, pos) { return new InsertChildEdit(text, parent, field, pos); } // edit_overwrite : String, Pos, Pos -> Edit // // Construct an edit to replace a range of source code with `text`. The range // must be at the toplevel: it can neither begin nor end inside a root node. export function edit_overwrite(text, from, to) { return new OverwriteEdit(text, from, to); } // edit_delete : ASTNode -> Edit // // Construct an edit to delete the given node. export function edit_delete(node) { if (node.parent) { return new DeleteChildEdit(node, node.parent); } else { return new DeleteRootEdit(node); } } // edit_replace : String, ASTNode -> Edit // // Construct an edit to replace `node` with `text`. export function edit_replace(text, node) { Eif (node.parent) { return new ReplaceChildEdit(text, node, node.parent); } else { return new ReplaceRootEdit(text, node); } } // performEdits : String, AST, Array<Edit>, Callback?, Callback? -> Void // // Attempt to commit a set of changes to Code Mirror. For more details, see the // `commitChanges` function. This function is identical to `commitChanges`, // except that this one takes higher-level `Edit` operations, constructed by the // functions: `edit_insert`, `edit_delete`, and `edit_replace`. Focus is // determined by the focus of the _last_ edit in `edits`. export function performEdits(origin, ast, edits, onSuccess=()=>{}, onError=()=>{}, annt) { // Ensure that all of the edits are valid. //console.log('XXX performEdits:55 doing performEdits'); for (const edit of edits) { Iif (!(edit instanceof Edit)) { throw new Error(`performEdits - invalid edit ${edit}: all edits must be instances of Edit.`); } } // Use the focus hint from the last edit provided. const lastEdit = edits[edits.length - 1]; const focusHint = (newAST) => lastEdit.focusHint(newAST); // Sort the edits from last to first, so that they don't interfere with // each other's source locations or indices. edits.sort((a, b) => poscmp(b.from, a.from)); // Group edits by shared ancestor, so that edits so grouped can be made with a // single textual edit. const editToEditGroup = groupEditsByAncestor(edits); // Convert the edits into CodeMirror-style change objects // (with `from`, `to`, and `text`, but not `removed` or `origin`). let changeArray = new Array(); for (const edit of edits) { let group = editToEditGroup.get(edit); if (group) { // Convert the group into a text edit. if (!group.completed) { changeArray.push(group.toChangeObject()); group.completed = true; } } else { changeArray.push(edit.toChangeObject(ast)); } } console.log(origin, "edits:", edits, "changeArray:", changeArray); // temporary logging /* More detailed logging: console.log(`${origin} - edits:`); for (let edit of edits) { console.log(` ${edit.toString()}`); } console.log(`${origin} - text edits:`); for (let edit of changeArray) { console.log(` ${edit.from.line}:${edit.from.ch}-${edit.to.line}:${edit.to.ch}="${edit.text}"`); } */ // Set the origins for (const c of changeArray) { c.origin = origin; } // Validate the text edits. let result = speculateChanges(changeArray); Eif (result.successful) { try { // Perform the text edits, and update the ast. SHARED.cm.operation(() => { for (let c of changeArray) { SHARED.cm.replaceRange(c.text, c.from, c.to, c.origin); } }); //console.log('XXX performEdits:110 calling commitChanges'); let {newAST, focusId} = commitChanges(changeArray, false, focusHint, result.newAST, annt); onSuccess({newAST, focusId}); } catch(e) { logResults(window.reducerActivities, e); } } else { onError(result.exception); } } class Edit { constructor(from, to) { this.from = from; this.to = to; } findDescendantNode(ancestor, id) { for (const node of ancestor.descendants()) { Eif (node.id === id) { return node; } } warn('performEdits', `Could not find descendant ${id} of ${ancestor.type} ${ancestor.id}`); } focusHint(newAST) { if (this.node.prev) { return newAST.getNodeById(this.node.prev.id) || "fallback"; } else { return newAST.getFirstRootNode(); } } toString() { return `${this.from.line}:${this.from.ch}-${this.to.line}:${this.to.ch}`; } } class OverwriteEdit extends Edit { constructor(text, from, to) { super(from, to); this.text = text; } isTextEdit() { return true; } toChangeObject(ast) { let text = addWhitespace(ast, this.from, this.to, this.text); this.changeObject = { text: text.split("\n"), from: this.from, to: this.to }; return this.changeObject; } focusHint(newAST) { if (this.changeObject) { return newAST.getNodeBeforeCur(changeEnd(this.changeObject)); } else { warn('OverwriteEdit', `Cannot determine focus hint before .toChangeObject(ast) is called.`); return "fallback"; } } toString() { return `Overwrite ${super.toString()}`; } } class InsertChildEdit extends Edit { constructor(text, parent, field, pos) { super(pos, pos); this.text = text; this.parent = getNode(parent); this.pos = pos; this.fakeAstInsertion = new FakeAstInsertion(this.parent, field, pos); } isTextEdit() { return false; } makeAstEdit(clonedAncestor) { let clonedParent = super.findDescendantNode(clonedAncestor, this.parent.id); this.fakeAstInsertion.insertChild(clonedParent, this.text); } focusHint(newAST) { return this.fakeAstInsertion.findChild(newAST) || "fallback"; } toString() { return `InsertChild ${super.toString()}`; } } class DeleteRootEdit extends Edit { constructor(node) { node = getNode(node); let range = node.srcRange(); super(range.from, range.to); this.node = node; } isTextEdit() { return true; } toChangeObject(_ast) { const {from, to} = removeWhitespace(this.from, this.to); return { text: [""], from, to }; } toString() { return `DeleteRoot ${super.toString()}`; } } class DeleteChildEdit extends Edit { constructor(node, parent) { node = getNode(node); parent = getNode(parent); let range = node.srcRange(); super(range.from, range.to); this.node = node; this.parent = parent; this.fakeAstReplacement = new FakeAstReplacement(parent, node); } isTextEdit() { return false; } makeAstEdit(clonedAncestor) { const clonedParent = super.findDescendantNode(clonedAncestor, this.parent.id); this.fakeAstReplacement.deleteChild(clonedParent); } toString() { return `DeleteChild ${super.toString()}`; } } class ReplaceRootEdit extends Edit { constructor(text, node) { node = getNode(node); let range = node.srcRange(); super(range.from, range.to); this.text = text; this.node = node; } isTextEdit() { return true; } toChangeObject(_ast) { return { text: this.text.split("\n"), from: this.from, to: this.to }; } focusHint(newAST) { return newAST.getNodeAfterCur(this.from); } toString() { return `ReplaceRoot ${super.toString()}="${this.text}"`; } } class ReplaceChildEdit extends Edit { constructor(text, node, parent) { node = getNode(node); parent = getNode(parent); let range = node.srcRange(); super(range.from, range.to); this.text = text; this.node = node; this.parent = parent; this.fakeAstReplacement = new FakeAstReplacement(parent, node); } isTextEdit() { return false; } makeAstEdit(clonedAncestor) { let clonedParent = super.findDescendantNode(clonedAncestor, this.parent.id); this.fakeAstReplacement.replaceChild(clonedParent, this.text); } focusHint(newAST) { return this.fakeAstReplacement.findChild(newAST) || "fallback"; } toString() { return `ReplaceChild ${super.toString()}="${this.text}"`; } } class EditGroup { constructor(ancestor, edits) { this.ancestor = getNode(ancestor); this.edits = edits; } toChangeObject() { // Perform the edits on a copy of the shared ancestor node. let range = this.ancestor.srcRange(); let clonedAncestor = cloneNode(this.ancestor); for (const edit of this.edits) { edit.makeAstEdit(clonedAncestor); } // Pretty-print to determine the new text. let width = prettyPrintingWidth - range.from.ch; let newText = clonedAncestor.pretty().display(width); return { from: range.from, to: range.to, text: newText, }; } } // Group edits by shared ancestor, so that edits so grouped can be made with a // single text replacement. Returns a Map from Edit to EditGroup. function groupEditsByAncestor(edits) { let editToEditGroup = new Map(); // {Edit: EditGroup} for (const edit of edits) { if (!edit.isTextEdit()) { // Start with the default assumption that this parent is independent. let group = new EditGroup(edit.parent, []); editToEditGroup.set(edit, group); // Check if any existing ancestors are below the parent. for (const [e, g] of editToEditGroup) { if (e !== edit && srcRangeIncludes(edit.parent.srcRange(), g.ancestor.srcRange())) { editToEditGroup.set(e, group); } } // Check if the parent is below an existing ancestor. for (const [e, g] of editToEditGroup) { if (e !== edit && srcRangeIncludes(g.ancestor.srcRange(), edit.parent.srcRange())) { editToEditGroup.set(edit, g); break; // Ancestors are disjoint; can only be contained in one. } } } } // Fill out the edit list of each edit group. for (const edit of edits) { let group = editToEditGroup.get(edit); if (group) { group.edits.push(edit); } } return editToEditGroup; } // If deleting a block would leave behind excessive whitespace, delete some of // that whitespace. function removeWhitespace(from, to) { let prevChar = SHARED.cm.getRange({line: from.line, ch: from.ch - 1}, from); let nextChar = SHARED.cm.getRange(to, {line: to.line, ch: to.ch + 1}); Iif (prevChar == " " && (nextChar == " " || nextChar == "")) { // Delete an excess space. return {from: {line: from.line, ch: from.ch - 1}, to: to}; } else Iif (nextChar == " " && prevChar == "") { // Delete an excess space. return {from: from, to: {line: to.line, ch: to.ch + 1}}; } else { return {from: from, to: to}; } } // Pad `text` with spaces as needed, when a block will be inserted. export function addWhitespace(ast, from, to, text) { // We may need to insert a newline to make sure that comments don't end up // getting associated with the wrong node, and we may need to insert a space // to ensure that different tokens don't end up getting glommed together. let prevChar = SHARED.cm.getRange({line: from.line, ch: from.ch - 1}, from); let nextChar = SHARED.cm.getRange(to, {line: to.line, ch: to.ch + 1}); if (ast.followsComment(from) && prevChar != "") { text = "\n" + text; } else if (!(prevChar == "" || prevChar == " ")) { text = " " + text; } if (ast.precedesComment(to) && nextChar != "") { text = text + "\n"; } else if (!(nextChar == "" || nextChar == " ")) { text = text + " "; } return text; } function getNode(node) { let {ast} = store.getState(); return ast.getNodeById(node.id); } |