All files / views/trees/definition-tree/nodes/properties property-node.ts

100% Statements 32/32
100% Branches 32/32
100% Functions 11/11
100% Lines 32/32

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                          24x           82x     2x   2x 1x     2x           9x 8x   8x 3x       9x       35x       9x       29x     2x     1x     1x                                   13x         6x     6x           1x   1x       2x   2x 2x   2x               7x               2x       5x   5x 3x     5x    
import * as vscode from "vscode";
import { XSD, RDF, RDFS, VocabularyRepository } from '@faubulous/mentor-rdf';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { any, getPropertyTypeFromRange } from '@src/utilities';
import { TreeNode, sortByLabel } from "@src/views/trees/tree-node";
import { DefinitionTreeNode } from "../../definition-tree-node";
 
/**
 * Node of a property in the definition tree.
 */
export class PropertyNode extends DefinitionTreeNode {
	protected get vocabulary() {
		return container.resolve<VocabularyRepository>(ServiceToken.VocabularyRepository);
	}
 
	/**
	 * Type of the property.
	 */
	propertyType: 'objectProperty' | 'dataProperty' | 'annotationProperty' = 'objectProperty';
 
	getContextValue(): string {
		let result = super.getContextValue();
 
		if (this.vocabulary.hasShapes(this.document.graphs, this.uri, this.getQueryOptions({ definedBy: undefined }))) {
			result += " shape-target";
		}
 
		return result;
	}
 
	getRange(propertyUri?: string): string {
		let rangeUri: string | undefined;
 
		if (propertyUri) {
			rangeUri = this.vocabulary.getRange(this.getDocumentGraphs(), propertyUri);
 
			if (!rangeUri) {
				rangeUri = this.vocabulary.getDatatype(this.getDocumentGraphs(), propertyUri);
			}
		}
 
		return rangeUri ?? RDFS.Resource;
	}
 
	getPropertyType(rangeUri?: string) {
		return getPropertyTypeFromRange(rangeUri);
	}
 
	getIconColorFromRange(rangeIri?: string) {
		return new vscode.ThemeColor('mentor.color.' + this.getPropertyType(rangeIri));
	}
 
	getIconNameFromRange(rangeIri?: string) {
		switch (rangeIri) {
			case XSD.date:
			case XSD.dateTime: {
				return 'calendar';
			}
			case XSD.boolean: {
				return 'symbol-boolean';
			}
			case XSD.base64Binary: {
				return 'file-binary';
			}
			case XSD.byte:
			case XSD.decimal:
			case XSD.double:
			case XSD.float:
			case XSD.int:
			case XSD.integer:
			case XSD.short:
			case XSD.nonNegativeInteger:
			case XSD.nonPositiveInteger:
			case XSD.negativeInteger:
			case XSD.positiveInteger:
			case XSD.long:
			case XSD.unsignedInt:
			case XSD.unsignedShort:
			case XSD.unsignedLong:
			case XSD.unsignedByte: {
				return 'symbol-number';
			}
			case XSD.string:
			case RDF.langString:
			case RDFS.Literal: {
				return 'symbol-text';
			}
			default: {
				return 'rdf-object-property';
			}
		}
	}
 
	override getIconColor() {
		const rangeIri = this.getRange(this.uri);
 
		return this.getIconColorFromRange(rangeIri);
	}
 
	override getIcon() {
		const rangeIri = this.getRange(this.uri);
 
		const iconName = this.getIconNameFromRange(rangeIri);
		const iconColor = this.getIconColorFromRange(rangeIri);
 
		return new vscode.ThemeIcon(iconName, iconColor);
	}
 
	/**
	 * Get the IRIs of the sub properties that are listed below this node.
	 * @returns An iterator of property IRIs.
	 */
	private _getSubPropertyIris(): IterableIterator<string> {
		return this.vocabulary.getSubProperties(this.getDocumentGraphs(), this.uri, this.getQueryOptions());
	}
 
	/**
	 * Indicates whether the property has sub properties, stopping at the first one.
	 * @returns `true` if the node has children, `false` otherwise.
	 */
	override hasChildren(): boolean {
		return any(this._getSubPropertyIris());
	}
 
	override getChildren(): TreeNode[] {
		const result = [];
 
		for (const p of this._getSubPropertyIris()) {
			result.push(this.createChildNode(PropertyNode, p));
		}
 
		return sortByLabel(result);
	}
}