Search in sources :

Example 1 with PerlVariable

use of com.perl5.lang.perl.psi.PerlVariable 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 PerlVariable

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

the class PerlVariableShadowingInspection method checkDeclaration.

@Override
public void checkDeclaration(ProblemsHolder holder, PerlVariableDeclarationElement variableDeclarationWrapper) {
    PerlVariable variable = variableDeclarationWrapper.getVariable();
    PsiElement declarationContainer = variableDeclarationWrapper.getParent();
    if (variable != null && !(declarationContainer instanceof PsiPerlVariableDeclarationLocal)) {
        PerlVariableDeclarationElement lexicalDeclaration = PerlResolveUtil.getLexicalDeclaration(variable);
        if (lexicalDeclaration instanceof PerlBuiltInVariable) {
            registerProblem(holder, variable, PerlBundle.message("perl.inspection.shadows.builtin", lexicalDeclaration.getVariable().getLineNumber()));
        } else if (lexicalDeclaration instanceof PerlImplicitVariableDeclaration) {
            registerProblem(holder, variable, PerlBundle.message("perl.inspection.shadows.implicit", lexicalDeclaration.getVariable().getLineNumber()));
        } else if (lexicalDeclaration != null) {
            registerProblem(holder, variable, PerlBundle.message("perl.inspection.shadows.other", lexicalDeclaration.getVariable().getLineNumber()));
        }
    }
}
Also used : PsiPerlVariableDeclarationLocal(com.perl5.lang.perl.psi.PsiPerlVariableDeclarationLocal) PerlBuiltInVariable(com.perl5.lang.perl.psi.impl.PerlBuiltInVariable) PerlVariableDeclarationElement(com.perl5.lang.perl.psi.PerlVariableDeclarationElement) PerlVariable(com.perl5.lang.perl.psi.PerlVariable) PerlImplicitVariableDeclaration(com.perl5.lang.perl.psi.impl.PerlImplicitVariableDeclaration) PsiElement(com.intellij.psi.PsiElement)

Example 3 with PerlVariable

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

the class PerlVariableNameCompletionProvider method fillWithFullQualifiedVariables.

