All files / commands reveal-shape-definition.ts

93.75% Statements 15/16
83.33% Branches 10/12
100% Functions 2/2
93.75% Lines 15/16

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                1x     3x 3x 3x   3x   1x     2x 2x   2x 1x     1x   1x 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 const revealShapeDefinition = {
	id: 'mentor.command.revealShapeDefinition',
	handler: async (arg: DefinitionTreeNode | string, restoreFocus: boolean = false) => {
		const uri = getIriFromArgument(arg);
		const contextService = container.resolve<IDocumentContextService>(ServiceToken.DocumentContextService);
		contextService.activateDocument().then((editor) => {
 
			if (!uri || !editor || !contextService.activeContext) {
				// If no id is provided, we fail gracefully.
				return;
			}
 
			const vocabulary = container.resolve<VocabularyRepository>(ServiceToken.VocabularyRepository);
			const shapeUri = vocabulary.getShapes(contextService.activeContext.graphs, uri, { includeBlankNodes: true }).next().value;
 
			if (!shapeUri) {
				return;
			}
 
			const location = new ResourceDefinitionProvider().provideDefinitionForResource(contextService.activeContext, shapeUri, true);
 
			Eif (location instanceof vscode.Location) {
				editor.selection = new vscode.Selection(location.range.start, location.range.end);
				editor.revealRange(location.range, vscode.TextEditorRevealType.InCenter);
 
				Iif (restoreFocus) {
					// Reset the focus to the definition tree.
					vscode.commands.executeCommand('mentor.view.definitionTree.focus');
				}
			}
		});
	}
};