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 | 1x 1x 1x 1x 1x 41x 41x 41x 41x 41x 41x 41x 41x 7x 41x 90x 55x 41x 41x 41x 27x 1x 1x 41x 14x 13x 1x 14x 14x 41x 13x 270x 13x 13x 13x 11x 13x 41x 1x 27x 27x 27x 27x 27x 13x 14x 14x 1x 13x 13x 27x 273x 273x 3x 270x 273x 273x 273x 270x 13x 12x 279x 12x 12x 209x 209x 209x 1x 208x 209x 209x 213x 213x 208x 208x 208x 9x 213x 42x 42x 1x 1x 40x 22x 22x 2x 20x 2x 18x 18x 3x 15x 15x 4x 4x 4x 4x 11x 1x 10x 2x 8x 1x 7x 22x 22x 22x 6x 2x 4x 2x 2x 5x 2x 2x 2x 2x 4x 4x 1x 3x 2x 1x 24x 24x 3x 21x 20x 4x 4x 4x 4x 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';
/**
* Maximum number of violated focus nodes for which ancestor walks are performed.
* Limits the cost of tree traversal when there are many violations.
* Direct leaf decorations are unaffected by this cap.
*/
const MAX_DECORATED_VIOLATIONS = 100;
/**
* 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 {
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()`.
*/
private _ancestorSeverity = new Map<string, string>();
private readonly _onDidChangeFileDecorations = new vscode.EventEmitter<vscode.Uri | vscode.Uri[] | undefined>();
readonly onDidChangeFileDecorations? = this._onDidChangeFileDecorations.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();
// 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);
}
});
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 SHACL violations for the new active document and refresh decorations.
this._updateShaclViolations();
this._onDidChangeFileDecorations.fire(undefined);
});
this._validationService.onDidValidate(() => {
// Reload violations for the currently active document and refresh decorations.
this._updateShaclViolations();
// Fire for violated URIs and ancestor URIs so VS Code proactively caches
// their decorations, then fire undefined to refresh all visible items.
const violatedUris = [...this._shaclViolations.keys()].map(iri => vscode.Uri.parse(iri));
const ancestorUris = [...this._ancestorSeverity.keys()].map(uri => vscode.Uri.parse(uri));
const allUris = [...violatedUris, ...ancestorUris];
if (allUris.length > 0) {
this._onDidChangeFileDecorations.fire(allUris);
}
this._onDidChangeFileDecorations.fire(undefined);
});
this._settings.onDidChange("view.activeLanguage", () => {
// When the active language changes, the decorations need to be updated.
this._onDidChangeFileDecorations.fire(undefined);
});
}
/**
* Rebuild the violations map from the last validation result for the currently active document.
*/
private _updateShaclViolations(): void {
this._shaclViolations.clear();
this._ancestorSeverity.clear();
const activeContext = this._contextService.activeContext;
const documentUri = activeContext?.uri;
if (!documentUri) {
return;
}
const last = this._validationService.getLastResult(documentUri);
if (!last) {
return;
}
// Severity precedence: Violation > Warning > Info
const severityRank: Record<string, number> = {
[SH.Violation]: 3,
[SH.Warning]: 2,
[SH.Info]: 1,
};
// Step 1: Build per-focus-node severity map.
// 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[iri]) {
continue;
}
const newRank = severityRank[entry.severity] ?? 0;
const existing = this._shaclViolations.get(iri);
const existingRank = existing ? (severityRank[existing] ?? 0) : 0;
if (newRank > existingRank) {
this._shaclViolations.set(iri, entry.severity);
}
}
// Step 2: Walk ancestors for each violated node (capped for performance).
// 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).
if (this._nodeProvider) {
// Sort by severity so the most important violations are processed first
// when we hit the cap.
const entries = [...this._shaclViolations.entries()]
.sort((a, b) => (severityRank[b[1]] ?? 0) - (severityRank[a[1]] ?? 0));
const limit = Math.min(entries.length, MAX_DECORATED_VIOLATIONS);
for (let i = 0; i < limit; i++) {
const [iri, severity] = entries[i];
const treeNode = this._nodeProvider.getNodeForUri(iri);
if (!treeNode) {
continue;
}
const rank = severityRank[severity] ?? 0;
let ancestor = treeNode.parent;
while (ancestor) {
// Use the node's resourceUri as the decoration key. Intermediate grouping
// nodes (e.g. PropertyClassNode, IndividualClassNode) override getResourceUri()
// to return a synthetic mentor: URI so they can be safely decorated without
// causing false positives on other tree branches that share the same real IRI.
const resourceUri = ancestor.getResourceUri()?.toString();
if (resourceUri?.startsWith('mentor:')) {
const existingAncestor = this._ancestorSeverity.get(resourceUri);
const existingAncestorRank = existingAncestor ? (severityRank[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 || uri.scheme === 'file' || uri.scheme === 'untitled') {
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[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[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 by walking .parent from each violated node.
if (uri.scheme === 'mentor') {
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;
}
} |