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

190
LINES

< > BotCompany Repo | #1034255 // G22LeftArrowScript (script as a concept)

JavaX fragment (include) [tags: use-pretranspiled]

Uses 2164K of libraries. Click here for Pure Java version (52274L/276K).

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
  bool runOnProjectOpen;
  
  // only when runOnProjectOpen is seT:
  // when to run in sequence (sorted with alphanumIC)
  S runOrder;
  
  // run stats
  long runCount;
  
  // info about last result (or error) of script run by mode
  Map<G22ScriptMode, WithTimestamp<PersistableOKOrError<G22TypeDesc>>> lastResultByMode;
  
  @Override void _doneLoading2() {
    //print("_doneLoading " + this);
    cMigrateField(this, "code", "text");
  }
  
  S myType() { ret dropPrefix("G22", shortClassName(this)); }
  
  toString { ret or2(description, myType() + " " + id); }
  
  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();
  }
  
  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(); }
  
  LASCompileResult newCompileResult() { ret new LASCompileResult; }
  
  transient LASCompileResult compileResultForAutoRun;
  LASCompileResult compileForAutoRun() {
    S code = codeForAutoRun();
    if (code == null) null;
    var cr = compileResultForAutoRun;
    if (cr != null && eq(cr.script, code))
      ret cr;
    cr = newCompileResult();
    cr.script(code);
    var parser = makeParser();
    cr.parser(parser);
    cr.compile();
    ret compileResultForAutoRun = cr;
  }
  
  transient LASCompileResult compileResultForSaved;
  LASCompileResult compileSaved() {
    S code = stableText();
    if (code == null) null;
    var cr = compileResultForSaved;
    if (cr != null && eq(cr.script, code))
      ret cr;
    cr = newCompileResult();
    cr.script(code);
    var parser = makeParser();
    cr.parser(parser);
    cr.compile();
    ret compileResultForSaved = cr;
  }
  
  LASCompileResult safestCompileResult() {
    try object compileForAutoRun();
    ret compileSaved();
  }
  
  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;
  }
}

Author comment

Began life as a copy of #1034204

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034255
Snippet name: G22LeftArrowScript (script as a concept)
Eternal ID of this version: #1034255/73
Text MD5: 6d54515978c6f2b1d1dd58805b8858d0
Transpilation MD5: eb44ed6d29d80cb1e0f3e253a187da25
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-12 19:45:53
Source code size: 5400 bytes / 190 lines
Pitched / IR pitched: No / No
Views / Downloads: 200 / 707
Version history: 72 change(s)
Referenced in: [show references]