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

!7

!include #1006440 // Concepts

sclass EditRecorder {
  Session session;
  
  // editor state
  S text = "";
  int caret;
  
  bool verbose = true;
  
  // update the text
  void update(S newText) {
    S a = text, b = newText;
    int i = lCommonPrefix(a, b);
    a = substring(a, i);
    b = substring(b, i);
    int j = lCommonSuffix(a, b);
    a = substring(a, 0, l(a)-j);
    b = substring(b, 0, l(b)-j);
    
    // Now differing part remains in a and b;
    // i and j give length of unchanged beginning+end
    
    if (empty(a) && empty(b)) {
      if (verbose) print("no change");
      ret;
    }
    
    EChange change = cnew(EChange, +session,
      startIndex := i, endIndex := i+l(a), text := b);
    if (verbose) print("change: " + renderConcept(change));
    
    // Verify the change we made
    assertEquals(newText, replayEdit(change, text));
    text = newText;
  }
  
  void updateCaret(int pos) {
    if (pos != caret) {
      cnew(ECaret, +session, +pos);
      caret = pos;
    }
  }
}

static Session session;
static JTextArea ta;
static EditRecorder er;

p-substance {
  db(); // persistence
  session = new Session;
  print("Session: " + session.id);
  ta = showText("");
  er = nu(EditRecorder, +session);
  onUpdate(ta, r { er.update(ta.getText()) });
  onCaret(ta, r { er.updateCaret(ta.getCaretPosition()) });
}

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: 586 / 1016
Version history: 4 change(s)
Referenced in: [show references]