From 695bb7d2f1eb95a009daada7d6b9f2dbb7237cb0 Mon Sep 17 00:00:00 2001 From: mikera Date: Sun, 25 Aug 2024 11:12:15 +0100 Subject: [PATCH] REPL improvements --- .../src/main/java/convex/core/Result.java | 6 +++++ .../java/convex/gui/panels/REPLPanel.java | 22 +++++++++++++++---- .../src/main/java/convex/api/Convex.java | 11 +++++----- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/convex-core/src/main/java/convex/core/Result.java b/convex-core/src/main/java/convex/core/Result.java index a5da398c6..7d1cf20f1 100644 --- a/convex-core/src/main/java/convex/core/Result.java +++ b/convex-core/src/main/java/convex/core/Result.java @@ -168,6 +168,11 @@ public Result withInfo(Keyword k, ACell v) { return new Result(values.assoc(INFO_POS, info)); } + + public Result withSource(Keyword source) { + return withInfo(Keywords.SOURCE, source); + } + /** * Returns the log for this Result. May be an empty vector. * @@ -381,4 +386,5 @@ public static Result fromException(Throwable e) { + } diff --git a/convex-gui/src/main/java/convex/gui/panels/REPLPanel.java b/convex-gui/src/main/java/convex/gui/panels/REPLPanel.java index 7903aa7bc..7ae9ac24c 100644 --- a/convex-gui/src/main/java/convex/gui/panels/REPLPanel.java +++ b/convex-gui/src/main/java/convex/gui/panels/REPLPanel.java @@ -46,6 +46,7 @@ import convex.core.transactions.ATransaction; import convex.core.transactions.Invoke; import convex.core.util.Utils; +import convex.gui.components.ActionButton; import convex.gui.components.ActionPanel; import convex.gui.components.BaseTextPane; import convex.gui.components.CodePane; @@ -61,6 +62,7 @@ public class REPLPanel extends JPanel { protected final CodePane input; protected final CodePane output; + private final JButton btnRun; private final JButton btnClear; private final JButton btnInfo; private final JCheckBox btnResults; @@ -186,10 +188,17 @@ public REPLPanel(Convex convex) { actionPanel = new ActionPanel(); add(actionPanel, "dock south"); + btnRun = new ActionButton("Run",0xe1c4,e -> { + sendMessage(input.getText()); + input.requestFocus(); + }); + actionPanel.add(btnRun); + btnClear = new JButton("Clear"); actionPanel.add(btnClear); btnClear.addActionListener(e -> { output.setText(""); + input.requestFocus(); }); btnInfo = new JButton("Connection Info"); @@ -253,14 +262,14 @@ private Address getAddress() { private void sendMessage(String s) { if (s.isBlank()) return; + output.append(s); + output.append("\n"); history.add(s); historyPosition=history.size(); SwingUtilities.invokeLater(() -> { input.setText(""); - output.append(s); - output.append("\n"); try { AList forms = Reader.readAll(s); ACell code = (forms.count()==1)?forms.get(0):forms.cons(Symbols.DO); @@ -302,12 +311,17 @@ private void sendMessage(String s) { } log.trace("Sent message"); - handleResult(start,future.get(5000, TimeUnit.MILLISECONDS)); + handleResult(start,future.get(3000, TimeUnit.MILLISECONDS)); } catch (ParseException e) { output.append(" PARSE ERROR: "+e.getMessage(),Color.RED); } catch (TimeoutException t) { - output.append(" TIMEOUT waiting for result",Color.RED); + output.append(" TIMEOUT waiting for result\n",Color.RED); + } catch (IllegalStateException t) { + // General errors we understand + output.append(" ERROR: ",Color.RED); + output.append(t.getMessage() + "\n"); } catch (Exception t) { + // Something bad..... output.append(" ERROR: ",Color.RED); output.append(t.getMessage() + "\n"); t.printStackTrace(); diff --git a/convex-peer/src/main/java/convex/api/Convex.java b/convex-peer/src/main/java/convex/api/Convex.java index b563e94de..9df86a34b 100644 --- a/convex-peer/src/main/java/convex/api/Convex.java +++ b/convex-peer/src/main/java/convex/api/Convex.java @@ -15,6 +15,7 @@ import convex.core.ErrorCodes; import convex.core.Result; +import convex.core.SourceCodes; import convex.core.State; import convex.core.crypto.AKeyPair; import convex.core.data.ABlob; @@ -265,7 +266,7 @@ public long getSequence(Address addr) throws TimeoutException, IOException { public long lookupSequence(Address origin) throws IOException, TimeoutException { AOp code= Special.forSymbol(Symbols.STAR_SEQUENCE); Result r= querySync(code,origin); - if (r.isError()) throw new RuntimeException("Error trying to get sequence number: "+r); + if (r.isError()) throw new IOException("Error trying to get sequence number: "+r); ACell rv=r.getValue(); if (!(rv instanceof CVMLong)) throw new RuntimeException("Unexpected sequence result type: "+Utils.getClassName(rv)); long seq=((CVMLong)rv).longValue(); @@ -371,7 +372,7 @@ private synchronized long getNextSequence(ATransaction t) throws IOException, Ti * @throws IOException If an IO Exception occurs (most likely the connection is broken) * @throws TimeoutException In case of timeout */ - public final synchronized CompletableFuture transact(ATransaction transaction) throws IOException, TimeoutException { + public final synchronized CompletableFuture transact(ATransaction transaction) throws TimeoutException, IOException { SignedData signed = prepareTransaction(transaction); CompletableFuture r= transact(signed); return r; @@ -388,7 +389,7 @@ public final synchronized CompletableFuture transact(ATransaction transa * @throws IOException If an IO Exception occurs (most likely the connection is broken) * @throws TimeoutException In case of timeout */ - public SignedData prepareTransaction(ATransaction transaction) throws TimeoutException, IOException { + public SignedData prepareTransaction(ATransaction transaction) throws IOException, TimeoutException { Address origin=transaction.getOrigin(); if (origin == null) { origin=address; @@ -496,7 +497,7 @@ public synchronized Result transactSync(String code) throws IOException, Timeout * @return A Future for the result of the transaction * @throws IOException If the connection is broken or send buffer is full */ - public abstract CompletableFuture transact(SignedData signed) throws IOException; + public abstract CompletableFuture transact(SignedData signed); /** * Submits a transfer transaction to the Convex network, returning a future once @@ -587,7 +588,7 @@ public synchronized Result transactSync(ATransaction transaction, long timeout) return Result.fromException(e.getCause()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); // set interrupt flag since an interruption has occurred - return Result.error(ErrorCodes.INTERRUPTED, "Transaction interrupted while awaiting result"); + return Result.error(ErrorCodes.INTERRUPTED, "Transaction interrupted while awaiting result").withSource(SourceCodes.CLIENT); } }