Search in sources :

Example 1 with GoFunctionOrMethodDeclaration

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

the class GoRecursiveCallMarkerProvider method collectSlowLineMarkers.

@Override
public void collectSlowLineMarkers(@NotNull List<PsiElement> elements, @NotNull Collection<LineMarkerInfo> result) {
    Set<Integer> lines = ContainerUtil.newHashSet();
    for (PsiElement element : elements) {
        if (element instanceof GoCallExpr) {
            PsiElement resolve = GoPsiImplUtil.resolveCall((GoCallExpr) element);
            if (resolve instanceof GoFunctionOrMethodDeclaration) {
                if (isRecursiveCall(element, (GoFunctionOrMethodDeclaration) resolve)) {
                    PsiDocumentManager instance = PsiDocumentManager.getInstance(element.getProject());
                    Document document = instance.getDocument(element.getContainingFile());
                    int textOffset = element.getTextOffset();
                    if (document == null)
                        continue;
                    int lineNumber = document.getLineNumber(textOffset);
                    if (!lines.contains(lineNumber)) {
                        result.add(new RecursiveMethodCallMarkerInfo(element));
                    }
                    lines.add(lineNumber);
                }
            }
        }
    }
}
Also used : GoFunctionOrMethodDeclaration(com.goide.psi.GoFunctionOrMethodDeclaration) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) GoCallExpr(com.goide.psi.GoCallExpr) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 2 with GoFunctionOrMethodDeclaration

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

the class GoAddFunctionBlockIntention method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
    PsiElement parent = element.getParent();
    if (parent instanceof GoFunctionOrMethodDeclaration) {
        GoBlock block = ((GoFunctionOrMethodDeclaration) parent).getBlock();
        if (block == null) {
            GoBlock newBlock = ObjectUtils.tryCast(parent.add(GoElementFactory.createBlock(project)), GoBlock.class);
            if (newBlock != null) {
                PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
                new GoSmartEnterProcessor.PlainEnterProcessor().doEnter(newBlock, newBlock.getContainingFile(), editor, false);
            }
        }
    }
}
Also used : GoFunctionOrMethodDeclaration(com.goide.psi.GoFunctionOrMethodDeclaration) GoBlock(com.goide.psi.GoBlock) PsiElement(com.intellij.psi.PsiElement) GoSmartEnterProcessor(com.goide.editor.smart.GoSmartEnterProcessor)

Example 3 with GoFunctionOrMethodDeclaration

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

the class GoTestRunConfigurationProducerBase method setupConfigurationFromContext.

@Override
protected boolean setupConfigurationFromContext(@NotNull GoTestRunConfiguration configuration, ConfigurationContext context, Ref sourceElement) {
    PsiElement contextElement = GoRunUtil.getContextElement(context);
    if (contextElement == null) {
        return false;
    }
    Module module = ModuleUtilCore.findModuleForPsiElement(contextElement);
    Project project = contextElement.getProject();
    if (module == null || !GoSdkService.getInstance(project).isGoModule(module))
        return false;
    if (!myFramework.isAvailable(module))
        return false;
    configuration.setModule(module);
    configuration.setTestFramework(myFramework);
    if (contextElement instanceof PsiDirectory) {
        configuration.setName(getPackageConfigurationName(((PsiDirectory) contextElement).getName()));
        configuration.setKind(GoTestRunConfiguration.Kind.DIRECTORY);
        String directoryPath = ((PsiDirectory) contextElement).getVirtualFile().getPath();
        configuration.setDirectoryPath(directoryPath);
        configuration.setWorkingDirectory(directoryPath);
        return true;
    }
    PsiFile file = contextElement.getContainingFile();
    if (myFramework.isAvailableOnFile(file)) {
        String importPath = ((GoFile) file).getImportPath(false);
        if (GoRunUtil.isPackageContext(contextElement) && StringUtil.isNotEmpty(importPath)) {
            configuration.setKind(GoTestRunConfiguration.Kind.PACKAGE);
            configuration.setPackage(importPath);
            configuration.setName(getPackageConfigurationName(importPath));
            return true;
        } else {
            GoFunctionOrMethodDeclaration function = findTestFunctionInContext(contextElement);
            if (function != null) {
                if (myFramework.isAvailableOnFunction(function)) {
                    configuration.setName(getFunctionConfigurationName(function, file.getName()));
                    configuration.setPattern("^" + function.getName() + "$");
                    configuration.setKind(GoTestRunConfiguration.Kind.PACKAGE);
                    configuration.setPackage(StringUtil.notNullize(((GoFile) file).getImportPath(false)));
                    return true;
                }
            } else if (hasSupportedFunctions((GoFile) file)) {
                configuration.setName(getFileConfigurationName(file.getName()));
                configuration.setKind(GoTestRunConfiguration.Kind.FILE);
                configuration.setFilePath(file.getVirtualFile().getPath());
                return true;
            }
        }
    }
    return false;
}
Also used : Project(com.intellij.openapi.project.Project) GoFile(com.goide.psi.GoFile) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) GoFunctionOrMethodDeclaration(com.goide.psi.GoFunctionOrMethodDeclaration) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement)

