All files / views/trees/definition-tree definition-node-decoration-provider.ts

88.67% Statements 141/159
85.18% Branches 92/108
94.44% Functions 17/18
88.67% Lines 141/159

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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467                              1x                                                                         1x   44x   44x   44x   44x   44x           44x                       44x           44x   44x   44x   44x             44x         7x       44x       103x       64x     44x 44x   44x     27x 1x   1x         27x               15x   14x   1x             15x                 18x         1x                                             33x   33x 33x 33x   33x       19x   19x 169x   169x 3x     166x 169x 169x   169x 166x         33x       33x 3x     30x 30x 30x   30x       30x 15x         33x 17x     16x 2x         16x                           7x 2x     5x 5x   5x 1x       4x   7x     154x 154x   154x 154x       154x       154x             154x   154x 157x   157x 154x 154x   154x 5x       157x           45x   45x   1x     1x     43x           26x   26x 2x     24x 6x     18x   18x 3x     15x   15x 4x 4x 4x   4x     11x 1x     10x     2x     8x 1x     7x 26x   26x   26x 6x 2x     4x     2x       2x     5x 2x 2x 2x   2x                 4x   4x 1x     3x 2x     1x       28x           28x 7x   7x     21x             24x 8x 8x 8x 8x     16x             16x             16x    
import * as vscode from 'vscode';
import { NamedNode, VocabularyRepository, SH } from '@faubulous/mentor-rdf';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { ISettingsService } from '@src/services/core';
import { IDocumentContextService } from '@src/services/document';
import { getConfig } from '@src/utilities/vscode/config';
import { ShaclValidationService } from '@src/services/validation/shacl-validation-service';
import { DefinitionNodeProvider } from './definition-node-provider';
import { DefinitionTreeNode } from './definition-tree-node';
 
/**
 * SHACL severity precedence used when a node aggregates several results:
 * `sh:Violation` > `sh:Warning` > `sh:Info`.
 */
const SEVERITY_RANK: Record<string, number> = {
	[SH.Violation]: 3,
	[SH.Warning]: 2,
	[SH.Info]: 1,
};
 
/**
 * Indicates the where missing language tags should be decorated.
 */
enum MissingLanguageTagDecorationScope {
	/**
	 * Disable the decoration of missing language tags.
	 */
	Disabled,
	/**
	 * Decorate missing language tags in all sources.
	 */
	All,
	/**
	 * Decorate missing language tags only in the active document.
	 */
	Document
}
 
/**
 * A decoration provider that adds a badge to definition tree nodes.
 */
export class DefinitionNodeDecorationProvider implements vscode.FileDecorationProvider, vscode.Disposable {
 
	/**
	 * URI schemes this provider decorates: `mentor:` container nodes and the
	 * schemes of resource IRIs rendered in the definition tree. The provider is
	 * registered window-wide, so every other scheme — editor tabs, notebook
	 * cells, `user:`/`workspace:`/`git:` documents — must be left untouched:
	 * returning a decoration for them re-colors their tab and Open Editors
	 * labels on every invalidation.
	 */
	private static readonly _decoratedSchemes = new Set(['mentor', 'http', 'https', 'urn']);
 
	private readonly _subscriptions: vscode.Disposable[] = [];
 
	private readonly _warningColor = new vscode.ThemeColor("list.warningForeground");
 
	private readonly _errorColor = new vscode.ThemeColor("list.errorForeground");
 
	private readonly _disabledColor = new vscode.ThemeColor("descriptionForeground");
 
	private _labelPredicates = new Set<string>();
 
	/**
	 * Maps focus node IRIs to their worst SHACL severity (`sh:Violation`, `sh:Warning`, `sh:Info`)
	 * for the currently active document's validation result.
	 */
	private _shaclViolations = new Map<string, string>();
 
	/**
	 * Maps ancestor node resource URIs to the worst SHACL severity among their descendant focus
	 * nodes. Built by walking the `.parent` chain of each violated tree node. Only `mentor:`
	 * URIs are recorded — real-IRI nodes that appear in multiple branches use synthetic
	 * `mentor:properties:<iri>` / `mentor:individuals:<iri>` URIs via `getResourceUri()`.
	 *
	 * Built lazily (see {@link _ensureAncestorSeverity}) the first time a container decoration is
	 * requested after a change, so the tree walk runs only when the tree is actually rendered and
	 * only once per validation — never eagerly on every `onDidValidate`.
	 */
	private _ancestorSeverity = new Map<string, string>();
 
