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

96.77% Statements 30/31
87.5% Branches 14/16
100% Functions 6/6
96.77% Lines 30/31

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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111                        22x         22x     22x 22x   22x 1x     22x 1x                   9x   9x 1x     8x 8x     8x 8x             8x   8x     8x 3x 3x 3x 3x       3x             3x             8x 8x               8x       8x     8x                 3x    
import * as vscode from 'vscode';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { ISparqlConnectionService, ISparqlQueryService } from '@src/languages/sparql/services';
import { getConfig } from '@src/utilities/vscode/config';
import { MENTOR_WORKSPACE_STORE } from '../services/sparql-connection-service';
import { SparqlConnection } from '../services/sparql-connection';
 
/**
 * 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;
	private _queryService: ISparqlQueryService;
 
	public readonly onDidChangeCodeLenses = this._onDidChangeCodeLenses.event;
 
	constructor() {
		this._connectionService = container.resolve<ISparqlConnectionService>(ServiceToken.SparqlConnectionService);
		this._queryService = container.resolve<ISparqlQueryService>(ServiceToken.SparqlQueryService);
 
		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 connectionUrl = this._getConnectionLabel(connection);
		const connectionCodeLens = new vscode.CodeLens(range, {
			title: `$(database)\u00A0Connection: ${connectionUrl}`,
			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}\u00A0Inference: ${inferenceText}`,
				tooltip: inferenceTooltip,
				command: 'mentor.command.toggleDocumentInference',
				arguments: [document.uri],
			});
 
			codeLenses.push(inferenceCodeLens);
		}
 
		// Only show the Execute code lens for regular SPARQL documents.
		// Notebook cells have a native run button that outputs results inline;
		// triggering executeSparqlQuery from a cell document would route to the
		// SPARQL results panel instead.
		Eif (document.uri.scheme !== 'vscode-notebook-cell') {
			codeLenses.push(new vscode.CodeLens(range, {
				title: '$(play)\u00A0Execute',
				command: 'mentor.command.executeSparqlQuery',
				tooltip: 'Execute this SPARQL query',
				arguments: [this._queryService.createQueryFromDocument(document)],
			}));
		}
 
		return codeLenses;
	}
 
	private _getConnectionLabel(connection: SparqlConnection): string {
		Iif (connection.id === MENTOR_WORKSPACE_STORE.id) {
			return connection.id;
		} else {
			return connection.endpointUrl;
		}
	}
 
	/**
	 * 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();
	}
}