All files / commands/sparql select-sparql-connection.ts

90.62% Statements 29/32
75% Branches 6/8
80% Functions 4/5
90.32% Lines 28/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                  1x         1x     5x 1x 1x     4x 4x     4x   4x 1x 1x     3x                       3x 3x         3x 3x 3x   3x 1x 1x     1x     3x               3x 1x   1x 1x     1x     3x    
import * as vscode from 'vscode';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { ISparqlConnectionRegistry } from '@src/languages/sparql/services';
import { IDocumentConnectionService } from '@src/languages/sparql/services';
 
/**
 * Title bar button that opens the connection settings.
 */
const MANAGE_CONNECTIONS_BUTTON: vscode.QuickInputButton = {
	iconPath: new vscode.ThemeIcon('gear'),
	tooltip: 'Manage connections…'
};
 
export const selectSparqlConnection = {
	id: 'mentor.command.selectSparqlConnection',
	handler: async (document: vscode.TextDocument) => {
		if (!document) {
			vscode.window.showWarningMessage('No document valid was provided.');
			return;
		}
 
		const connectionRegistry = container.resolve<ISparqlConnectionRegistry>(ServiceToken.SparqlConnectionRegistry);
		const documentConnectionService = container.resolve<IDocumentConnectionService>(ServiceToken.DocumentConnectionService);
 
		// Show a quick pick to select from existing SPARQL connections
		const connections = await connectionRegistry.getConnections();
 
		if (connections.length === 0) {
			vscode.window.showWarningMessage('No SPARQL endpoints configured. Please add one first.');
			return;
		}
 
		const items: any[] = connections.map(connection => ({
			label: `$(arrow-swap) ${connection.endpointUrl}`,
			description: connection.description,
			connection: connection,
			buttons: [{
				iconPath: new vscode.ThemeIcon('edit'),
				tooltip: 'Edit Connection',
				command: 'mentor.command.editSparqlConnection',
				args: [connection]
			}]
		}));
 
		const quickPick = vscode.window.createQuickPick();
		quickPick.items = items;
		// Do not pre-focus the first connection (the workspace store): assigning items
		// defaults the active item to the first row, which reads as if it were already
		// the selected connection. Clearing it leaves no connection highlighted until the
		// user picks one.
		quickPick.activeItems = [];
		quickPick.placeholder = 'Select a SPARQL endpoint';
		quickPick.buttons = [MANAGE_CONNECTIONS_BUTTON];
 
		quickPick.onDidTriggerButton((button) => {
			Eif (button === MANAGE_CONNECTIONS_BUTTON) {
				vscode.commands.executeCommand('mentor.command.manageSparqlConnections');
			}
 
			quickPick.hide();
		});
 
		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;
 
			Eif (selected?.connection) {
				await documentConnectionService.setQuerySourceForDocument(document.uri, selected.connection.id);
			}
 
			quickPick.hide();
		});
 
		quickPick.show();
	}
};