All files / languages/triplate route-rendered-triplate.ts

45.45% Statements 5/11
25% Branches 1/4
100% Functions 1/1
45.45% Lines 5/11

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                                        4x   4x 4x 4x 4x                                
import * as vscode from 'vscode';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { NotebookController } from '@src/services/notebook/notebook-controller';
import { isNotebookCell, getContextDocument } from '@src/utilities/query-context';
 
/**
 * Routes the rendered output of a triplate template to the right execution target.
 *
 * - SPARQL templates are executed via the generic `executeSparqlQueryFromString`
 *   command, which shows results in the cell output (notebook) or results panel (editor).
 * - RDF templates in a notebook cell render their result into the cell output as Turtle.
 * - RDF templates in a text editor open the rendered document as the end result.
 *
 * The compiled query/document text is never opened for SPARQL templates.
 *
 * @param source The template's source document or notebook cell.
 * @param rendered The rendered template output.
 */
export async function routeRenderedTriplate(source: vscode.TextDocument | vscode.NotebookCell, rendered: string): Promise<void> {
	const document = getContextDocument(source);
 
	Eif (document.languageId === 'sparql') {
		const documentUri = document.uri.toString();
		await vscode.commands.executeCommand('mentor.command.executeSparqlQueryFromString', rendered, documentUri, true);
		return;
	}
 
	if (isNotebookCell(source)) {
		const controller = container.resolve<NotebookController>(ServiceToken.NotebookController);
		await controller.renderContentInCell(source, rendered);
		return;
	}
 
	const renderedDocument = await vscode.workspace.openTextDocument({ 
		content: rendered,
		language: document.languageId
	});
 
	await vscode.window.showTextDocument(renderedDocument);
}