Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

81
LINES

< > BotCompany Repo | #1025350 // Text Edit Assist Spike [made in video]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (5528L/28K).

!7

cmodule TextAssist > DynPrintLogAndEnabled {
  int moduleID = 576; // editor ID, XXX
  S lastText;
  new L<Edit> 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<Int> 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<Edit> 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);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1025350
Snippet name: Text Edit Assist Spike [made in video]
Eternal ID of this version: #1025350/19
Text MD5: 25f42026d78e9576797fc2a59e2e4eb9
Transpilation MD5: 8dad33ec3b72edd38fa556c98c2b27d1
Author: stefan
Category: javax
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-10-14 21:26:41
Source code size: 2568 bytes / 81 lines
Pitched / IR pitched: No / No
Views / Downloads: 204 / 537
Version history: 18 change(s)
Referenced in: [show references]