All files / services/document document-context.interface.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
import * as vscode from 'vscode';
import type { CstNode, ILexingError } from 'chevrotain';
import { RangeIndex } from '@src/utilities/range-index';
import { Quad, Quad_Subject } from '@rdfjs/types';
import { LanguageTagUsageStats } from '@faubulous/mentor-rdf';
import { Label } from './document-context';
import { BlankNodeIdGenerator, IRecognitionException, IToken, RdfSyntax } from '@faubulous/mentor-rdf-parsers';
 
/**
 * The output of one parse pass over a document text: the token streams, the
 * quads or the concrete syntax tree, and the collected errors. Captured by
 * {@link ITokenizedDocumentContext.parse} so that the diagnostics service and
 * the triple loader can share a single tokenization and analysis per edit
 * instead of re-deriving them from the same text.
 */
export interface DocumentParseResult {
	/**
	 * The exact text the result was produced from; used as the freshness check.
	 */
	readonly text: string;
 
	/**
	 * The faithful token stream that IDE features and linters consume.
	 */
	readonly tokens: IToken[];
 
	/**
	 * The tokens consumed by the grammar parser — placeholder tokens for
	 * Triplate templates, identical to {@link tokens} otherwise. Released
	 * together with {@link cst}.
	 */
	readonly parseTokens?: IToken[];
 
	/**
	 * Whether the text is a Triplate template.
	 */
	readonly template: boolean;
 
	/**
	 * The quads of a valid Turtle document, read straight from the tokens
	 * without a syntax tree. Released once {@link IDocumentContext.loadTriples}
	 * has consumed them. A document that holds quads has no errors and no
	 * {@link cst}.
	 */
	readonly quads?: Quad[];
 
	/**
	 * The concrete syntax tree produced by the shared parser with error
	 * recovery enabled, for a document that was not read into {@link quads}.
	 * It is by far the largest part of the result and is released once
	 * {@link IDocumentContext.loadTriples} has consumed it; the tokens and
	 * errors stay for the diagnostics pass that follows.
	 */
	readonly cst?: CstNode;
 
	/**
	 * The characters the lexer could not match.
	 */
	readonly lexErrors: ILexingError[];
 
	/**
	 * A copy of the shared parser's recognition errors, which reset on every parse.
	 */
	readonly parserErrors: IRecognitionException[];
 
	/**
	 * A copy of the shared parser's semantic errors, which reset on every parse.
	 */
	readonly semanticErrors: IRecognitionException[];
}
 
/**
 * Interface for document context that provides access to RDF document specific data.
 */
export interface IDocumentContext {
	/**
	 * The URI of the document.
	 */
	readonly uri: vscode.Uri;
 
	/**
	 * The graphs in the triple store associated with the document.
	 */
	readonly graphs: string[];
 
	/**
	 * Get the URI of the document graph in the triple store.
	 */
	readonly graphIri: vscode.Uri;
 
	/**
	 * Get the base IRI of the document that can be used for resolving local names into IRIs.
	 */
	baseIri: string | undefined;
 
	/**
	 * Maps prefixes to namespace IRIs.
	 */
	namespaces: { [key: string]: string };
 
	/**
	 * Maps namespace IRIs to the location of their prefix definition in the document.
	 */
	namespaceDefinitions: RangeIndex;
 
	/**
	 * Maps IRIs that appear as subjects to the locations where they appear in the document.
	 */
	subjects: RangeIndex;
 
	/**
	 * Maps IRIs of all resources to the locations where they appear in the document.
	 */
	references: RangeIndex;
 
	/**
	 * A human-readable slug used as the URI fragment for the document's graph IRI.
	 * For notebook cells this replaces the opaque VS Code-assigned fragment.
	 * When undefined the raw URI fragment is used as-is.
	 */
	slug: string | undefined;
 
