!7 cmodule TextAssist > DynPrintLogAndEnabled { int moduleID = 576; // editor ID, XXX S lastText; new L lastEdits; srecord Edit(int lineNr, TextEdit edit) {} start { doEvery(1.0, r check); } 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(text), lineNr+2); print("Line to edit: " + lineToEdit); if (lineToEdit == null) ret; Edit edit = new Edit(lineNr+2, edit1); print("Proposed edit: " + edit); print("Would yield: " + replayTextEdit(edit, lineToEdit)); } }