All files / commands/sparql create-sparql-query-from-document.ts

100% Statements 28/28
100% Branches 12/12
100% Functions 2/2
100% Lines 28/28

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 86 87 88 89                                1x         9x 1x 1x     8x   8x 8x 8x   8x   8x 1x 1x     7x 7x         9x       9x 9x       1x 1x     6x   6x       6x 2x                           4x 4x   4x 1x     3x    
import * as vscode from 'vscode';
import { render } from 'triplate';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { IDocumentConnectionService, ITripleStoreConfigService } from '@src/languages/sparql/services';
import { IDocumentContextService } from '@src/services/document';
import { WorkspaceUri } from '@src/providers';
 
/**
 * Creates an untitled SPARQL document that queries the RDF document in the active editor.
 *
 * Workspace documents are loaded into the in-memory store as named graphs keyed by their workspace
 * URI, so the query is scoped with `GRAPH <documentIri>` — a plain triple pattern would match
 * nothing. That graph IRI is meaningful only to the workspace store, so for any other connection
 * the parameter is omitted and the template renders its unscoped branch instead.
 */
export const createSparqlQueryFromDocument = {
	id: 'mentor.command.createSparqlQueryFromDocument',
	handler: async (): Promise<void> => {
		const editor = vscode.window.activeTextEditor;
 
		if (!editor) {
			vscode.window.showErrorMessage('No active editor found.');
			return;
		}
 
		const documentUri = editor.document.uri;
 
		const storeConfigService = container.resolve<ITripleStoreConfigService>(ServiceToken.StoreConfigService);
		const documentConnectionService = container.resolve<IDocumentConnectionService>(ServiceToken.DocumentConnectionService);
		const connection = documentConnectionService.getConnectionForDocument(documentUri);
 
		const template = storeConfigService.getQueryTemplate(connection, 'documentQuery');
 
		if (!template) {
			vscode.window.showErrorMessage('SPARQL query template is not defined in settings.');
			return;
		}
 
		const isWorkspaceStore = storeConfigService.isWorkspaceConnectionId(connection.id);
		const graphIri = isWorkspaceStore ? getDocumentGraphIri(documentUri) : undefined;
 
		// The parameter is omitted rather than passed as `undefined`: triplate treats a present key
		// as bound and rejects the value, whereas an absent optional parameter makes the template's
		// `{% if %}` branch false and renders the unscoped query.
		const parameters: Record<string, string> = graphIri ? { documentIri: graphIri } : {};
 
		let content: string;
 
		try {
			content = render(template, parameters);
		} catch (error: any) {
			// A customized template may declare `documentIri` as required, which fails to bind when
			// no graph is known. Surface it instead of failing the command opaquely.
			vscode.window.showErrorMessage(`Could not render the document query template: ${error.message}`);
			return;
		}
 
		const document = await vscode.workspace.openTextDocument({ content, language: 'sparql' });
 
		await vscode.window.showTextDocument(document);
 
		// Bind the generated query to the store the source document targets, so executing it does
		// not silently fall back to the workspace store.
		if (!isWorkspaceStore) {
			await documentConnectionService.setQuerySourceForDocument(document.uri, connection.id);
		}
	}
};
 
/**
 * Resolves the IRI of the named graph a document is loaded into.
 *
 * Prefers the loaded document context, whose `graphIri` carries the slug fragment that notebook
 * cells are indexed under; falls back to the plain workspace URI for a document that is not indexed.
 * @param documentUri The URI of the document or notebook cell.
 * @returns The canonical graph IRI, or `undefined` if the document has no workspace URI.
 */
function getDocumentGraphIri(documentUri: vscode.Uri): string | undefined {
	const contextService = container.resolve<IDocumentContextService>(ServiceToken.DocumentContextService);
	const context = contextService.getContextFromUri(documentUri.toString());
 
	if (context) {
		return WorkspaceUri.toCanonicalString(context.graphIri);
	}
 
	return WorkspaceUri.toWorkspaceUri(documentUri)?.toString();
}