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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | 47x 47x 47x 47x 47x 357x 322x 47x 47x 47x 47x 1x 47x 1x 47x 47x 47x 47x 47x 47x 4x 2x 4x 1x 3x 3x 3x 2x 41x 40x 40x 1x 41x 49x 25x 41x 15x 3x 12x 15x 17x 17x 11x 11x 1x 49x 49x 21x 1x 20x 20x 20x 2x 2x 2x 20x 20x 7x 20x 1x 20x 1x 20x 1x 20x 7x 20x 1x 20x 1x 20x 1x 20x 1x 20x 20x 2x 18x 18x 18x 3x 3x 3x 18x 18x 18x 12x 18x 18x 18x 18x 2x 1x 1x 1x 1x 1x 18x 18x 18x 20x 10x 10x 10x 18x 8x 18x 5x 5x 1x 1x 5x 5x 3x 2x 5x 1x 1x | import * as vscode from 'vscode';
import { Uri, VocabularyRepository } from '@faubulous/mentor-rdf';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { ISettingsService, IWorkspaceIndexerService } from '@src/services/core';
import { IDocumentContextService } from '@src/services/document';
import { any } from '@src/utilities';
import { IDocumentContext } from '@src/services/document/document-context.interface';
import { DefinitionTreeLayout } from '@src/services/core/settings-service';
import { Debouncer } from '@src/utilities/debounce';
import { DefinitionTreeNode } from './definition-tree-node';
import { ClassesNode } from './nodes/classes/classes-node';
import { CollectionsNode } from './nodes/collections/collections-node';
import { ConceptSchemeNode } from './nodes/concept-scheme-node';
import { ConceptsNode } from './nodes/concepts/concepts-node';
import { IndividualsNode } from './nodes/individuals/individuals-node';
import { OntologyNode } from './nodes/ontology-node';
import { PropertiesNode } from './nodes/properties/properties-node';
import { RulesNode } from './nodes/rules/rules-node';
import { ShapesNode } from './nodes/shapes/shapes-node';
import { ValidatorsNode } from './nodes/validators/validators-node';
import { TreeNode, sortByLabel } from '../tree-node';
/**
* A combined tree node provider for RDF classes, properties and individuals.
*/
export class DefinitionNodeProvider implements vscode.TreeDataProvider<DefinitionTreeNode> {
/**
* The vocabulary document context.
*/
public document: IDocumentContext | undefined;
/**
* Cache of tree nodes keyed by their resource IRI, populated as the tree expands.
*/
private _nodeCache = new Map<string, DefinitionTreeNode>();
private _issueColorProvider: DefinitionNodeIssueColorProvider | undefined;
/**
* Reports whether the owning tree view is visible; set by the view via
* {@link setViewVisibilityProvider}. Without one, refreshes always run.
*/
private _isViewVisible: (() => boolean) | undefined;
/**
* Whether a refresh was requested while the view was hidden. Flushed by
* {@link flushPendingRefresh} when the view becomes visible again.
*/
private _refreshPending = false;
/**
* Coalesces refreshes: document context changes arrive once per file during
* bulk indexing and once per cell during notebook loads, and every refresh
* clears the node cache and rebuilds the full tree from store queries.
*/
private readonly _refreshDebouncer = new Debouncer(250);
private _onDidChangeTreeData: vscode.EventEmitter<DefinitionTreeNode | undefined> = new vscode.EventEmitter<DefinitionTreeNode | undefined>();
readonly onDidChangeTreeData: vscode.Event<DefinitionTreeNode | undefined> = this._onDidChangeTreeData.event;
private get vocabulary() {
return container.resolve<VocabularyRepository>(ServiceToken.VocabularyRepository);
}
private get settings() {
return container.resolve<ISettingsService>(ServiceToken.SettingsService);
}
private get contextService() {
return container.resolve<IDocumentContextService>(ServiceToken.DocumentContextService);
}
private get workspaceIndexerService() {
return container.resolve<IWorkspaceIndexerService>(ServiceToken.WorkspaceIndexerService);
}
constructor(issueColorProvider?: DefinitionNodeIssueColorProvider) {
this._issueColorProvider = issueColorProvider;
this.contextService.onDidChangeDocumentContext((context) => {
// Update the tree when the active document changed. Debounced: while
// typing, a fresh context is delivered on every reload tick.
this._refreshDebouncer.schedule(() => this.refresh(context));
});
this.workspaceIndexerService.onDidFinishIndexing(() => {
// Update the tree when the workspace has been indexed, incorporating definitions from external files.
this.refresh();
});
this.settings.onDidChange("view.definitionTree.labelStyle", () => this.refresh());
this.settings.onDidChange("view.definitionTree.defaultLayout", () => this.refresh());
this.settings.onDidChange("view.showReferences", () => this.refresh());
this.settings.onDidChange("view.showPropertyTypes", () => this.refresh());
this.settings.onDidChange("view.showIndividualTypes", () => this.refresh());
this.settings.onDidChange("view.activeLanguage", () => this.refresh());
}
setIssueColorProvider(issueColorProvider: DefinitionNodeIssueColorProvider | undefined): void {
this._issueColorProvider = issueColorProvider;
}
/**
* Lets the owning view report its visibility so refreshes of a hidden tree
* can be deferred until it is shown again (see {@link flushPendingRefresh}).
*/
setViewVisibilityProvider(isVisible: () => boolean): void {
this._isViewVisible = isVisible;
}
/**
* Runs a refresh that was deferred while the view was hidden. Called by the
* owning view when it becomes visible.
*/
flushPendingRefresh(): void {
if (this._refreshPending) {
this._refreshPending = false;
this.refresh();
}
}
/**
* Refresh the tree view.
*/
refresh(document?: IDocumentContext): void {
if (document) {
this.document = document;
}
if (!this.document) {
return;
}
// Rebuilding a hidden tree is wasted work — VS Code does not render it.
// Remember the request and flush it when the view becomes visible.
Iif (this._isViewVisible && !this._isViewVisible()) {
this._refreshPending = true;
return;
}
this._nodeCache.clear();
this._onDidChangeTreeData.fire(undefined);
}
getParent(node: DefinitionTreeNode): DefinitionTreeNode | undefined {
return node.parent;
}
getChildren(node?: DefinitionTreeNode): DefinitionTreeNode[] | null | undefined {
let children: DefinitionTreeNode[];
if (!node) {
const layout = this.settings.get<DefinitionTreeLayout>('view.definitionTree.defaultLayout');
children = layout === DefinitionTreeLayout.ByType
? this.getRootNodes()
: this.getRootNodesWithSources() as DefinitionTreeNode[];
} else {
children = node.getChildren() as DefinitionTreeNode[];
}
// Cache nodes with real resource IRIs so they can be revealed later.
for (const child of children) {
if (child.uri && !child.uri.startsWith('mentor:')) {
this._nodeCache.set(child.uri, child);
}
}
return children;
}
/**
* Find the tree node for a given resource IRI. Delegates resolution to each
* root node which uses type-aware, hierarchy-driven lookup internally.
* @param iri The resource IRI to search for.
* @returns The matching tree node, or undefined if not found.
*/
getNodeForUri(iri: string): DefinitionTreeNode | undefined {
if (this._nodeCache.has(iri)) {
return this._nodeCache.get(iri);
}
const roots = this.getChildren(undefined) ?? [];
for (const root of roots) {
const node = root.resolveNodeForUri(iri);
if (node) {
this._nodeCache.set(iri, node);
return node;
}
}
return undefined;
}
protected createRootNode<NodeType extends DefinitionTreeNode>(
NodeConstructor: new (document: IDocumentContext, id: string, iri: string, options?: any) => NodeType,
document: IDocumentContext,
iri: string,
options?: any
): NodeType {
const id = `<${iri}>`;
return new NodeConstructor(document, id, iri, options);
}
/**
* Get the root nodes of the document, grouped by type.
* @returns The root nodes of the document.
*/
getRootNodes() {
if (!this.document) {
return [];
}
const result: DefinitionTreeNode[] = [];
const ontologyUris = this.vocabulary.getOntologies(this.document.graphs);
for (const ontologyUri of ontologyUris) {
const node = this.createRootNode(OntologyNode, this.document, ontologyUri, { definedBy: ontologyUri });
// Note: Group by defintion source is disabled here, so we do not make the ontology nodes exapndable.
node.initialCollapsibleState = vscode.TreeItemCollapsibleState.None;
result.push(node);
}
const schemeUris = this.vocabulary.getConceptSchemes(this.document.graphs);
for (const schemeUri of schemeUris) {
result.push(this.createRootNode(ConceptSchemeNode, this.document, schemeUri));
}
if (any(this.vocabulary.getClasses(this.document.graphs))) {
result.push(this.createRootNode(ClassesNode, this.document, 'mentor:classes'));
}
if (any(this.vocabulary.getProperties(this.document.graphs))) {
result.push(this.createRootNode(PropertiesNode, this.document, 'mentor:properties'));
}
if (any(this.vocabulary.getIndividuals(this.document.graphs, undefined))) {
result.push(this.createRootNode(IndividualsNode, this.document, 'mentor:individuals'));
}
// Note: Concepts that are displayed below a concept scheme are listed under that scheme,
// so only the remaining concepts are listed here.
if (any(this.vocabulary.getUnscopedConcepts(this.document.graphs))) {
result.push(this.createRootNode(ConceptsNode, this.document, 'mentor:concepts', { inScheme: null }));
}
// Note: Collections that are associated with a concept scheme are listed under that scheme,
// so only the remaining collections are listed here.
if (any(this.vocabulary.getCollections(this.document.graphs, { inScheme: null }))) {
result.push(this.createRootNode(CollectionsNode, this.document, 'mentor:collections', { inScheme: null }));
}
if (any(this.vocabulary.getShapes(this.document.graphs, undefined))) {
result.push(this.createRootNode(ShapesNode, this.document, 'mentor:shapes'));
}
if (any(this.vocabulary.getRules(this.document.graphs, undefined))) {
result.push(this.createRootNode(RulesNode, this.document, 'mentor:rules'));
}
if (any(this.vocabulary.getValidators(this.document.graphs, undefined))) {
result.push(this.createRootNode(ValidatorsNode, this.document, 'mentor:validators'));
}
return result;
}
/**
* Get the root nodes of the document, grouped by definition source.
* @returns The root nodes of the document.
*/
getRootNodesWithSources(): TreeNode[] {
if (!this.document) {
return [];
}
const ontologyUris = [...this.vocabulary.getOntologies(this.document.graphs)];
const ontologyNodes = [];
for (const ontologyUri of ontologyUris) {
const node = this.createRootNode(OntologyNode, this.document, ontologyUri, { definedBy: ontologyUri });
node.initialCollapsibleState = vscode.TreeItemCollapsibleState.Expanded;
ontologyNodes.push(node);
}
const schemeUris = this.vocabulary.getConceptSchemes(this.document.graphs);
const schemeNodes = [];
for (const schemeUri of schemeUris) {
schemeNodes.push(this.createRootNode(ConceptSchemeNode, this.document, schemeUri));
}
const ontologies = new Set(ontologyUris);
const sourceUris = this.vocabulary.getDefinitionSources(this.document.graphs);
const sourceNodes = [];
for (let source of sourceUris) {
// Handle the case where rdfs:isDefinedBy refers to the ontology namespace
// but the ontology header is annotated with an absolute URI.
if (ontologies.has(source) || ontologies.has(Uri.getNormalizedUri(source))) {
continue;
}
const node = this.createRootNode(OntologyNode, this.document, source, { definedBy: source });
node.initialCollapsibleState = vscode.TreeItemCollapsibleState.Collapsed;
node.isReferenced = true;
sourceNodes.push(node);
}
const result = [
...sortByLabel(ontologyNodes),
...sortByLabel(schemeNodes),
...sortByLabel(sourceNodes)
];
// Note: For the root nodes we only want to show sources that actually contain *defined* classses.
// This is why we exclude referenced classes here, independent from the current setting.
let options = { notDefinedBy: new Set([...ontologyUris, ...sourceUris]), includeReferenced: false };
let hasUnknown: boolean =
any(this.vocabulary.getClasses(this.document.graphs, options)) ||
any(this.vocabulary.getProperties(this.document.graphs, options)) ||
any(this.vocabulary.getIndividuals(this.document.graphs, undefined, options)) ||
// Concepts and collections of a concept scheme are shown below the scheme, all others below 'Unknown'.
any(this.vocabulary.getUnscopedConcepts(this.document.graphs, options)) ||
any(this.vocabulary.getCollections(this.document.graphs, { ...options, inScheme: null })) ||
any(this.vocabulary.getShapes(this.document.graphs, undefined, options)) ||
any(this.vocabulary.getRules(this.document.graphs, options)) ||
any(this.vocabulary.getValidators(this.document.graphs, options));
if (hasUnknown) {
// Important: Reset the includeReferenced setting for the root nodes.
const node = this.createRootNode(OntologyNode, this.document, 'mentor:unknown', { notDefinedBy: options.notDefinedBy });
node.isReferenced = true;
result.push(node);
}
// If there is only one definition source, expand it by default.
if (result.length === 1) {
result[0].initialCollapsibleState = vscode.TreeItemCollapsibleState.Expanded;
}
return result;
}
/**
* Get the tree item of a node. The tooltip is left undefined so that VS Code resolves it
* on hover through {@link resolveTreeItem} instead of querying it for every node.
* @param node A definition tree node.
* @returns The tree item of the node without a tooltip.
*/
getTreeItem(node: DefinitionTreeNode): vscode.TreeItem {
const icon = this._getIconWithIssueColor(node);
return {
id: node.id,
collapsibleState: node.getCollapsibleState(),
contextValue: node.getContextValue(),
resourceUri: node.getResourceUri(),
iconPath: icon,
label: node.getLabel(),
description: node.getDescription(),
command: node.getCommand()
};
}
/**
* Resolve the tooltip of a tree item. VS Code calls this only when an item without a
* tooltip is hovered, so the label and description queries of the tooltip run for the
* hovered node only and not for every node when the tree is built.
* @param item The tree item returned by {@link getTreeItem}.
* @param node The node of the tree item.
* @returns The tree item with its tooltip set.
*/
resolveTreeItem(item: vscode.TreeItem, node: DefinitionTreeNode): vscode.TreeItem {
item.tooltip = node.getTooltip();
return item;
}
private _getIconWithIssueColor(node: DefinitionTreeNode): vscode.ThemeIcon | undefined {
const icon = node.getIcon();
if (!(icon instanceof vscode.ThemeIcon)) {
return icon;
}
const issueColor = this._issueColorProvider?.getIssueColor(node.getResourceUri());
if (!issueColor) {
return icon;
}
return new vscode.ThemeIcon(icon.id, issueColor);
}
}
export interface DefinitionNodeIssueColorProvider {
getIssueColor(uri: vscode.Uri | undefined): vscode.ThemeColor | undefined;
} |