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

100% Statements 40/40
78.57% Branches 11/14
100% Functions 10/10
100% Lines 40/40

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                              19x         19x         19x   19x   19x   19x     9x       4x       19x 1x 1x     1x           4x 4x   4x   4x 1x 1x       4x 1x 1x       4x 4x       7x 7x 1x     6x 4x     6x 1x     5x   5x 1x     4x   4x 1x 1x   1x                     4x         1x      
import * as vscode from 'vscode';
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 TurtleCodeLensProvider 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);
	}
 
	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)) {
				for (const range of context.subjects[iri]) {
					let n = Math.max(this._referenceProvider.provideReferencesForIri(iri).length - 1, 0);
 
					result.push(new vscode.CodeLens(new vscode.Range(
						new vscode.Position(range.start.line, range.start.character),
						new vscode.Position(range.end.line, range.end.character)
					), {
						command: 'mentor.command.findReferences',
						title: n + ' usages',
						arguments: [iri]
					}));
				}
			}
 
			resolve(result);
		});
	}
 
	resolveCodeLens?(codeLens: vscode.CodeLens, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeLens> {
		throw new Error('Method not implemented.');
	}
}