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

167
LINES

< > BotCompany Repo | #1034062 // JMiniJavaIDE

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

Uses 1485K of libraries. Click here for Pure Java version (20788L/133K).

sclass JMiniJavaIDE is Swingable {
  settable SimpleLiveValue<S> lvScript = stringLiveValue();
  S sectionTitle = "Mini Java IDE";
  
  sS helpText = "Javaaa";
  
  transient CompileResult compileResult;
  transient RSyntaxTextAreaWithSearch taScript;
  transient new Q compileQ;
  transient JLabel lblCompileResult;
  transient JButton btnRun;
  
  transient JavaCompletionProvider completionProvider;
  transient AutoCompletion autoComplete;
  
  transient settable IStringifier stringifier;
  
  class JavaCompletionProvider extends DefaultCompletionProvider {
    @Override
    public L<Completion> getCompletionByInputText(S inputText) {
      print("getCompletionByInputText: " + quote(inputText));
      ret ll(
        /*new BasicCompletion(this, inputText + "YesYes"),
        new BasicCompletion(this, inputText + "NoNo")*/);
    }
    
    @Override
    public L<Completion> getCompletionsImpl(JTextComponent comp) {
      ret getCompletionByInputText(getAlreadyEnteredText(comp));
    }
  }
  
  visual {
    taScript = liveValueRSyntaxTextArea_bothWays(lvScript);
    awtCalcEvery(taScript.textArea(), 5.0, -> compileQ.add(r compile));
    
    swing {
      completionProvider = new JavaCompletionProvider;
      completionProvider.setAutoActivationRules(true, " ");
      autoComplete = new AutoCompletion(completionProvider);
      autoComplete.setAutoActivationEnabled(true);
      autoComplete.install(taScript.textArea);
    }

    ret jCenteredSection(sectionTitle,
      centerAndSouthWithMargin(
        taScript.visualize(),
        centerAndEastWithMargin(
          jBorderlessHigherScrollPane(lblCompileResult = jlabel()),
          vstack(
            jline(
              btnRun = jbutton("Run" := rThread runButtonAction),
              jPopDownButton_noText(
                "Show Scripting Help", rThread { showTextWordWrapped("Gazelle 'Left arrow script' Help", helpText) },
              )
          ))
        )));
  }
  
  bool visible() { ret isShowing(lblCompileResult); }
  
  void compile {
    var script = lvScript!;
    var result = compileResult;
    if (result == null || !eq(result.script, script)) {
      setStatus("Compiling...");
      try {
        result = new CompileResult;
        result.script = script;
        result.className = "UserCode_" + now();
        result.adaptedScript = adaptScript(result);
        result.java = result.adaptedScript;
        //result.adaptedScript = "mainClassName " + result.className + "\n\n" + result.script;
        //result.java = transpileRaw(result.adaptedScript);

        result.compiledObject = veryQuickJava_finish_specialMainClass(result.className, result.java);
        print(compiledObject := result.compiledObject);
      } catch print e {
        result.compileError = e;
      }
      compileResult = result;
      setText(lblCompileResult, compileResult);
      updateRunButtonState();
    }
  }
  
  void updateRunButtonState() {
    setEnabled(btnRun, runButtonShouldBeEnabled());
  }
  
  swappable bool runButtonShouldBeEnabled() {
    ret compileResult != null && compileResult.runnable();
  }
  
  CompileResult freshCompileResult() {
    runInQAndWait(compileQ, r compile);
    ret compileResult;
  }
  
  O compiledObject() {
    ret freshCompileResult().compiledObject;
  }

  swappable void runButtonAction {
    runScript();
  }
  
  void runScript {
    //showText_fast_noWrap("Script Error", renderStackTrace(e));
    var result = freshCompileResult();
    if (result.compiledObject != null) {
      setStatus("Running");
      result.result = okOrError(-> callCompiledObject(result.compiledObject));
      setStatus(result.result.isOK()
        ? shorten(stringify(stringifier, result.result!))
        : exceptionToStringShorter(result.result.error()));
    }
  }

  class CompileResult {
    S script, adaptedScript, java;
    S className;
    Class compiledObject;
    Throwable compileError;
    OKOrError result;

    toString {
      if (compileError == null) ret "Compiled OK";
      
      S msg = compileError.getMessage();
      msg = dropPrefixFromLines("> ", msg);
      var errors = parseECJOutputForErrors(msg);
      if (nempty(errors)) {
        var error = first(errors);
        
        // rough but usually works
        print(codeLine := quote(error.codeLine));
        pnlQuoted(asList(lines_iterator(script)));
        int lineNr = indexOfLine(script, tabsToSpaces(error.codeLine));
        error.lineNr = lineNr+1;
        ret str(error);
      }
      ret msg;
    }
    
    bool runnable() { ret compiledObject != null; }
  }

  void setStatus(S status) {
    setText(lblCompileResult, status);
  }
  
  swappable S extraClassMembers(S script) {
    ret "";
  }
 
  swappable S adaptScript(CompileResult result) { 
    ret tok_moveImportsUp("class " + result.className + "{"
      + extraClassMembers(result.script) + "\n"
      + evalJava_prep(result.script)
      + "}");
  }
  
  swappable O callCompiledObject(O object) {
    ret callCalc(object);
  }
}

Author comment

Began life as a copy of #1034022

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1034062
Snippet name: JMiniJavaIDE
Eternal ID of this version: #1034062/36
Text MD5: a60584876120f4a75daab73705279aee
Transpilation MD5: 2a82bd0966a675efc785bedd3fb0a059
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-28 19:39:51
Source code size: 5168 bytes / 167 lines
Pitched / IR pitched: No / No
Views / Downloads: 190 / 417
Version history: 35 change(s)
Referenced in: [show references]