Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute cell with dependencies #14508

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -781,22 +781,26 @@
"title": "Gather code",
"shortTitle": "Gather",
"icon": "$(gather)",
"enablement": "!jupyter.webExtension",
"enablement": "isWorkspaceTrusted && !jupyter.webExtension",
"category": "Jupyter"
},
{
"command": "jupyter.selectSuccessorCells",
"title": "Select Successors",
"shortTitle": "Select",
"icon": "$(arrow-down)",
"enablement": "!jupyter.webExtension",
"title": "Select Dependent Cells",
"enablement": "isWorkspaceTrusted && !jupyter.webExtension",
"category": "Jupyter"
},
{
"command": "jupyter.runSuccessorCells",
"title": "Execute Cell with Dependencies",
"enablement": "isWorkspaceTrusted && !jupyter.webExtension",
"category": "Jupyter"
},
{
"command": "jupyter.debugCellSymbols",
"title": "Debug Cell Symbols",
"icon": "$(debug-alt-small)",
"enablement": "!jupyter.webExtension",
"enablement": "isWorkspaceTrusted && !jupyter.webExtension",
"category": "Jupyter"
}
],
Expand Down Expand Up @@ -964,6 +968,10 @@
{
"command": "jupyter.runAndDebugCell",
"when": "notebookKernel =~ /^ms-toolsai.jupyter\\// && jupyter.ispythonnotebook && notebookCellType == code && isWorkspaceTrusted && resource not in jupyter.notebookeditor.debugDocuments || !notebookKernel && jupyter.ispythonnotebook && notebookCellType == code && isWorkspaceTrusted"
},
{
"command": "jupyter.runSuccessorCells",
"when": "notebookKernel =~ /^ms-toolsai.jupyter\\// && jupyter.ispythonnotebook && notebookCellType == code && isWorkspaceTrusted && config.jupyter.executionAnalysis.enabled"
}
],
"interactive/toolbar": [
Expand Down
25 changes: 24 additions & 1 deletion src/standalone/executionAnalysis/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,35 @@ export function cellIndexesToRanges(indexes: number[]): vscode.NotebookRange[] {
.map((val) => new vscode.NotebookRange(val[0], val[1]));
}

export function findNotebook(document: vscode.TextDocument): vscode.NotebookDocument | undefined {
function findNotebook(document: vscode.TextDocument): vscode.NotebookDocument | undefined {
return vscode.workspace.notebookDocuments.find(
(doc) => doc.uri.authority === document.uri.authority && doc.uri.path === document.uri.path
);
}

export function findNotebookAndCell(
cell: vscode.NotebookCell | undefined
): { notebook: vscode.NotebookDocument; cell: vscode.NotebookCell } | undefined {
const doc =
vscode.workspace.textDocuments.find((doc) => doc.uri.toString() === cell?.document.uri.toString()) ??
vscode.window.activeTextEditor?.document;
if (!doc) {
return;
}

const notebook = findNotebook(doc);
if (!notebook) {
return;
}
const cells = notebook.getCells();
const currentCell = cells.find((cell) => cell.document.uri.toString() === doc.uri.toString());
if (!currentCell) {
return;
}

return { notebook, cell: currentCell };
}

// eslint-disable-next-line no-empty,@typescript-eslint/no-empty-function
export function noop() {}

Expand Down
45 changes: 17 additions & 28 deletions src/standalone/executionAnalysis/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import * as vscode from 'vscode';
import { activatePylance } from './pylance';
import { findNotebook, noop } from './common';
import { findNotebookAndCell, noop } from './common';
import { SymbolsTracker } from './symbols';

export async function activate(context: vscode.ExtensionContext): Promise<void> {
Expand All @@ -27,48 +27,37 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
vscode.commands.registerCommand(
'jupyter.selectSuccessorCells',
async (cell: vscode.NotebookCell | undefined) => {
const doc =
vscode.workspace.textDocuments.find(
(doc) => doc.uri.toString() === cell?.document.uri.toString()
) ?? vscode.window.activeTextEditor?.document;
if (!doc) {
return;
}

const notebook = findNotebook(doc);
if (!notebook) {
return;
}
const cells = notebook.getCells();
const currentCell = cells.find((cell) => cell.document.uri.toString() === doc.uri.toString());
if (!currentCell) {
const matched = findNotebookAndCell(cell);
if (!matched) {
return;
}

const { notebook, cell: currentCell } = matched;
await symbolsManager.selectSuccessorCells(notebook, currentCell);
}
)
);

context.subscriptions.push(
vscode.commands.registerCommand('jupyter.gatherCells', async (cell: vscode.NotebookCell | undefined) => {
const doc =
vscode.workspace.textDocuments.find((doc) => doc.uri.toString() === cell?.document.uri.toString()) ??
vscode.window.activeTextEditor?.document;
if (!doc) {
vscode.commands.registerCommand('jupyter.runSuccessorCells', async (cell: vscode.NotebookCell | undefined) => {
const matched = findNotebookAndCell(cell);
if (!matched) {
return;
}

const notebook = findNotebook(doc);
if (!notebook) {
return;
}
const cells = notebook.getCells();
const currentCell = cells.find((cell) => cell.document.uri.toString() === doc.uri.toString());
if (!currentCell) {
const { notebook, cell: currentCell } = matched;
await symbolsManager.runSuccessorCells(notebook, currentCell);
})
);

context.subscriptions.push(
vscode.commands.registerCommand('jupyter.gatherCells', async (cell: vscode.NotebookCell | undefined) => {
const matched = findNotebookAndCell(cell);
if (!matched) {
return;
}

const { notebook, cell: currentCell } = matched;
const gatheredCells = (await symbolsManager.gatherCells(notebook, currentCell)) as vscode.NotebookCell[];
if (gatheredCells) {
// console.log(gatheredCells?.map(cell => `${cell.index}:\n ${cell.document.getText()}\n`));
Expand Down
21 changes: 19 additions & 2 deletions src/standalone/executionAnalysis/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ export class NotebookDocumentSymbolTracker {

async selectSuccessorCells(cell: vscode.NotebookCell) {
await this.requestCellSymbolsSync();
const tokenSource = new vscode.CancellationTokenSource();
const refs = this._cellRefs.get(cell.document.uri.fragment);
const cells = this._notebookEditor.notebook.getCells();
const indexes: number[] = [];
Expand All @@ -266,8 +265,19 @@ export class NotebookDocumentSymbolTracker {

const cellRanges = cellIndexesToRanges(indexes);
this._notebookEditor.selections = cellRanges;
}

tokenSource.dispose();
async runSuccessorCells(cell: vscode.NotebookCell) {
await this.requestCellSymbolsSync();
const analysis = new CellAnalysis(this._cellExecution, this._cellRefs);
const successorCells = analysis.getSuccessorCells(cell) as vscode.NotebookCell[];
const cellRanges = cellIndexesToRanges(successorCells.map((cell) => cell.index));
await vscode.commands
.executeCommand('notebook.cell.execute', {
ranges: cellRanges.map((range) => ({ start: range.start, end: range.end })),
document: this._notebookEditor.notebook.uri
})
.then(noop, noop);
}

async debugSymbols() {
Expand Down Expand Up @@ -454,6 +464,13 @@ export class SymbolsTracker {
}
}

async runSuccessorCells(notebookDocument: vscode.NotebookDocument, cell: vscode.NotebookCell) {
const tracker = this._notebookDocumentSymbolTrackers.get(notebookDocument.uri.toString());
if (tracker) {
await tracker.runSuccessorCells(cell);
}
}

async selectSuccessorCells(notebookDocument: vscode.NotebookDocument, cell: vscode.NotebookCell) {
const tracker = this._notebookDocumentSymbolTrackers.get(notebookDocument.uri.toString());
if (tracker) {
Expand Down
Loading