All files / languages/sparql/providers sparql-code-lens-provider.ts

100% Statements 24/24
100% Branches 12/12
100% Functions 5/5
100% Lines 24/24

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                    22x       22x     22x   22x 1x     22x 1x                   9x   9x 1x     8x 8x     8x             8x   8x     8x 3x 3x 3x 3x       3x             3x     8x               3x    
import * as vscode from 'vscode';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { ISparqlConnectionService } from '@src/languages/sparql/services';
import { getConfig } from '@src/utilities/vscode/config';
 
/**
 * Provides a CodeLens to display and change the current SPARQL endpoint.
 */
export class SparqlCodeLensProvider implements vscode.CodeLensProvider {
	private _onDidChangeCodeLenses = new vscode.EventEmitter<void>();
 
	private _connectionService: ISparqlConnectionService;
 
	public readonly onDidChangeCodeLenses = this._onDidChangeCodeLenses.event;
 
	constructor() {
		this._connectionService = container.resolve<ISparqlConnectionService>(ServiceToken.SparqlConnectionService);
 
		this._connectionService.onDidChangeConnectionForDocument(() => {
			this.refresh();
		});
 
		this._connectionService.onDidChangeConnections(() => {
			this.refresh();
		});
	}
 
	/**
	 * Computes the CodeLens for a given document.
	 * @param document The document to compute the CodeLens for.
	 * @returns A promise that resolves to an array of CodeLenses.
	 */
	public async provideCodeLenses(document: vscode.TextDocument): Promise<vscode.CodeLens[]> {
		const connection = this._connectionService.getConnectionForDocument(document.uri);
 
		if (!connection) {
			return [];
		}
 
		const range = new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0));
		const codeLenses: vscode.CodeLens[] = [];
 
		// Connection CodeLens
		const connectionCodeLens = new vscode.CodeLens(range, {
			title: `$(database)\u00A0${connection.endpointUrl}`,
			tooltip: 'Click to change the SPARQL endpoint for this file',
			command: 'mentor.command.selectSparqlConnection',
			arguments: [document],
		});
 
		codeLenses.push(connectionCodeLens);
 
		const config = getConfig();
 
		// Inference status CodeLens (only for connections that support inference)
		if (config.get('inference.enabled') && this._connectionService.supportsInference(connection)) {
			const inferenceEnabled = this._connectionService.getInferenceEnabledForDocument(document.uri);
			const inferenceIcon = inferenceEnabled ? '$(lightbulb-sparkle)' : '$(lightbulb-sparkle)';
			const inferenceText = inferenceEnabled ? 'on' : 'off';
			const inferenceTooltip = inferenceEnabled
				? 'Inferred triples are included. Click to exclude them.'
				: 'Inferred triples are excluded. Click to include them.';
 
			const inferenceCodeLens = new vscode.CodeLens(range, {
				title: `${inferenceIcon}\u00A0${inferenceText}`,
				tooltip: inferenceTooltip,
				command: 'mentor.command.toggleDocumentInference',
				arguments: [document.uri],
			});
 
			codeLenses.push(inferenceCodeLens);
		}
 
		return codeLenses;
	}
 
	/**
	 * Public method to manually trigger a refresh of the CodeLenses.
	 * This is useful after a command changes the source for a notebook.
	 */
	public refresh(): void {
		this._onDidChangeCodeLenses.fire();
	}
}