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

207
LINES

< > BotCompany Repo | #1034363 // JLeftArrowScriptIDE (backup with two status bars)

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 CompileResult compileResult;
8  
  transient RSyntaxTextAreaWithSearch taScript;
9  
  transient new Q compileQ;
10  
  transient JLabel lblCompileResult;
11  
  transient JButton btnRun;
12  
  transient SingleComponentPanel scpResult;
13  
  
14  
  transient LeftArrowCompletionProvider completionProvider;
15  
  transient AutoCompletion autoComplete;
16  
  
17  
  settable transient G22Utils g22utils;
18  
  
19  
  settable double scriptTimeout = 10.0;
20  
  
21  
  settable transient bool showTitle = true;
22  
  
23  
  class LeftArrowCompletionProvider extends DefaultCompletionProvider {
24  
    /*@Override
25  
    public L<Completion> getCompletionByInputText(S inputText) {
26  
      print("getCompletionByInputText: " + quote(inputText));
27  
      ret ll(
28  
        new BasicCompletion(this, inputText + "YesYes"),
29  
        new BasicCompletion(this, inputText + "NoNo"));
30  
    }*/
31  
    
32  
    /*@Override
33  
    public L<Completion> getCompletions(JTextComponent comp) {
34  
      var l = super.getCompletions(comp);
35  
      print("getCompletions: " + l);
36  
      ret l;
37  
    }*/
38  
    
39  
    @Override
40  
    public L<Completion> getCompletionsImpl(JTextComponent comp) {
41  
      try {
42  
        print("getCompletionsImpl");
43  
        S text = getText(comp);
44  
        GazelleV_LeftArrowScriptParser parser = makeParser();
45  
        LeftArrowScriptAutoCompleter completer = new(g22utils, parser);
46  
        enableScaffolding(completer);
47  
        completer.seek(text, getCaretPosition(comp));
48  
        ret map(completer.searcher().withScores(), completion -> {
49  
          BasicCompletion c = new(this, completion!);
50  
          c.setRelevance((int) completion.score());
51  
          ret c;
52  
        });
53  
      } catch print e { ret ll(); }
54  
55  
      //ret getCompletionByInputText(getAlreadyEnteredText(comp));
56  
    }
57  
  }
58  
  
59  
  visual {
60  
    taScript = liveValueRSyntaxTextArea_bothWays(lvScript);
61  
    taScript.menuLessOperation();
62  
63  
    awtCalcEvery(taScript.textArea(), 1.0, -> compileQ.add(r compile));
64  
    
65  
    swing {
66  
      completionProvider = new LeftArrowCompletionProvider;
67  
      completionProvider.setAutoActivationRules(true, " ");
68  
      autoComplete = new AutoCompletion(completionProvider);
69  
      autoComplete.setAutoCompleteSingleChoices(false);
70  
      autoComplete.setAutoActivationEnabled(true);
71  
      autoComplete.setAutoActivationDelay(2000);
72  
      autoComplete.install(taScript.textArea);
73  
      print("Auto-complete installed on " + taScript.textArea);
74  
    }
75  
    
76  
    JComponent vis = centerAndSouthWithMargin(
77  
      taScript.visualize(),
78  
      centerAndEastWithMargin(
79  
        jBorderlessHigherScrollPane(lblCompileResult = jlabel()),
80  
        centerAndSouth(jpanel(),
81  
          jline(
82  
            btnRun = jbutton("Run" := rThread runScript),
83  
            jPopDownButton_noText(
84  
              "Show Scripting Help", rThread { showTextWordWrapped("Gazelle 'Left arrow script' Help", helpText) },
85  
              "Show Global Class Names", rThread showGlobalClassNames,
86  
            )
87  
        ))
88  
      ));
89  
90  
    if (showTitle)
91  
      vis = jCenteredSection(sectionTitle, vis);
92  
93  
    if (scpResult != null)
94  
      vis = jhsplit(
95  
        jCenteredSection("Image will show here", scpResult),
96  
        vis);
97  
        
98  
    ret vis;
99  
  }
100  
  
101  
  bool visible() { ret isShowing(lblCompileResult); }
102  
  
