All files / commands shape-definition-locations.ts

73.52% Statements 25/34
47.36% Branches 9/19
100% Functions 3/3
73.52% Lines 25/34

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                            2x 2x 2x   2x       1x 1x   1x 1x                                     2x 2x 2x   2x       2x 2x   2x 1x     1x 1x   1x 2x   2x 2x       1x   1x       1x        
import * as vscode from 'vscode';
import { VocabularyRepository } from '@faubulous/mentor-rdf';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { IDocumentContextService } from '@src/services/document';
import { ResourceDefinitionProvider } from '@src/providers';
import { DefinitionTreeNode, getIriFromArgument } from '@src/views/trees/definition-tree/definition-tree-node';
 
export interface ShapeDefinitionLocations {
	editor: vscode.TextEditor;
	locations: vscode.Location[];
}
 
function toLocationKey(location: vscode.Location): string {
	const uri = location.uri.toString();
	const start = location.range.start;
	const end = location.range.end;
 
	return `${uri}:${start.line}:${start.character}:${end.line}:${end.character}`;
}
 
function compareLocations(a: vscode.Location, b: vscode.Location): number {
	const uriA = a.uri.toString();
	const uriB = b.uri.toString();
 
	Eif (uriA !== uriB) {
		return uriA.localeCompare(uriB);
	}
 
	if (a.range.start.line !== b.range.start.line) {
		return a.range.start.line - b.range.start.line;
	}
 
	if (a.range.start.character !== b.range.start.character) {
		return a.range.start.character - b.range.start.character;
	}
 
	if (a.range.end.line !== b.range.end.line) {
		return a.range.end.line - b.range.end.line;
	}
 
	return a.range.end.character - b.range.end.character;
}
 
export async function resolveShapeDefinitionLocations(arg: DefinitionTreeNode | string): Promise<ShapeDefinitionLocations | undefined> {
	const iri = getIriFromArgument(arg);
	const contextService = container.resolve<IDocumentContextService>(ServiceToken.DocumentContextService);
	const editor = await contextService.activateDocument();
 
	Iif (!iri || !editor || !contextService.activeContext) {
		return undefined;
	}
 
	const vocabulary = container.resolve<VocabularyRepository>(ServiceToken.VocabularyRepository);
	const shapeUris = [...new Set(vocabulary.getShapes(contextService.activeContext.graphs, iri, { includeBlankNodes: true }))];
 
	if (shapeUris.length === 0) {
		return undefined;
	}
 
	const definitionProvider = new ResourceDefinitionProvider();
	const locationsByKey = new Map<string, vscode.Location>();
 
	for (const shapeUri of shapeUris) {
		const location = definitionProvider.provideDefinitionForResource(contextService.activeContext, shapeUri);
 
		Eif (location instanceof vscode.Location) {
			locationsByKey.set(toLocationKey(location), location);
		}
	}
 
	const locations = [...locationsByKey.values()].sort(compareLocations);
 
	Iif (locations.length === 0) {
		return undefined;
	}
 
	return {
		editor,
		locations,
	};
}