Search in sources :

Example 1 with PerlSubElement

use of com.perl5.lang.perl.psi.PerlSubElement in project Perl5-IDEA by Camelcade.

the class PerlDocumentationProvider method getCustomDocumentationElement.

@Nullable
@Override
public PsiElement getCustomDocumentationElement(@NotNull Editor editor, @NotNull PsiFile file, @Nullable PsiElement contextElement) {
    if (contextElement == null || contextElement.getLanguage() != PerlLanguage.INSTANCE) {
        return null;
    }
    IElementType elementType = contextElement.getNode().getElementType();
    if (contextElement instanceof PerlVariable) {
        // fixme try to search doc in package or declaration
        return PerlDocUtil.getPerlVarDoc((PerlVariable) contextElement);
    } else if (elementType == REGEX_MODIFIER) {
        return PerlDocUtil.getRegexModifierDoc(contextElement);
    } else if (elementType == REGEX_TOKEN) {
        return PerlDocUtil.resolveDocLink("perlretut", contextElement);
    } else if (elementType == VERSION_ELEMENT) {
        return PerlDocUtil.resolveDocLink("perldata/\"Version Strings\"", contextElement);
    } else if (isFunc(contextElement)) {
        return PerlDocUtil.getPerlFuncDoc(contextElement);
    } else if (isOp(contextElement)) {
        return PerlDocUtil.getPerlOpDoc(contextElement);
    } else if (contextElement instanceof PerlSubNameElement) {
        String packageName = ((PerlSubNameElement) contextElement).getPackageName();
        String subName = ((PerlSubNameElement) contextElement).getName();
        if (StringUtil.isNotEmpty(subName)) {
            PsiElement result = null;
            // search by link
            if (StringUtil.isNotEmpty(packageName) && !StringUtil.equals(PerlPackageUtil.MAIN_PACKAGE, packageName)) {
                result = PerlDocUtil.resolveDocLink(packageName + "/" + ((PerlSubNameElement) contextElement).getName(), contextElement);
            }
            // not found or main::
            if (result == null) {
                PsiElement target = null;
                if (contextElement.getParent() instanceof PerlSubElement) {
                    target = contextElement.getParent();
                } else {
                    PsiReference reference = contextElement.getReference();
                    if (reference != null) {
                        target = reference.resolve();
                    }
                }
                if (target != null) {
                    PsiFile targetFile = target.getContainingFile();
                    if (targetFile != null) {
                        PsiFile podFile = targetFile.getViewProvider().getPsi(PodLanguage.INSTANCE);
                        if (podFile != null) {
                            result = PerlDocUtil.searchPodElement(targetFile, PodDocumentPattern.headingAndItemPattern(subName));
                        }
                    }
                }
            }
            if (result != null) {
                return result;
            }
        }
    } else if (contextElement instanceof PerlNamespaceElement) {
        String packageName = ((PerlNamespaceElement) contextElement).getCanonicalName();
        if (StringUtil.equals(PerlPackageUtil.SUPER_PACKAGE, packageName)) {
            return PerlDocUtil.resolveDocLink("perlobj/Inheritance", contextElement);
        } else if (StringUtil.isNotEmpty(packageName)) {
            return PerlDocUtil.resolveDocLink(packageName, contextElement);
        }
    }
    return super.getCustomDocumentationElement(editor, file, contextElement);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PerlSubNameElement(com.perl5.lang.perl.psi.PerlSubNameElement) PerlSubElement(com.perl5.lang.perl.psi.PerlSubElement) PsiReference(com.intellij.psi.PsiReference) PerlNamespaceElement(com.perl5.lang.perl.psi.PerlNamespaceElement) PsiFile(com.intellij.psi.PsiFile) PerlVariable(com.perl5.lang.perl.psi.PerlVariable) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PerlSubElement

use of com.perl5.lang.perl.psi.PerlSubElement in project Perl5-IDEA by Camelcade.

the class PerlPackageUtil method processParentClassesSubs.

public static void processParentClassesSubs(PerlNamespaceDefinitionElement childClass, Set<String> processedSubsNames, Set<PerlNamespaceDefinitionElement> recursionMap, Processor<PerlSubElement> processor) {
    if (childClass == null || recursionMap.contains(childClass)) {
        return;
    }
    recursionMap.add(childClass);
    for (PerlNamespaceDefinitionElement parentNamespace : childClass.getParentNamespaceDefinitions()) {
        for (PsiElement subDefinitionBase : collectNamespaceSubs(parentNamespace)) {
            ProgressManager.checkCanceled();
            String subName = ((PerlSubElement) subDefinitionBase).getSubName();
            if (subDefinitionBase.isValid() && ((PerlSubElement) subDefinitionBase).isMethod() && !processedSubsNames.contains(subName)) {
                processedSubsNames.add(subName);
                processor.process(((PerlSubElement) subDefinitionBase));
            }
        }
        processParentClassesSubs(parentNamespace, processedSubsNames, recursionMap, processor);
    }
}
Also used : PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement) PerlSubElement(com.perl5.lang.perl.psi.PerlSubElement) PsiElement(com.intellij.psi.PsiElement)

