!7 cmodule TextAssist > DynPrintLogAndEnabled { int moduleID = 576; // editor ID, XXX S lastText; new L lastEdits; Edit proposedEdit; srecord Edit(int lineNr, TextEdit edit) {} start { doEvery(1.0, r check); } afterVisualize { addToContainer(buttons, jThreadedButton("Apply edit", r executeProposedEdit)); } void check enter { if (!enabled) ret; S text = cast dm_rcall getText(moduleID); pcall { compareTexts(lastText, text); } setField(lastText := text); } void compareTexts(S lastText, S text) { LS linesA = lines(lastText); LS linesB = lines(text); // need same number of lines if (l(linesA) != l(linesB)) ret with print("Line count changed, exiting"); L indices = differingIndices(linesA, linesB); if (empty(indices)) ret; if (l(indices) != 1) ret with print("# changed lines: " + l(indices) + ", exiting"); int i = first(indices); S oldLine = linesA.get(i), newLine = linesB.get(i); // Make a TextEdit to see what is changed in line TextEdit edit = createTextEdit(oldLine, newLine); if (edit == null) ret with print("Line not changed? Weird."); print("Line " + i + ": " + edit); addToListWithMaxSize(lastEdits, new Edit(i, edit), 2); // keep last 2 edits change(); proposeEdit(text); } void proposeEdit(S currentText) { L edits = takeLast(2, lastEdits); if (l(edits) < 2) ret; int lineNr = first(edits).lineNr; if (second(edits).lineNr != lineNr+1) ret; TextEdit edit1 = first(edits).edit, edit2 = second(edits).edit; //if (!eq(edit1.text, edit2.text)) ret with print("Differing text: " + edit1.text + "/" + edit2.text); if (!eq(edit1, edit2)) ret with print("Differing edits: " + edit1 + "/" + edit2); S lineToEdit = get(lines(currentText), lineNr+2); print("Line to edit: " + lineToEdit); if (lineToEdit == null) ret; setField(proposedEdit := new Edit(lineNr+2, edit1)); print("Proposed edit: " + proposedEdit); print("Would yield: " + replayTextEdit(proposedEdit.edit, lineToEdit)); } void executeProposedEdit enter { S text = cast dm_rcall getText(moduleID); LS lines = lines(text); S newLine = replayTextEdit(proposedEdit.edit, lines.get(proposedEdit.lineNr)); print("new line: " + newLine); lines.set(proposedEdit.lineNr, newLine); S newText = lines(lines); print("CHANGING TEXT"); dm_rcall setText(moduleID, newText); } }