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

192
LINES

< > BotCompany Repo | #1015560 // Snippet/File Editor v3 [OK]

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

Download Jar. Uses 6678K of libraries. Click here for Pure Java version (11874L/87K).

1  
!7
2  
3  
// What are we editing?
4  
static O editing; // File or snippet ID
5  
sS remoteText;
6  
7  
sS savedText;
8  
static EditorFrame e;
9  
static int caretPos;
10  
static SimpleLiveValue<O> lvEditing = new(O.class);
11  
static int fontSize = 16;
12  
static AutoCompletion autoCompletion;
13  
static ReliableSingleThread rstAutoComplete = new(f installAutoComplete);
14  
sbool installAutoComplete_first;
15  
static Set<S> autoComplete_lastSet;
16  
static DynamicHStack smartArea;
17  
18  
p-substance {
19  
  autoRestart();
20  
  load('savedText);
21  
  load('fontSize);
22  
  
23  
  e = new EditorFrame;
24  
  e._setFontSize(fontSize);
25  
  rstAutoComplete.trigger();
26  
  onChange(e.textArea, rTrigger(rstAutoComplete));
27  
  
28  
  addToWindowWithMargin(e, jHigherScrollPane(jfullcenter(smartArea = dynamicHStack())));
29  
  
30  
  e.setText(savedText);
31  
  load('caretPos);
32  
  setCaretPosition(e.textArea, caretPos);
33  
  showFrame(e);
34  
  frameIcon(e, #1101185 /*#1004655*/);
35  
  
36  
  addMenu(e, "New", "New text", f newText);
37  
38  
  addMenu(e, "Load", "Open snippet...", r {
39  
    selectSnippetID(vf1(f _loadSnippet));
40  
  }, "Open file...", r {
41  
    selectFile("File to open", vf1(f _loadFile));
42  
  });
43  
  
44  
  addMenu(e, "Save", mapLiveValue(func(O editing) -> S {
45  
    editing instanceof S ? "Save snippet " + editing
46  
      : editing instanceof File ? "Save file " + f2s((File) editing)
47  
      : "Save" }, S.class, lvEditing), r { saveThen(null) });
48  
  
49  
  addMenu(e, "Transpile",
50  
    "Save & Quick Transpile", r { transpile(false) },
51  
    "Save & Medium Transpile", r { transpile(true) });
52  
  addMenu(e, "Activate", "Butter-Reload", f _butterReload);
53  
  
54  
  addMenu(e, "View",
55  
    "Bigger font", r { e._setFontSize(setAndSave(fontSize := e._getFontSize()+1)) },
56  
    "Smaller font", r { e._setFontSize(setAndSave(fontSize := max(1, e._getFontSize()-1))) });
57  
  
58  
  O editing = cast readPersistentVar('editing);
59  
  if (editing instanceof S) _loadSnippet_noSet((S) editing);
60  
  else if (editing instanceof File) _loadFile_noSet((File) editing);
61  
62  
  hideConsole();
63  
  
64  
  awtCalcEvery(e, 1000, f update);
65  
  onFrameDeactivated(e, f fullUpdate);
66  
}
67  
68  
svoid update { update(windowActive(e)); }
69  
svoid fullUpdate { update(true); }
70  
svoid update(bool full) {
71  
  S text = e.getText();
72  
  setAndSaveIfChanged(savedText := text);
73  
  setAndSaveIfChanged(caretPos := getCaretPosition(e.textArea));
74  
    
75  
  if (!full) ret;
76  
  S status = remoteText != null && neq(remoteText, text) ? "Changes. " : "";
77  
  Pair<Int, S> p = testBracketHygieneExt(text);
78  
  status += p == null ? "Hygienic" : p.b;
79  
  silentStatus(trim(status));
80  
}
81  
82  
svoid status(S s) { silentStatus(infoBoxAndReturn(s)); }
83  
svoid silentStatus(S s) { e.setStatus(s); }
84  
85  
svoid saveThen(final Runnable next) {
86  
  fS text = getText(e.textArea);
87  
  if (editing instanceof S) {
88  
    status("Saving snippet...");
89  
    thread {
90  
      fS page = editSnippet((S) editing, text);
91  
      remoteText = text; // TODO: check save result
92  
      status("Saved snippet: " + page);
93  
      awtCallF(next);
94  
    }
95  
  } else if (editing instanceof File) {
96  
    status("Saving file...");
97  
    thread {
98  
      saveTextFile((File) editing, text);
99  
      remoteText = text;
100  
      status("Saved file");
101  
      awtCallF(next);
102  
    }
103  
  }
104  
}
105  
106  
svoid _butterReload() {
107  
  time { butterReload((S) editing); }
108  
  infoBox("Reloaded in " + lastTiming_format100ms());
109  
}
110  
111  
svoid transpile(final bool medium) { saveThen(r { _transpile(medium) }); }
112  
113  
svoid _transpile(final bool medium) {
114  
  status("Transpiling...");
115  
  //jdisable(btnEleuReload);
116  
  
117  
  thread "Transpiling" {
118  
    try {
119  
      final Pair<Bool, S> p = transpileOnServer((S) editing, medium ? "medium" : "quick");
120  
      awt {
121  
        if (p.a)
122  
          status("Transpiled OK!");
123  
        else {
124  
          status("Transpilation error. " + p.b);
125  
          showText("Transpilation Error", p.b);
126  
        }
127  
      }
128  
    } catch print e {
129  
      status("Transpilation problem. " + e);
130  
    }
131  
    //jenable(btnEleuReload);
132  
  }
133  
}
134  
135  
svoid _loadSnippet(S snippetID) {
136  
  _loadSnippet_noSet(snippetID);
137  
  e.textArea.setText(remoteText);
138  
}
139  
140  
svoid _loadSnippet_noSet(S snippetID) {
141  
  remoteText = loadSnippet(snippetID);
142  
  setAndSave('editing, snippetID);
143  
  lvEditing.set(snippetID);
144  
  frameTitle(e, snippetID + " - " + snippetTitle(snippetID));
145  
}
146  
147  
svoid _loadFile(File f) {
148  
  _loadFile_noSet(f);
149  
  e.textArea.setText(remoteText);
150  
}
151  
152  
svoid _loadFile_noSet(File f) {
153  
  remoteText = loadTextFile(f);
154  
  setAndSave('editing, f);
155  
  lvEditing.set(f);
156  
  frameTitle(e, f2s(f));
157  
}
158  
159  
svoid newText {
160  
  setAndSave(editing := null);
161  
  lvEditing.set(null);
162  
  remoteText = null;
163  
  S lastText = e.getText();
164  
  if (nempty(lastText)) logQuotedWithDate("saved-texts.log", lastText);
165  
  e.setText("");
166  
  frameTitle(e, programTitle());
167  
}
168  
169  
svoid cleanMeUp { update(false); }
170  
171  
svoid installAutoComplete {
172  
  if (installAutoComplete_first) installAutoComplete_first = false;
173  
  else sleep(500); // don't update too often
174  
  
175  
  temp tempMiniLoadingAnim();
176  
177  
  Set<S> set = concatListsToSet(allJavaKeywords(), standardFunctionNames(), deepWords(getText(e.textArea)));
178  
    
179  
  if (eq(set, autoComplete_lastSet)) ret;
180  
  autoComplete_lastSet = set;
181  
  
182  
  final new DefaultCompletionProvider provider;
183  
  provider.addCompletions(map(func(S s) { new BasicCompletion(provider, s) }, set));
184  
    
185  
  swing {
186  
    if (autoCompletion == null) {
187  
      (autoCompletion = new AutoCompletion(provider)).install(e.textArea);
188  
      call(autoCompletion, 'setHideOnCompletionProviderChange, false);
189  
    } else
190  
      autoCompletion.setCompletionProvider(provider);
191  
  }
192  
}

Author comment

Began life as a copy of #1015549

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1015560
Snippet name: Snippet/File Editor v3 [OK]
Eternal ID of this version: #1015560/11
Text MD5: cfc5d6c7f7623b279125586273bda73d
Transpilation MD5: 44f09c4fa3f14593b6b323e639dbaa49
Author: stefan
Category: javax / gui
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-17 16:36:56
Source code size: 5591 bytes / 192 lines
Pitched / IR pitched: No / No
Views / Downloads: 481 / 1182
Version history: 10 change(s)
Referenced in: [show references]