Example 3 with PerlSubElement

use of com.perl5.lang.perl.psi.PerlSubElement in project Perl5-IDEA by Camelcade.

the class PerlRenamePolyReferencedElementProcessor method prepareRenaming.

@Override
public void prepareRenaming(PsiElement element, String newName, Map<PsiElement, String> allRenames, SearchScope scope) {
    final String currentBaseName = ((PsiNameIdentifierOwner) element).getName();
    if (currentBaseName != null && StringUtil.isNotEmpty(newName)) {
        boolean globScanned = element instanceof PerlGlobVariable;
        for (PsiReference reference : ReferencesSearch.search(element, element.getUseScope()).findAll()) {
            if (reference instanceof PsiPolyVariantReference) {
                for (ResolveResult resolveResult : ((PsiPolyVariantReference) reference).multiResolve(false)) {
                    PsiElement resolveResultElement = resolveResult.getElement();
                    if (!allRenames.containsKey(resolveResultElement)) {
                        allRenames.put(resolveResultElement, newName);
                        if (!globScanned && resolveResultElement instanceof PerlGlobVariable) {
                            globScanned = true;
                            prepareRenaming(resolveResultElement, newName, allRenames, scope);
                        }
                    }
                }
            }
            processDocReference(currentBaseName, newName, reference, allRenames);
        }
        if (element instanceof PerlSubElement && ((PerlSubElement) element).isMethod()) {
            for (PerlSubElement overridingSub : PerlSubUtil.collectOverridingSubs((PerlSubElement) element)) {
                allRenames.put(overridingSub, newName);
            }
        }
        // following is the hack until #1730 is fixed
        Set<PsiElement> allElements = new THashSet<>(allRenames.keySet());
        allElements.stream().filter(e -> e instanceof PerlClassAccessorMethod).forEach(e -> {
            PerlClassAccessorMethod pairedMethod = ((PerlClassAccessorMethod) e).getPairedMethod();
            if (pairedMethod != null && allRenames.containsKey(e)) {
                allRenames.remove(pairedMethod);
            }
        });
    }
}
Also used : ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) PerlGlobVariable(com.perl5.lang.perl.psi.PerlGlobVariable) PerlSubUtil(com.perl5.lang.perl.util.PerlSubUtil) RenamePsiElementProcessor(com.intellij.refactoring.rename.RenamePsiElementProcessor) StringUtil(com.intellij.openapi.util.text.StringUtil) Set(java.util.Set) THashSet(gnu.trove.THashSet) SearchScope(com.intellij.psi.search.SearchScope) PerlClassAccessorMethod(com.perl5.lang.perl.parser.Class.Accessor.psi.impl.PerlClassAccessorMethod) Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) PsiElementRenameHandler(com.intellij.refactoring.rename.PsiElementRenameHandler) PerlIcons(com.perl5.PerlIcons) Map(java.util.Map) Messages(com.intellij.openapi.ui.Messages) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) PerlSubElement(com.perl5.lang.perl.psi.PerlSubElement) PodLanguage(com.perl5.lang.pod.PodLanguage) PerlClassAccessorMethod(com.perl5.lang.perl.parser.Class.Accessor.psi.impl.PerlClassAccessorMethod) THashSet(gnu.trove.THashSet) PerlSubElement(com.perl5.lang.perl.psi.PerlSubElement) PerlGlobVariable(com.perl5.lang.perl.psi.PerlGlobVariable)

Example 4 with PerlSubElement

use of com.perl5.lang.perl.psi.PerlSubElement in project Perl5-IDEA by Camelcade.

the class PerlCodeGeneratorImpl method getOverrideCodeText.

