export async function navigateToTestOrTarget()

in src/commands/navigation/navigationCommands.ts [16:73]


export async function navigateToTestOrTarget(gotoTest: boolean): Promise<void> {
    if (!window.activeTextEditor) {
        return;
    }
    const uri: Uri = window.activeTextEditor.document.uri;
    const result: ITestNavigationResult | undefined = await searchTestOrTarget(uri.toString(), gotoTest);
    if (!result?.items?.length) {
        const items: string[] = [SEARCH_FILES];
        if (gotoTest) {
            items.unshift(GENERATE_TESTS);
        }
        window.showQuickPick(items, {
            placeHolder: `${gotoTest ? 'Tests' : 'Test subjects'} not found for current file`,
        }).then((choice: string | undefined) => {
            if (choice === SEARCH_FILES) {
                let fileName: string = path.basename(window.activeTextEditor!.document.fileName);
                if (!gotoTest) {
                    fileName = fileName.replace(/Tests?/g, '');
                }
                commands.executeCommand(VSCodeCommands.WORKBENCH_ACTION_QUICK_OPEN, fileName.substring(0, fileName.lastIndexOf('.')));
            } else if (choice === GENERATE_TESTS) {
                commands.executeCommand(JavaTestRunnerCommands.JAVA_TEST_GENERATE_TESTS, uri, 0);
            }
        });
    } else if (result.items.length === 1) {
        window.showTextDocument(Uri.parse(result.items[0].uri));
    } else {
        const sortedResults: ITestNavigationItem[] = result.items.sort((a: ITestNavigationItem, b: ITestNavigationItem) => {
            if (a.outOfBelongingProject && !b.outOfBelongingProject) {
                return Number.MAX_SAFE_INTEGER;
            } else if (!a.outOfBelongingProject && b.outOfBelongingProject) {
                return Number.MIN_SAFE_INTEGER;
            } else {
                if (a.relevance === b.relevance) {
                    return a.simpleName.localeCompare(b.simpleName);
                }
                return a.relevance - b.relevance;
            }
        });
        const api: SymbolTree | undefined = await extensions.getExtension<SymbolTree>(REFERENCES_VIEW_EXTENSION)?.activate();
        if (api) {
            const title: string = gotoTest ? 'Tests' : 'Test Subjects';
            const input: TestNavigationInput = new TestNavigationInput(
                title,
                new Location(uri, new Range(
                    result.location.range.start.line,
                    result.location.range.start.character,
                    result.location.range.end.line,
                    result.location.range.end.line,
                )),
                sortedResults
            );
            api.setInput(input);
        } else {
            fallbackForNavigation(sortedResults);
        }
    }
}