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

199
LINES

< > BotCompany Repo | #1034955 // JLeftArrowScriptIDE backup

JavaX fragment (include)

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

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: #1034955
Snippet name: JLeftArrowScriptIDE backup
Eternal ID of this version: #1034955/1
Text MD5: 9ced74209884916c6fe478874ed335bc
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-17 20:38:11
Source code size: 6442 bytes / 199 lines
Pitched / IR pitched: No / No
Views / Downloads: 117 / 130
Referenced in: [show references]