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 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>> lastResultByMode; // is there a newer version of this script? new Ref 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 allTexts() { ret asSet(llNonNulls(text(), editedText(), codeForAutoRun())); } }