Example 4 with GoFunctionOrMethodDeclaration

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

the class GoTestRunConfigurationProducerBase method isConfigurationFromContext.

@Override
public boolean isConfigurationFromContext(@NotNull GoTestRunConfiguration configuration, ConfigurationContext context) {
    PsiElement contextElement = GoRunUtil.getContextElement(context);
    if (contextElement == null)
        return false;
    Module module = ModuleUtilCore.findModuleForPsiElement(contextElement);
    if (!Comparing.equal(module, configuration.getConfigurationModule().getModule()))
        return false;
    if (!Comparing.equal(myFramework, configuration.getTestFramework()))
        return false;
    PsiFile file = contextElement.getContainingFile();
    switch(configuration.getKind()) {
        case DIRECTORY:
            if (contextElement instanceof PsiDirectory) {
                String directoryPath = ((PsiDirectory) contextElement).getVirtualFile().getPath();
                return FileUtil.pathsEqual(configuration.getDirectoryPath(), directoryPath) && FileUtil.pathsEqual(configuration.getWorkingDirectory(), directoryPath);
            }
        case PACKAGE:
            if (!GoTestFinder.isTestFile(file))
                return false;
            if (!Comparing.equal(((GoFile) file).getImportPath(false), configuration.getPackage()))
                return false;
            if (GoRunUtil.isPackageContext(contextElement) && configuration.getPattern().isEmpty())
                return true;
            GoFunctionOrMethodDeclaration contextFunction = findTestFunctionInContext(contextElement);
            return contextFunction != null && myFramework.isAvailableOnFunction(contextFunction) ? configuration.getPattern().equals("^" + contextFunction.getName() + "$") : configuration.getPattern().isEmpty();
        case FILE:
            GoFunctionOrMethodDeclaration contextTestFunction = findTestFunctionInContext(contextElement);
            return contextTestFunction == null && GoTestFinder.isTestFile(file) && FileUtil.pathsEqual(configuration.getFilePath(), file.getVirtualFile().getPath());
    }
    return false;
}
Also used : GoFile(com.goide.psi.GoFile) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) GoFunctionOrMethodDeclaration(com.goide.psi.GoFunctionOrMethodDeclaration) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement)

Example 5 with GoFunctionOrMethodDeclaration

use of com.goide.psi.GoFunctionOrMethodDeclaration 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

GoFunctionOrMethodDeclaration (com.goide.psi.GoFunctionOrMethodDeclaration)5 PsiElement (com.intellij.psi.PsiElement)5 PsiFile (com.intellij.psi.PsiFile)3 GoFile (com.goide.psi.GoFile)2 Module (com.intellij.openapi.module.Module)2 PsiDirectory (com.intellij.psi.PsiDirectory)2 GoSmartEnterProcessor (com.goide.editor.smart.GoSmartEnterProcessor)1 GoBlock (com.goide.psi.GoBlock)1 GoCallExpr (com.goide.psi.GoCallExpr)1 GoFunctionDeclaration (com.goide.psi.GoFunctionDeclaration)1 GoMethodDeclaration (com.goide.psi.GoMethodDeclaration)1 GoReceiver (com.goide.psi.GoReceiver)1 TestStateInfo (com.intellij.execution.testframework.sm.runner.states.TestStateInfo)1 Document (com.intellij.openapi.editor.Document)1 Project (com.intellij.openapi.project.Project)1 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)1 Nullable (org.jetbrains.annotations.Nullable)1