Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | 1x 6x 6x 1x 1x 5x 5x 5x 6x 5x 5x 2x 3x 3x 2x 2x 1x 1x 3x 3x 6x 5x 5x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import * as vscode from 'vscode';
import { getFileName } from '@src/utilities';
import { findOpenNotebookByUri } from '@src/utilities/vscode/notebook';
export const addActiveEditorToNotebook = {
id: 'mentor.command.addActiveEditorToNotebook',
handler: async () => {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage('No active editor found.');
return;
}
const notebookUris = await getWorkspaceMentorNotebookUris();
const actionItems: ActionItem[] = [{ label: '$(add) Create New Notebook', action: 'create-new' }];
for (const notebookUri of notebookUris) {
actionItems.push({
label: `$(notebook) ${getNotebookFileName(notebookUri)}`,
description: notebookUri.toString(),
action: 'add-existing',
notebookUri,
});
}
const selectedAction = await vscode.window.showQuickPick(actionItems, {
placeHolder: 'Create a new notebook or choose an existing Mentor notebook',
});
if (!selectedAction) {
return;
}
const cellData = getEditorCellData(editor);
if (selectedAction.action === 'create-new') {
await createNewNotebook(cellData);
return;
}
Iif (!selectedAction.notebookUri) {
return;
}
await addToExistingNotebook(selectedAction.notebookUri, cellData);
}
};
type ActionItem = vscode.QuickPickItem & {
action: 'create-new' | 'add-existing';
notebookUri?: vscode.Uri;
};
function getEditorCellData(editor: vscode.TextEditor): vscode.NotebookCellData {
const document = editor.document;
return new vscode.NotebookCellData(
vscode.NotebookCellKind.Code,
document.getText(),
document.languageId
);
}
function getNotebookFileName(uri: vscode.Uri): string {
return getFileName(uri.toString()) || uri.toString();
}
async function getWorkspaceMentorNotebookUris(): Promise<vscode.Uri[]> {
const notebookUris = await vscode.workspace.findFiles('**/*.mnb');
return notebookUris.sort((a, b) => a.path.localeCompare(b.path));
}
async function createNewNotebook(cellData: vscode.NotebookCellData): Promise<void> {
const data = new vscode.NotebookData([cellData]);
const notebook = await vscode.workspace.openNotebookDocument('mentor-notebook', data);
await vscode.window.showNotebookDocument(notebook);
}
async function addToExistingNotebook(notebookUri: vscode.Uri, cellData: vscode.NotebookCellData): Promise<void> {
const notebook = findOpenNotebookByUri(notebookUri)
?? await vscode.workspace.openNotebookDocument(notebookUri);
const newCellIndex = notebook.cellCount;
const workspaceEdit = new vscode.WorkspaceEdit();
workspaceEdit.set(notebook.uri, [vscode.NotebookEdit.insertCells(notebook.cellCount, [cellData])]);
await vscode.workspace.applyEdit(workspaceEdit);
const notebookEditor = await vscode.window.showNotebookDocument(notebook);
const newCellRange = new vscode.NotebookRange(newCellIndex, newCellIndex + 1);
notebookEditor.selections = [newCellRange];
notebookEditor.revealRange(newCellRange, vscode.NotebookEditorRevealType.InCenter);
} |