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 | 63x 63x 63x 63x 29x 29x 29x 29x 29x 29x 29x 29x 63x 29x 29x 4x 25x 25x 25x 7x 7x 3x 22x 12x 12x 7x 15x 14x 14x 14x 1x 29x 8x 8x 63x 63x 6x 6x 6x 63x 14x 4x 4x 4x 4x 4x 4x 4x 4x 4x 25x 25x 63x 63x 39x 39x 39x 63x 25x 24x 24x 24x 29x 29x 5x 24x 24x 19x 5x 14x 22x 19x 4x 10x 10x 29x 27x 2x 1x 1x 1x 29x 1x 28x 28x 28x 28x 17x 28x 11x 28x | import * as vscode from 'vscode';
import { SH } from '@faubulous/mentor-rdf';
import { QuadContext, IToken } from '@faubulous/mentor-rdf-parsers';
import { countLeadingWhitespace, countTrailingWhitespace } from '@src/utilities';
import { IDocumentContext } from '@src/services/document/document-context.interface';
import { getLocalPartAndQuery } from '@src/utilities/uri';
import { ShaclValidationResult, ShaclValidationResultEntry } from './shacl-validation-service';
/**
* Maps SHACL validation results to VS Code diagnostics.
*/
export class ShaclDiagnosticsMapper {
/**
* Map a SHACL validation result to VS Code diagnostics.
* @param result The validation result.
* @param context The document context for resolving focus node positions.
* @returns An array of VS Code diagnostics.
*/
mapToDiagnostics(result: ShaclValidationResult, context: IDocumentContext, quadContexts?: QuadContext[]): vscode.Diagnostic[] {
const diagnostics: vscode.Diagnostic[] = [];
// Precompute the sorted subject start lines once so that resolving the
// next-subject boundary per result is a binary search instead of a scan.
const subjectStartLines = this._getSortedSubjectStartLines(context);
// Index the quad contexts once so that each result is matched with a lookup
// instead of a scan over all quads of the document.
const quadContextIndex = this._indexQuadContexts(quadContexts);
for (const entry of result.results) {
const range = this._resolveRange(entry, context, subjectStartLines, quadContextIndex);
const severity = this._mapSeverity(entry.severity);
const message = this._buildMessage(entry);
const diagnostic = new vscode.Diagnostic(range, message, severity);
diagnostic.source = 'SHACL';
diagnostic.code = getLocalPartAndQuery(entry.constraintComponent);
(diagnostic as vscode.Diagnostic & { data?: { focusNode: string } }).data = {
focusNode: entry.focusNode,
};
diagnostics.push(diagnostic);
}
return diagnostics;
}
/**
* Resolve the document range for a validation result entry.
* Prefers highlighting the offending predicate or value over the focus node.
*/
private _resolveRange(entry: ShaclValidationResultEntry, context: IDocumentContext, subjectStartLines: number[], quadContextIndex: Map<string, QuadContext>): vscode.Range {
// 0. Try to resolve the exact object token position from quad contexts.
// This handles cases where the same subject appears on multiple non-contiguous
// lines (e.g. repeated predicates), which defeats the heuristic anchor window.
const fromQuad = this._resolveRangeFromQuadContext(entry, quadContextIndex);
if (fromQuad) {
return fromQuad;
}
// Determine the focus node's start line so we can anchor relative lookups.
const focusNodeStartLine = this._getFocusNodeStartLine(entry.focusNode, context);
// Determine where the next subject starts so we can scope lookups
// to only the focus node's block (avoids picking up predicates that
// belong to a different subject further down in the document).
const nextSubjectStartLine = this._getNextSubjectStartLine(focusNodeStartLine, subjectStartLines);
// 1. Try the value IRI (most specific).
if (entry.value) {
const r = this._firstRangeAfterLine(context.references.getRanges(entry.value), focusNodeStartLine, nextSubjectStartLine);
if (r) {
return r;
}
}
// 2. Try the predicate path.
if (entry.path) {
const r = this._firstRangeAfterLine(context.references.getRanges(entry.path), focusNodeStartLine, nextSubjectStartLine);
if (r) {
return r;
}
}
// 3. Fall back to the focus node subject position.
if (focusNodeStartLine !== undefined) {
const r = context.subjects.first(entry.focusNode) ?? context.references.first(entry.focusNode);
Eif (r) {
return new vscode.Range(r.start.line, r.start.character, r.end.line, r.end.character);
}
}
// 4. Last resort: top of the file.
return new vscode.Range(0, 0, 0, 0);
}
/**
* Resolves the range for a validation result entry by finding the exact matching
* quad in the quad context list and returning the range of its object token.
* Returns undefined if no quad context is available or no match is found.
*/
private _resolveRangeFromQuadContext(entry: ShaclValidationResultEntry, quadContextIndex: Map<string, QuadContext>): vscode.Range | undefined {
if (!entry.value || !entry.path) return undefined;
const match = quadContextIndex.get(this._getQuadKey(entry.focusNode, entry.path, entry.value));
return match ? this._rangeFromToken(match.objectToken) : undefined;
}
/**
* Indexes quad contexts by their subject, predicate and object values. The first quad
* context of a statement wins, as with a search from the start of the document.
* @param quadContexts The quad contexts of the document, if available.
*/
private _indexQuadContexts(quadContexts?: QuadContext[]): Map<string, QuadContext> {
const index = new Map<string, QuadContext>();
for (const quadContext of quadContexts ?? []) {
const key = this._getQuadKey(quadContext.subject.value, quadContext.predicate.value, quadContext.object.value);
Eif (!index.has(key)) {
index.set(key, quadContext);
}
}
return index;
}
/**
* Returns the index key of a statement.
*/
private _getQuadKey(subject: string, predicate: string, object: string): string {
return `${subject}\u0000${predicate}\u0000${object}`;
}
/**
* Converts a 1-based chevrotain IToken to a 0-based VS Code Range,
* adjusting for any leading/trailing whitespace in the token image.
*/
private _rangeFromToken(token: IToken): vscode.Range {
const startLine = token.startLine ? token.startLine - 1 : 0;
const startCharacter = token.startColumn ? token.startColumn - 1 : 0;
const startWhitespace = countLeadingWhitespace(token.image);
const endLine = token.endLine ? token.endLine - 1 : 0;
const endCharacter = token.endColumn ? token.endColumn - 1 : 0;
const endWhitespace = countTrailingWhitespace(token.image);
const start = new vscode.Position(startLine, startCharacter + startWhitespace);
const end = new vscode.Position(endLine, endCharacter - endWhitespace + 1);
return new vscode.Range(start, end);
}
/**
* Returns the start line of the focus node in the document, or undefined if not found.
*/
private _getFocusNodeStartLine(focusNode: string, context: IDocumentContext): number | undefined {
const range = context.subjects.first(focusNode) ?? context.references.first(focusNode);
return range?.start.line;
}
/**
* Returns the start lines of all subjects in the document, sorted ascending.
*/
private _getSortedSubjectStartLines(context: IDocumentContext): number[] {
const lines: number[] = [];
for (const subject of context.subjects.keys()) {
const first = context.subjects.first(subject);
Eif (first) {
lines.push(first.start.line);
}
}
return lines.sort((a, b) => a - b);
}
/**
* Returns the smallest subject start line that is strictly greater than
* the given focus node start line, or undefined if no such subject exists.
* @param subjectStartLines The subject start lines, sorted ascending.
*/
private _getNextSubjectStartLine(focusNodeStartLine: number | undefined, subjectStartLines: number[]): number | undefined {
if (focusNodeStartLine === undefined) return undefined;
// Binary search for the first start line strictly greater than the focus node's.
let low = 0;
let high = subjectStartLines.length;
while (low < high) {
const mid = (low + high) >>> 1;
if (subjectStartLines[mid] > focusNodeStartLine) {
high = mid;
} else {
low = mid + 1;
}
}
return low < subjectStartLines.length ? subjectStartLines[low] : undefined;
}
/**
* Returns the first range from the given array whose start line is >= anchorLine
* and < beforeLine (if given). Returns undefined if no matching range exists.
*/
private _firstRangeAfterLine(ranges: import('vscode-languageserver-types').Range[] | undefined, anchorLine: number | undefined, beforeLine?: number): vscode.Range | undefined {
if (!ranges?.length) {
return undefined;
}
const candidates = anchorLine !== undefined
? ranges.filter(r => r.start.line >= anchorLine && (beforeLine === undefined || r.start.line < beforeLine))
: ranges;
if (!candidates.length) {
return undefined;
}
const r = candidates[0];
return new vscode.Range(r.start.line, r.start.character, r.end.line, r.end.character);
}
/**
* Map a SHACL severity IRI to a VS Code diagnostic severity.
*/
private _mapSeverity(severity: string): vscode.DiagnosticSeverity {
if (severity === SH.Violation) {
return vscode.DiagnosticSeverity.Error;
}
if (severity === SH.Warning) {
return vscode.DiagnosticSeverity.Warning;
}
Eif (severity === SH.Info) {
return vscode.DiagnosticSeverity.Information;
}
return vscode.DiagnosticSeverity.Error;
}
/**
* Build a human-readable message from a validation result entry.
*/
private _buildMessage(entry: ShaclValidationResultEntry): string {
if (entry.messages.length > 0) {
return entry.messages.join('; ');
}
const parts: string[] = [];
const component = getLocalPartAndQuery(entry.constraintComponent);
parts.push(`Constraint violation: ${component}`);
if (entry.path) {
parts.push(`Path: ${getLocalPartAndQuery(entry.path)}`);
}
if (entry.value) {
parts.push(`Value: ${entry.value}`);
}
return parts.join(' | ');
}
}
|