	/**
	 * Indicates whether the document is parsed using a tokenizing parser and
	 * thus provides access to its tokens.
	 * @note XML documents are not tokenized.
	 * @returns `true` if the context provides a token stream, narrowing it to {@link ITokenizedDocumentContext}.
	 */
	isTokenized(): this is ITokenizedDocumentContext;
 
	/**
	 * Maps IRIs of subjects that have an asserted rdf:type to the location of the type assertion.
	 */
	typeAssertions: RangeIndex;
 
	/**
	 * Maps IRIs of subjects that are class or property definitions to the location of the definition.
	 */
	typeDefinitions: RangeIndex;
 
	/**
	 * The number of literals per language tag in the asserted triples of the document;
	 * literals without a language tag are counted under the empty tag. Determines the
	 * primary language and the languages offered for selection.
	 */
	languageTagStats: LanguageTagUsageStats;
 
	/**
	 * The most often used language tag in the document.
	 */
	readonly primaryLanguage: string | undefined;
 
	/**
	 * The ISO 639-3 language tag of the user-selected display document language.
	 */
	activeLanguageTag: string | undefined;
 
	/**
	 * The language portion of the active ISO 639-3 language tag without the regional part.
	 * e.g. 'en' for the language tags 'en' or 'en-gb'.
	 */
	readonly activeLanguage: string | undefined;
 
	/**
	 * The predicates to be used for retrieving labels and descriptions for resources.
	 */
	readonly predicates: {
		label: string[];
		description: string[];
	};
 
	/**
	 * Indicates whether parser output has been delivered by the language server,
	 * so that triples can be (re)loaded. See {@link isLoaded} for store state.
	 */
	readonly isParsed: boolean;
 
	/**
	 * Indicates whether the document is fully loaded.
	 */
	readonly isLoaded: boolean;
 
	/**
	 * Indicates whether the document is temporary and not persisted.
	 */
	readonly isTemporary: boolean;
 
	/**
	 * Indicates whether the document shows the read-only source of a preset graph
	 * shipped with Mentor. Its graph is owned by the store, not by the document, and
	 * it is not part of the workspace.
	 */
	readonly isPreset: boolean;
 
	/**
	 * Parses the given document text and updates the context's derived state
	 * (tokens or parse data, namespaces, references, type assertions). This is
	 * the authoritative update path: token-based contexts use a file-scoped
	 * blank node ID generator so blank node identities are stable across reloads.
	 * @param text The current document text.
	 * @returns The tokens of the document, or an empty array for contexts that are not token-based.
	 */
	parse(text: string): IToken[];
 
	/**
	 * Loads triples into the triple store using existing tokens.
	 * @param data The file content.
	 */
	loadTriples(data: string): Promise<void>;
 
	/**
	 * Infers new triples from the document, if not already done.
	 */
	infer(): Promise<void>;
 
	/**
	 * Get the full IRI of a resource at the given position in the document.
	 * @param position The position in the document.
	 * @returns The full IRI of the resource or `undefined` if not found.
	 */
	getIriAtPosition(position: vscode.Position): string | undefined;
 
	/**
	 * Get a literal value at the given position in the document.
	 * @param position The position in the document.
	 * @returns The literal value at the position or `undefined` if there is no literal value at that position.
	 */
	getLiteralAtPosition(position: vscode.Position): string | undefined;
 
	/**
	 * Event handler for when the document is changed.
	 * @param e The document change event.
	 */
	onDidChangeDocument(e: vscode.TextDocumentChangeEvent): Promise<void>;
 
	/**
	 * Get the text document with the given URI.
	 * @returns The text document if it is loaded, undefined otherwise.
	 */
	getTextDocument(): vscode.TextDocument | undefined;
 
	/**
	 * Get the prefix for a namespace IRI.
	 * @param namespaceIri The namespace IRI.
	 * @returns The prefix for the namespace IRI or `undefined`.
	 */
	getPrefixForNamespaceIri(namespaceIri: string): string | undefined;
 
