Uses 1485K of libraries. Click here for Pure Java version (20788L/133K).
1 | sclass JMiniJavaIDE is Swingable { |
2 | settable SimpleLiveValue<S> lvScript = stringLiveValue(); |
3 | S sectionTitle = "Mini Java IDE"; |
4 | |
5 | sS helpText = "Javaaa"; |
6 | |
7 | transient CompileResult compileResult; |
8 | transient RSyntaxTextAreaWithSearch taScript; |
9 | transient new Q compileQ; |
10 | transient JLabel lblCompileResult; |
11 | transient JButton btnRun; |
12 | |
13 | transient JavaCompletionProvider completionProvider; |
14 | transient AutoCompletion autoComplete; |
15 | |
16 | transient settable IStringifier stringifier; |
17 | |
18 | class JavaCompletionProvider extends DefaultCompletionProvider { |
19 | @Override |
20 | public L<Completion> getCompletionByInputText(S inputText) { |
21 | print("getCompletionByInputText: " + quote(inputText)); |
22 | ret ll( |
23 | /*new BasicCompletion(this, inputText + "YesYes"), |
24 | new BasicCompletion(this, inputText + "NoNo")*/); |
25 | } |
26 | |
27 | @Override |
28 | public L<Completion> getCompletionsImpl(JTextComponent comp) { |
29 | ret getCompletionByInputText(getAlreadyEnteredText(comp)); |
30 | } |
31 | } |
32 | |
33 | visual { |
34 | taScript = liveValueRSyntaxTextArea_bothWays(lvScript); |
35 | awtCalcEvery(taScript.textArea(), 5.0, -> compileQ.add(r compile)); |
36 | |
37 | swing { |
38 | completionProvider = new JavaCompletionProvider; |
39 | completionProvider.setAutoActivationRules(true, " "); |
40 | autoComplete = new AutoCompletion(completionProvider); |
41 | autoComplete.setAutoActivationEnabled(true); |
42 | autoComplete.install(taScript.textArea); |
43 | } |
44 | |
45 | ret jCenteredSection(sectionTitle, |
46 | centerAndSouthWithMargin( |
47 | taScript.visualize(), |
48 | centerAndEastWithMargin( |
49 | jBorderlessHigherScrollPane(lblCompileResult = jlabel()), |
50 | vstack( |
51 | jline( |
52 | btnRun = jbutton("Run" := rThread runButtonAction), |
53 | jPopDownButton_noText( |
54 | "Show Scripting Help", rThread { showTextWordWrapped("Gazelle 'Left arrow script' Help", helpText) }, |
55 | ) |
56 | )) |
57 | ))); |
58 | } |
59 | |
60 | bool visible() { ret isShowing(lblCompileResult); } |
61 | |
62 | void compile { |
63 | var script = lvScript!; |
64 | var result = compileResult; |
65 | if (result == null || !eq(result.script, script)) { |
66 | setStatus("Compiling..."); |
67 | try { |
68 | result = new CompileResult; |
69 | result.script = script; |
70 | result.className = "UserCode_" + now(); |
71 | result.adaptedScript = adaptScript(result); |
72 | result.java = result.adaptedScript; |
73 | //result.adaptedScript = "mainClassName " + result.className + "\n\n" + result.script; |
74 | //result.java = transpileRaw(result.adaptedScript); |
75 | |
76 | result.compiledObject = veryQuickJava_finish_specialMainClass(result.className, result.java); |
77 | print(compiledObject := result.compiledObject); |
78 | } catch print e { |
79 | result.compileError = e; |
80 | } |
81 | compileResult = result; |
82 | setText(lblCompileResult, compileResult); |
83 | updateRunButtonState(); |
84 | } |
85 | } |
86 | |
87 | void updateRunButtonState() { |
88 | setEnabled(btnRun, runButtonShouldBeEnabled()); |
89 | } |
90 | |
91 | swappable bool runButtonShouldBeEnabled() { |
92 | ret compileResult != null && compileResult.runnable(); |
93 | } |
94 | |
95 | CompileResult freshCompileResult() { |
96 | runInQAndWait(compileQ, r compile); |
97 | ret compileResult; |
98 | } |
99 | |
100 | O compiledObject() { |
101 | ret freshCompileResult().compiledObject; |
102 | } |
103 | |
104 | swappable void runButtonAction { |
105 | runScript(); |
106 | } |
107 | |
108 | void runScript { |
109 | //showText_fast_noWrap("Script Error", renderStackTrace(e)); |
110 | var result = freshCompileResult(); |
111 | if (result.compiledObject != null) { |
112 | setStatus("Running"); |
113 | result.result = okOrError(-> callCompiledObject(result.compiledObject)); |
114 | setStatus(result.result.isOK() |
115 | ? shorten(stringify(stringifier, result.result!)) |
116 | : exceptionToStringShorter(result.result.error())); |
117 | } |
118 | } |
119 | |
120 | class CompileResult { |
121 | S script, adaptedScript, java; |
122 | S className; |
123 | Class compiledObject; |
124 | Throwable compileError; |
125 | OKOrError result; |
126 | |
127 | toString { |
128 | if (compileError == null) ret "Compiled OK"; |
129 | |
130 | S msg = compileError.getMessage(); |
131 | msg = dropPrefixFromLines("> ", msg); |
132 | var errors = parseECJOutputForErrors(msg); |
133 | if (nempty(errors)) { |
134 | var error = first(errors); |
135 | |
136 | // rough but usually works |
137 | print(codeLine := quote(error.codeLine)); |
138 | pnlQuoted(asList(lines_iterator(script))); |
139 | int lineNr = indexOfLine(script, tabsToSpaces(error.codeLine)); |
140 | error.lineNr = lineNr+1; |
141 | ret str(error); |
142 | } |
143 | ret msg; |
144 | } |
145 | |
146 | bool runnable() { ret compiledObject != null; } |
147 | } |
148 | |
149 | void setStatus(S status) { |
150 | setText(lblCompileResult, status); |
151 | } |
152 | |
153 | swappable S extraClassMembers(S script) { |
154 | ret ""; |
155 | } |
156 | |
157 | swappable S adaptScript(CompileResult result) { |
158 | ret tok_moveImportsUp("class " + result.className + "{" |
159 | + extraClassMembers(result.script) + "\n" |
160 | + evalJava_prep(result.script) |
161 | + "}"); |
162 | } |
163 | |
164 | swappable O callCompiledObject(O object) { |
165 | ret callCalc(object); |
166 | } |
167 | } |
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: | 191 / 420 |
Version history: | 35 change(s) |
Referenced in: | [show references] |