// Some of the functions are dependent on the concepts field, // others are global. sclass G22Utils is AutoCloseable { settable BackgroundProcessesUI backgroundProcessesUI; settable Enterable module; settable G22MasterStuff masterStuff; settable Concepts concepts; settable G22ProjectActions projectActions; gettable G22AutoStarter autoStarter = new(this); gettable CombinedStringifier stringifier = new( o -> o cast BufferedImage ? "Image (" + o.getWidth() + "*" + o.getHeight() + " px)" : null ); ImageSurface stdImageSurface() { var is = pixelatedImageSurface().setAutoZoomToDisplay(true).repaintInThread(false); is.defaultImageDir = -> dbDir(); is.specialPurposed = true; new ImageSurface_PositionToolTip(is); ret is; } ImageSurface stdImageSurface(BufferedImage etc img) { var is = stdImageSurface(); is.setImage(img); ret is; } S stringify(O o) { ret stringifier.toString(o); } event settingUpParser(GazelleV_LeftArrowScriptParser parser); event settingUpScriptIDE(JLeftArrowScriptIDE ide); GazelleV_LeftArrowScriptParser leftArrowParser() { new GazelleV_LeftArrowScriptParser parser; parser.g22utils(this); settingUpParser(parser); ret parser; } void basicParserTest() { var parser = leftArrowParser(); print(classContainerPrefixes := parser.classContainerPrefixes()); assertEquals(pair(1, 2), parser.parse("new Pair 1 2")!); } ifclass JLeftArrowScriptIDE JLeftArrowScriptIDE leftArrowIDE() { new JLeftArrowScriptIDE ide; ide.g22utils(this); ide.scriptTimeout(projectWideScriptTimeout()); settingUpScriptIDE(ide); ret ide; } endif File byteCodePath() { ret assertNotNull(getBytecodePathForClass(this)); } simplyCached ClassNameResolver classNameResolver() { ret new ClassNameResolver().byteCodePath(byteCodePath()).init(); } File databasesMotherDir() { ret javaxDataDir("Gazelle-22"); } AutoCloseable enter() { ret module?.enter(); } S defaultDBName() { ret "Default"; } File lastOpenedDBsFile() { ret newFile(databasesMotherDir(), "Last Opened"); } File autoUpdateFile() { ret newFile(databasesMotherDir(), "Auto-Update"); } bool autoUpdateEnabled() { ret fileExists(autoUpdateFile()); } void setAutoUpdate(bool b) { createOrRemoveFile(autoUpdateFile(), b); } LS dbsToOpen() { new LS dbNames; for (S name : tlft(loadTextFile(lastOpenedDBsFile()))) if (fileExists(newFile(databasesMotherDir(), name))) dbNames.add(name); if (empty(dbNames)) dbNames.add(defaultDBName()); ret dbNames; } void setOpenDBs(Cl dbDirs) { new LS dbNames; for (File dbDir : dbDirs) if (sameFile(databasesMotherDir(), dirOfFile(dbDir))) dbNames.add(fileName(dbDir)); saveTextFile(lastOpenedDBsFile(), lines(dbNames)); } ifclass SimpleCRUD_v2 void setupScriptCRUD(SimpleCRUD_v2 crud, bool allowRunOnProjectOpen default false) { crud.useNewChangeHandler(true); crud.editableFieldsForItem = x -> llNonNulls("description", allowRunOnProjectOpen ? "runOnProjectOpen" : null, allowRunOnProjectOpen ? "runOrder" : null); //G22LeftArrowScript.f_description().getName()); crud.multiLineField("text"); crud.multiLineField("editingText"); crud.makeTextArea = text -> jMinHeight(200, crud.makeTextArea_base(text)); crud.humanizeFieldNames = false; crud.iconButtons(true); crud.itemToMap_inner2 = c -> scriptToMap(c, allowRunOnProjectOpen); } endif MapSO scriptToMap(G22LeftArrowScript c, bool allowRunOnProjectOpen default false) { ret litorderedmap( "Description" := str(c), "Status" := renderScriptStatus(c), "LoC" := renderScriptLoC(c), "Run on project open" := allowRunOnProjectOpen && c.runOnProjectOpen ? "Yes" + appendBracketed("prio " + c.runOrder) : null); } S renderScriptStatus(G22LeftArrowScript c) { ret or2_rev("Empty", joinNemptiesWithSpacedPlus( c.isClearForAutoRun() ? "Clear for auto-run" : null, c.isSavedDistinctFromAutoRunVersion() ? "Saved (not cleared)" : null, c.isEditing() ? "Editing" : null )); } S renderScriptLoC(G22LeftArrowScript c) { ret n2(intMax(mapLL linesOfCode_javaTok( c.editingText, c.text, c.codeForAutoRun()))); } // e.g. for an image file L labelsForFile(File file) { if (file == null) null; File labelsFile = appendToFileName(file, ".labels"); LS labels = tlft(loadTextFile(labelsFile)); ret map getLabel(labels); } File labelsFile(File file) { if (file == null) null; ret appendToFileName(file, ".labels"); } void setLabelsForFile(File file, L labels) { LS list = map(labels, label -> label.name); File f = labelsFile(file); saveTextFile(f, lines(list)); print("Saved " + nLabels(list) + " (" + joinWithComma(list) + ") to " + f); } G22Label getLabel(S name) { if (empty(name)) null; if (containsNewLine(name)) fail("No newlines in label names allowed: " + name); ret uniqCI(concepts, G22Label, +name); } File dbDir() { ret conceptsDir(concepts); } File fileInDbDir(S name) { ret newFile(dbDir(), name); } record GazelleDB(S name, File dir) { simplyCached bool loaded() { ret containsFile(masterStuff.openConceptDirs(), dir); } File conceptsFile() { ret conceptsFileIn(dir); } } L gazelleDBs() { new L dbs; for (File dir : listDirsContainingFileNamed(databasesMotherDir(), "concepts.structure.gz")) dbs.add(new GazelleDB(fileName(dir), dir)); ret dbs; } ifclass RSyntaxTextAreaWithSearch RSyntaxTextAreaWithSearch newSyntaxTextArea(IF1 wrapStatusLabel default null) { RSyntaxTextAreaWithSearch ta = new(wrapStatusLabel); ta.textArea().setHighlightCurrentLine(false); ta.menuLessOperation(); ret ta; } endif File projectStoryTextFile() { ret newFile(dbDir(), "story.txt"); } S projectName() { ret fileName(dbDir()); } close { autoStarter.close(); } // project vars O getProjectVar(S name) { G22Variable var = conceptWhere(concepts, G22Variable, +name); ret var?.value; } O waitForProjectVar(S name) { G22Variable var = conceptWhere(concepts, G22Variable, +name); if (var == null) var = uniq(concepts, G22Variable, +name); O o = var.value; try object o; ret waitUntilNotNull(var.varValue()); } void setTransientProjectVar(S name, O value) { G22Variable var = uniq(concepts, G22Variable, +name); cset(var, persistent := false); var.value(value); } // timeouts double defaultScriptTimeout() { ret 10.0; } double projectWideScriptTimeout() { ret or(toDoubleOrNull(getProjectVar("!Script Timeout")), defaultScriptTimeout()); } // calling scripts // No timeout (timeout is handled by calling script) O callScript(long id) { var script = getScript(id); ret script!; //ret evalWithTimeoutOrTypedException(timeoutSeconds, -> script!); } }