	/**
	 * Updates a namespace prefix definition in the document.
	 * @param oldPrefix The prefix to be replaced.
	 * @param newPrefix The prefix to replace the old prefix.
	 */
	updateNamespacePrefix(oldPrefix: string, newPrefix: string): void;
 
	/**
	 * Get the label of a resource according to the current user preferences for the display of labels.
	 * @param subjectUri URI of the resource.
	 * @returns A label for the resource as a string literal.
	 */
	getResourceLabel(subjectUri: string): Label;
 
	/**
	 * Get a rendered version of a SHACL path as a string.
	 * @param node The object of a SHACL path triple.
	 * @returns A rendered version of the SHACL path as a string.
	 */
	getPropertyPathLabel(node: Quad_Subject): string;
 
	/**
	 * Get the description of a resource.
	 * @param subjectUri URI of the resource.
	 * @returns A description for the resource as a string literal.
	 */
	getResourceDescription(subjectUri: string): Label | undefined;
 
	/**
	 * Get the IRI of a resource.
	 * @param subjectIri IRI of the resource.
	 * @returns A IRI for the resource as a string literal.
	 */
	getResourceIri(subjectIri: string): string;
 
	/**
	 * Get the tooltip for a resource.
	 * @param subjectUri URI of the resource.
	 * @returns A markdown string containing the label, description and URI of the resource.
	 */
	getResourceTooltip(subjectUri: string): vscode.MarkdownString;
}
 
/**
 * Interface for document contexts that have been tokenized and thus provide access to the tokens of the document.
 */
export interface ITokenizedDocumentContext extends IDocumentContext {
	/**
	 * The RDF syntax of the document, which determines its lexer and parser.
	 */
	readonly syntax: RdfSyntax;
 
	/**
	 * The tokens of the document, if the document has been tokenized.
	 */
	tokens: IToken[];
 
	/**
	 * Tokenizes the given text synchronously using the document's own syntax,
	 * without requiring the language server.
	 * @param text The text to tokenize.
	 * @param blankNodeIdGenerator Optional blank node ID generator. Use a file-scoped
	 * generator when the tokens are meant for triple loading so that blank node
	 * identities are stable across reloads; without one, the default generator is
	 * used, which is only suitable for positional lookups.
	 * @returns The tokens of the text.
	 */
	tokenize(text: string, blankNodeIdGenerator?: BlankNodeIdGenerator): IToken[];
 
	/**
	 * Tokenizes the current text of an open document for positional lookups, such as
	 * completions. The result is cached per document version, so the providers that run
	 * on the same keystroke share a single tokenization.
	 * @param document The open text document of this context.
	 * @returns The tokens of the document's current text.
	 */
	tokenizeDocument(document: vscode.TextDocument): IToken[];
 
	/**
	 * Sets the tokens of the document and updates the derived indexes
	 * (namespaces, references, type assertions and definitions).
	 * @param tokens An array of tokens.
	 */
	setTokens(tokens: IToken[]): void;
 
	/**
	 * Returns the result of the last {@link IDocumentContext.parse} pass when
	 * it was produced from exactly the given text, or `undefined` when it is
	 * stale or no parse has run yet. Callers fall back to deriving the data
	 * themselves in that case.
	 * @param text The current document text.
	 */
	getParseResult(text: string): DocumentParseResult | undefined;
 
	/**
	 * Drops the cached parse result. Called once no consumer will read it
	 * again — after a file has been indexed and diagnosed, or when its editor
	 * closes — so that only the tokens and the derived indexes stay in memory.
	 * A later consumer re-derives the data from the text.
	 */
	releaseParseResult(): void;
 
	/**
	 * Drops the token stream and the cached parse result of a document that is
	 * not open in an editor. The index maps and graphs stay, so the document
	 * remains loaded; opening it in an editor parses it again.
	 */
	releaseTokens(): void;
}