	/**
	 * Whether {@link _ancestorSeverity} needs rebuilding from the current {@link _shaclViolations}.
	 * Set when validations change; cleared once the (lazy) rebuild runs.
	 */
	private _ancestorSeverityDirty = true;
 
	private readonly _onDidChangeFileDecorations = new vscode.EventEmitter<vscode.Uri | vscode.Uri[] | undefined>();
 
	readonly onDidChangeFileDecorations? = this._onDidChangeFileDecorations.event;
 
	private readonly _onDidChangeSeverities = new vscode.EventEmitter<void>();
 
	/**
	 * Fired when a validation changed the SHACL severities of the active document's nodes.
	 * The tree colors its node icons by severity, so it rebuilds on this event; validations
	 * that leave the severities unchanged, which are most of them, do not fire it.
	 */
	readonly onDidChangeSeverities = this._onDidChangeSeverities.event;
 
	private _decorationScope: MissingLanguageTagDecorationScope;
 
	private get _vocabulary() {
		return container.resolve<VocabularyRepository>(ServiceToken.VocabularyRepository);
	}
 
	private get _settings() {
		return container.resolve<ISettingsService>(ServiceToken.SettingsService);
	}
 
	private get _contextService() {
		return container.resolve<IDocumentContextService>(ServiceToken.DocumentContextService);
	}
 
	private get _validationService() {
		return container.resolve<ShaclValidationService>(ServiceToken.ShaclValidationService);
	}
 
	constructor(private readonly _nodeProvider?: DefinitionNodeProvider) {
		this._decorationScope = this._getDecorationScopeFromConfiguration();
 
		this._subscriptions.push(
			// If the configuration for decorating missing language tags changes, update the decoration provider.
			vscode.workspace.onDidChangeConfiguration((e) => {
				if (e.affectsConfiguration('mentor.definitionTree.decorateMissingLanguageTags')) {
					this._decorationScope = this._getDecorationScopeFromConfiguration();
 
					this._onDidChangeFileDecorations.fire(undefined);
				}
 
				// The cached label predicates otherwise only update on document context
				// changes; apply a settings change immediately.
				Iif (e.affectsConfiguration('mentor.predicates.label')) {
					this._labelPredicates = new Set(this._contextService.activeContext?.predicates.label ?? []);
 
					this._onDidChangeFileDecorations.fire(undefined);
				}
			}),
 
			this._contextService.onDidChangeDocumentContext((context) => {
				if (context) {
					// When the context changes, the label predicates need to be updated.
					this._labelPredicates = new Set(context?.predicates.label ?? []);
				} else {
					this._labelPredicates = new Set();
				}
 
				// Reload the (cheap) leaf violations for the new active document and invalidate
				// the lazily-built ancestor aggregation; VS Code re-requests visible items.
				// Always fire: the grey and language decorations depend on the new context
				// even when the SHACL severities are unchanged.
				this._reloadViolations(true);
			}),
 
			this._validationService.onDidValidate(() => {
				// Rebuild only the cheap leaf map and mark the ancestor aggregation dirty. The
				// expensive tree walk is deferred to _ensureAncestorSeverity, which runs lazily
				// when a container decoration is actually requested (i.e. the tree is visible).
				// Validations fire for every open document (and on every edit that drops a
				// result); skip the repaint when the severities did not change.
				this._reloadViolations(false);
			}),
 
			this._settings.onDidChange("view.activeLanguage", () => {
				// When the active language changes, the decorations need to be updated.
				this._onDidChangeFileDecorations.fire(undefined);
			})
		);
	}
 
	dispose(): void {
		for (const subscription of this._subscriptions) {
			subscription.dispose();
		}
 
		this._subscriptions.length = 0;
		this._onDidChangeFileDecorations.dispose();
		this._onDidChangeSeverities.dispose();
	}
 
