Skip to content

Commit bbabb97

Browse files
committed
Improvements in notebook usability
- Added opening notebooks in context of a project - Also added change project context button in the notebook toolbar - Added few more tests - Added localization in notebooks - Fixed jshell flow and updated open jshell label - Other cleanup and fixes - updated netbeans patch - cleanup of some unused code and added license header to missing files - formatted files and removed unused imports - updated ajv to latest version: 8.17.1 - updated artifactory urls - fixed the execution status issue - removed console.log and replaced with standard extension logger
1 parent bba1c43 commit bbabb97

35 files changed

+849
-274
lines changed

build.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
patches/dev-dependency-licenses.diff
8080
patches/nb-telemetry.diff
8181
patches/change-method-parameters-refactoring-qualified-names.diff
82-
patches/javavscode-375.diff
8382
patches/upgrade-lsp4j.diff
8483
patches/java-notebooks.diff
8584
</string>

nbcode/notebooks/src/org/netbeans/modules/nbcode/java/notebook/CellState.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ protected CompletableFuture<CellStateResponse> requestLatestCellState() {
144144
}
145145
return client.getNotebookCellState(new NotebookCellStateParams(notebookUri, cellUri));
146146
}
147-
148-
protected VersionAwareContent getVersionAwareContent(){
147+
148+
protected VersionAwareContent getVersionAwareContent() {
149149
return this.content.get();
150150
}
151151

nbcode/notebooks/src/org/netbeans/modules/nbcode/java/notebook/JshellStreamsHandler.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.netbeans.modules.nbcode.java.notebook;
1717

18-
import java.io.ByteArrayOutputStream;
1918
import java.io.IOException;
2019
import java.io.InputStream;
2120
import java.io.PrintStream;
@@ -26,65 +25,66 @@
2625

2726
/**
2827
* Handles JShell output and error streams for notebook execution.
29-
*
28+
*
3029
* @author atalati
3130
*/
3231
public class JshellStreamsHandler implements AutoCloseable {
32+
3333
private static final Logger LOG = Logger.getLogger(JshellStreamsHandler.class.getName());
3434
private final String notebookId;
3535
private final StreamingOutputStream outStream;
3636
private final StreamingOutputStream errStream;
3737
private final PrintStream printOutStream;
3838
private final PrintStream printErrStream;
3939
private final InputStream inputStream;
40-
40+
4141
public JshellStreamsHandler(String notebookId, BiConsumer<String, byte[]> streamCallback) {
4242
this(notebookId, streamCallback, streamCallback);
4343
}
44-
45-
public JshellStreamsHandler(String notebookId,
46-
BiConsumer<String, byte[]> outStreamCallback,
47-
BiConsumer<String, byte[]> errStreamCallback) {
44+
45+
public JshellStreamsHandler(String notebookId,
46+
BiConsumer<String, byte[]> outStreamCallback,
47+
BiConsumer<String, byte[]> errStreamCallback) {
4848
if (notebookId == null || notebookId.trim().isEmpty()) {
4949
throw new IllegalArgumentException("Notebook Id cannot be null or empty");
5050
}
51-
51+
5252
this.notebookId = notebookId;
5353
this.outStream = new StreamingOutputStream(createCallback(outStreamCallback));
5454
this.errStream = new StreamingOutputStream(createCallback(errStreamCallback));
5555
this.printOutStream = new PrintStream(outStream);
5656
this.printErrStream = new PrintStream(errStream);
5757
this.inputStream = new CustomInputStream(LanguageClientInstance.getInstance().getClient());
5858
}
59-
59+
6060
private Consumer<byte[]> createCallback(BiConsumer<String, byte[]> callback) {
6161
return callback != null ? output -> callback.accept(notebookId, output) : null;
6262
}
63-
63+
6464
public void setOutStreamCallback(BiConsumer<String, byte[]> callback) {
6565
outStream.setCallback(createCallback(callback));
6666
}
67-
67+
6868
public void setErrStreamCallback(BiConsumer<String, byte[]> callback) {
6969
errStream.setCallback(createCallback(callback));
7070
}
71-
71+
7272
public PrintStream getPrintOutStream() {
7373
return printOutStream;
7474
}
75-
75+
7676
public PrintStream getPrintErrStream() {
7777
return printErrStream;
7878
}
79-
79+
8080
public InputStream getInputStream() {
8181
return inputStream;
8282
}
83-
83+
8484
public String getNotebookId() {
8585
return notebookId;
8686
}
87-
87+
8888
public void flushOutputStreams() {
8989
try {
9090
outStream.flush();
@@ -93,7 +93,7 @@ public void flushOutputStreams() {
9393
// nothing can be done
9494
}
9595
}
96-
96+
9797
@Override
9898
public void close() {
9999
try {
@@ -106,4 +106,4 @@ public void close() {
106106
LOG.log(Level.WARNING, "IOException occurred while closing the streams {0}", ex.getMessage());
107107
}
108108
}
109-
}
109+
}

nbcode/notebooks/src/org/netbeans/modules/nbcode/java/notebook/LanguageClientInstance.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* @author atalati
2424
*/
2525
public class LanguageClientInstance {
26+
2627
private WeakReference<NbCodeLanguageClient> client = null;
2728

2829
private LanguageClientInstance() {
@@ -31,17 +32,17 @@ private LanguageClientInstance() {
3132
public static LanguageClientInstance getInstance() {
3233
return LanguageClientInstance.Singleton.instance;
3334
}
34-
35+
3536
private static class Singleton {
3637

3738
private static final LanguageClientInstance instance = new LanguageClientInstance();
3839
}
39-
40-
public NbCodeLanguageClient getClient(){
41-
return this.client == null ? null: this.client.get();
40+
41+
public NbCodeLanguageClient getClient() {
42+
return this.client == null ? null : this.client.get();
4243
}
43-
44-
public void setClient(NbCodeLanguageClient client){
44+
45+
public void setClient(NbCodeLanguageClient client) {
4546
this.client = new WeakReference<>(client);
4647
}
4748
}

nbcode/notebooks/src/org/netbeans/modules/nbcode/java/notebook/NotebookCommandsHandler.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public class NotebookCommandsHandler implements CommandProvider {
3434
private static final String NBLS_JSHELL_EXEC = "nbls.jshell.execute.cell";
3535
private static final String NBLS_JSHELL_INTERRUPT = "nbls.jshell.interrupt.cell";
3636
private static final String NBLS_OPEN_PROJECT_JSHELL = "nbls.jshell.project.open";
37-
private static final Set<String> COMMANDS = new HashSet<>(Arrays.asList(NBLS_JSHELL_EXEC, NBLS_OPEN_PROJECT_JSHELL, NBLS_JSHELL_INTERRUPT));
37+
private static final String NBLS_NOTEBOOK_PROJECT_MAPPING = "nbls.notebook.project.context";
38+
private static final Set<String> COMMANDS = new HashSet<>(Arrays.asList(NBLS_JSHELL_EXEC, NBLS_OPEN_PROJECT_JSHELL, NBLS_JSHELL_INTERRUPT, NBLS_NOTEBOOK_PROJECT_MAPPING));
3839

3940
@Override
4041
public Set<String> getCommands() {
@@ -47,11 +48,13 @@ public CompletableFuture<Object> runCommand(String command, List<Object> argumen
4748

4849
switch (command) {
4950
case NBLS_JSHELL_EXEC:
50-
return CodeEval.getInstance().evaluate(arguments).thenApply(list -> (Object)list);
51+
return CodeEval.getInstance().evaluate(arguments).thenApply(list -> (Object) list);
5152
case NBLS_JSHELL_INTERRUPT:
5253
return CompletableFuture.completedFuture(CodeEval.getInstance().interrupt(arguments));
5354
case NBLS_OPEN_PROJECT_JSHELL:
5455
return CommandHandler.openJshellInProjectContext(arguments).thenApply(list -> (Object) list);
56+
case NBLS_NOTEBOOK_PROJECT_MAPPING:
57+
return CommandHandler.getNotebookProjectMappingPath(arguments).thenApply(prj -> (Object) prj);
5558
default:
5659
return CompletableFuture.failedFuture(new UnsupportedOperationException("Command not supported: " + command));
5760
}

nbcode/notebooks/src/org/netbeans/modules/nbcode/java/notebook/NotebookConfigs.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,17 @@
3333
*/
3434
public class NotebookConfigs {
3535

36-
private static final String[] NOTEBOOK_CONFIG_LABELS = {"notebook.classpath", "notebook.modulepath", "notebook.addmodules", "notebook.enablePreview", "notebook.implicitImports"};
36+
private static final String[] NOTEBOOK_CONFIG_LABELS = {"notebook.classpath",
37+
"notebook.modulepath",
38+
"notebook.addmodules",
39+
"notebook.enablePreview",
40+
"notebook.implicitImports",
41+
"notebook.projects.mapping"};
3742
private String classPath = null;
3843
private String modulePath = null;
3944
private String addModules = null;
4045
private boolean enablePreview = false;
46+
private JsonObject notebookProjectMapping = new JsonObject();
4147
private List<String> implicitImports = null;
4248
private CompletableFuture<Void> initialized;
4349

@@ -65,6 +71,10 @@ public List<String> getImplicitImports() {
6571
return implicitImports;
6672
}
6773

74+
public JsonObject getNotebookProjectMapping() {
75+
return notebookProjectMapping;
76+
}
77+
6878
private NotebookConfigs() {
6979

7080
}
@@ -121,6 +131,10 @@ private CompletableFuture<Void> initializeConfigs() throws InterruptedException,
121131
implicitImports = ((JsonArray) c.get(4)).asList().stream().map((elem) -> elem.getAsString()).toList();
122132

123133
}
134+
if (c.get(5) != null) {
135+
notebookProjectMapping = (JsonObject) c.get(5);
136+
137+
}
124138
}
125139
});
126140

nbcode/notebooks/src/org/netbeans/modules/nbcode/java/notebook/NotebookDocumentServiceHandlerImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,16 @@ public class NotebookDocumentServiceHandlerImpl implements NotebookDocumentServi
8585
public void didOpen(DidOpenNotebookDocumentParams params) {
8686
try {
8787
NbCodeLanguageClient client = LanguageClientInstance.getInstance().getClient();
88-
if(client == null){
88+
if (client == null) {
8989
return;
9090
}
91-
client.showStatusBarMessage(new ShowStatusMessageParams(MessageType.Info,"Intializing Java kernel for notebook."));
92-
NotebookSessionManager.getInstance().createSession(params.getNotebookDocument()).whenComplete((JShell jshell,Throwable t) -> {
91+
client.showStatusBarMessage(new ShowStatusMessageParams(MessageType.Info, "Intializing Java kernel for notebook."));
92+
NotebookSessionManager.getInstance().createSession(params.getNotebookDocument()).whenComplete((JShell jshell, Throwable t) -> {
9393
if (t == null) {
9494
client.showStatusBarMessage(new ShowStatusMessageParams(MessageType.Info, "Java kernel initialized successfully"));
9595
} else {
9696
// if package import fails user is not informed ?
97-
client.showMessage(new MessageParams(MessageType.Error,"Error could not initialize Java kernel for the notebook."));
97+
client.showMessage(new MessageParams(MessageType.Error, "Error could not initialize Java kernel for the notebook."));
9898
LOG.log(Level.SEVERE, "Error could not initialize Java kernel for the notebook. : {0}", t.getMessage());
9999
}
100100
});

nbcode/notebooks/src/org/netbeans/modules/nbcode/java/notebook/NotebookDocumentStateManager.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private void updateNotebookCellContent(NotebookDocumentChangeEventCellTextConten
150150
}
151151
int newVersion = contentChange.getDocument().getVersion();
152152
String currentContent = cellState.getContent();
153-
153+
154154
try {
155155
String updatedContent = applyContentChanges(currentContent, contentChange.getChanges());
156156
cellState.setContent(updatedContent, newVersion);
@@ -220,6 +220,7 @@ private String applyRangeChange(String content, TextDocumentContentChangeEvent c
220220

221221
return result.toString();
222222
}
223+
223224
// protected methods for ease of unit testing
224225
protected void addNewCellState(NotebookCell cell, TextDocumentItem item) {
225226
if (cell == null || item == null) {
@@ -236,8 +237,8 @@ protected void addNewCellState(NotebookCell cell, TextDocumentItem item) {
236237
throw new RuntimeException("Failed to create cell state", e);
237238
}
238239
}
239-
240-
protected Map<String, CellState> getCellsMap(){
240+
241+
protected Map<String, CellState> getCellsMap() {
241242
return cellsMap;
242243
}
243244
}

0 commit comments

Comments
 (0)