All files / languages/triplate/providers triplate-code-lens-provider.ts

94.44% Statements 17/18
100% Branches 8/8
50% Functions 1/2
94.44% Lines 17/18

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                          7x         7x         7x     7x   7x 1x     6x                 6x 5x   5x                 6x   6x   5x 8x 8x   8x                 6x              
import * as vscode from 'vscode';
import { isTemplate } from 'triplate';
import { TriplateCompileCache } from '../triplate-compile-cache';
 
/**
 * Provides CodeLenses for triplate templates: a top-of-file "Run" lens that
 * prompts for parameter values, and a "Run" lens above each `example` block
 * that renders using the example's declared values.
 */
export class TriplateCodeLensProvider implements vscode.CodeLensProvider {
	/**
	 * The cache of compiled templates, used to determine the locations of `example` blocks.
	 */
	private readonly _cache = new TriplateCompileCache();
	
	/**
	 * Event emitter that signals when the CodeLenses should be refreshed.
	 */
	private _onDidChangeCodeLenses = new vscode.EventEmitter<void>();
 
	/**
	 * Fires when the CodeLenses should be refreshed, e.g., when the compiled template cache changes.
	 */
	public readonly onDidChangeCodeLenses = this._onDidChangeCodeLenses.event;
 
	provideCodeLenses(document: vscode.TextDocument): vscode.CodeLens[] {
		const text = document.getText();
 
		if (!isTemplate(text)) {
			return [];
		}
 
		const codeLenses: vscode.CodeLens[] = [];
 
		// Top-of-file Run lens (prompts for parameter values). For `.sparql`-language
		// documents, SparqlCodeLensProvider supplies this lens itself instead, since its
		// lens for the same range is the one that reliably ends up first on screen, so skip
		// it here to avoid a duplicate. For other Triplate-flavored languages (turtle, trig,
		// etc.) this provider is the only one contributing lenses, so it owns the Run lens.
		// The Run lens is shown in notebook cells too, so the lens group stays consistent
		// with how it appears in a standalone editor.
		if (document.languageId !== 'sparql') {
			const topRange = new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0));
 
			codeLenses.push(new vscode.CodeLens(topRange, {
				title: '$(play)\u00A0Run',
				tooltip: 'Render this template with parameter values',
				command: 'mentor.command.executeTriplateTemplate',
				arguments: [document.uri.toString()],
			}));
		}
 
		// Per-example Run lenses (render using the example's declared values).
		const compiled = this._cache.get(document);
 
		if (compiled) {
			// `triplate` tracks each example block's 1-based line/column.
			for (const example of compiled.examples) {
				const position = new vscode.Position(Math.max(0, example.line - 1), Math.max(0, example.column - 1));
				const range = new vscode.Range(position, position);
 
				codeLenses.push(new vscode.CodeLens(range, {
					title: `$(play)\u00A0Run`,
					tooltip: example.description ?? `Render the "${example.id}" example`,
					command: 'mentor.command.executeTriplateExample',
					arguments: [document.uri.toString(), example.id],
				}));
			}
		}
 
		return codeLenses;
	}
 
	public refresh(): void {
		this._onDidChangeCodeLenses.fire();
	}
}