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

275
LINES

< > BotCompany Repo | #1034022 // JLeftArrowScriptIDE

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

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

sclass JLeftArrowScriptIDE > MetaWithChangeListeners is Swingable {
  settable IVarWithNotify<S> lvScript = stringLiveValue();
  settable S sectionTitle = "Left arrow script";
  settable bool withResultPanel = true;
  
  // how long to wait before compiling after text is changed (seconds)
  settable double compileDelay = 0.1;
  
  sS helpText = !include string #1034045;
  
  transient settableWithVar LASCompileResult compileResult;
  
  transient RSyntaxTextAreaWithSearch taScript;
  transient new RSTADummyParser dummyParser; // for error highlighting
  
  transient new Q compileQ;
  transient gettable JPanel buttons = jline();
  transient JButton btnRun;
  transient CollapsibleLeftPanel collapsibleResultPanel;
  transient G22ScriptResultPanel resultPanel;
  transient JPopDownButton popDownButton;
  
  transient LeftArrowCompletionProvider completionProvider;

  settable transient G22Utils g22utils;
  
  settable double scriptTimeout = 10.0;
  
  settable transient bool showTitle = true;
  
  transient swappable GazelleV_LeftArrowScriptParser makeParser() {
    ret g22utils.leftArrowParser();
  }
  
  class LeftArrowCompletionProvider extends DefaultCompletionProvider {
    @Override
    public L<Completion> getCompletionsImpl(JTextComponent comp) {
      try {
        S text = getText(comp);
        GazelleV_LeftArrowScriptParser parser = makeParser2();
        LeftArrowScriptAutoCompleter completer = new(g22utils, parser);
        enableScaffolding(completer);
        completer.seek(text, getCaretPosition(comp));
        ret map(completer.searcher().withScores(), completion -> {
          BasicCompletion c = new(this, completion!);
          c.setRelevance((int) completion.score());
          ret c;
        });
      } catch print e { ret ll(); }
    }
  }
  
  JComponent wrapStatusLabel(JComponent lbl) {
    onLeftClick(lbl, -> {
      LineAndColumn lineAndCol = compileResult?.errorLineAndCol();
      if (lineAndCol != null) {
        moveCaretToLineAndCol(textArea(), lineAndCol);
        focus(textArea());
      }
    });
    
    popDownButton = swing(-> new JPopDownButton("Help"));
    popDownButton.onFillingMenu(menu -> addMenuItems(menu,
      "Show Scripting Help", rThread {
        showTextWordWrapped("Gazelle 'Left arrow script' Help", helpText)
      },
      "Show Global Class Names", rThread showGlobalClassNames,
      "Convert to Java", rThread convertToJava,
    ));

    ret
      centerAndEastWithMargin(
        jBorderlessHigherScrollPane(lbl),
        jfullcenter(buttons));
  }
  
  cachedVisual swing(-> {
    taScript = g22utils.newSyntaxTextArea(l1 wrapStatusLabel);
    
    bindTextComponentToVarWithNotify_noInitialUndo(textArea(), lvScript);
    
    dummyParser.install(textArea());
    //onCtrlEnter(textArea(), rThread runScript);
    
    // F5 to run!
    addKeyListener(textArea(), functionKeyListener(5, rThread runScript);
    
    // Ctrl+B to go to definition
    addKeyListener(textArea(), ctrlLetterKeyListener('b', rThread goToDefinition);
    
    // Permanent auto-compile on any text input
    awtCalcEvery(textArea(), compileDelay, -> compileQ.add(r compile));

    installCompletionProvider(completionProvider = new LeftArrowCompletionProvider, textArea());

    JComponent vis = taScript.visualize();

    if (showTitle)
      vis = jCenteredSection(sectionTitle, vis);
    vis = wrapSection(vis);
    
    addAll(buttons,
      btnRun = toolTip("Run script (F5)", jbutton("Run" := rThread runScript)),
      popDownButton);

    if (withResultPanel) {
      resultPanel = new G22ScriptResultPanel;
      collapsibleResultPanel = new CollapsibleLeftPanel(false, "Output", resultPanel.visualize(), vis);
      collapsibleResultPanel.sidePanelMargins = c -> withTopAndLeftMargin(c);
      ret collapsibleResultPanel.visualize();
    } else
      ret vis;
  });
  
  swappable JComponent wrapSection(JComponent c) { ret c; }
  
  RSyntaxTextArea textArea() {
    if (taScript == null) visualize();
    ret taScript.textArea();
  }
  
  void setText(S text) { main setText(textArea(), text); }
  
  bool visible() { ret isShowing(textArea()); }
  
  void compile {
    var script = lvScript!;
    var result = compileResult;
    if (result == null || !eq(result.script, script)) {
      result = newCompileResult();
      result.script = script;
      result.makeParser = l0 makeParser;
      result.compile();
      
      compileResult(result);
      showStatus(str(compileResult));
      updateRunButtonState();
      
      // send errors to highlighter
      L<RSTADummyParser.Error> errors = new L;
      if (result.compileError != null) {
        LineAndColumn lineAndCol = result.errorLineAndCol();
        if (lineAndCol != null)
          errors.add(new RSTADummyParser.Error()
            .msg(result.errorToString())
            .start(lineAndCol)
            .end(LineAndColumn(lineAndCol.line+1, 1)));
      }
      dummyParser.setErrors(result.script, errors, textArea());
    }
  }
  
  void updateRunButtonState() {
    setEnabled(btnRun, runButtonShouldBeEnabled());
  }
  
  swappable bool runButtonShouldBeEnabled() {
    ret compileResult != null && compileResult.runnable();
  }
  
  LASCompileResult freshCompileResult() {
    runInQAndWait(compileQ, r compile);
    ret compileResult;
  }
  
  GazelleV_LeftArrowScript.Script parsedScript() {
    ret freshCompileResult().parsedScript;
  }

  swappable void runScript() {
    var result = freshCompileResult();
    if (result.parsedScript != null) {
      var value = runResultWithTimestamps(-> callCompiledObjectWithTimeout(result.parsedScript));
      showScriptResult(value);
    }
  }
  
  void showScriptResult(OKOrError result) {
    if (result cast RunResultWithTimestamps)
      resultPanel?.logView.setText(str(result.printOutput()));
    
    if (result.isOK()) {
      setStatus(shorten(g22utils.stringify(result!)));
      var objVisualizer = G22JavaObjectVisualizer(g22utils, result!);
      if (result cast RunResultWithTimestamps) {
        var duration = result.duration();
        print(+duration);
        if (duration != null) {
          long nanos = result.duration().toNanos();
          printVars(+nanos, +objVisualizer);
          objVisualizer.nanos(nanos);
        }
      }
      if (collapsibleResultPanel != null)
        objVisualizer.withTypeAndTime(false);
      resultPanel?.scpResult.set(objVisualizer);
      if (collapsibleResultPanel != null)
        collapsibleResultPanel.sidePanelName("Output" + appendBracketed(objVisualizer.objectInfos()));
    } else {
      setStatus(exceptionToStringShorter_dontDropOuterExceptions(result.error()));
      resultPanel?.scpResult.set(jErrorView(result.getError()));
    }
    collapsibleResultPanel?.expand();
  }

  /* compile result used to include result of execution
  class CompileResult > LASCompileResult {
    OKOrError result;
  }*/
  
  void setStatus aka showStatus(S status) {
    taScript?.setStatus(" " + unnull(status));
  }

  void showRuntimeError(Throwable e) {
    showStatus(exceptionToStringShorter(e));
  }
  
  swappable VarContext makeVarContextForExecution() { ret new FlexibleVarContext; }
  
  O callCompiledObjectWithTimeout(double timeoutSeconds default scriptTimeout,
    GazelleV_LeftArrowScript.Script script,
    VarContext ctx default makeVarContextForExecution()) {
    ret g22utils.evalRegisteredCode(timeoutSeconds, str(script),
      -> script.get(ctx));
  }
  
  GazelleV_LeftArrowScriptParser makeParser2() {
    var parser = makeParser();
    print("Function containers: " + parser.functionContainers);
    ret parser;
  }
  
  void showGlobalClassNames() {
    showText("Global Class Names", pnlToString(toCIMap(makeParser().globalClassNames())));
  }
  
  // only after visualize
  void setEditable(bool b) {
    main setEditable(textArea(), b);
  }
  
  swappable LASCompileResult newCompileResult() { ret new LASCompileResult; }
  
  void convertToJava {
    pcall-infobox {
      new ConvertLASToJava converter;
      enableScaffolding(converter);
      showText("Java conversion - " + sectionTitle(),
        strOrNull(converter.get(parsedScript(), true)));
    }
  }
  
  void goToDefinition {
    S text, int iChar = unpair textAndCaretPosition(textArea());
    LS tok = lasTok(text);
    int iTok = charToTokenIndex_left(tok, iChar);
    S token = lastIdentifier(subList(tok, iTok-2, iTok+1));
    printVars("goToDefinition", +iTok, token := quote(token));

    if (token != null) {
      S name = lookupStandardFunctionOrClassNameIC(token);
      if (name != null) {
        S snippetID = sfOrSCSnippet(token);
        if (snippetID != null) { // Should always be true
          infoBox("Opening " + quote(name));
          g22utils.openInBrowser(snippetURL(snippetID));
          ret;
        }
      }
      
      flatInfoBox("No information found for " + token);
    }
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1034022
Snippet name: JLeftArrowScriptIDE
Eternal ID of this version: #1034022/204
Text MD5: d60155f7dbeeb03ef57a8e381bee124d
Transpilation MD5: 16649219ab65718b301402c7c8539c44
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-13 19:09:36
Source code size: 9141 bytes / 275 lines
Pitched / IR pitched: No / No
Views / Downloads: 470 / 1687
Version history: 203 change(s)
Referenced in: [show references]