	/**
	 * Rebuild the cheap per-focus-node leaf severity map for the active document and invalidate
	 * the lazily-built ancestor aggregation, then ask VS Code to re-request decorations for all
	 * visible items. Cheap enough to run on every validation / context change.
	 * @param forceFire Fire the change event even when the severities are unchanged — needed
	 * when the active context changed, because the other decorations depend on it.
	 */
	private _reloadViolations(forceFire: boolean): void {
		const next = new Map<string, string>();
 
		const activeContext = this._contextService.activeContext;
		const documentUri = activeContext?.uri;
		const last = documentUri ? this._validationService.getLastResult(documentUri) : undefined;
 
		if (last) {
			// Only include violations whose focus node is a subject in the active document.
			// This prevents false-positive decorations for nodes that are merely referenced
			// (e.g. as sh:path objects) but have violations originating from imported shapes.
			const subjects = activeContext?.subjects;
 
			for (const entry of last.results) {
				const iri = entry.focusNode;
 
				if (subjects && !subjects.has(iri)) {
					continue;
				}
 
				const newRank = SEVERITY_RANK[entry.severity] ?? 0;
				const existing = next.get(iri);
				const existingRank = existing ? (SEVERITY_RANK[existing] ?? 0) : 0;
 
				if (newRank > existingRank) {
					next.set(iri, entry.severity);
				}
			}
		}
 
		const severitiesChanged = !DefinitionNodeDecorationProvider._severitiesEqual(this._shaclViolations, next);
 
		// Unchanged severities mean unchanged decorations: keep the lazily-built
		// ancestor aggregation and skip the workbench-wide repaint.
		if (!forceFire && !severitiesChanged) {
			return;
		}
 
		this._shaclViolations = next;
		this._ancestorSeverity.clear();
		this._ancestorSeverityDirty = true;
 
		this._onDidChangeFileDecorations.fire(undefined);
 
		// A change of the active context rebuilds the tree anyway; only a validation needs to
		// ask for the rebuild that recolors the node icons.
		if (severitiesChanged && !forceFire) {
			this._onDidChangeSeverities.fire();
		}
	}
 
	private static _severitiesEqual(a: Map<string, string>, b: Map<string, string>): boolean {
		if (a.size !== b.size) {
			return false;
		}
 
		for (const [iri, severity] of a) {
			Iif (b.get(iri) !== severity) {
				return false;
			}
		}
 
		return true;
	}
 
	/**
	 * Lazily build {@link _ancestorSeverity} from the current leaf violations, once per change.
	 *
	 * Called from {@link _getShaclSeverity} only when a container (`mentor:`) decoration is
	 * actually requested — i.e. the tree is being rendered — so the tree walk never runs for a
	 * hidden panel and never runs per file during a batch. For every violated leaf (uncapped) we
	 * resolve its tree node against a single, reused root list and walk its `.parent` chain,
	 * recording the worst severity on each `mentor:` ancestor URI. Fetching the roots once avoids
	 * the per-node cold rebuild that `getNodeForUri` would otherwise incur.
	 */
	private _ensureAncestorSeverity(): void {
		if (!this._ancestorSeverityDirty) {
			return;
		}
 
		this._ancestorSeverityDirty = false;
		this._ancestorSeverity.clear();
 
		if (!this._nodeProvider || this._shaclViolations.size === 0) {
			return;
		}
 
		// Resolve the root nodes once and reuse them for every violation lookup.
		const roots = this._nodeProvider.getChildren(undefined) ?? [];
 
		for (const [iri, severity] of this._shaclViolations) {
			let treeNode: DefinitionTreeNode | undefined;
 
			for (const root of roots) {
				treeNode = root.resolveNodeForUri(iri);
 
				Eif (treeNode) {
					break;
				}
			}
 
			Iif (!treeNode) {
				continue;
			}
 
			const rank = SEVERITY_RANK[severity] ?? 0;
 
			// Only record severity for `mentor:` container nodes — intermediate nodes with real
			// IRIs are skipped because FileDecorationProvider decorates by URI, and the same IRI
			// may appear in multiple tree branches (e.g. a property that is both an ancestor under
			// shapes and a leaf under properties). Grouping nodes override getResourceUri() to
			// return a synthetic mentor: URI so they can be decorated without cross-branch bleed.
			let ancestor = treeNode.parent;
 
			while (ancestor) {
				const resourceUri = ancestor.getResourceUri()?.toString();
 
				if (resourceUri?.startsWith('mentor:')) {
					const existingAncestor = this._ancestorSeverity.get(resourceUri);
					const existingAncestorRank = existingAncestor ? (SEVERITY_RANK[existingAncestor] ?? 0) : 0;
 
					if (rank > existingAncestorRank) {
						this._ancestorSeverity.set(resourceUri, severity);
					}
				}
 
				ancestor = ancestor.parent;
			}
		}
	}
 