103  
  void compile {
104  
    var script = lvScript!;
105  
    var result = compileResult;
106  
    if (result == null || !eq(result.script, script)) {
107  
      try {
108  
        result = new CompileResult;
109  
        result.script = script;
110  
        result.parser = makeParser();
111  
        result.parsedScript = result.parser.parse(result.script);
112  
        print(result.parsedScript);
113  
      } catch e {
114  
        print(e);
115  
        result.compileError = e;
116  
      }
117  
      compileResult = result;
118  
      setText(lblCompileResult, compileResult);
119  
      updateRunButtonState();
120  
    }
121  
  }
122  
  
123  
  void updateRunButtonState() {
124  
    setEnabled(btnRun, runButtonShouldBeEnabled());
125  
  }
126  
  
127  
  swappable bool runButtonShouldBeEnabled() {
128  
    ret compileResult != null && compileResult.runnable();
129  
  }
130  
  
131  
  CompileResult freshCompileResult() {
132  
    runInQAndWait(compileQ, r compile);
133  
    ret compileResult;
134  
  }
135  
  
136  
  GazelleV_LeftArrowScript.Script parsedScript() {
137  
    ret freshCompileResult().parsedScript;
138  
  }
139  
140  
  swappable void runScript() {
141  
    var result = freshCompileResult();
142  
    if (result.parsedScript != null) {
143  
      result.result = okOrError(-> callCompiledObjectWithTimeout(result.parsedScript));
144  
      showScriptResult(result.result);
145  
    }
146  
  }
147  
  
148  
  void showScriptResult(OKOrError result) {
149  
    if (result.isOK())
150  
      setStatus(shorten(g22utils.stringify(result!)));
151  
    else
152  
      setStatus(exceptionToStringShorter_dontDropOuterExceptions(result.error()));
153  
  }
154  
155  
  class CompileResult {
156  
    S script;
157  
    GazelleV_LeftArrowScriptParser parser;
158  
    Throwable compileError;
159  
    GazelleV_LeftArrowScript.Script parsedScript;
160  
    OKOrError result;
161  
162  
    toString {
163  
      ret compileError != null ? exceptionToStringShorter_dontDropOuterExceptions(compileError) : "Compiled OK";
164  
    }
165  
    
166  
    bool runnable() { ret parsedScript != null; }
167  
  }
168  
  
169  
  void setStatus(S status) {
170  
    setText(lblCompileResult, status);
171  
  }
172  
173  
  swappable void modifyParser(GazelleV_LeftArrowScriptParser parser) {
174  
  }
175  
  
176  
  void showStatus(S status) {
177  
    setText(lblCompileResult, status);
178  
  }
179  
  
180  
  void showRuntimeError(Throwable e) {
181  
    showStatus(exceptionToStringShorter(e));
182  
  }
183  
  
184  
  void withResultPanel {
185  
    scpResult if null = singleComponentPanel();
186  
  }
187  
  
188  
  swappable VarContext makeVarContextForExecution() { ret new VarContext; }
189  
  
190  
  O callCompiledObjectWithTimeout(double timeoutSeconds default scriptTimeout, GazelleV_LeftArrowScript.Script object, VarContext ctx default makeVarContextForExecution()) {
191  
    O result = evalWithTimeoutOrTypedException(timeoutSeconds, -> object.get(ctx));
192  
    if (result cast BufferedImage)
193  
      scpResult?.set(jscroll_centered_borderless(g22utils.stdImageSurface(result)));
194  
    ret result;
195  
  }
196  
  
197  
  GazelleV_LeftArrowScriptParser makeParser() {
198  
    var parser = g22utils.leftArrowParser();
199  
    modifyParser(parser);
200  
    print("Function containers: " + parser.functionContainers);
201  
    ret parser;
202  
  }
203  
  
204  
  void showGlobalClassNames() {
205  
    showText("Global Class Names", pnlToString(toCIMap(makeParser().globalClassNames())));
206  
  }
207  
}

Author comment

Began life as a copy of #1034022

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): bhatertpkbcr, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034363
Snippet name: JLeftArrowScriptIDE (backup with two status bars)
Eternal ID of this version: #1034363/3
Text MD5: 1d62dac4df01b5ffef0d3d8fa33120f6
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-03 09:34:01
Source code size: 6607 bytes / 207 lines
Pitched / IR pitched: No / No
Views / Downloads: 140 / 159
Version history: 2 change(s)
Referenced in: [show references]