All files / commands open-document.ts

89.65% Statements 26/29
93.75% Branches 15/16
66.66% Functions 2/3
92.59% Lines 25/27

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    1x     7x 1x         1x 1x     6x 1x 1x     5x 5x       5x 2x   2x 1x         1x 1x       4x   1x   1x 1x   1x   1x           3x   2x     1x      
import * as vscode from 'vscode';
 
export const openDocument = {
    id: 'mentor.command.openDocument',
    handler: async (documentIri: string, query?: string) => {
        if (!documentIri && query) {
            const document = await vscode.workspace.openTextDocument({
                content: query,
                language: 'sparql'
            });
 
            await vscode.window.showTextDocument(document);
            return;
        }
 
        if (!documentIri) {
            vscode.window.showErrorMessage('No document IRI provided.');
            return;
        }
 
        try {
            const uri = vscode.Uri.parse(documentIri);
 
            // For closed untitled documents fall back to opening a new editor with the
            // query text, because the buffer no longer exists in VS Code's memory.
            if (uri.scheme === 'untitled' && query) {
                const isOpen = vscode.workspace.textDocuments.some(d => d.uri.toString() === documentIri);
 
                if (!isOpen) {
                    const document = await vscode.workspace.openTextDocument({
                        content: query,
                        language: 'sparql'
                    });
 
                    await vscode.window.showTextDocument(document);
                    return;
                }
            }
 
            if (uri.scheme === 'vscode-notebook-cell') {
                // Get the notebook URI (remove the cell fragment)
                const notebookUri = uri.with({ scheme: 'file', fragment: '' });
 
                const notebook = await vscode.workspace.openNotebookDocument(notebookUri);
                const notebookEditor = await vscode.window.showNotebookDocument(notebook);
 
                const cell = notebookEditor.notebook.getCells().find(c => c.document.uri.toString() === uri.toString());
 
                Iif (cell) {
                    const range = new vscode.NotebookRange(cell.index, cell.index + 1);
 
                    notebookEditor.revealRange(range, vscode.NotebookEditorRevealType.Default);
                }
            } else {
                const document = await vscode.workspace.openTextDocument(uri);
 
                await vscode.window.showTextDocument(document);
            }
        } catch (error: any) {
            vscode.window.showErrorMessage(`Failed to open document: ${error.message}`);
        }
    }
};