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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 1x 1x 1x 10x 10x 10x 10x 1x 1x 10x 1x 1x 1x 10x 10x 13x 1x 12x 10x 12x 1x 11x 11x 11x 1x 10x 10x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 10x 5x 5x 1x 4x 4x 5x | import * as vscode from 'vscode';
import { SH, VocabularyRepository } from '@faubulous/mentor-rdf';
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';
import { isDocumentVisible } from '@src/utilities/vscode/editors';
import { Debouncer } from '@src/utilities/debounce';
/**
* 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;
/**
* The IRIs of the usage lenses that are not resolved yet. Counting the usages of an IRI
* scans every indexed document, so it is deferred to {@link resolveCodeLens}, which VS Code
* only calls for the lenses in the visible part of an editor.
*/
private readonly _usageLensIris = new WeakMap<vscode.CodeLens, string>();
/**
* Coalesces refresh fires: every fire makes VS Code re-request the lenses of
* all visible documents, and computing usage counts scans every indexed context.
*/
private readonly _refreshDebouncer = new Debouncer(250);
private readonly _onDidChangeCodeLenses = new vscode.EventEmitter<void>();
onDidChangeCodeLenses: vscode.Event<void> = this._onDidChangeCodeLenses.event;
constructor(
private readonly _contextService: IDocumentContextService,
private readonly _workspaceIndexerService: IWorkspaceIndexerService,
private readonly _vocabulary: VocabularyRepository
) {
this._referenceProvider = new ResourceReferenceProvider(this._contextService);
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((context) => {
Iif (!this._enabled) {
return;
}
// Usage counts change with any indexed document, but refreshing on every
// context change turns bulk indexing into a lens-refresh storm. Refresh
// only when the changed document itself is visible; counts in other
// visible editors catch up once indexing finishes (see waitForIndexed).
Iif (context && !isDocumentVisible(context.uri)) {
return;
}
this._refreshDebouncer.schedule(() => this._onDidChangeCodeLenses.fire());
});
this._initialized = true;
this._initializing = false;
}
async provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): Promise<vscode.CodeLens[]> {
if (this._initializing) {
return [];
}
if (!this._initialized) {
await this._initialize();
}
if (!this._enabled) {
return [];
}
// Suppress usage lenses inside notebook cells: a subject on line 0 would place
// a usage lens on the same line as the cell-id slug, and VS Code clips
// overflowing CodeLenses, hiding the slug. Usage counts are not meaningful for a
// transient cell anyway.
Iif (document.uri.scheme === 'vscode-notebook-cell') {
return [];
}
const context = this._contextService.contexts[document.uri.toString()];
if (!context) {
return [];
}
const result: vscode.CodeLens[] = [];
for (const iri of context.subjects.keys()) {
Iif (token?.isCancellationRequested) {
return [];
}
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.getRanges(iri)) {
const codeLensRange = new vscode.Range(
new vscode.Position(range.start.line, range.start.character),
new vscode.Position(range.end.line, range.end.character)
);
// The usage count is filled in by resolveCodeLens.
const usageLens = new vscode.CodeLens(codeLensRange);
this._usageLensIris.set(usageLens, iri);
result.push(usageLens);
if (shapeCount > 0) {
result.push(new vscode.CodeLens(codeLensRange, {
command: 'mentor.command.showShapeReferences',
title: shapeTitle,
arguments: [iri]
}));
}
}
}
return result;
}
/**
* Fills in the usage count of a usage lens. Counting scans every indexed document, so it
* runs only for the lenses VS Code shows, not for every subject of the document.
* @param codeLens A lens returned by {@link provideCodeLenses}.
* @returns The lens with its command.
*/
resolveCodeLens(codeLens: vscode.CodeLens): vscode.CodeLens {
const iri = this._usageLensIris.get(codeLens);
if (iri === undefined) {
return codeLens;
}
const usageCount = Math.max(this._referenceProvider.countReferencesForIri(iri) - 1, 0);
codeLens.command = {
command: 'mentor.command.findReferences',
title: `${usageCount} usage${usageCount === 1 ? '' : 's'}`,
arguments: [iri]
};
return codeLens;
}
}
|