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

191
LINES

< > BotCompany Repo | #1034729 // JLeftArrowScriptIDE backup

JavaX fragment (include)

1  
sclass JLeftArrowScriptIDE is Swingable {
2  
  settable SimpleLiveValue<S> lvScript = stringLiveValue();
3  
  settable S sectionTitle = "Left arrow script";
4  
  
5  
  sS helpText = !include string #1034045;
6  
  
7  
  transient LASCompileResult compileResult;
8  
  transient RSyntaxTextAreaWithSearch taScript;
9  
  transient new Q compileQ;
10  
  transient JButton btnRun;
11  
  transient SingleComponentPanel scpResult;
12  
  transient SingleComponentPanel scpMoreButtons = singleComponentPanel();
13  
  transient JPopDownButton popDownButton;
14  
  
15  
  transient LeftArrowCompletionProvider completionProvider;
16  
17  
  settable transient G22Utils g22utils;
18  
  
19  
  settable double scriptTimeout = 10.0;
20  
  
21  
  settable transient bool showTitle = true;
22  
  
23  
  transient swappable GazelleV_LeftArrowScriptParser makeParser() {
24  
    ret g22utils.leftArrowParser();
25  
  }
26  
  
27  
  class LeftArrowCompletionProvider extends DefaultCompletionProvider {
28  
    @Override
29  
    public L<Completion> getCompletionsImpl(JTextComponent comp) {
30  
      try {
31  
        S text = getText(comp);
32  
        GazelleV_LeftArrowScriptParser parser = makeParser2();
33  
        LeftArrowScriptAutoCompleter completer = new(g22utils, parser);
34  
        enableScaffolding(completer);
35  
        completer.seek(text, getCaretPosition(comp));
36  
        ret map(completer.searcher().withScores(), completion -> {
37  
          BasicCompletion c = new(this, completion!);
38  
          c.setRelevance((int) completion.score());
39  
          ret c;
40  
        });
41  
      } catch print e { ret ll(); }
42  
    }
43  
  }
44  
  
45  
  JComponent wrapStatusLabel(JComponent lbl) {
46  
    popDownButton = swing(-> new JPopDownButton);
47  
    popDownButton.onFillingMenu(menu -> {
48  
      addMenuItems(menu,
49  
        "Show Scripting Help", rThread { showTextWordWrapped("Gazelle 'Left arrow script' Help", helpText) },
50  
        "Show Global Class Names", rThread showGlobalClassNames);
51  
    });
52  
53  
    ret
54  
      centerAndEastWithMargin(
55  
        jBorderlessHigherScrollPane(lbl),
56  
        jfullcenter(
57  
          jline(
58  
            scpMoreButtons,
59  
            btnRun = jbutton("Run" := rThread runScript),
60  
            popDownButton
61  
          )));
62  
  }
63  
  
64  
  cachedVisual swing(-> {
65  
    taScript = g22utils.newSyntaxTextArea(l1 wrapStatusLabel);
66  
    bindTextComponentToLiveValue_bothWays(textArea(), lvScript);
67  
    onCtrlEnter(textArea(), rThread runScript);
68  
    awtCalcEvery(textArea(), 1.0, -> compileQ.add(r compile));
69  
70  
    installCompletionProvider(completionProvider = new LeftArrowCompletionProvider, textArea());
71  
72  
    JComponent vis = taScript.visualize();
73  
74  
    if (showTitle)
75  
      vis = wrapSection(jCenteredSection(sectionTitle, vis));
76  
77  
    if (scpResult != null)
78  
      vis = jhsplit(
79  
        jCenteredSection("Script Result", scpResult),
80  
        vis);
81  
        
82  
    ret vis;
83  
  });
84  
  
85  
  swappable JComponent wrapSection(JComponent c) { ret c; }
86  
  
87  
  RSyntaxTextArea textArea() {
88  
    if (taScript == null) visualize();
89  
    ret taScript.textArea();
90  
  }
91  
  
92  
  void setText(S text) { main setText(textArea(), text); }
93  
  
94  
  bool visible() { ret isShowing(textArea()); }
95  
  
96  
  void compile {
97  
    var script = lvScript!;
98  
    var result = compileResult;
99  
    if (result == null || !eq(result.script, script)) {
100  
      try {
101  
        result = newCompileResult();
102  
        result.script = script;
103  
        result.parser = makeParser();
104  
        result.parsedScript = result.parser.parse(result.script);
105  
        print(result.parsedScript);
106  
      } catch e {
107  
        print(e);
108  
        result.compileError = e;
109  
      }
110  
      compileResult = result;
111  
      showStatus(str(compileResult));
112  
      updateRunButtonState();
113  
    }
114  
  }
115  
  
116  
  void updateRunButtonState() {
117  
    setEnabled(btnRun, runButtonShouldBeEnabled());
118  
  }
119  
  
120  
  swappable bool runButtonShouldBeEnabled() {
121  
    ret compileResult != null && compileResult.runnable();
122  
  }
123  
  
124  
  LASCompileResult freshCompileResult() {
125  
    runInQAndWait(compileQ, r compile);
126  
    ret compileResult;
127  
  }
128  
  
129  
  GazelleV_LeftArrowScript.Script parsedScript() {
130  
    ret freshCompileResult().parsedScript;
131  
  }
132  
133  
  swappable void runScript() {
134  
    var result = freshCompileResult();
135  
    if (result.parsedScript != null) {
136  
      var value = okOrError(-> callCompiledObjectWithTimeout(result.parsedScript));
137  
      showScriptResult(value);
138  
    }
139  
  }
140  
  
141  
  void showScriptResult(OKOrError result) {
142  
    if (result.isOK())
143  
      setStatus(shorten(g22utils.stringify(result!)));
144  
    else
145  
      setStatus(exceptionToStringShorter_dontDropOuterExceptions(result.error()));
146  
  }
147  
148  
  /* compile result used to include result of execution
149  
  class CompileResult > LASCompileResult {
150  
    OKOrError result;
151  
  }*/
152  
  
153  
  void setStatus aka showStatus(S status) {
154  
    taScript?.setStatus(status);
155  
  }
156  
157  
  void showRuntimeError(Throwable e) {
158  
    showStatus(exceptionToStringShorter(e));
159  
  }
160  
  
161  
  void withResultPanel {
162  
    scpResult if null = singleComponentPanel();
163  
  }
164  
  
165  
  swappable VarContext makeVarContextForExecution() { ret new VarContext; }
166  
  
167  
  O callCompiledObjectWithTimeout(double timeoutSeconds default scriptTimeout, GazelleV_LeftArrowScript.Script object, VarContext ctx default makeVarContextForExecution()) {
168  
    O result = evalWithTimeoutOrTypedException(timeoutSeconds, -> object.get(ctx));
169  
    scpResult?.set(G22JavaObjectVisualizer(g22utils, result));
170  
    ret result;
171  
  }
172  
  
173  
  GazelleV_LeftArrowScriptParser makeParser2() {
174  
    var parser = makeParser();
175  
    print("Function containers: " + parser.functionContainers);
176  
    ret parser;
177  
  }
178  
  
179  
  void showGlobalClassNames() {
180  
    showText("Global Class Names", pnlToString(toCIMap(makeParser().globalClassNames())));
181  
  }
182  
  
183  
  // only after visualize
184  
  void setEditable(bool b) {
185  
    main setEditable(textArea(), b);
186  
  }
187  
  
188  
  swappable LASCompileResult newCompileResult() { ret new LASCompileResult; }
189  
  
190  
  void moreButtons(JComponent c) { scpMoreButtons.set(c); }
191  
}

Author comment

Began life as a copy of #1034022

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034729
Snippet name: JLeftArrowScriptIDE backup
Eternal ID of this version: #1034729/1
Text MD5: b2e94b4babf7a4c7b3640c4adcd7c375
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-07 20:58:11
Source code size: 5957 bytes / 191 lines
Pitched / IR pitched: No / No
Views / Downloads: 156 / 167
Referenced in: [show references]