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

233
LINES

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

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

Transpiled version (64968L) is out of date.

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;
  
  // is there a newer version of this script?
  new Ref<G22LeftArrowScript> supercededBy;
  
  @Override void _doneLoading2() {
    //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 #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/86
Text MD5: aa30270dc9931849cff63da5567470e4
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-11-26 19:00:12
Source code size: 6583 bytes / 233 lines
Pitched / IR pitched: No / No
Views / Downloads: 249 / 859
Version history: 85 change(s)
Referenced in: [show references]