@Nullable
@Override
public String getOverrideCodeText(PsiElement subBase) {
    if (subBase instanceof PerlSubElement) {
        PerlSubElement perlSubBase = (PerlSubElement) subBase;
        StringBuilder code = new StringBuilder();
        code.append("#@override\n");
        PerlSubAnnotations annotations = perlSubBase.getAnnotations();
        if (annotations != null) {
            if (annotations.isDeprecated()) {
                code.append("#@deprecated\n");
            }
            if (annotations.isAbstract()) {
                code.append("#@abstract\n");
            }
            if (annotations.isMethod() || subBase instanceof PerlMethodDefinition) {
                code.append("#@method\n");
            }
            if (StringUtil.isNotEmpty(annotations.getReturns())) {
                code.append("#@returns ");
                code.append(annotations.getReturns());
                code.append("\n");
            }
        }
        code.append("sub ");
        code.append(perlSubBase.getSubName());
        code.append("{\n");
        List<String> superArgs = new ArrayList<>();
        List<PerlSubArgument> arguments = Collections.emptyList();
        if (perlSubBase instanceof PerlSubDefinitionElement) {
            // noinspection unchecked
            arguments = ((PerlSubDefinitionElement) perlSubBase).getSubArgumentsList();
            if (!arguments.isEmpty()) {
                boolean useShift = false;
                for (PerlSubArgument argument : arguments) {
                    if (StringUtil.isNotEmpty(argument.getVariableClass())) {
                        useShift = true;
                        break;
                    }
                }
                if (useShift) {
                    for (PerlSubArgument argument : arguments) {
                        if (!argument.isEmpty()) {
                            code.append("my ");
                            code.append(argument.getVariableClass());
                            code.append(" ");
                            String superArg = argument.toStringShort();
                            superArgs.add(superArg);
                            code.append(superArg);
                            code.append(" = ");
                        }
                        code.append("shift;\n");
                    }
                } else {
                    code.append("my ");
                    code.append('(');
                    boolean insertComma = false;
                    for (PerlSubArgument argument : arguments) {
                        if (insertComma) {
                            code.append(", ");
                        } else {
                            insertComma = true;
                        }
                        String superArg = argument.toStringShort();
                        superArgs.add(superArg);
                        code.append(superArg);
                    }
                    code.append(") = @_;\n");
                }
            } else {
                code.append("my ($self) = @_;\n");
            }
        }
        if (!superArgs.isEmpty()) {
            superArgs.remove(0);
        }
        if (!arguments.isEmpty() && !arguments.get(0).isEmpty()) {
            // noinspection StringConcatenationInsideStringBufferAppend
            code.append(arguments.get(0).toStringShort() + "->SUPER::" + perlSubBase.getSubName() + "(" + StringUtil.join(superArgs, ", ") + ");\n");
        }
        code.append("}");
        return code.toString();
    }
    return null;
}
Also used : PerlSubAnnotations(com.perl5.lang.perl.psi.utils.PerlSubAnnotations) PerlSubArgument(com.perl5.lang.perl.psi.utils.PerlSubArgument) PerlSubElement(com.perl5.lang.perl.psi.PerlSubElement) ArrayList(java.util.ArrayList) PerlMethodDefinition(com.perl5.lang.perl.psi.PerlMethodDefinition) PerlSubDefinitionElement(com.perl5.lang.perl.psi.PerlSubDefinitionElement) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with PerlSubElement

use of com.perl5.lang.perl.psi.PerlSubElement in project Perl5-IDEA by Camelcade.

the class PerlSubReferenceSimple method getResolveResults.

@NotNull
public List<ResolveResult> getResolveResults(List<PsiElement> relatedItems) {
    List<ResolveResult> result = new ArrayList<>();
    resetFlags();
    for (PsiElement element : relatedItems) {
        if (!isAutoloaded() && element instanceof PerlIdentifierOwner && PerlSubUtil.SUB_AUTOLOAD.equals(((PerlIdentifierOwner) element).getName())) {
            setAutoloaded();
        }
        if (!isConstant() && element instanceof PerlLightConstantDefinitionElement) {
            setConstant();
        }
        if (!isDeclared() && element instanceof PerlSubDeclarationElement) {
            setDeclared();
        }
        if (!isDefined() && element instanceof PerlSubDefinitionElement) {
            setDefined();
        }
        if (!isXSub() && element instanceof PerlSubElement && ((PerlSubElement) element).isXSub()) {
            setXSub();
        }
        if (!isAliased() && element instanceof PerlGlobVariable) {
            setAliased();
        }
        result.add(new PsiElementResolveResult(element));
    }
    return result;
}
Also used : PerlLightConstantDefinitionElement(com.perl5.lang.perl.parser.constant.psi.light.PerlLightConstantDefinitionElement) PerlSubElement(com.perl5.lang.perl.psi.PerlSubElement) ArrayList(java.util.ArrayList) PerlGlobVariable(com.perl5.lang.perl.psi.PerlGlobVariable) PerlSubDeclarationElement(com.perl5.lang.perl.psi.PerlSubDeclarationElement) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) ResolveResult(com.intellij.psi.ResolveResult) PerlSubDefinitionElement(com.perl5.lang.perl.psi.PerlSubDefinitionElement) PsiElement(com.intellij.psi.PsiElement) PerlIdentifierOwner(com.perl5.lang.perl.psi.properties.PerlIdentifierOwner) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PerlSubElement (com.perl5.lang.perl.psi.PerlSubElement)9 PsiElement (com.intellij.psi.PsiElement)6 PsiFile (com.intellij.psi.PsiFile)4 PerlSubDefinitionElement (com.perl5.lang.perl.psi.PerlSubDefinitionElement)4 NotNull (org.jetbrains.annotations.NotNull)4 ResolveResult (com.intellij.psi.ResolveResult)3 ArrayList (java.util.ArrayList)3 Nullable (org.jetbrains.annotations.Nullable)3 PsiElementResolveResult (com.intellij.psi.PsiElementResolveResult)2 PsiReference (com.intellij.psi.PsiReference)2 PerlGlobVariable (com.perl5.lang.perl.psi.PerlGlobVariable)2 PerlSubDeclarationElement (com.perl5.lang.perl.psi.PerlSubDeclarationElement)2 THashSet (gnu.trove.THashSet)2 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 Messages (com.intellij.openapi.ui.Messages)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 com.intellij.psi (com.intellij.psi)1 SearchScope (com.intellij.psi.search.SearchScope)1 ReferencesSearch (com.intellij.psi.search.searches.ReferencesSearch)1