private void fillWithFullQualifiedVariables(@NotNull PsiElement variableNameElement, @NotNull CompletionResultSet resultSet) {
    PsiElement perlVariable = variableNameElement.getParent();
    Project project = variableNameElement.getProject();
    GlobalSearchScope resolveScope = variableNameElement.getResolveScope();
    String variableName = variableNameElement.getText();
    boolean forceShortMain = StringUtil.startsWith(variableName, PerlPackageUtil.PACKAGE_SEPARATOR);
    final CompletionResultSet finalResultSet = resultSet;
    Processor<PerlVariableDeclarationElement> scalarDefaultProcessor = wrapper -> {
        String fullQualifiedName = wrapper.getFullQualifiedName();
        if (fullQualifiedName != null) {
            finalResultSet.addElement(PerlVariableCompletionUtil.getScalarLookupElement(adjustName(fullQualifiedName, forceShortMain)));
        }
        return true;
    };
    Processor<PerlVariableDeclarationElement> arrayDefaultProcessor = wrapper -> {
        String fullQualifiedName = wrapper.getFullQualifiedName();
        if (fullQualifiedName != null) {
            finalResultSet.addElement(PerlVariableCompletionUtil.getArrayLookupElement(adjustName(fullQualifiedName, forceShortMain)));
        }
        return true;
    };
    Processor<PerlVariableDeclarationElement> hashDefaultProcessor = wrapper -> {
        String fullQualifiedName = wrapper.getFullQualifiedName();
        if (fullQualifiedName != null) {
            finalResultSet.addElement(PerlVariableCompletionUtil.getHashLookupElement(adjustName(fullQualifiedName, forceShortMain)));
        }
        return true;
    };
    if (perlVariable instanceof PsiPerlScalarVariable) {
        PerlScalarUtil.processDefinedGlobalScalars(project, resolveScope, scalarDefaultProcessor);
        PerlArrayUtil.processDefinedGlobalArrays(project, resolveScope, wrapper -> {
            String fullQualifiedName = wrapper.getFullQualifiedName();
            if (fullQualifiedName != null) {
                finalResultSet.addElement(PerlVariableCompletionUtil.getArrayElementLookupElement(adjustName(fullQualifiedName, forceShortMain)));
            }
            return true;
        });
        PerlHashUtil.processDefinedGlobalHashes(project, resolveScope, wrapper -> {
            String fullQualifiedName = wrapper.getFullQualifiedName();
            if (fullQualifiedName != null) {
                finalResultSet.addElement(PerlVariableCompletionUtil.getHashElementLookupElement(adjustName(fullQualifiedName, forceShortMain)));
            }
            return true;
        });
    } else if (perlVariable instanceof PerlGlobVariable) {
        PerlScalarUtil.processDefinedGlobalScalars(project, resolveScope, scalarDefaultProcessor);
        PerlArrayUtil.processDefinedGlobalArrays(project, resolveScope, arrayDefaultProcessor);
        PerlHashUtil.processDefinedGlobalHashes(project, resolveScope, hashDefaultProcessor);
        // globs
        PerlGlobUtil.processDefinedGlobsNames(project, resolveScope, typeglob -> {
            String adjustedName = adjustName(typeglob.getCanonicalName(), forceShortMain);
            if (adjustedName != null) {
                finalResultSet.addElement(PerlVariableCompletionUtil.getGlobLookupElement(adjustedName));
            }
            return true;
        });
    } else if (perlVariable instanceof PsiPerlArrayVariable) {
        PerlArrayUtil.processDefinedGlobalArrays(project, resolveScope, arrayDefaultProcessor);
        PerlHashUtil.processDefinedGlobalHashes(project, resolveScope, wrapper -> {
            String fullQualifiedName = wrapper.getFullQualifiedName();
            if (fullQualifiedName != null) {
                finalResultSet.addElement(PerlVariableCompletionUtil.getHashSliceLookupElement(adjustName(fullQualifiedName, forceShortMain)));
            }
            return true;
        });
    } else if (perlVariable instanceof PsiPerlArrayIndexVariable) {
        // global arrays
        PerlArrayUtil.processDefinedGlobalArrays(project, resolveScope, arrayDefaultProcessor);
    } else if (perlVariable instanceof PsiPerlHashVariable) {
        // global hashes
        PerlHashUtil.processDefinedGlobalHashes(project, resolveScope, hashDefaultProcessor);
    }
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) PlainPrefixMatcher(com.intellij.codeInsight.completion.PlainPrefixMatcher) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) StringUtil(com.intellij.openapi.util.text.StringUtil) CompletionParameters(com.intellij.codeInsight.completion.CompletionParameters) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) CompletionResultSet(com.intellij.codeInsight.completion.CompletionResultSet) PerlExportDescriptor(com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor) CompletionProvider(com.intellij.codeInsight.completion.CompletionProvider) Processor(com.intellij.util.Processor) PsiElement(com.intellij.psi.PsiElement) PerlVariableCompletionUtil(com.perl5.lang.perl.idea.completion.util.PerlVariableCompletionUtil) Project(com.intellij.openapi.project.Project) com.perl5.lang.perl.psi(com.perl5.lang.perl.psi) PerlElementPatterns(com.perl5.lang.perl.idea.PerlElementPatterns) PerlInternalIndexKeysProcessor.adjustName(com.perl5.lang.perl.util.processors.PerlInternalIndexKeysProcessor.adjustName) NotNull(org.jetbrains.annotations.NotNull) PerlNamespaceEntityProcessor(com.perl5.lang.perl.util.processors.PerlNamespaceEntityProcessor) com.perl5.lang.perl.util(com.perl5.lang.perl.util) CompletionResultSet(com.intellij.codeInsight.completion.CompletionResultSet) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiElement(com.intellij.psi.PsiElement)

Example 4 with PerlVariable

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

the class PerlVariableCompletionUtil method fillWithUnresolvedVars.

public static void fillWithUnresolvedVars(@NotNull PerlVariableNameElement variableNameElement, @NotNull CompletionResultSet resultSet) {
    final PerlLexicalScope lexicalScope = PsiTreeUtil.getParentOfType(variableNameElement, PerlLexicalScope.class);
    PsiElement perlVariable = variableNameElement.getParent();
    final Set<String> collectedNames = new THashSet<>();
    if (lexicalScope != null && perlVariable instanceof PerlVariable) {
        final int minOffset = variableNameElement.getTextOffset();
        final PerlVariableType actualType = ((PerlVariable) perlVariable).getActualType();
        lexicalScope.accept(new PerlRecursiveVisitor() {

            @Override
            public void visitPerlVariable(@NotNull PerlVariable perlVariable) {
                if (perlVariable.isValid() && !(perlVariable.getParent() instanceof PerlVariableDeclarationElement) && perlVariable.getTextOffset() > minOffset && actualType == perlVariable.getActualType()) {
                    String variableName = perlVariable.getName();
                    if (StringUtil.isNotEmpty(variableName) && !collectedNames.contains(variableName) && perlVariable.getLexicalDeclaration() == null) {
                        collectedNames.add(variableName);
                        resultSet.addElement(LookupElementBuilder.create(variableName));
                    }
                }
                super.visitPerlVariable(perlVariable);
            }
        });
    }
}
Also used : PerlVariableType(com.perl5.lang.perl.psi.utils.PerlVariableType) PerlLexicalScope(com.perl5.lang.perl.psi.properties.PerlLexicalScope) PsiElement(com.intellij.psi.PsiElement) THashSet(gnu.trove.THashSet)

