Search in sources :

Example 1 with GoMethodDeclaration

use of com.goide.psi.GoMethodDeclaration in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoTestLocator method getLocation.

@NotNull
@Override
public List<Location> getLocation(@NotNull String protocolId, @NotNull String path, @NotNull Project project, @NotNull GlobalSearchScope scope) {
    if (PROTOCOL.equals(protocolId)) {
        IdFilter idFilter = GoIdFilter.getTestsFilter(project);
        List<String> locationDataItems = StringUtil.split(path, ".");
        // Location is a function name, e.g. `TestCheckItOut`
        if (locationDataItems.size() == 1) {
            return ContainerUtil.mapNotNull(GoFunctionIndex.find(path, project, scope, idFilter), function -> PsiLocation.fromPsiElement(project, function));
        }
        // Location is a method name, e.g. `FooSuite.TestCheckItOut`
        if (locationDataItems.size() == 2) {
            List<Location> locations = ContainerUtil.newArrayList();
            for (GoTypeSpec typeSpec : GoTypesIndex.find(locationDataItems.get(0), project, scope, idFilter)) {
                for (GoMethodDeclaration method : typeSpec.getMethods()) {
                    if (locationDataItems.get(1).equals(method.getName())) {
                        ContainerUtil.addIfNotNull(locations, PsiLocation.fromPsiElement(method));
                    }
                }
            }
            return locations;
        }
    } else if (SUITE_PROTOCOL.equals(protocolId)) {
        IdFilter idFilter = GoIdFilter.getTestsFilter(project);
        return ContainerUtil.mapNotNull(GoTypesIndex.find(path, project, scope, idFilter), spec -> PsiLocation.fromPsiElement(project, spec));
    } else {
        return Collections.emptyList();
    }
    throw new RuntimeException("Unsupported location: " + path);
}
Also used : GoFunctionIndex(com.goide.stubs.index.GoFunctionIndex) GoTypeSpec(com.goide.psi.GoTypeSpec) GoTypesIndex(com.goide.stubs.index.GoTypesIndex) StringUtil(com.intellij.openapi.util.text.StringUtil) GoMethodDeclaration(com.goide.psi.GoMethodDeclaration) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) IdFilter(com.intellij.util.indexing.IdFilter) ContainerUtil(com.intellij.util.containers.ContainerUtil) PsiLocation(com.intellij.execution.PsiLocation) SMTestLocator(com.intellij.execution.testframework.sm.runner.SMTestLocator) GoIdFilter(com.goide.stubs.index.GoIdFilter) List(java.util.List) Project(com.intellij.openapi.project.Project) Location(com.intellij.execution.Location) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) IdFilter(com.intellij.util.indexing.IdFilter) GoIdFilter(com.goide.stubs.index.GoIdFilter) GoMethodDeclaration(com.goide.psi.GoMethodDeclaration) GoTypeSpec(com.goide.psi.GoTypeSpec) PsiLocation(com.intellij.execution.PsiLocation) Location(com.intellij.execution.Location) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with GoMethodDeclaration

use of com.goide.psi.GoMethodDeclaration in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoTestRunLineMarkerProvider method getInfo.

@Nullable
@Override
public Info getInfo(PsiElement e) {
    if (e != null && e.getNode().getElementType() == GoTypes.IDENTIFIER) {
        PsiElement parent = e.getParent();
        PsiFile file = e.getContainingFile();
        if (!GoTestFinder.isTestFile(file)) {
            return null;
        }
        if (GoRunUtil.isPackageContext(e)) {
            return new Info(AllIcons.RunConfigurations.TestState.Run_run, TOOLTIP_PROVIDER, ExecutorAction.getActions(0));
        } else if (parent instanceof GoFunctionOrMethodDeclaration) {
            GoTestFunctionType functionType = GoTestFunctionType.fromName(((GoFunctionOrMethodDeclaration) parent).getName());
            if (functionType != null) {
                if (parent instanceof GoFunctionDeclaration) {
                    return getInfo(GoTestLocator.PROTOCOL + "://" + ((GoFunctionDeclaration) parent).getName(), e.getProject());
                } else if (parent instanceof GoMethodDeclaration) {
                    GoReceiver receiver = ((GoMethodDeclaration) parent).getReceiver();
                    PsiElement receiverIdentifier = receiver != null ? receiver.getIdentifier() : null;
                    String receiverText = receiverIdentifier != null ? receiverIdentifier.getText() + "." : "";
                    return getInfo(GoTestLocator.PROTOCOL + "://" + receiverText + ((GoMethodDeclaration) parent).getName(), e.getProject());
                }
            }
        }
    }
    return null;
}
Also used : GoFunctionDeclaration(com.goide.psi.GoFunctionDeclaration) GoMethodDeclaration(com.goide.psi.GoMethodDeclaration) GoReceiver(com.goide.psi.GoReceiver) PsiFile(com.intellij.psi.PsiFile) GoFunctionOrMethodDeclaration(com.goide.psi.GoFunctionOrMethodDeclaration) TestStateInfo(com.intellij.execution.testframework.sm.runner.states.TestStateInfo) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GoMethodDeclaration (com.goide.psi.GoMethodDeclaration)2 GoFunctionDeclaration (com.goide.psi.GoFunctionDeclaration)1 GoFunctionOrMethodDeclaration (com.goide.psi.GoFunctionOrMethodDeclaration)1 GoReceiver (com.goide.psi.GoReceiver)1 GoTypeSpec (com.goide.psi.GoTypeSpec)1 GoFunctionIndex (com.goide.stubs.index.GoFunctionIndex)1 GoIdFilter (com.goide.stubs.index.GoIdFilter)1 GoTypesIndex (com.goide.stubs.index.GoTypesIndex)1 Location (com.intellij.execution.Location)1 PsiLocation (com.intellij.execution.PsiLocation)1 SMTestLocator (com.intellij.execution.testframework.sm.runner.SMTestLocator)1 TestStateInfo (com.intellij.execution.testframework.sm.runner.states.TestStateInfo)1 Project (com.intellij.openapi.project.Project)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 ContainerUtil (com.intellij.util.containers.ContainerUtil)1 IdFilter (com.intellij.util.indexing.IdFilter)1 Collections (java.util.Collections)1