All files / commands validate-document.ts

84.21% Statements 16/19
80% Branches 8/10
100% Functions 1/1
84.21% Lines 16/19

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            1x     4x   4x 1x       3x 3x   3x     3x   3x         3x   3x   3x 1x   2x         2x 1x          
import * as vscode from 'vscode';
import { container } from 'tsyringe';
import { IWorkspaceIndexerService } from '@src/services/core';
import { ServiceToken } from '@src/services/tokens';
import { ShaclValidationService } from '@src/services/validation/shacl-validation-service';
 
export const validateDocument = {
	id: 'mentor.command.validateDocument',
	handler: async () => {
		const editor = vscode.window.activeTextEditor;
 
		if (!editor) {
			return;
		}
 
		// Wait for background workspace indexing to finish so required shape graphs are available.
		const indexerService = container.resolve<IWorkspaceIndexerService>(ServiceToken.WorkspaceIndexerService);
		await indexerService.waitForIndexed();
 
		const validationService = container.resolve<ShaclValidationService>(ServiceToken.ShaclValidationService);
 
		// If no SHACL shapes are configured for this document, open shape configuration first.
		const effectiveShapes = validationService.getEffectiveShapeGraphs(editor.document.uri);
 
		Iif (effectiveShapes.length === 0) {
			await vscode.commands.executeCommand('mentor.command.manageShaclShapes');
			return;
		}
 
		const result = await validationService.validateDocument(editor.document.uri);
 
		Iif (!result) {
			return;
		} else if (result.conforms) {
			vscode.window.showInformationMessage('SHACL validation: No issues found.');
		} else {
			const action = await vscode.window.showWarningMessage(
				`SHACL validation: ${result.results.length} issue(s) found.`,
				'View'
			);
 
			if (action === 'View') {
				await vscode.commands.executeCommand('workbench.panel.markers.view.focus');
			}
		}
	}
};