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)

concept G22LeftArrowScript > ConceptWithChangeListeners {
  settableWithVar S description;
  
  // the script (last "saved" version)
  settableWithVar S text;
  
  // the latest code that has been cleared to auto-run (if any)
  settableWithVar ClearForAutoRun<S> clearedForAutoRun;

  // text currently being modified in an editor (or null)
  // Note that "" doesn't count as null.
  settableWithVar S editingText;
  
  settableWithVar S importNote;
  
  // auto-run when project is opened
  settableWithVar bool runOnProjectOpen;
  
  // only when runOnProjectOpen is seT:
  // when to run in sequence (sorted with alphanumIC)
  settableWithVar S runOrder;
  
  // run stats
  long runCount;
  
  // info about last result (or error) of script run by mode
  Map<G22ScriptMode, WithTimestamp<PersistableOKOrError<G22TypeDesc>>> lastResultByMode;
  
  // is there a newer version of this script?
  new Ref<G22LeftArrowScript> supercededBy;
  
  // Put meta-functions here (e.g. for improving an IDE showing
  // this script)
  settableWithVar S metaCode;
  
  void _doneLoading2 :: after {
    //print("_doneLoading " + this);
    cMigrateField(this, "code", "text");
  }
  
  S myType() { ret dropPrefix("G22", shortClassName(this)); }
  
  toString {
    if (empty(description)) 
      ret myType() + " " + id;
    else
      ret "Script " + id + ": " + description;
  }
  
  void initEditingText {
    editingText(unnull(or(editingText, text))); 
  }
  
  void receiveEditingText(S text) {
    //printVars_shorten("receiveEditingText", +text);
    editingText(text);
  }
  
  S stableText() { ret text; }
  
  void completeEdit aka save() {
    S t = editingText;
    //printVars_shorten("completeEdit", +t);
    if (t != null) {
      setTextWithHistory(t);
      //printVars_shorten("completeEdit", +editingText);
    }
  }
  
  void setTextWithHistory(S text) {
    if (eq(this.text, text)) ret;
    text(text);
    saveTextToHistory();
  }
  
  void discardEdit {
    S text = editedText();
    if (text == null) ret;
    saveFieldToHistory("discardingEdit", text);
    editingText(null);
  }
  
  S textForEditing() {
    initEditingText();
    ret editingText;
  }
  
  File historyFile() {
    ret fileInConceptsDir("History/" + shortDynName(this) + id + ".history");
  }
  
  void saveTextToHistory() {
    saveFieldToHistory("text", text);
  }
  
  void saveFieldToHistory(S field, S value) {
    File historyFile = historyFile();
    if (historyFile == null) ret;
    S contents = value == null ? " empty" : " (" + nLines(value) + ", " + nChars(value) + "):\n" +
      indentx(value) + "\n" +
      "\n";
    appendToTextFile(historyFile,
      "\n===\n" +
      "Concept ID: " + id + "\n" +
      "Date: " + dateWithMSUTC() + "\n" +
      firstToUpper(field) + contents +
      "===" + "\n");
  }
  
  bool isSaved() { ret text != null; }
  bool isSavedDistinctFromAutoRunVersion() { ret isSaved() && !eq(text, codeForAutoRun()); }
  bool isEditing() { ret editedText() != null; }
  bool isClearForAutoRun() { ret clearedForAutoRun != null; }
  
  S editedText() { ret eq(editingText, text) ? null : editingText; }
  
  S codeForAutoRun() { ret getVar(clearedForAutoRun()); }
  
  // return code clear for auto run if available
  // otherwise returns saved code if available
  // in all other cases returns null
  // (never returns text being edited)
  S safestCode() { ret or(codeForAutoRun(), stableText()); }
  
  void clearForAutoRun {
    if (!isSaved()) ret;
    S text = text();
    saveFieldToHistory("Auto-runnable code", text);
    clearedForAutoRun(new ClearForAutoRun(text));
  }
  
  void forgetAutoRunCode {
    if (!isClearForAutoRun()) ret;
    saveFieldToHistory("Auto-runnable code", null);
    clearedForAutoRun(null);
    compileResultForAutoRun = null;
  }
  
  void forgetCompileResults {
    compileResultForAutoRun = compileResultForSaved = null;
  }

  G22Utils g22utils() {
    ret assertNotNull(g22utils := main g22utils(_concepts()));
  }
  
  GazelleV_LeftArrowScriptParser makeParser() {
    ret g22utils().leftArrowParser()
      .sourceInfo(this);
  }
  
  LASCompileResult newCompileResult() { ret new LASCompileResult; }
  
  LASCompileResult returnOrCompile(LASCompileResult lastCompileResult, S code) {
    if (g22utils().compileResultValid(lastCompileResult, code))
      ret lastCompileResult;
    var cr = newCompileResult();
    cr.script(code);
    var parser = makeParser();
    cr.parser(parser);
    cr.compile();
    ret cr;
  }
  
  transient LASCompileResult compileResultForAutoRun;
  LASCompileResult compileForAutoRun() {
    S code = codeForAutoRun();
    if (code == null) null;
    var cr = returnOrCompile(compileResultForAutoRun, code);
    ret compileResultForAutoRun = cr;
  }
  
  transient LASCompileResult compileResultForSaved;
  LASCompileResult compileSaved() {
    S code = stableText();
    if (code == null) null;
    var cr = returnOrCompile(compileResultForSaved, code);
    ret compileResultForSaved = cr;
  }
  
  transient LASCompileResult compileResultForEditing;
  LASCompileResult compileEditing() {
    S code = editingText();
    if (code == null) null;
    var cr = returnOrCompile(compileResultForEditing, code);
    ret compileResultForEditing = cr;
  }
  
  LASCompileResult safestCompileResult() {
    try object compileForAutoRun();
    ret compileSaved();
  }
  
  LASCompileResult latestCompileResult() {
    try object compileEditing();
    try object compileSaved();
    ret compileForAutoRun();
  }
  
  LASCompileResult latestRunnableCompileResult() {
    var cr = compileEditing();
    if (cr != null && cr.runnable()) ret cr;
    cr = compileSaved();
    if (cr != null && cr.runnable()) ret cr;
    cr = compileForAutoRun();
    if (cr != null && cr.runnable()) ret cr;
    null;
  }
  
  O evaluateWithoutTimeout() {
    var cr = safestCompileResult();
    if (cr == null)
      fail("Not saved: " + this);
      
    temp g22utils().enter(); // enter module
    ret cr.parsedScriptMandatory()!;
  }
  
  O evaluateAutoRunWithoutTimeout() {
    var cr = compileForAutoRun();
    if (cr == null)
      fail("Not cleared for auto-run: " + this);
      
    temp g22utils().enter(); // enter module
    ret cr.parsedScriptMandatory()!;
  }
  
  S renderRunOnProjectOpenStatus() {
    ret runOnProjectOpen
      ? "Yes" + appendBracketed(appendPrefixIfNempty("prio ", runOrder))
      : null;
  }
  
  Set<S> allTexts() {
    ret asSet(llNonNulls(text(), editedText(), codeForAutoRun()));
  }
}

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: 66 / 77
Referenced in: [show references]