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

237
LINES

< > BotCompany Repo | #1036608 // G22LeftArrowScript (backup before subclassing G22Text)

JavaX fragment (include)

1  
concept G22LeftArrowScript > ConceptWithChangeListeners {
2  
  settableWithVar S description;
3  
  
4  
  // the script (last "saved" version)
5  
  settableWithVar S text;
6  
  
7  
  // the latest code that has been cleared to auto-run (if any)
8  
  settableWithVar ClearForAutoRun<S> clearedForAutoRun;
9  
10  
  // text currently being modified in an editor (or null)
11  
  // Note that "" doesn't count as null.
12  
  settableWithVar S editingText;
13  
  
14  
  settableWithVar S importNote;
15  
  
16  
  // auto-run when project is opened
17  
  settableWithVar bool runOnProjectOpen;
18  
  
19  
  // only when runOnProjectOpen is seT:
20  
  // when to run in sequence (sorted with alphanumIC)
21  
  settableWithVar S runOrder;
22  
  
23  
  // run stats
24  
  long runCount;
25  
  
26  
  // info about last result (or error) of script run by mode
27  
  Map<G22ScriptMode, WithTimestamp<PersistableOKOrError<G22TypeDesc>>> lastResultByMode;
28  
  
29  
  // is there a newer version of this script?
30  
  new Ref<G22LeftArrowScript> supercededBy;
31  
  
32  
  // Put meta-functions here (e.g. for improving an IDE showing
33  
  // this script)
34  
  settableWithVar S metaCode;
35  
  
36  
  void _doneLoading2 :: after {
37  
    //print("_doneLoading " + this);
38  
    cMigrateField(this, "code", "text");
39  
  }
40  
  
41  
  S myType() { ret dropPrefix("G22", shortClassName(this)); }
42  
  
43  
  toString {
44  
    if (empty(description)) 
45  
      ret myType() + " " + id;
46  
    else
47  
      ret "Script " + id + ": " + description;
48  
  }
49  
  
50  
  void initEditingText {
51  
    editingText(unnull(or(editingText, text))); 
52  
  }
53  
  
54  
  void receiveEditingText(S text) {
55  
    //printVars_shorten("receiveEditingText", +text);
56  
    editingText(text);
57  
  }
58  
  
59  
  S stableText() { ret text; }
60  
  
61  
  void completeEdit aka save() {
62  
    S t = editingText;
63  
    //printVars_shorten("completeEdit", +t);
64  
    if (t != null) {
65  
      setTextWithHistory(t);
66  
      //printVars_shorten("completeEdit", +editingText);
67  
    }
68  
  }
69  
  
70  
  void setTextWithHistory(S text) {
71  
    if (eq(this.text, text)) ret;
72  
    text(text);
73  
    saveTextToHistory();
74  
  }
75  
  
76  
  void discardEdit {
77  
    S text = editedText();
78  
    if (text == null) ret;
79  
    saveFieldToHistory("discardingEdit", text);
80  
    editingText(null);
81  
  }
82  
  
83  
  S textForEditing() {
84  
    initEditingText();
85  
    ret editingText;
86  
  }
87  
  
88  
  File historyFile() {
89  
    ret fileInConceptsDir("History/" + shortDynName(this) + id + ".history");
90  
  }
91  
  
92  
  void saveTextToHistory() {
93  
    saveFieldToHistory("text", text);
94  
  }
95  
  
96  
  void saveFieldToHistory(S field, S value) {
97  
    File historyFile = historyFile();
98  
    if (historyFile == null) ret;
99  
    S contents = value == null ? " empty" : " (" + nLines(value) + ", " + nChars(value) + "):\n" +
100  
      indentx(value) + "\n" +
101  
      "\n";
102  
    appendToTextFile(historyFile,
103  
      "\n===\n" +
104  
      "Concept ID: " + id + "\n" +
105  
      "Date: " + dateWithMSUTC() + "\n" +
106  
      firstToUpper(field) + contents +
107  
      "===" + "\n");
108  
  }
109  
  
110  
  bool isSaved() { ret text != null; }
111  
  bool isSavedDistinctFromAutoRunVersion() { ret isSaved() && !eq(text, codeForAutoRun()); }
112  
  bool isEditing() { ret editedText() != null; }
113  
  bool isClearForAutoRun() { ret clearedForAutoRun != null; }
114  
  
115  
  S editedText() { ret eq(editingText, text) ? null : editingText; }
116  
  
117  
  S codeForAutoRun() { ret getVar(clearedForAutoRun()); }
118  
  
119  
  // return code clear for auto run if available
120  
  // otherwise returns saved code if available
121  
  // in all other cases returns null
122  
  // (never returns text being edited)
123  
  S safestCode() { ret or(codeForAutoRun(), stableText()); }
124  
  
125  
  void clearForAutoRun {
126  
    if (!isSaved()) ret;
127  
    S text = text();
128  
    saveFieldToHistory("Auto-runnable code", text);
129  
    clearedForAutoRun(new ClearForAutoRun(text));
130  
  }
131  
  
132  
  void forgetAutoRunCode {
133  
    if (!isClearForAutoRun()) ret;
134  
    saveFieldToHistory("Auto-runnable code", null);
135  
    clearedForAutoRun(null);
136  
    compileResultForAutoRun = null;
137  
  }
138  
  
139  
  void forgetCompileResults {
140  
    compileResultForAutoRun = compileResultForSaved = null;
141  
  }
142  
143  
  G22Utils g22utils() {
144  
    ret assertNotNull(g22utils := main g22utils(_concepts()));
145  
  }
146  
  
147  
  GazelleV_LeftArrowScriptParser makeParser() {
148  
    ret g22utils().leftArrowParser()
149  
      .sourceInfo(this);
150  
  }
151  
  
152  
  LASCompileResult newCompileResult() { ret new LASCompileResult; }
153  
  
154  
  LASCompileResult returnOrCompile(LASCompileResult lastCompileResult, S code) {
155  
    if (g22utils().compileResultValid(lastCompileResult, code))
156  
      ret lastCompileResult;
157  
    var cr = newCompileResult();
158  
    cr.script(code);
159  
    var parser = makeParser();
160  
    cr.parser(parser);
161  
    cr.compile();
162  
    ret cr;
163  
  }
164  
  
165  
  transient LASCompileResult compileResultForAutoRun;
166  
  LASCompileResult compileForAutoRun() {
167  
    S code = codeForAutoRun();
168  
    if (code == null) null;
169  
    var cr = returnOrCompile(compileResultForAutoRun, code);
170  
    ret compileResultForAutoRun = cr;
171  
  }
172  
  
173  
  transient LASCompileResult compileResultForSaved;
174  
  LASCompileResult compileSaved() {
175  
    S code = stableText();
176  
    if (code == null) null;
177  
    var cr = returnOrCompile(compileResultForSaved, code);
178  
    ret compileResultForSaved = cr;
179  
  }
180  
  
181  
  transient LASCompileResult compileResultForEditing;
182  
  LASCompileResult compileEditing() {
183  
    S code = editingText();
184  
    if (code == null) null;
185  
    var cr = returnOrCompile(compileResultForEditing, code);
186  
    ret compileResultForEditing = cr;
187  
  }
188  
  
189  
  LASCompileResult safestCompileResult() {
190  
    try object compileForAutoRun();
191  
    ret compileSaved();
192  
  }
193  
  
194  
  LASCompileResult latestCompileResult() {
195  
    try object compileEditing();
196  
    try object compileSaved();
197  
    ret compileForAutoRun();
198  
  }
199  
  
200  
  LASCompileResult latestRunnableCompileResult() {
201  
    var cr = compileEditing();
202  
    if (cr != null && cr.runnable()) ret cr;
203  
    cr = compileSaved();
204  
    if (cr != null && cr.runnable()) ret cr;
205  
    cr = compileForAutoRun();
206  
    if (cr != null && cr.runnable()) ret cr;
207  
    null;
208  
  }
209  
  
210  
  O evaluateWithoutTimeout() {
211  
    var cr = safestCompileResult();
212  
    if (cr == null)
213  
      fail("Not saved: " + this);
214  
      
215  
    temp g22utils().enter(); // enter module
216  
    ret cr.parsedScriptMandatory()!;
217  
  }
218  
  
219  
  O evaluateAutoRunWithoutTimeout() {
220  
    var cr = compileForAutoRun();
221  
    if (cr == null)
222  
      fail("Not cleared for auto-run: " + this);
223  
      
224  
    temp g22utils().enter(); // enter module
225  
    ret cr.parsedScriptMandatory()!;
226  
  }
227  
  
228  
  S renderRunOnProjectOpenStatus() {
229  
    ret runOnProjectOpen
230  
      ? "Yes" + appendBracketed(appendPrefixIfNempty("prio ", runOrder))
231  
      : null;
232  
  }
233  
  
234  
  Set<S> allTexts() {
235  
    ret asSet(llNonNulls(text(), editedText(), codeForAutoRun()));
236  
  }
237  
}

Author comment

Began life as a copy of #1034255

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): elmgxqgtpvxh, mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1036608
Snippet name: G22LeftArrowScript (backup before subclassing G22Text)
Eternal ID of this version: #1036608/1
Text MD5: 143090ff5c0fcc3e1bc040b487f0a737
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-04-27 18:28:35
Source code size: 6731 bytes / 237 lines
Pitched / IR pitched: No / No
Views / Downloads: 158 / 204
Referenced in: [show references]