	private _getDecorationScopeFromConfiguration(): MissingLanguageTagDecorationScope {
		const result = getConfig().get('definitionTree.decorateMissingLanguageTags');
 
		switch (result) {
			case 'Document': {
				return MissingLanguageTagDecorationScope.Document;
			}
			case 'All': {
				return MissingLanguageTagDecorationScope.All;
			}
			default: {
				return MissingLanguageTagDecorationScope.Disabled;
			}
		}
	}
 
	provideFileDecoration(uri: vscode.Uri, token: vscode.CancellationToken) {
		const context = this._contextService.activeContext;
 
		if (!context || !uri || !DefinitionNodeDecorationProvider._decoratedSchemes.has(uri.scheme)) {
			return undefined;
		}
 
		if (uri.scheme === 'mentor') {
			return this._buildShaclDecoration(this._getShaclSeverity(uri), false);
		}
 
		const shaclDecoration = this._buildShaclDecoration(this._getShaclSeverity(uri), false);
 
		if (shaclDecoration) {
			return shaclDecoration;
		}
 
		const node = new NamedNode(uri.toString());
 
		if (!context.subjects.has(node.value)) {
			const result = new vscode.FileDecoration(undefined, undefined, this._disabledColor);
			result.propagate = false;
			result.tooltip = `This subject is not defined in the active document.`;
 
			return result;
		}
 
		if (this._decorationScope === MissingLanguageTagDecorationScope.Disabled) {
			return undefined;
		}
 
		if (!context.primaryLanguage || !context.activeLanguage) {
			// Note: The document may not have a language set if 
			// there are no language tags used in the document.
			return undefined;
		}
 
		if (!context.references.has(node.value)) {
			return undefined;
		}
 
		const graphUris = this._decorationScope === MissingLanguageTagDecorationScope.Document ? context.graphs : undefined;
		const activeLanguage = context.activeLanguage;
 
		let hasLabels = false;
 
		for (let triple of this._vocabulary.store.matchAll(graphUris, node, null, null, false)) {
			if (triple.object.termType !== "Literal" || !this._labelPredicates.has(triple.predicate.value)) {
				continue;
			}
 
			if (!triple.object.language || triple.object.language.startsWith(activeLanguage)) {
				// Either there is no language tag (valid for all languages) 
				// or the language tag is in the active language.
				return undefined;
			}
 
			// Only enable the decoration if the subject is a subject in the configured graphs (document or entire background).
			hasLabels = true;
		}
 
		if (hasLabels) {
			const result = new vscode.FileDecoration(undefined, undefined, this._warningColor);
			result.propagate = true;
			result.tooltip = `This definition is not available in the active language @${activeLanguage}.`;
 
			return result;
		}
	}
 
	/**
	 * Returns the SHACL issue color for the given resource URI.
	 * Only warning and violation severities are treated as issues for tree icon coloring.
	 */
	getIssueColor(uri: vscode.Uri | undefined): vscode.ThemeColor | undefined {
		const severity = this._getShaclSeverity(uri);
 
		if (severity === SH.Violation) {
			return this._errorColor;
		}
 
		if (severity === SH.Warning) {
			return this._warningColor;
		}
 
		return undefined;
	}
 
	private _getShaclSeverity(uri: vscode.Uri | undefined): string | undefined {
		Iif (!uri) {
			return undefined;
		}
 
		// Container nodes (mentor: scheme) and intermediate ancestor nodes are decorated via the
		// ancestor severity map, built lazily on first request (only when the tree is rendered).
		if (uri.scheme === 'mentor') {
			this._ensureAncestorSeverity();
 
			return this._ancestorSeverity.get(uri.toString());
		}
 
		return this._shaclViolations.get(uri.toString());
	}
 
	/**
	 * Build a SHACL decoration for the given severity, or undefined if no severity is set.
	 */
	private _buildShaclDecoration(severity: string | undefined, propagate: boolean): vscode.FileDecoration | undefined {
		if (severity === SH.Violation) {
			const result = new vscode.FileDecoration('●', 'SHACL violation', this._errorColor);
			result.propagate = propagate;
			result.tooltip = 'This node has a SHACL violation.';
			return result;
		}
 
		Iif (severity === SH.Warning) {
			const result = new vscode.FileDecoration('●', 'SHACL warning', this._warningColor);
			result.propagate = propagate;
			result.tooltip = 'This node has a SHACL warning.';
			return result;
		}
 
		Iif (severity === SH.Info) {
			const result = new vscode.FileDecoration('●', 'SHACL info', this._warningColor);
			result.propagate = false;
			result.tooltip = 'This node has a SHACL info message.';
			return result;
		}
 
		return undefined;
	}
}