Skip to content

Commit 9f58b53

Browse files
committed
addressed review comments
1 parent 00c5e51 commit 9f58b53

File tree

14 files changed

+135
-62
lines changed

14 files changed

+135
-62
lines changed

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,19 @@ public CompletableFuture<Either<List<CompletionItem>, CompletionList>> getCodeCo
6262
}
6363

6464
SourceCodeAnalysis sourceCodeAnalysis = instance.sourceCodeAnalysis();
65-
String textToComplete = getTextToComplete(
65+
List<Suggestion> suggestions = getSuggestions(
6666
params.getTextDocument().getUri(),
6767
params.getPosition(),
6868
state,
6969
sourceCodeAnalysis
7070
);
7171

72-
List<Suggestion> suggestions = sourceCodeAnalysis.completionSuggestions(
73-
textToComplete,
74-
textToComplete.length(),
75-
new int[1]
76-
);
77-
7872
List<CompletionItem> completionItems = new ArrayList<>();
7973
HashSet<String> visited = new HashSet<>();
8074

8175
for (Suggestion suggestion : suggestions) {
8276
String continuation = suggestion.continuation();
83-
if(visited.add(continuation)){
77+
if (visited.add(continuation)) {
8478
completionItems.add(createCompletionItem(continuation));
8579
}
8680
}
@@ -101,14 +95,28 @@ private CompletionItem createCompletionItem(String label) {
10195
return item;
10296
}
10397

104-
private String getTextToComplete(String uri, Position position, NotebookDocumentStateManager state, SourceCodeAnalysis sourceCodeAnalysis) {
98+
private List<Suggestion> getSuggestions(String uri, Position position, NotebookDocumentStateManager state, SourceCodeAnalysis sourceCodeAnalysis) {
10599
CellState cellState = state.getCell(uri);
106100
String content = cellState.getContent();
107101
int cursorOffset = NotebookUtils.getOffset(content, position);
108-
102+
int[] anchor = new int[1];
109103
String offsetText = content.substring(0, cursorOffset);
110104
List<String> snippets = NotebookUtils.getCodeSnippets(sourceCodeAnalysis, offsetText);
111-
// TODO: Code completion only considers the last incomplete snippet, missing context from earlier snippets which are not executed.
112-
return snippets.isEmpty() ? "" : snippets.getLast();
105+
106+
String lastSnippet = snippets.isEmpty() ? "" : snippets.getLast();
107+
List<Suggestion> suggestions = new ArrayList<>();
108+
suggestions.addAll(sourceCodeAnalysis.completionSuggestions(
109+
lastSnippet,
110+
lastSnippet.length(),
111+
anchor
112+
));
113+
if (snippets.size() > 1) {
114+
suggestions.addAll(sourceCodeAnalysis.completionSuggestions(
115+
offsetText,
116+
offsetText.length(),
117+
anchor
118+
));
119+
}
120+
return suggestions;
113121
}
114122
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,20 @@
3636
import org.netbeans.modules.java.lsp.server.notebook.NotebookCellExecutionProgressResultParams.Builder;
3737
import org.netbeans.modules.java.lsp.server.notebook.NotebookCellExecutionProgressResultParams.EXECUTION_STATUS;
3838
import org.netbeans.modules.java.lsp.server.protocol.NbCodeLanguageClient;
39+
import org.openide.util.NbBundle;
3940
import org.openide.util.RequestProcessor;
4041

4142
/**
4243
*
4344
* @author atalati
4445
*/
46+
@NbBundle.Messages({"InterruptCodeCell.exec.success=Code execution stopped successfully",
47+
"InterruptCodeCell.info.msg=Code execution stopped successfully"})
4548
public class CodeEval {
4649

4750
private static final Logger LOG = Logger.getLogger(CodeEval.class.getName());
48-
private static final String CODE_EXEC_INTERRUPT_SUCCESS_MESSAGE = "Code execution stopped successfully";
49-
private static final String CODE_EXEC_INTERRUPTED_MESSAGE = "Code execution was interrupted";
51+
private static final String CODE_EXEC_INTERRUPT_SUCCESS_MESSAGE = Bundle.InterruptCodeCell_exec_success();
52+
private static final String CODE_EXEC_INTERRUPTED_MESSAGE = Bundle.InterruptCodeCell_info_msg();
5053
private static final Pattern LINEBREAK = Pattern.compile("\\R");
5154

5255
private final Map<String, RequestProcessor> codeExecMap = new ConcurrentHashMap<>();

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@
2626
import java.util.logging.Logger;
2727
import org.netbeans.modules.java.lsp.server.input.ShowInputBoxParams;
2828
import org.netbeans.modules.java.lsp.server.protocol.NbCodeLanguageClient;
29+
import org.openide.util.NbBundle;
2930

3031
/**
3132
*
3233
* @author atalati
3334
*/
35+
@NbBundle.Messages({"GetUserInput.msg=Please provide scanner input here"})
3436
public class CustomInputStream extends InputStream {
3537

3638
private static final Logger LOG = Logger.getLogger(CustomInputStream.class.getName());
3739
private ByteArrayInputStream currentStream;
3840
private final WeakReference<NbCodeLanguageClient> client;
39-
private static final String USER_PROMPT_REQUEST = "Please provide scanner input here";
41+
private static final String USER_PROMPT_REQUEST = Bundle.GetUserInput_msg();
4042

4143
public CustomInputStream(NbCodeLanguageClient client) {
4244
this.client = new WeakReference<>(client);

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,17 @@
7272
import org.eclipse.lsp4j.jsonrpc.messages.Either;
7373
import org.eclipse.lsp4j.jsonrpc.messages.Either3;
7474
import org.netbeans.modules.java.lsp.server.protocol.ShowStatusMessageParams;
75+
import org.openide.util.NbBundle;
7576
import org.openide.util.lookup.ServiceProvider;
7677

7778
/**
7879
*
7980
* @author atalati
8081
*/
8182
@ServiceProvider(service = NotebookDocumentServiceHandler.class)
83+
@NbBundle.Messages({"kernel.initializing=Intializing Java kernel for notebook.",
84+
"kernel.initialize.success=Java kernel initialized successfully.",
85+
"kernel.initialize.failed=Error could not initialize Java kernel for the notebook."})
8286
public class NotebookDocumentServiceHandlerImpl implements NotebookDocumentServiceHandler {
8387

8488
private static final Logger LOG = Logger.getLogger(NotebookDocumentServiceHandler.class.getName());
@@ -93,13 +97,13 @@ public void didOpen(DidOpenNotebookDocumentParams params) {
9397
if (client == null) {
9498
return;
9599
}
96-
client.showStatusBarMessage(new ShowStatusMessageParams(MessageType.Info, "Intializing Java kernel for notebook."));
100+
client.showStatusBarMessage(new ShowStatusMessageParams(MessageType.Info, Bundle.kernel_initializing()));
97101
NotebookSessionManager.getInstance().createSession(params.getNotebookDocument()).whenComplete((JShell jshell, Throwable t) -> {
98102
if (t == null) {
99-
client.showStatusBarMessage(new ShowStatusMessageParams(MessageType.Info, "Java kernel initialized successfully"));
103+
client.showStatusBarMessage(new ShowStatusMessageParams(MessageType.Info, Bundle.kernel_initialize_success()));
100104
} else {
101105
// if package import fails user is not informed ?
102-
client.showMessage(new MessageParams(MessageType.Error, "Error could not initialize Java kernel for the notebook."));
106+
client.showMessage(new MessageParams(MessageType.Error, Bundle.kernel_initialize_failed()));
103107
LOG.log(Level.SEVERE, "Error could not initialize Java kernel for the notebook. : {0}", t.getMessage());
104108
}
105109
});
@@ -130,12 +134,7 @@ public void didClose(DidCloseNotebookDocumentParams params) {
130134
String notebookUri = params.getNotebookDocument().getUri();
131135
NotebookSessionManager.getInstance().closeSession(notebookUri);
132136
notebookStateMap.remove(notebookUri);
133-
134-
// notebookCellMap.forEach((k,v) ->{
135-
// if(params.getNotebookDocument().getUri().equals(v)){
136-
// notebookCellMap.remove(k);
137-
// }
138-
// });
137+
notebookCellMap.entrySet().removeIf(entry -> notebookUri.equals(entry.getValue()));
139138
}
140139

141140
@Override
@@ -252,5 +251,5 @@ public CompletableFuture<List<InlayHint>> inlayHint(InlayHintParams params) {
252251
@Override
253252
public CompletableFuture<List<InlineValue>> inlineValue(InlineValueParams params) {
254253
return CompletableFuture.completedFuture(new ArrayList<>());
255-
}
254+
}
256255
}

nbcode/notebooks/src/org/netbeans/modules/nbcode/java/project/ProjectContext.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,16 @@
3333
import org.openide.filesystems.FileObject;
3434
import org.openide.util.Exceptions;
3535
import org.openide.util.Lookup;
36+
import org.openide.util.NbBundle;
3637

3738
/**
3839
*
3940
* @author atalati
4041
*/
42+
@NbBundle.Messages({"Select.project.title=Select Project",
43+
"CurrentProjectContext.title=Current project context: ",
44+
"NoProjectFound.msg=No projects found",
45+
"NoProjectContextFound.msg=No project context"})
4146
public class ProjectContext {
4247

4348
public static Project getProject(String uri) {
@@ -87,7 +92,7 @@ private static CompletableFuture<List<Project>> selectFromMultipleProjects(Proje
8792
if (client == null) {
8893
return CompletableFuture.completedFuture(new ArrayList<>());
8994
}
90-
String title = "Select Project";
95+
String title = Bundle.Select_project_title();
9196
List<QuickPickItem> items = new ArrayList<>();
9297
Map<String, Project> prjMap = new HashMap<>();
9398
for (Project prj : prjs) {
@@ -96,8 +101,8 @@ private static CompletableFuture<List<Project>> selectFromMultipleProjects(Proje
96101
prjMap.put(displayName, prj);
97102
items.add(item);
98103
}
99-
String placeholder = defaultPrjSelected != null ? "Current project context: " + defaultPrjSelected.getName()
100-
: items.isEmpty() ? "No projects found" : "No project context";
104+
String placeholder = defaultPrjSelected != null ? Bundle.CurrentProjectContext_title() + defaultPrjSelected.getName()
105+
: items.isEmpty() ? Bundle.NoProjectFound_msg() : Bundle.NoProjectContextFound_msg();
101106

102107
ShowQuickPickParams params = new ShowQuickPickParams(title, placeholder, false, items);
103108
return client.showQuickPick(params).thenApply(selected -> {

vscode/l10n/bundle.l10n.en.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,19 @@
110110
"jdk.notebook.create.error_msg.failed": "Failed to create a notebook",
111111
"jdk.jshell.open.error_msg.failed": "Some error occurred while launching Jshell",
112112
"jdk.notebook.project.mapping.error_msg.failed": "An error occurred while changing the project context of the notebook",
113-
"jdk.notebook.create.new.notebook.input.name": "Enter new Java notebook file name"
113+
"jdk.notebook.create.new.notebook.input.name": "Enter new Java notebook file name",
114+
"jdk.notebook.parsing.empty.file.error_msg.title": "Empty Notebook",
115+
"jdk.notebook.parsing.empty.file.error_msg.desc": "The notebook file appears to be empty.",
116+
"jdk.notebook.parsing.error_msg.title": "Error Opening Notebook",
117+
"jdk.notebook.parsing.error_msg.desc": "Failed to open notebook: {message}",
118+
"jdk.notebook.parsing.invalid.structure.error_msg.title": "Invalid Notebook Structure",
119+
"jdk.notebook.parsing.invalid.structure.error_msg.desc": "Missing or invalid `cells` array.",
120+
"jdk.notebook.cell.parsing.error_msg.title": "Cell parsing error",
121+
"jdk.notebook.cell.type.error_msg": "Invalid cell type: {cellType}",
122+
"jdk.notebook.cell.missing.error_msg": "Missing field: {fieldName}",
123+
"jdk.notebook.serializer.error_msg": "Failed to serialize notebook: {errorMessage}",
124+
"jdk.notebook.cell.serializer.error_msg": "Failed to serialize one or more cells",
125+
"jdk.notebook.validation.failed.error_msg": "Notebook JSON validation failed",
126+
"jdk.notebook.mime_type.not.found.cell.output": "Mime Type: ${mimeType}, Content Length: {contentLength}",
127+
"jdk.notebook.cell.language.not.found": "Doesn't support ${languageId} execution"
114128
}

vscode/l10n/bundle.l10n.ja.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,19 @@
110110
"jdk.notebook.create.error_msg.failed": "Failed to create a notebook",
111111
"jdk.jshell.open.error_msg.failed": "Some error occurred while launching Jshell",
112112
"jdk.notebook.project.mapping.error_msg.failed": "An error occurred while changing the project context of the notebook",
113-
"jdk.notebook.create.new.notebook.input.name": "Enter new Java notebook file name"
113+
"jdk.notebook.create.new.notebook.input.name": "Enter new Java notebook file name",
114+
"jdk.notebook.parsing.empty.file.error_msg.title": "Empty Notebook",
115+
"jdk.notebook.parsing.empty.file.error_msg.desc": "The notebook file appears to be empty.",
116+
"jdk.notebook.parsing.error_msg.title": "Error Opening Notebook",
117+
"jdk.notebook.parsing.error_msg.desc": "Failed to open notebook: {message}",
118+
"jdk.notebook.parsing.invalid.structure.error_msg.title": "Invalid Notebook Structure",
119+
"jdk.notebook.parsing.invalid.structure.error_msg.desc": "Missing or invalid `cells` array.",
120+
"jdk.notebook.cell.parsing.error_msg.title": "Cell parsing error",
121+
"jdk.notebook.cell.type.error_msg": "Invalid cell type: {cellType}",
122+
"jdk.notebook.cell.missing.error_msg": "Missing field: {fieldName}",
123+
"jdk.notebook.serializer.error_msg": "Failed to serialize notebook: {errorMessage}",
124+
"jdk.notebook.cell.serializer.error_msg": "Failed to serialize one or more cells",
125+
"jdk.notebook.validation.failed.error_msg": "Notebook JSON validation failed",
126+
"jdk.notebook.mime_type.not.found.cell.output": "Mime Type: ${mimeType}\nContent Length: {contentLength}",
127+
"jdk.notebook.cell.language.not.found": "Doesn't support ${languageId} execution"
114128
}

vscode/l10n/bundle.l10n.zh-cn.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,19 @@
110110
"jdk.notebook.create.error_msg.failed": "Failed to create a notebook",
111111
"jdk.jshell.open.error_msg.failed": "Some error occurred while launching Jshell",
112112
"jdk.notebook.project.mapping.error_msg.failed": "An error occurred while changing the project context of the notebook",
113-
"jdk.notebook.create.new.notebook.input.name": "Enter new Java notebook file name"
113+
"jdk.notebook.create.new.notebook.input.name": "Enter new Java notebook file name",
114+
"jdk.notebook.parsing.empty.file.error_msg.title": "Empty Notebook",
115+
"jdk.notebook.parsing.empty.file.error_msg.desc": "The notebook file appears to be empty.",
116+
"jdk.notebook.parsing.error_msg.title": "Error Opening Notebook",
117+
"jdk.notebook.parsing.error_msg.desc": "Failed to open notebook: {message}",
118+
"jdk.notebook.parsing.invalid.structure.error_msg.title": "Invalid Notebook Structure",
119+
"jdk.notebook.parsing.invalid.structure.error_msg.desc": "Missing or invalid `cells` array.",
120+
"jdk.notebook.cell.parsing.error_msg.title": "Cell parsing error",
121+
"jdk.notebook.cell.type.error_msg": "Invalid cell type: {cellType}",
122+
"jdk.notebook.cell.missing.error_msg": "Missing field: {fieldName}",
123+
"jdk.notebook.serializer.error_msg": "Failed to serialize notebook: {errorMessage}",
124+
"jdk.notebook.cell.serializer.error_msg": "Failed to serialize one or more cells",
125+
"jdk.notebook.validation.failed.error_msg": "Notebook JSON validation failed",
126+
"jdk.notebook.mime_type.not.found.cell.output": "Mime Type: ${mimeType}\nContent Length: {contentLength}",
127+
"jdk.notebook.cell.language.not.found": "Doesn't support ${languageId} execution"
114128
}

vscode/src/notebooks/codeCellExecution.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* under the License.
2020
*/
2121

22-
import { commands, NotebookCell, NotebookCellExecution, NotebookCellOutput, NotebookController } from "vscode";
22+
import { commands, NotebookCell, NotebookCellExecution, NotebookCellOutput, NotebookController, window, workspace } from "vscode";
2323
import { LOGGER } from "../logger";
2424
import { NotebookCellExecutionResult } from "../lsp/protocol";
2525
import { createErrorOutputItem } from "./utils";
@@ -132,7 +132,12 @@ export class CodeCellExecution {
132132
this.execution.clearOutput();
133133
await this.execution.replaceOutput(this.output);
134134
this.execution.token.onCancellationRequested(async () => {
135-
await commands.executeCommand(nbCommands.interruptNotebookCellExecution, this.notebookId);
135+
try {
136+
await commands.executeCommand(nbCommands.interruptNotebookCellExecution, this.notebookId);
137+
} catch (error) {
138+
LOGGER.error("Some Error occurred while interrupting code cell: " + error);
139+
window.showErrorMessage("Cannot interrupt code cell");
140+
}
136141
});
137142
return;
138143
}

vscode/src/notebooks/kernel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export class IJNBKernel implements Disposable {
145145
const exec = controller.createNotebookCellExecution(cell);
146146
exec.executionOrder = this.getIncrementedExecutionCounter(notebookId);
147147
exec.start(Date.now());
148-
await exec.replaceOutput(createErrorOutput(new Error(`Doesn't support ${cell.document.languageId} execution`)));
148+
await exec.replaceOutput(createErrorOutput(new Error(l10n.value("jdk.notebook.cell.language.not.found", { languageId: cell.document.languageId }))));
149149
exec.end(false, Date.now());
150150
}
151151

0 commit comments

Comments
 (0)