All files / languages/sparql sparql-document.ts

100% Statements 12/12
100% Branches 4/4
100% Functions 6/6
100% Lines 12/12

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                                27x               5x               4x       1x         1x   1x       2x 1x         3x 1x       2x 2x        
import * as vscode from 'vscode';
import type { CstNode } from 'chevrotain';
import { Quad } from '@rdfjs/types';
import { Store, VocabularyRepository } from '@faubulous/mentor-rdf';
import { RdfSyntax, SparqlReader } from '@faubulous/mentor-rdf-parsers';
import { ISettingsService } from '@src/services/core';
import { getConfig } from '@src/utilities/vscode/config';
import { ParserFactory } from '@src/languages/parser-factory';
import { TurtleDocument } from '@src/languages/turtle/turtle-document';
import { WorkspaceUri } from '@src/providers/workspace-uri';
 
/**
 * A document context for SPARQL documents.
 */
export class SparqlDocument extends TurtleDocument {
	constructor(uri: vscode.Uri, store: Store, vocabulary: VocabularyRepository, settings: ISettingsService) {
		super(uri, RdfSyntax.Sparql, store, vocabulary, settings);
	}
 
	/**
	 * Indicates whether SPARQL queries are parsed into RDF triples using the
	 * SPARQL Syntax Vocabulary (sparql:) and loaded into the workspace store.
	 */
	protected get parseQueriesEnabled(): boolean {
		return getConfig().get('index.parseSparqlQueries', false);
	}
 
	/**
	 * SPARQL documents are considered loaded once they have been parsed, also after their tokens were released.
	 * Unlike RDF documents, SPARQL queries don't necessarily produce graphs.
	 */
	override get isLoaded(): boolean {
		return this.isIndexed;
	}
 
	protected override readQuads(cst: CstNode): Quad[] {
		const reader = ParserFactory.createReader(this.syntax) as SparqlReader;
 
		// Emit the root query or update node as the workspace URI of the
		// document instead of a blank node, so queries are addressable by
		// the IRI of the file or notebook cell that contains them.
		reader.rootIri = this._store.dataFactory.namedNode(WorkspaceUri.toCanonicalString(this.graphIri));
 
		return reader.visit(cst) as Quad[];
	}
 
	public override async infer(): Promise<void> {
		if (this.parseQueriesEnabled) {
			await super.infer();
		}
	}
 
	public override async loadTriples(data: string): Promise<void> {
		if (this.parseQueriesEnabled) {
			await super.loadTriples(data);
		} else {
			// The query is not loaded into the store, so it contributes no literals and
			// nothing reads its syntax tree.
			this.languageTagStats = {};
			this._releaseParsedContent();
		}
	}
}