All files / providers inference-uri.ts

100% Statements 11/11
100% Branches 8/8
100% Functions 3/3
100% Lines 11/11

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            19x           19x               13x   13x                       4x 4x     4x   4x                 3x 2x   1x      
import * as vscode from 'vscode';
 
/**
 * Helper class for generating and parsing Mentor inference graph URIs.
 */
export class InferenceUri {
	static readonly queryAppendix = `inference`;
 
	/**
	 * A regular expression to match Mentor inference graph URIs in text documents.
	 * @note This is intentionally a string so that any modifiers for the evaluation can be easily applied as needed.
	 */
	static readonly uriRegex = `(<[^\\s>]+>)${this.queryAppendix}$`;
 
	/**
	 * Checks if a URI is an inference graph URI.
	 * @param uri The URI to check.
	 * @returns True if the URI is an inference graph URI, false otherwise.
	 */
	static isInferenceUri(uri: string | vscode.Uri): boolean {
		const u = typeof uri === "string" ? uri : uri.toString(true);
 
		return u.endsWith(this.queryAppendix);
	}
 
	/**
	 * Converts any URI (https://..) to a inference graph URI.
	 * @param originalUri The URI to convert.
	 * @returns The corresponding Mentor inference graph URI.
	 */
	static toInferenceUri(originalUri: string | vscode.Uri): string {
		// Note: Append the inferenceGraphFragment to any existing fragment id or we create a new one. The reasoning 
		// is that when sorting or prefixing IRIs the inference graph will be at the end and sorted after the 
		// original IRI and existing prefixes will still work.
		const u = typeof originalUri === "string" ? originalUri : originalUri.toString(true);
		const n = u.indexOf('?');
 
		// If there is no fragment id, then append one.
		const inferenceUri = n > -1 ? u + '&' : u + '?';
 
		return inferenceUri + this.queryAppendix;
	}
 
	/**
	 * Restore a Mentor inference graph URI to the original URI.
	 * @param inferenceUri A Mentor inference graph URI.
	 * @returns The inference URI without the inference graph appendix if present, otherwise the unmodified argument.
	 */
	static toUri(inferenceUri: string): string {
		if (this.isInferenceUri(inferenceUri)) {
			return inferenceUri.substring(0, inferenceUri.length - this.queryAppendix.length - 1);
		} else {
			return inferenceUri;
		}
	}
}