All files / commands set-notebook-inference.ts

76.47% Statements 26/34
36.66% Branches 11/30
50% Functions 1/2
78.78% Lines 26/33

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          1x     3x         3x                           3x 3x     3x 1x 1x       2x           2x       2x 1x       1x 1x   1x 1x   1x     1x     1x     1x 1x 1x   1x     1x     1x       3x      
import * as vscode from 'vscode';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { ISparqlConnectionService } from '@src/languages/sparql/services';
 
export const setNotebookInference = {
	id: 'mentor.command.setNotebookInference',
	handler: async (context?: any) => {
		const connectionService = container.resolve<ISparqlConnectionService>(ServiceToken.SparqlConnectionService);
 
		// Get the notebook from various possible argument types
		let notebook: vscode.NotebookDocument | undefined;
 
		Iif (context && typeof context === 'object') {
			if ('notebook' in context && context.notebook) {
				// Direct NotebookEditor from notebook toolbar
				notebook = context.notebook;
			} else if ('notebookEditor' in context && context.notebookEditor) {
				// From notebook toolbar context object
				notebook = context.notebookEditor.notebook;
			} else if ('scheme' in context && 'fsPath' in context) {
				// URI passed
				notebook = vscode.workspace.notebookDocuments.find(n => n.uri.toString() === context.toString());
			}
		}
		
		// Fallback to active notebook editor
		Eif (!notebook) {
			notebook = vscode.window.activeNotebookEditor?.notebook;
		}
 
		if (!notebook) {
			vscode.window.showWarningMessage('No notebook is currently open.');
			return;
		}
 
		// Show quick pick to select inference setting
		const items = [
			{ label: 'On', description: 'Include inferred triples in query results.', value: true },
			{ label: 'Off', description: 'Only return asserted triples.', value: false },
			{ label: 'Default', description: 'Use connection defaults for each cell.', value: undefined }
		];
 
		const selected = await vscode.window.showQuickPick(items, {
			placeHolder: 'Set inference for all cells in this notebook'
		});
 
		if (selected === undefined) {
			return; // User cancelled
		}
 
		// Update all cells in the notebook
		const cells = notebook.getCells();
		const edits: vscode.NotebookEdit[] = [];
 
		for (const cell of cells) {
			const metadata = { ...cell.metadata };
 
			Iif (selected.value === undefined) {
				delete metadata.inferenceEnabled;
			} else {
				metadata.inferenceEnabled = selected.value;
			}
 
			edits.push(vscode.NotebookEdit.updateCellMetadata(cell.index, metadata));
		}
 
		Eif (edits.length > 0) {
			const workspaceEdit = new vscode.WorkspaceEdit();
			workspaceEdit.set(notebook.uri, edits);
 
			await vscode.workspace.applyEdit(workspaceEdit);
 
			// Notify listeners to refresh code lenses
			connectionService.notifyDocumentConnectionChanged(notebook.uri);
		}
 
		const statusText = selected.value === undefined
			? 'Cleared inference settings'
			: selected.value ? 'Enabled inference' : 'Disabled inference';
 
		vscode.window.setStatusBarMessage(`${statusText} for all ${cells.length} cells`, 3000);
	}
};