All files / commands reveal-definition.ts

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

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