Example 5 with PerlVariable

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

the class PerlXNamedValue method computeMySourcePosition.

protected boolean computeMySourcePosition(@Nullable XNavigatable navigatable, @Nullable final XInlineDebuggerDataCallback callback) {
    String name = myPerlValueDescriptor.getName();
    if (StringUtil.isEmpty(name) || name.length() < 2) {
        return false;
    }
    final PerlVariableType variableType = PerlVariableType.bySigil(name.charAt(0));
    if (variableType == null || variableType == PerlVariableType.CODE) {
        return false;
    }
    final String variableName = name.substring(1);
    final XSourcePosition sourcePosition = myStackFrame.getSourcePosition();
    if (sourcePosition == null) {
        return false;
    }
    final Project project = myStackFrame.getPerlExecutionStack().getSuspendContext().getDebugSession().getProject();
    final VirtualFile virtualFile = sourcePosition.getFile();
    PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
    if (!(psiFile instanceof PerlFileImpl)) {
        return false;
    }
    PsiElement element = psiFile.findElementAt(sourcePosition.getOffset());
    if (element == null) {
        return false;
    }
    if (navigatable != null) {
        PerlVariableDeclarationSearcher variableProcessor = new PerlVariableDeclarationSearcher(variableName, variableType, element);
        PerlResolveUtil.treeWalkUp(element, variableProcessor);
        PerlVariableDeclarationElement result = variableProcessor.getResult();
        if (result == null) {
            return false;
        }
        navigatable.setSourcePosition(XSourcePositionImpl.createByElement(result));
    } else if (callback != null) {
        final Document document = psiFile.getViewProvider().getDocument();
        if (document == null) {
            return false;
        }
        final boolean[] found = new boolean[] { false };
        PerlVariableDeclarationSearcher variableProcessor = new PerlVariableDeclarationSearcher(variableName, variableType, element) {

            @Override
            public boolean execute(@NotNull PsiElement possibleElement, @NotNull ResolveState state) {
                boolean result = super.execute(possibleElement, state);
                if (!result) {
                    registerElement(getResult());
                } else if (possibleElement instanceof PerlVariable && ((PerlVariable) possibleElement).getActualType() == variableType && StringUtil.equals(variableName, ((PerlVariable) possibleElement).getName())) {
                    registerElement(possibleElement);
                }
                return result;
            }

            private void registerElement(@Nullable PsiElement targetElement) {
                if (targetElement == null) {
                    return;
                }
                found[0] = true;
                try {
                    if (mySourcePositionMethod != null) {
                        mySourcePositionMethod.invoke(callback, XSourcePositionImpl.createByElement(targetElement));
                    } else if (myLegacyMethod != null) {
                        myLegacyMethod.invoke(callback, virtualFile, document, document.getLineNumber(targetElement.getTextOffset()));
                    } else {
                        found[0] = false;
                    }
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        };
        PerlResolveUtil.treeWalkUp(element, variableProcessor);
        return found[0];
    }
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PerlVariableDeclarationElement(com.perl5.lang.perl.psi.PerlVariableDeclarationElement) PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) PerlVariableType(com.perl5.lang.perl.psi.utils.PerlVariableType) PerlVariableDeclarationSearcher(com.perl5.lang.perl.psi.references.scopes.PerlVariableDeclarationSearcher) Document(com.intellij.openapi.editor.Document) InvocationTargetException(java.lang.reflect.InvocationTargetException) ResolveState(com.intellij.psi.ResolveState) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) PerlVariable(com.perl5.lang.perl.psi.PerlVariable) XSourcePosition(com.intellij.xdebugger.XSourcePosition) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiElement (com.intellij.psi.PsiElement)13 PerlVariable (com.perl5.lang.perl.psi.PerlVariable)7 PerlVariableType (com.perl5.lang.perl.psi.utils.PerlVariableType)6 NotNull (org.jetbrains.annotations.NotNull)6 PerlVariableDeclarationElement (com.perl5.lang.perl.psi.PerlVariableDeclarationElement)5 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)4 Project (com.intellij.openapi.project.Project)4 CompletionResultSet (com.intellij.codeInsight.completion.CompletionResultSet)3 StringUtil (com.intellij.openapi.util.text.StringUtil)3 PsiReference (com.intellij.psi.PsiReference)3 PerlExportDescriptor (com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor)3 com.perl5.lang.perl.psi (com.perl5.lang.perl.psi)3 PerlLexicalScope (com.perl5.lang.perl.psi.properties.PerlLexicalScope)3 THashSet (gnu.trove.THashSet)3 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 PsiFile (com.intellij.psi.PsiFile)2 PsiScopeProcessor (com.intellij.psi.scope.PsiScopeProcessor)2 IElementType (com.intellij.psi.tree.IElementType)2 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)2 PerlIcons (com.perl5.PerlIcons)2