All files / languages/turtle preset-graph-document.ts

100% Statements 10/10
50% Branches 1/2
100% Functions 4/4
100% Lines 10/10

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                                                15x   15x       7161x                 14x   14x 14x   14x 3808x     14x   14x                
import * as vscode from 'vscode';
import { LanguageTagUsageStats, Store, VocabularyRepository } from '@faubulous/mentor-rdf';
import { RdfSyntax } from '@faubulous/mentor-rdf-parsers';
import { ISettingsService } from '@src/services/core';
import { PresetGraph } from '@src/services/core/preset-graph';
import { addLanguageTagUsage } from '@src/utilities/rdf';
import { TurtleDocument } from './turtle-document';
 
/**
 * A document context for the read-only source of a preset graph shipped with Mentor,
 * such as a framework vocabulary or a preset shape graph.
 *
 * The store loads the preset graphs at startup and on reindexing, so the context only
 * borrows the graph: loading the document never writes to the store and removing the
 * context never deletes the graph. The context provides the source ranges for language
 * features such as go to definition.
 */
export class PresetGraphDocument extends TurtleDocument {
	/**
	 * The preset graph whose source the document shows.
	 */
	readonly graph: PresetGraph;
 
	constructor(uri: vscode.Uri, graph: PresetGraph, store: Store, vocabulary: VocabularyRepository, settings: ISettingsService) {
		super(uri, RdfSyntax.Turtle, store, vocabulary, settings);
 
		this.graph = graph;
	}
 
	override get isPreset(): boolean {
		return true;
	}
 
	/**
	 * Points the context to the preset graph in the store and counts the language tags
	 * of the document, without adding its triples to the store again.
	 * @param data The document text.
	 */
	override async loadTriples(data: string): Promise<void> {
		const languageTagStats: LanguageTagUsageStats = {};
 
		this.graphs.length = 0;
		this.graphs.push(this.graph.graphIri);
 
		for (const quad of this.getParseResult(data)?.quads ?? []) {
			addLanguageTagUsage(languageTagStats, quad.object);
		}
 
		this._releaseParsedContent();
 
		this.languageTagStats = languageTagStats;
	}
 
	/**
	 * Does nothing: the inference for the preset graph is executed when the store loads it.
	 */
	override async infer(): Promise<void> { }
}