All files / languages/turtle/providers turtle-usage-codelens-provider.ts

100% Statements 49/49
77.27% Branches 17/22
100% Functions 11/11
100% Lines 49/49

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 131 132 133 134 135 136 137 138 139 140 141 142 143 144                                22x         22x         22x   22x   22x   22x     15x       7x       7x       22x 1x 1x     1x           7x 7x   7x   7x 1x 1x       7x 1x 1x       7x 7x       10x 10x 1x     9x 7x     9x 1x     8x   8x 1x     7x   7x 4x 4x     4x 4x   4x 4x   4x         4x   4x           4x 1x                 7x         1x      
import * as vscode from 'vscode';
import { SH, VocabularyRepository } from '@faubulous/mentor-rdf';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { IWorkspaceIndexerService } from '@src/services/core';
import { IDocumentContextService } from '@src/services/document';
import { ResourceReferenceProvider } from '@src/providers/resource-reference-provider';
import { getConfig } from '@src/utilities/vscode/config';
 
/**
 * Provides usage information for resource definitions in Turtle documents.
 */
export class TurtleUsageCodeLensProvider implements vscode.CodeLensProvider {
	/**
	 * Indicates whether the workspace has been initialized.
	 */
	private _initialized: boolean = false;
 
	/**
	 * Indicates whether the provider is initializing.
	 */
	private _initializing: boolean = false;
 
	/**
	 * Indicates whether the provider is enabled.
	 */
	private _enabled: boolean = true;
 
	private readonly _referenceProvider: ResourceReferenceProvider = new ResourceReferenceProvider();
 
	private readonly _onDidChangeCodeLenses = new vscode.EventEmitter<void>();
 
	onDidChangeCodeLenses: vscode.Event<void> = this._onDidChangeCodeLenses.event;
 
	private get _contextService() {
		return container.resolve<IDocumentContextService>(ServiceToken.DocumentContextService);
	}
 
	private get _workspaceIndexerService() {
		return container.resolve<IWorkspaceIndexerService>(ServiceToken.WorkspaceIndexerService);
	}
 
	private get _vocabulary() {
		return container.resolve<VocabularyRepository>(ServiceToken.VocabularyRepository);
	}
 
	constructor() {
		vscode.workspace.onDidChangeConfiguration((e) => {
			Eif (e.affectsConfiguration('mentor.editor.codeLensEnabled')) {
				this._enabled = getConfig().get('editor.codeLensEnabled', true);
 
				// Fire the event to refresh the code lenses.
				this._onDidChangeCodeLenses.fire();
			}
		});
	}
 
	private async _initialize() {
		this._initializing = true;
		this._initialized = false;
 
		this._enabled = getConfig().get('editor.codeLensEnabled', true);
 
		this._workspaceIndexerService.waitForIndexed().then(() => {
			Eif (this._enabled) {
				this._onDidChangeCodeLenses.fire();
			}
		});
 
		this._contextService.onDidChangeDocumentContext(() => {
			Eif (this._enabled) {
				this._onDidChangeCodeLenses.fire();
			}
		});
 
		this._initialized = true;
		this._initializing = false;
	}
 
	provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeLens[]> {
		return new Promise(async (resolve, reject) => {
			if (this._initializing) {
				return [];
			}
 
			if (!this._initialized) {
				await this._initialize();
			}
 
			if (!this._enabled) {
				return [];
			}
 
			const context = this._contextService.contexts[document.uri.toString()];
 
			if (!context) {
				return [];
			}
 
			const result = [];
 
			for (const iri of Object.keys(context.subjects)) {
				const isShapeResource = this._vocabulary.hasType(context.graphs, iri, SH.Shape);
				const shapeUris = isShapeResource
					? []
					: [...new Set(this._vocabulary.getShapes(context.graphs, iri, { includeBlankNodes: true }))];
				const shapeCount = shapeUris.length;
				const shapeTitle = `${shapeCount} shape${shapeCount === 1 ? '' : 's'}`;
 
				for (const range of context.subjects[iri]) {
					let n = Math.max(this._referenceProvider.provideReferencesForIri(iri).length - 1, 0);
					
					const codeLensRange = new vscode.Range(
						new vscode.Position(range.start.line, range.start.character),
						new vscode.Position(range.end.line, range.end.character)
					);
 
					const usageTitle = `${n} usage${n === 1 ? '' : 's'}`;
 
					result.push(new vscode.CodeLens(codeLensRange, {
						command: 'mentor.command.findReferences',
						title: usageTitle,
						arguments: [iri]
					}));
 
					if (shapeCount > 0) {
						result.push(new vscode.CodeLens(codeLensRange, {
							command: 'mentor.command.showShapeReferences',
							title: shapeTitle,
							arguments: [iri]
						}));
					}
				}
			}
 
			resolve(result);
		});
	}
 
	resolveCodeLens?(codeLens: vscode.CodeLens, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeLens> {
		throw new Error('Method not implemented.');
	}
}