All files / providers resource-tooltip-provider.ts

95% Statements 38/40
85% Branches 17/20
85.71% Functions 6/7
94.87% Lines 37/39

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                    12x 12x   12x           12x   12x 1x     11x                 12x   12x 8x   8x 3x     5x   5x 2x     3x     4x                       4x   4x 2x       2x   2x 1x 1x     2x 1x       1x     4x       1x       4x 4x     4x             4x     10x 2x 2x         2x      
import * as vscode from 'vscode';
import { Store, NamedNode } from '@faubulous/mentor-rdf';
import { IDocumentContextService } from '@src/services/document';
 
/**
 * Provides hover information for tokens in RDF documents and for HTTP/HTTPS URIs in any file type.
 */
export class ResourceTooltipProvider implements vscode.HoverProvider {	
	constructor(
		context: vscode.ExtensionContext,
		private readonly _contextService: IDocumentContextService,
		private readonly _store: Store
	) {
		context.subscriptions.push(vscode.languages.registerHoverProvider('*', this));
	}
 
	provideHover(document: vscode.TextDocument, position: vscode.Position): vscode.ProviderResult<vscode.Hover> {
		// The tooltip reads labels and descriptions from the triple store; while the
		// document is loading, its own triples may not be there yet.
		const uri = document.uri.toString();
 
		if (this._contextService.isLoading(uri)) {
			return this._contextService.waitUntilLoaded(uri).then(() => this._provideHover(document, position));
		}
 
		return this._provideHover(document, position);
	}
 
	/**
	 * Returns the hover for a position once the document is loaded.
	 * @param document The document.
	 * @param position The position of the hover.
	 */
	private _provideHover(document: vscode.TextDocument, position: vscode.Position): vscode.Hover | null {
		const context = this._contextService.contexts[document.uri.toString()];
 
		if (context) {
			const iri = context.getIriAtPosition(position);
 
			if (iri) {
				return new vscode.Hover(context.getResourceTooltip(iri));
			}
 
			const literalValue = context.getLiteralAtPosition(position);
 
			if (literalValue) {
				return new vscode.Hover(literalValue);
			}
 
			return null;
		}
 
		return this._getGenericUriHover(document, position);
	}
 
	/**
	 * Resolves a full HTTP/HTTPS URI at the given position and returns a tooltip for it
	 * if it is a known resource in the workspace store. Used both as a generic fallback
	 * and for URIs inside triplate frontmatter (which the document context does not tokenize).
	 * @param document The document to check for a URI at the given position.
	 * @param position The position to check for a URI.
	 * @returns A hover with resource information if the URI is known, or null otherwise.
	 */
	private _getGenericUriHover(document: vscode.TextDocument, position: vscode.Position): vscode.Hover | null {
		const iri = this._getUriAtTextPosition(document, position);
 
		if (!iri) {
			return null;
		}
 
		// Only surface known resources (those with triples in the workspace store).
		let isKnown = false;
 
		for (const _ of this._store.matchAll(undefined, new NamedNode(iri), null, null, false)) {
			isKnown = true;
			break;
		}
 
		if (!isKnown) {
			return null;
		}
 
		// Any loaded context shares the same workspace store for label/description lookups.
		const workspaceContext = this._contextService.activeContext
			?? Object.values(this._contextService.contexts).find(c => c.isLoaded);
 
		Iif (!workspaceContext) {
			return null;
		}
 
		return new vscode.Hover(workspaceContext.getResourceTooltip(iri));
	}
 
	private _getUriAtTextPosition(document: vscode.TextDocument, position: vscode.Position): string | null {
		const line = document.lineAt(position.line).text;
		const cursor = position.character;
 
		// Patterns in order of specificity; captured group 1 is the clean URI without delimiters.
		const patterns: [RegExp, number][] = [
			[/<(https?:\/\/[^>\s]+)>/g, 1],
			[/"(https?:\/\/[^"]+)"/g, 1],
			[/'(https?:\/\/[^']+)'/g, 1],
			[/(https?:\/\/[^\s"'<>`(){}\[\],;]+)/g, 1],
		];
 
		for (const [pattern, group] of patterns) {
			let match: RegExpExecArray | null;
 
			while ((match = pattern.exec(line)) !== null) {
				Eif (cursor >= match.index && cursor <= match.index + match[0].length) {
					return match[group];
				}
			}
		}
 
		return null;
	}
}