All files / languages/sparql/providers sparql-completion-item-provider.ts

100% Statements 46/46
90.9% Branches 20/22
100% Functions 6/6
100% Lines 45/45

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                                        21x           21x     1x       4x   4x 1x     3x   3x     2x 2x   1x     1x   1x 1x       1x       3x 1x   2x         10x   10x   5x 5x   2x   3x     7x   7x       5x   2x         8x 8x     8x   8x 8x     8x 1x     8x   15x 11x         11x 1x     11x 11x 11x   11x     8x    
import * as vscode from "vscode";
import { container } from 'tsyringe';
import { RdfToken } from "@faubulous/mentor-rdf-parsers";
import { ServiceToken } from '@src/services/tokens';
import { ISparqlConnectionService } from '@src/languages/sparql/services';
import { TurtleCompletionItemProvider } from "@src/languages/turtle/providers";
import { TurtleDocument } from "@src/languages/turtle";
 
export class SparqlCompletionItemProvider extends TurtleCompletionItemProvider {
	/**
	 * The characters that trigger completion when typed.
	 * 
	 * @remarks The provider will also trigger on any position where the current 
	 * token starts with a trigger character, even if the character itself is 
	 * not yet typed (e.g. when completing an IRI that starts with '<', the 
	 * completion will trigger as soon as the user types '<' or when they start 
	 * typing an IRI that was auto-closed with '>' (e.g. <htt>), without 
	 * requiring them to type another trigger character. This is to provide a 
	 * smoother completion experience for IRIs.
	 */
	public readonly triggerCharacters = new Set([':', '<']);
 
	/**
	 * Timeout in milliseconds to wait for fresh tokens from the language server
	 * when the stored tokens are stale at the moment a trigger-character completion fires.
	 */
	private readonly _tokenSyncTimeout = 2000;
 
	private get connectionService() {
		return container.resolve<ISparqlConnectionService>(ServiceToken.SparqlConnectionService);
	}
 
	override async provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, t: vscode.CancellationToken, completion: vscode.CompletionContext): Promise<vscode.CompletionItem[] | null> {
		const context = this.contextService.getDocumentContext(document, TurtleDocument);
 
		if (!context) {
			return null;
		}
 
		let n = context.getTokenIndexAtPosition(position);
 
		if (n < 1) {
			// Tokens are stale — the language server hasn't delivered an update yet.
			// Wait for the next token delivery before retrying.
			try {
				await this.contextService.onNextTokenDelivery(document.uri.toString(), this._tokenSyncTimeout);
			} catch {
				return null;
			}
 
			n = context.getTokenIndexAtPosition(position);
 
			Eif (n < 1) {
				return null;
			}
		}
 
		return this.getCompletionItems(document, context, n) as Promise<vscode.CompletionItem[] | null>;
	}
 
	override getCompletionItems(document: vscode.TextDocument, context: any, tokenIndex: number): vscode.ProviderResult<vscode.CompletionItem[]> {
		if (this.isGraphDefinitionContext(context, tokenIndex)) {
			return this.getGraphIriCompletionItems(document, context, tokenIndex);
		} else {
			return super.getCompletionItems(document, context, tokenIndex);
		}
	}
 
	isGraphDefinitionContext(context: TurtleDocument, tokenIndex: number) {
		let n = -1;
 
		if (context.tokens[tokenIndex].image.startsWith('<')) {
			// If the current token is either '<' or an IRI that was auto-closed with '>' (e.g. <htt>)
			n = tokenIndex;
		} else if (context.tokens[tokenIndex - 1]?.image === '<') {
			// If the token is not yet closed, then the previous token must be '<'
			n = tokenIndex - 1;
		} else {
			return false;
		}
 
		const previousToken = context.tokens[n - 1];
 
		switch (previousToken?.tokenType.name) {
			case RdfToken.GRAPH.name:
			case RdfToken.FROM.name:
			case RdfToken.NAMED.name:
				return true;
			default:
				return false;
		}
	}
 
	async getGraphIriCompletionItems(document: vscode.TextDocument, context: TurtleDocument, tokenIndex: number): Promise<vscode.CompletionItem[]> {
		const result = [];
		const token = context.tokens[tokenIndex];
 
		// The token might be completely or partially enclosed in angle brackets.
		let value = token.image;
 
		Eif (value.startsWith('<')) {
			value = value.slice(1);
		}
 
		if (value.endsWith('>')) {
			value = value.slice(0, -1);
		}
 
		const graphs = await this.connectionService.getGraphsForDocument(document.uri);
 
		for (const iri of graphs.filter(g => g.startsWith(value))) {
			let label = iri.substring(value.length);
 
			// If the user has already typed a trigger character, we should 
			// not include it in the completion item label as this would result 
			// in duplication (e.g. 'workspace::' instead of 'ex:').
			if (this.triggerCharacters.has(label[0])) {
				label = label.slice(1);
			}
 
			const item = new vscode.CompletionItem(label, vscode.CompletionItemKind.Reference);
			item.detail = iri;
			item.insertText = new vscode.SnippetString(label);
 
			result.push(item);
		}
 
		return result;
	}
}