Skip to content

Commit f0ac0ee

Browse files
committed
Added reset kernel tool in notebook
1 parent 5fc6e25 commit f0ac0ee

File tree

12 files changed

+110
-14
lines changed

12 files changed

+110
-14
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@ 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 String NBLS_NOTEBOOK_RESET_SESSION = "nbls.notebook.reset.session";
3738
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));
39+
private static final Set<String> COMMANDS = new HashSet<>(Arrays.asList(NBLS_JSHELL_EXEC,
40+
NBLS_OPEN_PROJECT_JSHELL,
41+
NBLS_JSHELL_INTERRUPT,
42+
NBLS_NOTEBOOK_RESET_SESSION,
43+
NBLS_NOTEBOOK_PROJECT_MAPPING));
3944

4045
@Override
4146
public Set<String> getCommands() {
@@ -53,6 +58,10 @@ public CompletableFuture<Object> runCommand(String command, List<Object> argumen
5358
return CompletableFuture.completedFuture(CodeEval.getInstance().interrupt(arguments));
5459
case NBLS_OPEN_PROJECT_JSHELL:
5560
return CommandHandler.openJshellInProjectContext(arguments).thenApply(list -> (Object) list);
61+
case NBLS_NOTEBOOK_RESET_SESSION:
62+
String notebookUri = NotebookUtils.getArgument(arguments, 0, String.class);
63+
return NotebookSessionManager.getInstance().resetSession(notebookUri)
64+
.thenApply(result -> (Object) null);
5665
case NBLS_NOTEBOOK_PROJECT_MAPPING:
5766
return CommandHandler.getNotebookProjectMappingPath(arguments).thenApply(prj -> (Object) prj);
5867
default:

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Map;
2727
import java.util.concurrent.CancellationException;
2828
import java.util.concurrent.CompletableFuture;
29+
import java.util.concurrent.CompletionException;
2930
import java.util.concurrent.ConcurrentHashMap;
3031
import java.util.concurrent.ExecutionException;
3132
import java.util.function.BiConsumer;
@@ -38,11 +39,15 @@
3839
import org.netbeans.modules.nbcode.java.project.ProjectConfigurationUtils;
3940
import org.netbeans.modules.nbcode.java.project.ProjectContext;
4041
import org.netbeans.modules.nbcode.java.project.ProjectContextInfo;
42+
import org.openide.util.NbBundle;
4143

4244
/**
4345
*
4446
* @author atalati
4547
*/
48+
@NbBundle.Messages({
49+
"MSG_JshellResetError=Some internal error occurred while trying to reset notebook session"
50+
})
4651
public class NotebookSessionManager {
4752

4853
private static final Logger LOG = Logger.getLogger(NotebookSessionManager.class.getName());
@@ -101,12 +106,15 @@ private JShell jshellBuildWithProject(Project prj, JshellStreamsHandler streamsH
101106

102107
public CompletableFuture<JShell> createSession(NotebookDocument notebookDoc) {
103108
String notebookId = notebookDoc.getUri();
109+
return createSession(notebookId);
110+
}
104111

112+
public CompletableFuture<JShell> createSession(String notebookId) {
105113
return sessions.computeIfAbsent(notebookId, id -> {
106114
JshellStreamsHandler handler = new JshellStreamsHandler(id, CodeEval.getInstance().outStreamFlushCb, CodeEval.getInstance().errStreamFlushCb);
107115
jshellStreamsMap.put(id, handler);
108116

109-
CompletableFuture<JShell> future = jshellBuilder(notebookDoc.getUri(), handler);
117+
CompletableFuture<JShell> future = jshellBuilder(notebookId, handler);
110118

111119
future.thenAccept(jshell -> onJshellInit(notebookId, jshell))
112120
.exceptionally(ex -> {
@@ -241,15 +249,17 @@ public ProjectContextInfo getNotebookPrjNameContext(String notebookId) {
241249

242250
public void closeSession(String notebookUri) {
243251
CompletableFuture<JShell> future = sessions.remove(notebookUri);
244-
JShell jshell = future.getNow(null);
245-
if (jshell != null) {
246-
jshell.close();
247-
}
248-
JshellStreamsHandler handler = jshellStreamsMap.remove(notebookUri);
249-
if (handler != null) {
250-
handler.close();
252+
if (future != null) {
253+
JShell jshell = future.getNow(null);
254+
if (jshell != null) {
255+
jshell.close();
256+
}
257+
JshellStreamsHandler handler = jshellStreamsMap.remove(notebookUri);
258+
if (handler != null) {
259+
handler.close();
260+
}
261+
notebookPrjMap.remove(notebookUri);
251262
}
252-
notebookPrjMap.remove(notebookUri);
253263
}
254264

255265
private CompletableFuture<Project> getProjectContextForNotebook(String notebookUri) {
@@ -283,4 +293,14 @@ private CompletableFuture<Project> getProjectContextForNotebook(String notebookU
283293
});
284294

285295
}
296+
297+
public CompletableFuture<Void> resetSession(String notebookUri) {
298+
closeSession(notebookUri);
299+
return createSession(notebookUri)
300+
.thenApply(jshell -> (Void) null)
301+
.exceptionally(ex -> {
302+
LOG.log(Level.SEVERE, "Error creating new session after reset", ex);
303+
throw new CompletionException(Bundle.MSG_JshellResetError(), ex);
304+
});
305+
}
286306
}

vscode/l10n/bundle.l10n.en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@
111111
"jdk.notebook.create.error_msg.failed": "Failed to create a notebook",
112112
"jdk.jshell.open.error_msg.failed": "Some error occurred while launching JShell",
113113
"jdk.notebook.project.mapping.error_msg.failed": "An error occurred while changing the project context of the notebook",
114+
"jdk.notebook.restart.kernel.error_msg.failed": "An error occurred while trying to restart the notebook kernel",
115+
"jdk.notebook.restart.kernel.msg.success": "Restarted the notebook kernel successfully",
116+
"jdk.notebook.restart.kernel.msg.consent": "Are you sure you want to restart the notebook kernel? All definitions will be cleared.",
114117
"jdk.notebook.create.new.notebook.input.name": "Enter a file name for the new Java notebook",
115118
"jdk.notebook.parsing.empty.file.error_msg.title": "Empty Notebook",
116119
"jdk.notebook.parsing.empty.file.error_msg.desc": "The notebook file appears to be empty.",

vscode/l10n/bundle.l10n.ja.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@
111111
"jdk.notebook.create.error_msg.failed": "ノートブックの作成に失敗しました",
112112
"jdk.jshell.open.error_msg.failed": "JShellの起動中にエラーが発生しました",
113113
"jdk.notebook.project.mapping.error_msg.failed": "ノートブックのプロジェクト・コンテキストを変更中にエラーが発生しました",
114+
"jdk.notebook.restart.kernel.error_msg.failed": "An error occurred while trying to restart the notebook kernel",
115+
"jdk.notebook.restart.kernel.msg.success": "Restarted the notebook kernel successfully",
116+
"jdk.notebook.restart.kernel.msg.consent": "Are you sure you want to restart the notebook kernel? All definitions will be cleared.",
114117
"jdk.notebook.create.new.notebook.input.name": "新規Javaノートブックのファイル名を入力します",
115118
"jdk.notebook.parsing.empty.file.error_msg.title": "空のノートブック",
116119
"jdk.notebook.parsing.empty.file.error_msg.desc": "ノートブック・ファイルは空であるようです。",

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@
111111
"jdk.notebook.create.error_msg.failed": "无法创建记事本",
112112
"jdk.jshell.open.error_msg.failed": "启动 JShell 时出错",
113113
"jdk.notebook.project.mapping.error_msg.failed": "更改记事本的项目上下文时出错",
114+
"jdk.notebook.restart.kernel.error_msg.failed": "An error occurred while trying to restart the notebook kernel",
115+
"jdk.notebook.restart.kernel.msg.success": "Restarted the notebook kernel successfully",
116+
"jdk.notebook.restart.kernel.msg.consent": "Are you sure you want to restart the notebook kernel? All definitions will be cleared.",
114117
"jdk.notebook.create.new.notebook.input.name": "输入新 Java 记事本的文件名",
115118
"jdk.notebook.parsing.empty.file.error_msg.title": "记事本为空",
116119
"jdk.notebook.parsing.empty.file.error_msg.desc": "记事本文件似乎为空。",

vscode/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,13 @@
616616
"title": "%jdk.notebook.change.project%",
617617
"category": "Java",
618618
"icon": "$(pencil)"
619+
},
620+
{
621+
"command": "jdk.notebook.restart.kernel",
622+
"shortTitle": "%jdk.notebook.restart.kernel.short.title%",
623+
"title": "%jdk.notebook.restart.kernel%",
624+
"category": "Java",
625+
"icon": "$(refresh)"
619626
}
620627
],
621628
"keybindings": [
@@ -738,6 +745,10 @@
738745
{
739746
"command": "jdk.notebook.change.project",
740747
"when": "false"
748+
},
749+
{
750+
"command": "jdk.notebook.restart.kernel",
751+
"when": "false"
741752
}
742753
],
743754
"view/title": [
@@ -818,6 +829,11 @@
818829
"command": "jdk.notebook.change.project",
819830
"group": "navigation@1",
820831
"when": "nbJdkReady && resourceExtname == .ijnb"
832+
},
833+
{
834+
"command": "jdk.notebook.restart.kernel",
835+
"group": "navigation/execute@5",
836+
"when": "nbJdkReady && resourceExtname == .ijnb"
821837
}
822838
]
823839
},

vscode/package.nls.ja.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"workbench.action.debug.start": "デバッグの開始",
1515
"jdk.notebook.new": "新規ノートブックを作成...",
1616
"jdk.notebook.change.project": "プロジェクト・コンテキスト",
17+
"jdk.notebook.restart.kernel": "Restart Kernel",
18+
"jdk.notebook.restart.kernel.short.title": "Restart",
1719
"jdk.project.run": "デバッグなしでプロジェクトの実行",
1820
"jdk.project.debug": "プロジェクトのデバッグ",
1921
"jdk.project.test": "プロジェクトのテスト",

vscode/package.nls.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"workbench.action.debug.start": "Start Debugging",
1515
"jdk.notebook.new": "Create New Notebook...",
1616
"jdk.notebook.change.project": "Project Context",
17+
"jdk.notebook.restart.kernel": "Restart Kernel",
18+
"jdk.notebook.restart.kernel.short.title": "Restart",
1719
"jdk.project.run": "Run Project Without Debugging",
1820
"jdk.project.debug": "Debug Project",
1921
"jdk.project.test": "Test Project",

vscode/package.nls.zh-cn.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"workbench.action.debug.start": "启动调试",
1515
"jdk.notebook.new": "创建新记事本...",
1616
"jdk.notebook.change.project": "项目上下文",
17+
"jdk.notebook.restart.kernel": "Restart Kernel",
18+
"jdk.notebook.restart.kernel.short.title": "Restart",
1719
"jdk.project.run": "运行项目但不调试",
1820
"jdk.project.debug": "调试项目",
1921
"jdk.project.test": "测试项目",

vscode/src/commands/commands.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ export const extCommands = {
5353
projectDeleteEntry: appendPrefixToCommand("foundProjects.deleteEntry"),
5454
createNotebook: appendPrefixToCommand("notebook.new"),
5555
openJshellInProject: appendPrefixToCommand("jshell.project"),
56-
notebookChangeProjectContext: appendPrefixToCommand("notebook.change.project")
56+
notebookChangeProjectContext: appendPrefixToCommand("notebook.change.project"),
57+
resetNotebookSession: appendPrefixToCommand("notebook.restart.kernel"),
5758
}
5859

5960
export const builtInCommands = {
@@ -90,5 +91,6 @@ export const nbCommands = {
9091
executeNotebookCell: appendPrefixToCommand("jshell.execute.cell"),
9192
interruptNotebookCellExecution: appendPrefixToCommand("jshell.interrupt.cell"),
9293
openJshellInProject: appendPrefixToCommand("jshell.project.open"),
93-
createNotebookProjectContext: appendPrefixToCommand("notebook.project.context")
94+
createNotebookProjectContext: appendPrefixToCommand("notebook.project.context"),
95+
resetNotebookSession: appendPrefixToCommand("notebook.reset.session")
9496
}

0 commit comments

Comments
 (0)