Run and Add Cell keybinding support (#3896)

- As part of this, fixed bug in the insertCell API where it didn't add to the end / failed if no cells existed
This commit is contained in:
Kevin Cunnane
2019-02-04 14:02:15 -08:00
committed by GitHub
parent 15929e8cf2
commit 2fce771214
12 changed files with 185 additions and 34 deletions

View File

@@ -45,6 +45,18 @@
"dark": "resources/dark/open_notebook_inverse.svg",
"light": "resources/light/open_notebook.svg"
}
},
{
"command": "notebook.command.runactivecell",
"title": "%notebook.command.runactivecell%"
},
{
"command": "notebook.command.addcode",
"title": "%notebook.command.addcode%"
},
{
"command": "notebook.command.addtext",
"title": "%notebook.command.addtext%"
}
],
"menus": {
@@ -54,6 +66,18 @@
},
{
"command": "notebook.command.open"
},
{
"command": "notebook.command.runactivecell",
"when": "notebookEditorVisible"
},
{
"command": "notebook.command.addcode",
"when": "notebookEditorVisible"
},
{
"command": "notebook.command.addtext",
"when": "notebookEditorVisible"
}
],
"objectExplorer/item/context": [
@@ -68,6 +92,21 @@
{
"command": "notebook.command.new",
"key": "Ctrl+Shift+N"
},
{
"command": "notebook.command.runactivecell",
"key": "F5",
"when": "notebookEditorVisible"
},
{
"command": "notebook.command.addcode",
"key": "Ctrl+Shift+C",
"when": "notebookEditorVisible"
},
{
"command": "notebook.command.addtext",
"key": "Ctrl+Shift+T",
"when": "notebookEditorVisible"
}
]
},

View File

@@ -5,5 +5,8 @@
"notebook.pythonPath.description": "Local path to python installation used by Notebooks.",
"notebook.sqlKernelEnabled.description": "Enable SQL kernel in notebook editor (Preview). Requires reloading this window to take effect",
"notebook.command.new": "New Notebook",
"notebook.command.open": "Open Notebook"
"notebook.command.open": "Open Notebook",
"notebook.command.runactivecell": "Run Cell",
"notebook.command.addcode": "Add Code Cell",
"notebook.command.addtext": "Add Text Cell"
}

View File

@@ -11,31 +11,43 @@ import * as nls from 'vscode-nls';
const localize = nls.loadMessageBundle();
let counter = 0;
const noNotebookVisible = localize('noNotebookVisible', 'No notebook editor is active');
export function activate(extensionContext: vscode.ExtensionContext) {
extensionContext.subscriptions.push(vscode.commands.registerCommand('notebook.command.new', ( connectionId? : string) => {
let title = `Untitled-${counter++}`;
let untitledUri = vscode.Uri.parse(`untitled:${title}`);
let options: sqlops.nb.NotebookShowOptions = connectionId? {
viewColumn : null,
preserveFocus : true,
preview: null,
providerId : null,
connectionId : connectionId,
defaultKernel : null
} : null;
sqlops.nb.showNotebookDocument(untitledUri, options).then(success => {
}, (err: Error) => {
vscode.window.showErrorMessage(err.message);
});
newNotebook(connectionId);
}));
extensionContext.subscriptions.push(vscode.commands.registerCommand('notebook.command.open', () => {
openNotebook();
}));
extensionContext.subscriptions.push(vscode.commands.registerCommand('notebook.command.runactivecell', () => {
runActiveCell();
}));
extensionContext.subscriptions.push(vscode.commands.registerCommand('notebook.command.addcode', () => {
addCell('code');
}));
extensionContext.subscriptions.push(vscode.commands.registerCommand('notebook.command.addtext', () => {
addCell('markdown');
}));
}
function newNotebook(connectionId: string) {
let title = `Untitled-${counter++}`;
let untitledUri = vscode.Uri.parse(`untitled:${title}`);
let options: sqlops.nb.NotebookShowOptions = connectionId ? {
viewColumn: null,
preserveFocus: true,
preview: null,
providerId: null,
connectionId: connectionId,
defaultKernel: null
} : null;
sqlops.nb.showNotebookDocument(untitledUri, options).then(success => {
}, (err: Error) => {
vscode.window.showErrorMessage(err.message);
});
}
async function openNotebook(): Promise<void> {
try {
let filter = {};
@@ -53,6 +65,38 @@ async function openNotebook(): Promise<void> {
}
}
async function runActiveCell(): Promise<void> {
try {
let notebook = sqlops.nb.activeNotebookEditor;
if (notebook) {
await notebook.runCell();
} else {
throw new Error(noNotebookVisible);
}
} catch (err) {
vscode.window.showErrorMessage(err);
}
}
async function addCell(cellType: sqlops.nb.CellType): Promise<void> {
try {
let notebook = sqlops.nb.activeNotebookEditor;
if (notebook) {
await notebook.edit((editBuilder: sqlops.nb.NotebookEditorEdit) => {
// TODO should prompt and handle cell placement
editBuilder.insertCell({
cell_type: cellType,
source: ''
});
});
} else {
throw new Error(noNotebookVisible);
}
} catch (err) {
vscode.window.showErrorMessage(err);
}
}
// this method is called when your extension is deactivated
export function deactivate() {
}