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).

1  
!7
2  
3  
cmodule TextAssist > DynPrintLogAndEnabled {
4  
  int moduleID = 576; // editor ID, XXX
5  
  S lastText;
6  
  new L<Edit> lastEdits;
7  
  Edit proposedEdit;
8  
9  
  srecord Edit(int lineNr, TextEdit edit) {}
10  
  
11  
  start {
12  
    doEvery(1.0, r check);
13  
  }
14  
15  
  afterVisualize {
16  
    addToContainer(buttons, jThreadedButton("Apply edit", r executeProposedEdit));
17  
  }
18  
  
19  
  void check enter {
20  
    if (!enabled) ret;
21  
    S text = cast dm_rcall getText(moduleID);
22  
    pcall {
23  
      compareTexts(lastText, text);
24  
    }
25  
    setField(lastText := text);
26  
  }
27  
28  
  void compareTexts(S lastText, S text) {
29  
    LS linesA = lines(lastText);
30  
    LS linesB = lines(text);
31  
32  
    // need same number of lines
33  
    if (l(linesA) != l(linesB)) ret with print("Line count changed, exiting");
34  
35  
    L<Int> indices = differingIndices(linesA, linesB);
36  
    if (empty(indices)) ret;
37  
    if (l(indices) != 1) ret with print("# changed lines: " + l(indices) + ", exiting");
38  
39  
    int i = first(indices);
40  
    S oldLine = linesA.get(i), newLine = linesB.get(i);
41  
42  
    // Make a TextEdit to see what is changed in line
43  
    TextEdit edit = createTextEdit(oldLine, newLine);
44  
    if (edit == null) ret with print("Line not changed? Weird.");
45  
46  
    print("Line " + i + ": " + edit);
47  
48  
    addToListWithMaxSize(lastEdits, new Edit(i, edit), 2); // keep last 2 edits
49  
    change();
50  
51  
    proposeEdit(text);
52  
  }
53  
54  
  void proposeEdit(S currentText) {
55  
    L<Edit> edits = takeLast(2, lastEdits);
56  
    if (l(edits) < 2) ret;
57  
    int lineNr = first(edits).lineNr;
58  
    if (second(edits).lineNr != lineNr+1) ret;
59  
    TextEdit edit1 = first(edits).edit, edit2 = second(edits).edit;
60  
    //if (!eq(edit1.text, edit2.text)) ret with print("Differing text: " + edit1.text + "/" + edit2.text);
61  
    if (!eq(edit1, edit2)) ret with print("Differing edits: " + edit1 + "/" + edit2);
62  
63  
    S lineToEdit = get(lines(currentText), lineNr+2);
64  
    print("Line to edit: " + lineToEdit);
65  
    if (lineToEdit == null) ret;
66  
    setField(proposedEdit := new Edit(lineNr+2, edit1));
67  
    print("Proposed edit: " + proposedEdit);
68  
    print("Would yield: " + replayTextEdit(proposedEdit.edit, lineToEdit));
69  
  }
70  
71  
  void executeProposedEdit enter {
72  
    S text = cast dm_rcall getText(moduleID);
73  
    LS lines = lines(text);
74  
    S newLine = replayTextEdit(proposedEdit.edit, lines.get(proposedEdit.lineNr));
75  
    print("new line: " + newLine);
76  
    lines.set(proposedEdit.lineNr, newLine);
77  
    S newText = lines(lines);
78  
    print("CHANGING TEXT");
79  
    dm_rcall setText(moduleID, newText);
80  
  }
81  
}

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: 205 / 539
Version history: 18 change(s)
Referenced in: [show references]