All files / commands select-sparql-connection.ts

85.18% Statements 23/27
75% Branches 6/8
75% Functions 3/4
84.61% Lines 22/26

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          1x     3x 1x 1x     2x     2x   2x 1x 1x     1x                       1x         1x 1x 1x   1x               1x 1x   1x   1x 1x     1x     1x    
import * as vscode from 'vscode';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { ISparqlConnectionService } from '@src/languages/sparql/services';
 
export const selectSparqlConnection = {
	id: 'mentor.command.selectSparqlConnection',
	handler: async (document: vscode.TextDocument) => {
		if (!document) {
			vscode.window.showWarningMessage('No document valid was provided.');
			return;
		}
 
		const service = container.resolve<ISparqlConnectionService>(ServiceToken.SparqlConnectionService);
 
		// Show a quick pick to select from existing SPARQL connections
		const connections = await service.getConnections();
 
		if (connections.length === 0) {
			vscode.window.showWarningMessage('No SPARQL endpoints configured. Please add one first.');
			return;
		}
 
		const items: any[] = connections.map(connection => ({
			label: `$(database) ${connection.endpointUrl}`,
			description: connection.description,
			connection: connection,
			buttons: [{
				iconPath: new vscode.ThemeIcon('edit'),
				tooltip: 'Edit Connection',
				command: 'mentor.command.editSparqlConnection',
				args: [connection]
			}]
		}));
 
		items.push({
			label: '$(database-connection) Manage Connections...',
			command: 'mentor.command.manageSparqlConnections'
		});
 
		const quickPick = vscode.window.createQuickPick();
		quickPick.items = items;
		quickPick.placeholder = 'Select a SPARQL endpoint';
 
		quickPick.onDidTriggerItemButton(async (e) => {
			const button = e.button as any;
 
			vscode.commands.executeCommand(button.command, ...button.args);
 
			quickPick.hide();
		});
 
		quickPick.onDidChangeSelection(async (e) => {
			const selected = e[0] as any;
 
			Iif (selected?.command) {
				await vscode.commands.executeCommand(selected.command);
			} else if (Eselected?.connection) {
				await service.setQuerySourceForDocument(document.uri, selected.connection.id);
			}
 
			quickPick.hide();
		});
 
		quickPick.show();
	}
};