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

@@ -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() {
}