All files / providers inference-uri-handler.ts

100% Statements 18/18
100% Branches 8/8
100% Functions 3/3
100% Lines 18/18

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                      3x         7x 7x 7x           5x 3x   3x 3x   3x 1x       2x   2x 1x 1x 1x 1x   1x     1x        
import * as vscode from 'vscode';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { IPrefixLookupService } from '@src/services/document';
import { Store } from '@faubulous/mentor-rdf';
import { InferenceUri } from '@src/providers/inference-uri';
 
export class InferenceUriHandler implements vscode.UriHandler {
	readonly extensionId: string;
 
	private get _store() {
		return container.resolve<Store>(ServiceToken.Store);
	}
 
	constructor() {
		// Self-register with the extension context for automatic disposal
		const context = container.resolve<vscode.ExtensionContext>(ServiceToken.ExtensionContext);
		this.extensionId = context.extension.id;
		context.subscriptions.push(
			vscode.window.registerUriHandler(this)
		);
	}
 
	async handleUri(uri: vscode.Uri) {
		if (uri.authority === this.extensionId && uri.path === '/inference') {
			try {
				// Parse the query parameter directly
				const query = new URLSearchParams(uri.query);
				const targetUri = query.get('uri');
 
				if (!targetUri) {
					throw new Error('No URI provided in inference request');
				}
 
				// Decode the URI parameter
				const inferenceUri = InferenceUri.toInferenceUri(targetUri);
 
				if (this._store.hasGraph(inferenceUri)) {
					const prefixLookup = container.resolve<IPrefixLookupService>(ServiceToken.PrefixLookupService);
					const namespaces = prefixLookup.getInferencePrefixes();
					const content = await this._store.serializeGraph(inferenceUri, 'text/turtle', undefined, namespaces);
					const document = await vscode.workspace.openTextDocument({ content, language: 'turtle' });
 
					await vscode.window.showTextDocument(document);
				}
			} catch (error) {
				vscode.window.showErrorMessage(`Failed to load inference graph: ${error}`);
			}
		}
	}
}