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 | 116x 116x 48x 48x 48x 48x 1x 6x 6x 1x 5x 5x 2x 3x 3x 3x 3x 1x 2x 27x 27x 27x 26x 26x 26x 26x 26x 26x 26x 22x 22x 22x 22x 1x 1x 27x 27x 22x 22x 22x 22x 116x 111x 22x 22x 84x 84x 84x 27x 27x 116x 3x 113x 79x 41x 38x 34x 84x 18x 18x 66x 34x 32x 27x 27x 27x 23x 23x 2x 4x 21x 21x 20x 40x 40x 30x 1x 21x 31x 263x 120x 268x 151x 117x 117x 1x 116x 116x 116x | import * as vscode from "vscode";
import { Uri, VocabularyRepository } from "@faubulous/mentor-rdf";
import { IToken, RdfToken } from "@faubulous/mentor-rdf-parsers";
import { IDocumentContextService } from '@src/services/document';
import { getNamespaceIriFromPrefixedName, getPropertyTypeFromRange, getTokenIndexAtPosition, getTripleComponentType, isTypeAssertionObject, TripleComonentType } from "@src/utilities";
import { TurtleDocument } from '@src/languages/turtle/turtle-document';
import { TurtleFeatureProvider } from '@src/languages/turtle/turtle-feature-provider';
import { WorkspaceUri } from "@src/providers/workspace-uri";
/**
* Represents a completion item that has an associated IRI (Internationalized Resource Identifier).
*/
class IriCompletionItem extends vscode.CompletionItem {
/**
* The IRI of the subject.
*/
iri: string;
/**
* Creates a new completion item.
*
* Completion items must have at least a {@link CompletionItem.label label} which then
* will be used as insert text as well as for sorting and filtering.
*
* @param iri The IRI of the completion item.
* @param label The label of the completion.
* @param kind The {@link CompletionItemKind kind} of the completion.
*/
constructor(iri: string, label: string | vscode.CompletionItemLabel, kind?: vscode.CompletionItemKind) {
super(label, kind);
this.iri = iri;
}
}
/**
* Provides completion items for Turtle documents based on the cursor position in the document. The
* completion items return subject/object definitions when the cursor is on a subject or object, in
* a triple. If the cursor is on a predicate, the completion items return predicate definitions. The
* definitions are resolved in the following order: local definitions (file), then global definitions
* (e.g. RDF, RDFS..).
*/
export class TurtleCompletionItemProvider extends TurtleFeatureProvider implements vscode.CompletionItemProvider<vscode.CompletionItem> {
/**
* Maximum number of completion items to return.
*/
readonly maxCompletionItems = 10;
constructor(
protected readonly _contextService: IDocumentContextService,
protected readonly _vocabulary: VocabularyRepository
) {
super();
}
resolveCompletionItem?(item: vscode.CompletionItem, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CompletionItem> {
return item;
}
provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, t: vscode.CancellationToken, completion: vscode.CompletionContext): vscode.ProviderResult<vscode.CompletionItem[] | vscode.CompletionList> {
// The suggestions are read from the triple store; while the document is
// loading, its own triples may not be there yet.
const uri = document.uri.toString();
if (this._contextService.isLoading(uri)) {
return this._contextService.waitUntilLoaded(uri).then(() => this.provideCompletionItems(document, position, t, completion));
}
const context = this._contextService.getDocumentContext(document, TurtleDocument);
if (!context || t.isCancellationRequested) {
return null;
}
// Tokenize the current document text so that completions are always based on
// the up-to-date buffer content, also for documents that are not indexed (e.g.
// untitled documents). The tokens are cached per document version and shared
// with the other providers that run on the same keystroke.
const tokens = context.tokenizeDocument(document);
// Fast typing cancels the previous request per keystroke; skip the store
// queries when the result would be discarded anyway.
Iif (t.isCancellationRequested) {
return null;
}
const n = getTokenIndexAtPosition(tokens, position);
if (n < 1) {
return null;
}
return this.getCompletionItems(document, context, tokens, n);
}
protected getCompletionItems(document: vscode.TextDocument, context: TurtleDocument, tokens: IToken[], tokenIndex: number): vscode.ProviderResult<vscode.CompletionItem[] | vscode.CompletionList> {
let result: IriCompletionItem[] = [];
let queryGraphs: string[] | undefined;
let componentType: TripleComonentType;
let isTypeAssertion = false;
if (this._isLocalPartDefinitionContext(tokens, tokenIndex)) {
// Note: The context's graph IRI falls back to the document URI for documents
// that cannot be mapped into the workspace (e.g. untitled documents). It is
// the graph where the document's triples are stored.
const documentIri = WorkspaceUri.toCanonicalString(context.graphIri);
componentType = getTripleComponentType(tokens, tokenIndex);
isTypeAssertion = componentType === 'object' && isTypeAssertionObject(tokens, tokenIndex, context.namespaces);
const currentToken = tokens[tokenIndex];
const namespaceIri = getNamespaceIriFromPrefixedName(context.namespaces, currentToken.image);
const localPart = currentToken.image.split(":")[1];
if (namespaceIri) {
const iri = (namespaceIri + localPart).toLowerCase();
queryGraphs = [documentIri, namespaceIri];
// Primarily query the context graph for retrieving completion items.
result = this._getLocalPartCompletionItems(componentType, iri, queryGraphs);
// If none are found, query the background graph for retrieving additional items from other ontologies.
if (result.length == 0) {
queryGraphs = undefined;
result = this._getLocalPartCompletionItems(componentType, iri, undefined);
}
}
}
let items: IriCompletionItem[] = [];
if (result.length > 0) {
// The completion widget can only render the built-in symbol icons via the
// item kind, so map the resource categories from the definition tree to
// the closest matching kinds: classes → Class, data properties → Property,
// object properties (relations) → Reference, individuals → Value.
const classes = new Set(this._vocabulary.getClasses(queryGraphs));
const properties = new Set(this._vocabulary.getProperties(queryGraphs));
// Rank the candidates by their relevance for the position in the triple
// BEFORE truncation so that preferred categories survive the cut-off:
// classes for type assertion objects, classes and individuals for subjects
// and other objects. The candidates are already sorted by label, and
// Array.sort is stable, so sorting by priority yields (priority, label) order.
const priorities = new Map<string, number>();
for (const item of result) {
priorities.set(item.iri, this._getCompletionItemPriority(item.iri, componentType, isTypeAssertion, classes, properties));
}
result.sort((a, b) => priorities.get(a.iri)! - priorities.get(b.iri)!);
items = result.slice(0, this.maxCompletionItems);
for (const item of items) {
// Encode the priority in the sort text so that the displayed order also
// respects the position preference when VS Code's fuzzy scores tie.
item.sortText = `${priorities.get(item.iri)}_${item.label}`;
item.kind = this._getCompletionItemKind(item.iri, queryGraphs, classes, properties);
item.detail = context.getResourceDescription(item.iri)?.value;
}
}
// When the result is truncated, mark the list as incomplete so that VS Code
// re-invokes the provider on further keystrokes. Otherwise VS Code only
// filters the initially returned items client-side and matching items beyond
// the cut-off would never appear as the user narrows the search by typing.
const isIncomplete = result.length > this.maxCompletionItems;
return new vscode.CompletionList(items, isIncomplete);
}
/**
* Ranks a completion candidate by its relevance for the position in the triple.
* Lower values rank higher.
*
* - Type assertion objects (`a` / `rdf:type`): classes (0) before individuals (1) before properties (2).
* - Subjects and other objects: classes and individuals (0) before properties (1).
* - Predicates: uniform priority; the candidates are properties only.
*
* @param iri The IRI of the completion candidate.
* @param componentType The position of the completion in the triple.
* @param isTypeAssertion Whether the completion is the object of a type assertion.
* @param classes The set of class IRIs defined in the queried graphs.
* @param properties The set of property IRIs defined in the queried graphs.
*/
private _getCompletionItemPriority(iri: string, componentType: TripleComonentType, isTypeAssertion: boolean, classes: Set<string>, properties: Set<string>): number {
if (componentType === 'predicate') {
return 0;
}
if (isTypeAssertion) {
if (classes.has(iri)) {
return 0;
}
return properties.has(iri) ? 2 : 1;
}
// Subjects and non-type-assertion objects: prefer named individuals and
// classes over properties.
return properties.has(iri) ? 1 : 0;
}
/**
* Determines the completion item kind for a resource IRI using the same
* classification as the definition tree icons.
* @param iri The IRI of the completion candidate.
* @param graphs The graphs that were queried for the completion candidates.
* @param classes The set of class IRIs defined in the queried graphs.
* @param properties The set of property IRIs defined in the queried graphs.
*/
private _getCompletionItemKind(iri: string, graphs: string[] | undefined, classes: Set<string>, properties: Set<string>): vscode.CompletionItemKind {
if (properties.has(iri)) {
// Distinguish literal-valued data properties from object properties
// (relations) based on the property range, as the definition tree does.
const range = this._vocabulary.getRange(graphs, iri) ?? this._vocabulary.getDatatype(graphs, iri);
return getPropertyTypeFromRange(range) === 'dataProperty'
? vscode.CompletionItemKind.Field
: vscode.CompletionItemKind.Interface;
}
if (classes.has(iri)) {
return vscode.CompletionItemKind.Class;
} else {
return vscode.CompletionItemKind.Value;
}
}
private _isLocalPartDefinitionContext(tokens: IToken[], tokenIndex: number): boolean {
const currentToken = tokens[tokenIndex];
const currentType = currentToken?.tokenType.name;
return currentType === RdfToken.PNAME_LN.name || currentType === RdfToken.PNAME_NS.name;
}
/**
* Collects all completion items whose IRI starts with the given search prefix.
* @returns All matching items sorted by label; the caller is responsible for truncation.
*/
private _getLocalPartCompletionItems(componentType: TripleComonentType, uri: string, graphs: string[] | undefined): IriCompletionItem[] {
let items: Record<string, IriCompletionItem> = {};
if (componentType === 'predicate') {
// In this case we only want to return properties.
for (let property of this._vocabulary.getProperties(graphs)) {
this._addLocalPartCompletionItem(items, uri, property);
}
} else {
// Here we want to return all subjects, including properties as those can be subject or objects too.
const contexts = [];
if (graphs) {
for (const g of graphs) {
const c = this._contextService.getDocumentContextFromUri(g);
if (c) {
contexts.push(c);
}
}
} else {
contexts.push(...Object.values(this._contextService.contexts));
}
for (const c of contexts) {
for (const subject of c.subjects.keys()) {
this._addLocalPartCompletionItem(items, uri, subject);
}
}
}
// Sort by label so that truncation by the caller is deterministic. Note that
// sorting the items without a comparator would compare their object identity
// which is meaningless.
return Object.values(items).sort((a, b) => (a.label as string).localeCompare(b.label as string));
}
private _addLocalPartCompletionItem(result: Record<string, IriCompletionItem>, namespaceIri: string, subjectIri: string) {
if (result[subjectIri] || !subjectIri.toLowerCase().startsWith(namespaceIri)) {
return;
}
const localPart = Uri.getLocalPart(subjectIri);
if (!localPart) {
return;
}
const item = new IriCompletionItem(subjectIri, localPart, vscode.CompletionItemKind.Value);
item.iri = subjectIri;
result[subjectIri] = item;
}
} |