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 | 256x 63x 256x 256x 256x 256x 256x 256x 256x 5x 59x 59x 256x 20x 19x 19x 19x 14x 8x 8x 20x 18x 10x 10x 9x 1x 20x 54x 256x 256x 256x 256x 3x 11x 3x 2x 1x 5x 5x 3x 3x 21x 21x 21x 12x 12x 12x 10x 2x 2x 1x 1x 2x 2x 2x 1x 1x 2x 8x 96x 96x 96x 96x 28x 17x 15x 15x 1x 14x 3x 11x 1x 11x 2x 11x 10x 2x 1x 1x 23x 9x 91x 90x 88x 5x 5x 8x 7x 2x 5x 1x 5x 42x 42x 42x 42x 2x 40x 8x 2x 2x 1x 1x 1x 1x 7x 5x 5x 5x 5x 15x | import * as vscode from 'vscode';
import { Quad_Subject } from "@rdfjs/types";
import { Store, VocabularyRepository, _OWL, _RDF, _RDFS, _SH, _SKOS, _SKOS_XL, SH } from '@faubulous/mentor-rdf';
import { Uri, NamedNode, BlankNode, Literal } from '@faubulous/mentor-rdf';
import { LanguageTagUsageStats } from '@faubulous/mentor-rdf';
import { IToken } from '@faubulous/mentor-rdf-parsers';
import { ISettingsService } from '@src/services/core';
import { WorkspaceUri } from '@src/providers/workspace-uri';
import { TreeLabelStyle } from '@src/services/core/settings-service';
import { getPredicatesConfig } from '@src/utilities/vscode/config';
import { IDocumentContext, ITokenizedDocumentContext } from './document-context.interface';
import { RangeIndex } from '@src/utilities/range-index';
/**
* A literal value with optional language tag.
*/
export interface Label {
/**
* The value of the literal.
*/
value: string;
/**
* The language tag of the literal, if any.
*/
language: string | undefined;
}
/**
* A class that provides access to RDF document specific data such as namespaces, graphs and token maps.
*/
export abstract class DocumentContext implements IDocumentContext {
readonly uri: vscode.Uri;
readonly graphs: string[] = [];
slug: string | undefined;
get graphIri(): vscode.Uri {
return WorkspaceUri.toWorkspaceUri(this.uri, this.slug) || this.uri;
}
baseIri: string | undefined;
namespaces: { [key: string]: string } = {};
namespaceDefinitions = new RangeIndex();
subjects = new RangeIndex();
references = new RangeIndex();
// TODO: Remove all type definitions from this map and query the combination of typeAssertion and typeDefinitions instead.
typeAssertions = new RangeIndex();
typeDefinitions = new RangeIndex();
private _languageTagStats: LanguageTagUsageStats = {};
get languageTagStats(): LanguageTagUsageStats {
return this._languageTagStats;
}
set languageTagStats(stats: LanguageTagUsageStats) {
this._languageTagStats = stats;
// The primary language is derived from the statistics; derive it again on the next read.
this._primaryLanguage = null;
}
private _primaryLanguage: string | undefined | null = null;
get primaryLanguage(): string | undefined {
if (this._primaryLanguage === null) {
let maxFrequency = -1;
this._primaryLanguage = undefined;
for (let [lang, frequency] of Object.entries(this._languageTagStats)) {
if (lang && frequency > maxFrequency) {
maxFrequency = frequency;
this._primaryLanguage = lang;
}
}
}
return this._primaryLanguage ?? undefined;
}
private _activeLanguageTag: string | undefined;
get activeLanguageTag(): string | undefined {
return this._activeLanguageTag;
}
set activeLanguageTag(value: string | undefined) {
this._activeLanguageTag = value;
if (value) {
this._activeLanguage = value.split('-')[0];
} else {
this._activeLanguage = undefined;
}
}
private _activeLanguage: string | undefined;
get activeLanguage(): string | undefined {
return this._activeLanguage;
}
/**
* The label and description predicates from the `mentor.predicates.*` settings.
* Backed by a cache that invalidates on configuration changes, so setting
* changes apply immediately (e.g. to hover tooltips) without a configuration
* read per access — this getter runs per tree node during a tree refresh.
*/
get predicates(): { label: string[]; description: string[] } {
return getPredicatesConfig();
}
constructor(
documentUri: vscode.Uri,
protected readonly _store: Store,
protected readonly _vocabulary: VocabularyRepository,
protected readonly _settings: ISettingsService
) {
this.uri = documentUri;
}
abstract get isLoaded(): boolean;
abstract get isParsed(): boolean;
abstract isTokenized(): this is ITokenizedDocumentContext;
get isTemporary(): boolean {
return this.uri.scheme == 'git' || this.uri.scheme == 'untitled';
}
get isPreset(): boolean {
return false;
}
abstract parse(text: string): IToken[];
abstract loadTriples(data: string): Promise<void>;
abstract infer(): Promise<void>;
abstract getIriAtPosition(position: vscode.Position): string | undefined;
abstract getLiteralAtPosition(position: vscode.Position): string | undefined;
async onDidChangeDocument(e: vscode.TextDocumentChangeEvent): Promise<void> { };
getTextDocument(): vscode.TextDocument | undefined {
return vscode.workspace.textDocuments.find(d => d.uri.toString() === this.uri.toString());
}
getPrefixForNamespaceIri(namespaceIri: string): string | undefined {
for (let [prefix, iri] of Object.entries(this.namespaces)) {
if (iri === namespaceIri) {
return prefix;
}
}
}
updateNamespacePrefix(oldPrefix: string, newPrefix: string) {
const uri = this.namespaces[oldPrefix];
if (!uri) return;
delete this.namespaces[oldPrefix];
this.namespaces[newPrefix] = uri;
}
getResourceLabel(subjectUri: string): Label {
// TODO: Fix #10 in mentor-rdf; Refactor node identifiers to be node instances instead of strings.
const subject = subjectUri.includes(':') ? new NamedNode(subjectUri) : new BlankNode(subjectUri);
const treeLabelStyle = this._settings.get<TreeLabelStyle>('view.definitionTree.labelStyle', TreeLabelStyle.AnnotatedLabels);
switch (treeLabelStyle) {
case TreeLabelStyle.AnnotatedLabels: {
const predicates = this.predicates.label.map(p => new NamedNode(p));
// First, try to find a description in the current graph.
let result = this._getResourceAnnotationFromPredicates(this.graphs, subject, predicates);
if (result) {
return result;
}
// If none is found, try to find a description in the default graph.
result = this._getResourceAnnotationFromPredicates(undefined, subject, predicates);
if (result) {
return result;
}
// Fallback to URI labels without prefixes.
break;
}
case TreeLabelStyle.UriLabelsWithPrefix: {
const namespace = Uri.getNamespaceIri(subjectUri);
let prefix = "?";
for (let [p] of Object.entries(this.namespaces).filter(([_, ns]) => ns == namespace)) {
prefix = p;
break;
}
return {
value: `${prefix}:${Uri.getLocalPart(subjectUri) || subjectUri}`,
language: undefined
};
}
}
return {
value: decodeURIComponent(Uri.getLocalPart(subjectUri) || subjectUri),
language: undefined
};
}
/**
* Get an annotation to a resource from a list of predicates. Either in the active document language or in the primary language.
* @param graphUris URIs of the graphs to query.
* @param subject A subject node.
* @param predicates A list of predicates to reqtrieve the label from.
* @returns The label of the resource as a string literal.
*/
private _getResourceAnnotationFromPredicates(graphUris: string[] | string | undefined, subject: NamedNode | BlankNode, predicates: NamedNode[]): Label | undefined {
let languageLabel: Literal | undefined = undefined;
let primaryLabel: Literal | undefined = undefined;
let fallbackLabel: Literal | undefined = undefined;
for (let p of predicates) {
for (let q of this._store.matchAll(graphUris, subject, p, null, false)) {
if (q.object.termType === 'Literal') {
const literal = q.object as Literal;
// Prefer to return non-empty values.
if (literal.value.length == 0) {
continue;
}
// Check if the literal language matches the active language
if (literal.language === this.activeLanguageTag) {
return literal;
}
// Store the literal if it matches the active language without the regional part.
if (this.activeLanguage && literal.language.startsWith(this.activeLanguage)) {
languageLabel = literal;
}
// Store the literal if it matches the primary language
if (this.primaryLanguage === literal.language) {
primaryLabel = literal;
}
// Store the first literal as a fallback value.
if (!fallbackLabel) {
fallbackLabel = literal;
}
} else if (p.value === SH.path) {
return {
value: this.getPropertyPathLabel(q.object as Quad_Subject),
language: undefined
};
} else {
return {
value: Uri.getLocalPart(q.object.value) || '',
language: undefined
};
}
}
// If we have found a label given the current predicates, we can stop
// searching as the predicates are ordered in priority.
if (languageLabel || primaryLabel || fallbackLabel) {
break;
}
}
if (languageLabel) return languageLabel;
if (primaryLabel) return primaryLabel;
return fallbackLabel;
}
getPropertyPathLabel(node: Quad_Subject): string {
let result = [];
for (let c of this._vocabulary.getPropertyPathTokens(this.graphs, node)) {
if (typeof (c) === 'string') {
if (c === '|' || c === '/') {
result.push(` ${c} `);
} else {
result.push(c);
}
} else {
result.push(this.getResourceLabel(c.value).value);
}
}
return result.join('');
}
getResourceDescription(subjectUri: string): Label | undefined {
// TODO: Fix #10 in mentor-rdf; This is a hack: we need to return nodes from the Mentor RDF API instead of strings.
const subject = subjectUri.includes(':') ? new NamedNode(subjectUri) : new BlankNode(subjectUri);
const predicates = this.predicates.description.map(p => new NamedNode(p));
// First, try to find a description in the current graph.
let result = this._getResourceAnnotationFromPredicates(this.graphs, subject, predicates);
if (result) {
return result;
}
return this._getResourceAnnotationFromPredicates(undefined, subject, predicates);
}
getResourceIri(subjectIri: string): string {
// TODO: Add support for virtual file systems provided by vscode such as vscode-vfs.
if (subjectIri.startsWith('file')) {
const u = vscode.Uri.parse(subjectIri);
// Resolve relative file IRIs with regards to the directory of the current document.
if (u.authority === '..') {
// For a file URI the namespace is the directory of the current document.
const directory = Uri.getNamespaceIri(this.uri.toString());
const filePath = subjectIri.split('//')[1];
const fileUrl = vscode.Uri.joinPath(vscode.Uri.parse(directory), filePath);
// Allow navigating to the relative file.
return '[' + filePath + '](' + fileUrl + ')';
}
}
return subjectIri;
}
getResourceTooltip(subjectUri: string): vscode.MarkdownString {
const iri = this.getResourceIri(subjectUri);
const label = this.getResourceLabel(subjectUri);
const description = this.getResourceDescription(subjectUri);
let lines = [
`**${label.value}**`,
description?.value,
iri
];
return new vscode.MarkdownString(lines.filter(line => line).join('\n\n'), true);
}
} |