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

61
LINES

< > BotCompany Repo | #1006438 // Auto-Saving Text Editor [WORKS]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 3874K of libraries. Click here for Pure Java version (10285L/70K/240K).

1  
!7
2  
3  
!include #1006440 // Concepts
4  
5  
sclass EditRecorder {
6  
  Session session;
7  
  
8  
  // editor state
9  
  S text = "";
10  
  int caret;
11  
  
12  
  bool verbose = true;
13  
  
14  
  // update the text
15  
  void update(S newText) {
16  
    S a = text, b = newText;
17  
    int i = lCommonPrefix(a, b);
18  
    a = substring(a, i);
19  
    b = substring(b, i);
20  
    int j = lCommonSuffix(a, b);
21  
    a = substring(a, 0, l(a)-j);
22  
    b = substring(b, 0, l(b)-j);
23  
    
24  
    // Now differing part remains in a and b;
25  
    // i and j give length of unchanged beginning+end
26  
    
27  
    if (empty(a) && empty(b)) {
28  
      if (verbose) print("no change");
29  
      ret;
30  
    }
31  
    
32  
    EChange change = cnew(EChange, +session,
33  
      startIndex := i, endIndex := i+l(a), text := b);
34  
    if (verbose) print("change: " + renderConcept(change));
35  
    
36  
    // Verify the change we made
37  
    assertEquals(newText, replayEdit(change, text));
38  
    text = newText;
39  
  }
40  
  
41  
  void updateCaret(int pos) {
42  
    if (pos != caret) {
43  
      cnew(ECaret, +session, +pos);
44  
      caret = pos;
45  
    }
46  
  }
47  
}
48  
49  
static Session session;
50  
static JTextArea ta;
51  
static EditRecorder er;
52  
53  
p-substance {
54  
  db(); // persistence
55  
  session = new Session;
56  
  print("Session: " + session.id);
57  
  ta = showText("");
58  
  er = nu(EditRecorder, +session);
59  
  onUpdate(ta, r { er.update(ta.getText()) });
60  
  onCaret(ta, r { er.updateCaret(ta.getCaretPosition()) });
61  
}

Author comment

Began life as a copy of #1006437

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1006438
Snippet name: Auto-Saving Text Editor [WORKS]
Eternal ID of this version: #1006438/5
Text MD5: 4346e4b8f10138e2dc223613d7c4cf2a
Transpilation MD5: e1dfdacefeca504bc1f33330f07c2076
Author: stefan
Category: javax / tools
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-03-25 16:30:50
Source code size: 1407 bytes / 61 lines
Pitched / IR pitched: No / No
Views / Downloads: 590 / 1021
Version history: 4 change(s)
Referenced in: [show references]