All files / utilities query-context.ts

100% Statements 14/14
75% Branches 9/12
100% Functions 5/5
100% Lines 12/12

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                                15x 1x     14x 15x           15x 2x 2x   2x 2x         12x               15x                 11x    
// Note: this module imports 'vscode' and must NOT be re-exported from the
// utilities barrel (./index.ts), which is consumed by the language servers where
// 'vscode' is unavailable. Import it directly via '@src/utilities/query-context'.
import * as vscode from 'vscode';
 
/**
 * Resolves a document URI to the text document or notebook cell it identifies.
 *
 * Searches the open text documents first, then the cells of all open notebooks
 * (a notebook cell document has the `vscode-notebook-cell` scheme). When no URI
 * is given, falls back to the active text editor's document.
 *
 * @param uri A document or notebook-cell URI, or `undefined` to use the active editor.
 * @returns The matching text document or notebook cell, or `undefined` if none was found.
 */
export function resolveQueryContext(uri?: vscode.Uri | string): vscode.TextDocument | vscode.NotebookCell | undefined {
	if (!uri) {
		return vscode.window.activeTextEditor?.document;
	}
 
	const parsed = typeof uri === 'string' ? vscode.Uri.parse(uri) : uri;
	const target = parsed.toString();
 
	// Notebook cell documents also appear in `workspace.textDocuments`, so for a
	// cell URI we must resolve the owning NotebookCell first — otherwise the plain
	// text document is returned and the result is routed to the panel rather than
	// the cell output.
	if (parsed.scheme === 'vscode-notebook-cell') {
		for (const notebook of vscode.workspace.notebookDocuments) {
			const cell = notebook.getCells().find(c => c.document.uri.toString() === target);
 
			Eif (cell) {
				return cell;
			}
		}
	}
 
	return vscode.workspace.textDocuments.find(doc => doc.uri.toString() === target);
}
 
/**
 * Type guard that narrows a query context to a notebook cell.
 * @param context A text document or notebook cell.
 */
export function isNotebookCell(context: vscode.TextDocument | vscode.NotebookCell): context is vscode.NotebookCell {
	return 'notebook' in context && !!(context as vscode.NotebookCell).notebook;
}
 
/**
 * Returns the underlying text document for a query context, whether it is a plain
 * text document or a notebook cell.
 * @param context A text document or notebook cell.
 */
export function getContextDocument(context: vscode.TextDocument | vscode.NotebookCell): vscode.TextDocument {
	return isNotebookCell(context) ? context.document : context;
}