All files / languages/turtle/providers turtle-validation-codelens-provider.ts

100% Statements 56/56
90% Branches 27/30
100% Functions 12/12
100% Lines 56/56

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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144                        12x   12x   12x   12x   12x     6x       1x       9x       12x 2x 1x 1x           1x 1x   1x   1x 1x 1x       1x 1x 1x       1x 1x 1x       1x 1x       7x 7x 1x     6x 1x     6x 1x     5x   5x 1x     4x 4x   4x 4x 4x 3x     7x   7x 1x 3x 1x   2x     4x           4x 2x               4x   4x 2x       2x             4x        
import * as vscode from 'vscode';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { IWorkspaceIndexerService } from '@src/services/core';
import { IDocumentContextService } from '@src/services/document';
import { ShaclValidationService } from '@src/services/validation/shacl-validation-service';
import { getConfig } from '@src/utilities/vscode/config';
 
/**
 * Provides SHACL validation CodeLens actions at the top of RDF documents.
 */
export class TurtleValidationCodeLensProvider implements vscode.CodeLensProvider {
	private _initialized: boolean = false;
 
	private _initializing: boolean = false;
 
	private _enabled: boolean = false;
 
	private readonly _onDidChangeCodeLenses = new vscode.EventEmitter<void>();
 
	onDidChangeCodeLenses: vscode.Event<void> = this._onDidChangeCodeLenses.event;
 
	private get _contextService() {
		return container.resolve<IDocumentContextService>(ServiceToken.DocumentContextService);
	}
 
	private get _workspaceIndexerService() {
		return container.resolve<IWorkspaceIndexerService>(ServiceToken.WorkspaceIndexerService);
	}
 
	private get _validationService() {
		return container.resolve<ShaclValidationService>(ServiceToken.ShaclValidationService);
	}
 
	constructor() {
		vscode.workspace.onDidChangeConfiguration((e) => {
			if (e.affectsConfiguration('mentor.shacl') || e.affectsConfiguration('mentor.shacl.enabled')) {
				this._enabled = getConfig().get('shacl.enabled', false);
				this._onDidChangeCodeLenses.fire();
			}
		});
	}
 
	private async _initialize() {
		this._initializing = true;
		this._initialized = false;
 
		this._enabled = getConfig().get('shacl.enabled', false);
 
		this._workspaceIndexerService.waitForIndexed().then(() => {
			Eif (this._enabled) {
				this._onDidChangeCodeLenses.fire();
			}
		});
 
		this._contextService.onDidChangeDocumentContext(() => {
			Eif (this._enabled) {
				this._onDidChangeCodeLenses.fire();
			}
		});
 
		this._validationService.onDidValidate(() => {
			Eif (this._enabled) {
				this._onDidChangeCodeLenses.fire();
			}
		});
 
		this._initialized = true;
		this._initializing = false;
	}
 
	provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeLens[]> {
		return new Promise(async (resolve) => {
			if (this._initializing) {
				return resolve([]);
			}
 
			if (!this._initialized) {
				await this._initialize();
			}
 
			if (!this._enabled) {
				return resolve([]);
			}
 
			const context = this._contextService.contexts[document.uri.toString()];
 
			if (!context) {
				return resolve([]);
			}
 
			const range = new vscode.Range(0, 0, 0, 0);
			const result: vscode.CodeLens[] = [];
 
			const shapeFiles = this._validationService.getEffectiveShapeGraphs(document.uri);
			const shapeCount = shapeFiles.length;
			const shapeFilesTooltip = shapeCount > 0
				? `Configured SHACL shapes:\n\n${shapeFiles.map(shapeFile => `- ${shapeFile}`).join('\n')}`
				: 'Configure SHACL shape files for this document';
 
			let title = "";
 
			if (shapeCount > 1) {
				title += `$(file)\u00A0Validation: ${shapeCount} files enabled`;
			} else if (shapeCount === 1) {
				title += `$(file)\u00A0Validation: ${shapeCount} file enabled`;
			} else {
				title += `$(file)\u00A0Validation: not configured`;
			}
 
			result.push(new vscode.CodeLens(range, {
				title: title,
				command: 'mentor.command.manageShaclShapes',
				tooltip: shapeFilesTooltip
			}));
 
			if (shapeCount > 0) {
				result.push(new vscode.CodeLens(range, {
					title: '$(run-coverage)\u00A0Validate',
					command: 'mentor.command.validateDocument',
					tooltip: 'Validate this document against configured SHACL shape files'
				}));
			}
 
			// Show status from last validation, if available
			const lastResult = this._validationService.getLastResult(document.uri);
 
			if (lastResult) {
				const statusTitle = lastResult.conforms
					? '$(pass)\u00A0Conforms'
					: `$(error)\u00A0${lastResult.results.length} issue(s)`;
 
				result.push(new vscode.CodeLens(range, {
					title: statusTitle,
					command: 'mentor.command.viewShaclReport',
					tooltip: 'View the SHACL validation report'
				}));
			}
 
			return resolve(result);
		});
	}
}