Skip to content

Commit 695bb7d

Browse files
committed
REPL improvements
1 parent 8a6bbe5 commit 695bb7d

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

convex-core/src/main/java/convex/core/Result.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ public Result withInfo(Keyword k, ACell v) {
168168
return new Result(values.assoc(INFO_POS, info));
169169
}
170170

171+
172+
public Result withSource(Keyword source) {
173+
return withInfo(Keywords.SOURCE, source);
174+
}
175+
171176
/**
172177
* Returns the log for this Result. May be an empty vector.
173178
*
@@ -381,4 +386,5 @@ public static Result fromException(Throwable e) {
381386

382387

383388

389+
384390
}

convex-gui/src/main/java/convex/gui/panels/REPLPanel.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import convex.core.transactions.ATransaction;
4747
import convex.core.transactions.Invoke;
4848
import convex.core.util.Utils;
49+
import convex.gui.components.ActionButton;
4950
import convex.gui.components.ActionPanel;
5051
import convex.gui.components.BaseTextPane;
5152
import convex.gui.components.CodePane;
@@ -61,6 +62,7 @@ public class REPLPanel extends JPanel {
6162

6263
protected final CodePane input;
6364
protected final CodePane output;
65+
private final JButton btnRun;
6466
private final JButton btnClear;
6567
private final JButton btnInfo;
6668
private final JCheckBox btnResults;
@@ -186,10 +188,17 @@ public REPLPanel(Convex convex) {
186188
actionPanel = new ActionPanel();
187189
add(actionPanel, "dock south");
188190

191+
btnRun = new ActionButton("Run",0xe1c4,e -> {
192+
sendMessage(input.getText());
193+
input.requestFocus();
194+
});
195+
actionPanel.add(btnRun);
196+
189197
btnClear = new JButton("Clear");
190198
actionPanel.add(btnClear);
191199
btnClear.addActionListener(e -> {
192200
output.setText("");
201+
input.requestFocus();
193202
});
194203

195204
btnInfo = new JButton("Connection Info");
@@ -253,14 +262,14 @@ private Address getAddress() {
253262

254263
private void sendMessage(String s) {
255264
if (s.isBlank()) return;
265+
output.append(s);
266+
output.append("\n");
256267

257268
history.add(s);
258269
historyPosition=history.size();
259270

260271
SwingUtilities.invokeLater(() -> {
261272
input.setText("");
262-
output.append(s);
263-
output.append("\n");
264273
try {
265274
AList<ACell> forms = Reader.readAll(s);
266275
ACell code = (forms.count()==1)?forms.get(0):forms.cons(Symbols.DO);
@@ -302,12 +311,17 @@ private void sendMessage(String s) {
302311
}
303312
log.trace("Sent message");
304313

305-
handleResult(start,future.get(5000, TimeUnit.MILLISECONDS));
314+
handleResult(start,future.get(3000, TimeUnit.MILLISECONDS));
306315
} catch (ParseException e) {
307316
output.append(" PARSE ERROR: "+e.getMessage(),Color.RED);
308317
} catch (TimeoutException t) {
309-
output.append(" TIMEOUT waiting for result",Color.RED);
318+
output.append(" TIMEOUT waiting for result\n",Color.RED);
319+
} catch (IllegalStateException t) {
320+
// General errors we understand
321+
output.append(" ERROR: ",Color.RED);
322+
output.append(t.getMessage() + "\n");
310323
} catch (Exception t) {
324+
// Something bad.....
311325
output.append(" ERROR: ",Color.RED);
312326
output.append(t.getMessage() + "\n");
313327
t.printStackTrace();

convex-peer/src/main/java/convex/api/Convex.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import convex.core.ErrorCodes;
1717
import convex.core.Result;
18+
import convex.core.SourceCodes;
1819
import convex.core.State;
1920
import convex.core.crypto.AKeyPair;
2021
import convex.core.data.ABlob;
@@ -265,7 +266,7 @@ public long getSequence(Address addr) throws TimeoutException, IOException {
265266
public long lookupSequence(Address origin) throws IOException, TimeoutException {
266267
AOp<ACell> code= Special.forSymbol(Symbols.STAR_SEQUENCE);
267268
Result r= querySync(code,origin);
268-
if (r.isError()) throw new RuntimeException("Error trying to get sequence number: "+r);
269+
if (r.isError()) throw new IOException("Error trying to get sequence number: "+r);
269270
ACell rv=r.getValue();
270271
if (!(rv instanceof CVMLong)) throw new RuntimeException("Unexpected sequence result type: "+Utils.getClassName(rv));
271272
long seq=((CVMLong)rv).longValue();
@@ -371,7 +372,7 @@ private synchronized long getNextSequence(ATransaction t) throws IOException, Ti
371372
* @throws IOException If an IO Exception occurs (most likely the connection is broken)
372373
* @throws TimeoutException In case of timeout
373374
*/
374-
public final synchronized CompletableFuture<Result> transact(ATransaction transaction) throws IOException, TimeoutException {
375+
public final synchronized CompletableFuture<Result> transact(ATransaction transaction) throws TimeoutException, IOException {
375376
SignedData<ATransaction> signed = prepareTransaction(transaction);
376377
CompletableFuture<Result> r= transact(signed);
377378
return r;
@@ -388,7 +389,7 @@ public final synchronized CompletableFuture<Result> transact(ATransaction transa
388389
* @throws IOException If an IO Exception occurs (most likely the connection is broken)
389390
* @throws TimeoutException In case of timeout
390391
*/
391-
public SignedData<ATransaction> prepareTransaction(ATransaction transaction) throws TimeoutException, IOException {
392+
public SignedData<ATransaction> prepareTransaction(ATransaction transaction) throws IOException, TimeoutException {
392393
Address origin=transaction.getOrigin();
393394
if (origin == null) {
394395
origin=address;
@@ -496,7 +497,7 @@ public synchronized Result transactSync(String code) throws IOException, Timeout
496497
* @return A Future for the result of the transaction
497498
* @throws IOException If the connection is broken or send buffer is full
498499
*/
499-
public abstract CompletableFuture<Result> transact(SignedData<ATransaction> signed) throws IOException;
500+
public abstract CompletableFuture<Result> transact(SignedData<ATransaction> signed);
500501

501502
/**
502503
* Submits a transfer transaction to the Convex network, returning a future once
@@ -587,7 +588,7 @@ public synchronized Result transactSync(ATransaction transaction, long timeout)
587588
return Result.fromException(e.getCause());
588589
} catch (InterruptedException e) {
589590
Thread.currentThread().interrupt(); // set interrupt flag since an interruption has occurred
590-
return Result.error(ErrorCodes.INTERRUPTED, "Transaction interrupted while awaiting result");
591+
return Result.error(ErrorCodes.INTERRUPTED, "Transaction interrupted while awaiting result").withSource(SourceCodes.CLIENT);
591592
}
592593
}
593594

0 commit comments

Comments
 (0)