Skip to content

Commit c85f4fb

Browse files
authored
feat: add cmd+enter command for running focused block (#102)
1 parent 192c29c commit c85f4fb

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@
7070
"key": "escape",
7171
"when": "isCompositeNotebook && !editorHoverVisible && !suggestWidgetVisible && !isComposing && !inSnippetMode && !exceptionWidgetVisible && !selectionAnchorSet && !LinkedEditingInputVisible && !renameInputVisible && !editorHasSelection && !accessibilityHelpWidgetVisible && !breakpointWidgetVisible && !findWidgetVisible && !markersNavigationVisible && !parameterHintsVisible && !editorHasMultipleSelections && !notificationToastsVisible && !notebookEditorFocused && !inlineChatVisible",
7272
"command": "interactive.input.clear"
73+
},
74+
{
75+
"key": "cmd+enter",
76+
"mac": "cmd+enter",
77+
"win": "ctrl+enter",
78+
"linux": "ctrl+enter",
79+
"when": "notebookEditorFocused && !findInputFocussed && !replaceInputFocussed",
80+
"command": "jupyter.notebookeditor.runfocusedcell"
7381
}
7482
],
7583
"commands": [
@@ -544,6 +552,12 @@
544552
"category": "Notebook",
545553
"enablement": "!jupyter.webExtension"
546554
},
555+
{
556+
"command": "jupyter.notebookeditor.runfocusedcell",
557+
"title": "%jupyter.command.jupyter.notebookeditor.runfocusedcell.title%",
558+
"category": "Notebook",
559+
"enablement": "notebookEditorFocused"
560+
},
547561
{
548562
"command": "jupyter.expandallcells",
549563
"title": "%jupyter.command.jupyter.expandallcells.title%",

package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"message": "Add Empty Cell to Notebook File",
9393
"comment": ["{Locked='Notebook'}"]
9494
},
95+
"jupyter.command.jupyter.notebookeditor.runfocusedcell.title": "Run Focused Cell",
9596
"jupyter.command.jupyter.interruptkernel.title": "Interrupt Kernel",
9697
"jupyter.command.jupyter.interruptkernel.shorttitle": "Interrupt",
9798
"jupyter.command.jupyter.restartkernel.title": "Restart Kernel",

src/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export interface ICommandNameArgumentTypeMapping {
5858
[DSCommands.RestartKernelAndRunUpToSelectedCell]: [{ notebookEditor: { notebookUri: Uri } } | undefined];
5959
[DSCommands.NotebookEditorRemoveAllCells]: [];
6060
[DSCommands.NotebookEditorRunAllCells]: [];
61+
[DSCommands.NotebookEditorRunFocusedCell]: [];
6162
[DSCommands.NotebookEditorAddCellBelow]: [];
6263
[DSCommands.ExpandAllCells]: [];
6364
[DSCommands.CollapseAllCells]: [];

src/notebooks/notebookCommandListener.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ export class NotebookCommandListener implements INotebookCommandHandler, IExtens
6868
this.disposableRegistry.push(
6969
commands.registerCommand(Commands.NotebookEditorRunAllCells, () => this.runAllCells())
7070
);
71+
this.disposableRegistry.push(
72+
commands.registerCommand(Commands.NotebookEditorRunFocusedCell, () => this.runFocusedCell())
73+
);
7174
this.disposableRegistry.push(
7275
commands.registerCommand(Commands.NotebookEditorAddCellBelow, () => this.addCellBelow())
7376
);
@@ -115,6 +118,27 @@ export class NotebookCommandListener implements INotebookCommandHandler, IExtens
115118
}
116119
}
117120

121+
private runFocusedCell() {
122+
const editor = window.activeNotebookEditor;
123+
if (!editor) {
124+
return;
125+
}
126+
127+
// Get the first selection range
128+
const range = editor.selections[0];
129+
if (!range) {
130+
return;
131+
}
132+
133+
// Execute the cell at the start of the selection
134+
commands
135+
.executeCommand('notebook.cell.execute', {
136+
ranges: [{ start: range.start, end: range.start + 1 }],
137+
document: editor.notebook.uri
138+
})
139+
.then(noop, noop);
140+
}
141+
118142
private addCellBelow() {
119143
if (window.activeNotebookEditor) {
120144
commands.executeCommand('notebook.cell.insertCodeCellBelow').then(noop, noop);

src/platform/common/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ export namespace Commands {
189189
export const NotebookEditorRemoveAllCells = 'jupyter.notebookeditor.removeallcells';
190190
export const NotebookEditorRunAllCells = 'jupyter.notebookeditor.runallcells';
191191
export const NotebookEditorRunSelectedCell = 'jupyter.notebookeditor.runselectedcell';
192+
export const NotebookEditorRunFocusedCell = 'jupyter.notebookeditor.runfocusedcell';
192193
export const NotebookEditorAddCellBelow = 'jupyter.notebookeditor.addcellbelow';
193194
export const ExpandAllCells = 'jupyter.expandallcells';
194195
export const CollapseAllCells = 'jupyter.collapseallcells';

0 commit comments

Comments
 (0)