All files / utilities/vscode editors.ts

41.66% Statements 5/12
33.33% Branches 2/6
33.33% Functions 2/6
44.44% Lines 4/9

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                  9x   9x 3x     6x                                        
import * as vscode from 'vscode';
import { findOpenNotebookByUri } from './notebook';
 
/**
 * Whether a text or notebook document is currently open, i.e. VS Code holds its
 * content in memory and keeps it in sync with the file on disk.
 * @param uri The document URI.
 */
export function isDocumentOpen(uri: vscode.Uri): boolean {
	const key = uri.toString();
 
	if (vscode.workspace.textDocuments.some(document => document.uri.toString() === key)) {
		return true;
	}
 
	return findOpenNotebookByUri(uri) !== undefined;
}
 
/**
 * Whether a document is currently visible in an editor. Includes notebook cell
 * editors, which `vscode.window.visibleTextEditors` does not contain. The
 * fallback to an empty list keeps this callable in test environments whose
 * mocks predate `visibleNotebookEditors`.
 * @param uri The document URI.
 */
export function isDocumentVisible(uri: vscode.Uri): boolean {
	const key = uri.toString();
 
	if (vscode.window.visibleTextEditors.some(editor => editor.document.uri.toString() === key)) {
		return true;
	}
 
	return (vscode.window.visibleNotebookEditors ?? []).some(editor =>
		editor.notebook.getCells().some(cell => cell.document.uri.toString() === key));
}