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 | 20x 20x 25x 1x 1x 1x 3x 3x 1x 2x 2x 1x 1x 8x 8x 7x 7x 7x 7x 7x 7x 5x 5x 5x 5x 14x 5x 1x 1x 8x 8x 8x 8x 6x 6x 1x 2x 5x 5x 4x 8x 8x 5x 1x 5x 6x 36x 6x 6x 15x 6x 39x 18x 21x 21x 1x 20x 20x 20x | import * as vscode from "vscode";
import { container } from "tsyringe";
import { Uri, VocabularyRepository } from "@faubulous/mentor-rdf";
import { RdfToken } from "@faubulous/mentor-rdf-parsers";
import { ServiceToken } from '@src/services/tokens';
import { IDocumentContextService } from '@src/services/document';
import { getNamespaceIriFromPrefixedName, getTripleComponentType, 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;
protected get contextService() {
return container.resolve<IDocumentContextService>(ServiceToken.DocumentContextService);
}
private get vocabulary() {
return container.resolve<VocabularyRepository>(ServiceToken.VocabularyRepository);
}
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[]> {
const context = this.contextService.getDocumentContext(document, TurtleDocument);
if (!context) {
return null;
}
const n = context.getTokenIndexAtPosition(position);
if (n < 1) {
return null;
}
return this.getCompletionItems(document, context, n);
}
protected getCompletionItems(document: vscode.TextDocument, context: TurtleDocument, tokenIndex: number): vscode.ProviderResult<vscode.CompletionItem[]> {
const result = [];
if (this._isLocalPartDefinitionContext(context, tokenIndex)) {
const documentIri = WorkspaceUri.toWorkspaceUri(document.uri);
const componentType = getTripleComponentType(context.tokens, tokenIndex);
const currentToken = context.tokens[tokenIndex];
const namespaceIri = getNamespaceIriFromPrefixedName(context.namespaces, currentToken.image);
const localPart = currentToken.image.split(":")[1];
if (documentIri && namespaceIri) {
const iri = (namespaceIri + localPart).toLowerCase();
const graphs = [documentIri.toString()];
graphs.push(namespaceIri);
// Primarily query the context graph for retrieving completion items.
for (let item of this._getLocalPartCompletionItems(context, componentType, iri, graphs)) {
result.push(item);
}
// If none are found, query the background graph for retrieving additional items from other ontologies.
if (result.length == 0) {
for (let item of this._getLocalPartCompletionItems(context, componentType, iri, undefined)) {
result.push(item);
}
}
}
}
return result;
}
private _isLocalPartDefinitionContext(context: TurtleDocument, tokenIndex: number): boolean {
const currentToken = context.tokens[tokenIndex];
const currentType = currentToken?.tokenType.name;
return currentType === RdfToken.PNAME_LN.name || currentType === RdfToken.PNAME_NS.name;
}
private _getLocalPartCompletionItems(context: TurtleDocument, componentType: TripleComonentType, uri: string, graphs: string[] | undefined): vscode.CompletionItem[] {
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 (let subject of Object.keys(c.subjects)) {
this._addLocalPartCompletionItem(items, uri, subject);
}
}
}
const result = Object.values(items).sort().slice(0, this.maxCompletionItems);
for (let item of result) {
item.detail = context.getResourceDescription(item.iri)?.value;
}
return result;
}
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;
}
} |