Search in sources :

Example 11 with PerlVariable

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

the class PerlAnnotator method annotate.

@Override
public void annotate(@NotNull final PsiElement element, @NotNull AnnotationHolder holder) {
    IElementType elementType = PsiUtilCore.getElementType(element);
    if (elementType == NYI_STATEMENT) {
        holder.createInfoAnnotation(element, "Unimplemented statement").setTextAttributes(CodeInsightColors.TODO_DEFAULT_ATTRIBUTES);
    } else if (element instanceof PerlGlobVariable && ((PerlGlobVariable) element).isBuiltIn()) {
        holder.createInfoAnnotation(element, null).setTextAttributes(PERL_GLOB_BUILTIN);
    } else if (element instanceof PerlVariable && ((PerlVariable) element).isBuiltIn()) {
        holder.createInfoAnnotation(element, null).setTextAttributes(VARIABLE_KEYS_MAP.get(element.getClass()));
    } else if (elementType == LABEL_DECLARATION || elementType == LABEL_EXPR) {
        holder.createInfoAnnotation(element.getFirstChild(), null).setTextAttributes(PerlSyntaxHighlighter.PERL_LABEL);
    } else if (elementType == PACKAGE) {
        assert element instanceof PerlNamespaceElement;
        PerlNamespaceElement namespaceElement = (PerlNamespaceElement) element;
        PsiElement parent = namespaceElement.getParent();
        if (parent instanceof PerlNamespaceDefinitionWithIdentifier) {
            decorateElement(namespaceElement, holder, PerlSyntaxHighlighter.PERL_PACKAGE_DEFINITION, false);
        } else {
            if (namespaceElement.isPragma()) {
                decorateElement(namespaceElement, holder, PerlSyntaxHighlighter.PERL_PACKAGE_PRAGMA, false);
            } else if (namespaceElement.isBuiltin()) {
                decorateElement(namespaceElement, holder, PerlSyntaxHighlighter.PERL_PACKAGE_CORE, false);
            }
        }
    } else if (element instanceof PerlPolyNamedElement) {
        TextAttributesKey subAttribute = PerlSyntaxHighlighter.PERL_SUB_DEFINITION;
        if (elementType == PerlConstantsWrapperElementType.CONSTANT_WRAPPER) {
            // fixme some interface?
            subAttribute = PerlSyntaxHighlighter.PERL_CONSTANT;
        }
        for (PerlDelegatingLightNamedElement lightNamedElement : ((PerlPolyNamedElement) element).getLightElements()) {
            TextAttributesKey currentKey = lightNamedElement instanceof PerlSubDefinition ? subAttribute : PerlSyntaxHighlighter.PERL_PACKAGE_DEFINITION;
            PsiElement navigationElement = lightNamedElement.getNavigationElement();
            holder.createInfoAnnotation(ElementManipulators.getValueTextRange(navigationElement).shiftRight(lightNamedElement.getTextOffset()), null).setEnforcedTextAttributes(adjustTextAttributes(currentScheme.getAttributes(currentKey), false));
        }
    } else if (// instanceof PerlSubNameElement
    elementType == SUB_NAME) {
        PsiElement parent = element.getParent();
        if (parent instanceof PsiPerlSubDeclaration) {
            holder.createInfoAnnotation(element, null).setTextAttributes(PerlSyntaxHighlighter.PERL_SUB_DECLARATION);
        } else if (parent instanceof PerlSubDefinitionElement) {
            if ("AUTOLOAD".equals(((PerlSubNameElement) element).getName())) {
                holder.createInfoAnnotation(element, null).setTextAttributes(PerlSyntaxHighlighter.PERL_AUTOLOAD);
            } else {
                holder.createInfoAnnotation(element, null).setTextAttributes(PerlSyntaxHighlighter.PERL_SUB_DEFINITION);
            }
        } else if (parent instanceof PerlMethod) {
            // fixme don't we need to take multiple references here?
            PsiElement grandParent = parent.getParent();
            PerlNamespaceElement methodNamespace = ((PerlMethod) parent).getNamespaceElement();
            if (// / not ...->method fixme shouldn't we use isObjectMethod here?
            !(grandParent instanceof PsiPerlNestedCall) && // no explicit NS or it's core
            (methodNamespace == null || methodNamespace.isCORE()) && ((PerlSubNameElement) element).isBuiltIn()) {
                decorateElement(element, holder, PerlSyntaxHighlighter.PERL_SUB_BUILTIN);
            } else {
                PsiReference reference = element.getReference();
                if (reference instanceof PerlSubReference) {
                    ((PerlSubReference) reference).multiResolve(false);
                    if (((PerlSubReference) reference).isConstant()) {
                        holder.createInfoAnnotation(element, "Constant").setTextAttributes(PerlSyntaxHighlighter.PERL_CONSTANT);
                    } else if (((PerlSubReference) reference).isAutoloaded()) {
                        holder.createInfoAnnotation(element, "Auto-loaded sub").setTextAttributes(PerlSyntaxHighlighter.PERL_AUTOLOAD);
                    } else if (((PerlSubReference) reference).isXSub()) {
                        holder.createInfoAnnotation(element, "XSub").setTextAttributes(PerlSyntaxHighlighter.PERL_XSUB);
                    }
                }
            }
        }
    }
}
Also used : PerlSubReference(com.perl5.lang.perl.psi.references.PerlSubReference) PsiReference(com.intellij.psi.PsiReference) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) IElementType(com.intellij.psi.tree.IElementType) PerlDelegatingLightNamedElement(com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement) PsiElement(com.intellij.psi.PsiElement)

Example 12 with PerlVariable

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

the class PerlVariableNameCompletionProvider method fillWithImportedVariables.

private void fillWithImportedVariables(@NotNull PsiElement variableNameElement, @NotNull CompletionResultSet resultSet) {
    PerlNamespaceDefinitionElement namespaceContainer = PerlPackageUtil.getNamespaceContainerForElement(variableNameElement);
    if (namespaceContainer == null) {
        return;
    }
    String packageName = namespaceContainer.getPackageName();
    if (// incomplete package definition
    StringUtil.isEmpty(packageName)) {
        return;
    }
    PerlNamespaceEntityProcessor<PerlExportDescriptor> processor = null;
    PsiElement perlVariable = variableNameElement.getParent();
    if (perlVariable instanceof PsiPerlScalarVariable) {
        processor = new PerlNamespaceEntityProcessor<PerlExportDescriptor>() {

            @Override
            public boolean process(String namespaceName, PerlExportDescriptor entity) {
                LookupElementBuilder lookupElement = null;
                if (entity.isScalar()) {
                    lookupElement = PerlVariableCompletionUtil.getScalarLookupElement(entity.getImportedName());
                } else if (entity.isArray()) {
                    lookupElement = PerlVariableCompletionUtil.getArrayElementLookupElement(entity.getImportedName());
                } else if (entity.isHash()) {
                    lookupElement = PerlVariableCompletionUtil.getHashElementLookupElement(entity.getImportedName());
                }
                if (lookupElement != null) {
                    resultSet.addElement(lookupElement.withTypeText(entity.getRealPackage(), true));
                }
                return true;
            }
        };
    } else if (perlVariable instanceof PsiPerlArrayVariable || perlVariable instanceof PsiPerlArrayIndexVariable) {
        processor = new PerlNamespaceEntityProcessor<PerlExportDescriptor>() {

            @Override
            public boolean process(String namespaceName, PerlExportDescriptor entity) {
                LookupElementBuilder lookupElement = null;
                if (entity.isArray()) {
                    lookupElement = PerlVariableCompletionUtil.getArrayLookupElement(entity.getImportedName());
                } else if (entity.isHash()) {
                    lookupElement = PerlVariableCompletionUtil.getHashSliceLookupElement(entity.getImportedName());
                }
                if (lookupElement != null) {
                    resultSet.addElement(lookupElement.withTypeText(entity.getRealPackage(), true));
                }
                return true;
            }
        };
    } else if (perlVariable instanceof PsiPerlHashVariable) {
        processor = new PerlNamespaceEntityProcessor<PerlExportDescriptor>() {

            @Override
            public boolean process(String namespaceName, PerlExportDescriptor entity) {
                LookupElementBuilder lookupElement = null;
                if (entity.isHash()) {
                    lookupElement = PerlVariableCompletionUtil.getHashLookupElement(entity.getImportedName());
                }
                if (lookupElement != null) {
                    resultSet.addElement(lookupElement.withTypeText(entity.getRealPackage(), true));
                }
                return true;
            }
        };
    }
    if (processor != null) {
        PerlUtil.processImportedEntities(namespaceContainer, processor);
    }
}
Also used : PerlExportDescriptor(com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) PerlNamespaceEntityProcessor(com.perl5.lang.perl.util.processors.PerlNamespaceEntityProcessor) PsiElement(com.intellij.psi.PsiElement)

Example 13 with PerlVariable

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

the class PerlVariableCompletionUtil method fillWithBuiltInVariables.

public static void fillWithBuiltInVariables(@NotNull PsiElement variableNameElement, @NotNull CompletionResultSet resultSet) {
    PsiElement perlVariable = variableNameElement.getParent();
    PerlBuiltInVariablesService perlBuiltInVariablesService = PerlBuiltInVariablesService.getInstance(variableNameElement.getProject());
    if (perlVariable instanceof PsiPerlScalarVariable) {
        perlBuiltInVariablesService.processScalars((element, state) -> {
            // noinspection ConstantConditions
            resultSet.addElement(getScalarLookupElement(((PerlVariableDeclarationElement) element).getName()).withBoldness(true));
            return true;
        });
        perlBuiltInVariablesService.processArrays((element, state) -> {
            // noinspection ConstantConditions
            resultSet.addElement(getArrayElementLookupElement(((PerlVariableDeclarationElement) element).getName()).withBoldness(true));
            return true;
        });
        perlBuiltInVariablesService.processHashes((element, state) -> {
            // noinspection ConstantConditions
            resultSet.addElement(getHashElementLookupElement(((PerlVariableDeclarationElement) element).getName()).withBoldness(true));
            return true;
        });
    } else if (perlVariable instanceof PsiPerlArrayVariable) {
        perlBuiltInVariablesService.processArrays((element, state) -> {
            // noinspection ConstantConditions
            resultSet.addElement(getArrayLookupElement(((PerlVariableDeclarationElement) element).getName()).withBoldness(true));
            // noinspection ConstantConditions
            resultSet.addElement(getArraySliceLookupElement(((PerlVariableDeclarationElement) element).getName()).withBoldness(true));
            return true;
        });
        perlBuiltInVariablesService.processHashes((element, state) -> {
            // noinspection ConstantConditions
            resultSet.addElement(getHashSliceLookupElement(((PerlVariableDeclarationElement) element).getName()).withBoldness(true));
            return true;
        });
    } else if (perlVariable instanceof PsiPerlArrayIndexVariable) {
        perlBuiltInVariablesService.processArrays((element, state) -> {
            // noinspection ConstantConditions
            resultSet.addElement(getArrayLookupElement(((PerlVariableDeclarationElement) element).getName()).withBoldness(true));
            return true;
        });
    } else if (perlVariable instanceof PsiPerlHashVariable) {
        perlBuiltInVariablesService.processHashes((element, state) -> {
            // noinspection ConstantConditions
            resultSet.addElement(getHashLookupElement(((PerlVariableDeclarationElement) element).getName()).withBoldness(true));
            return true;
        });
    } else if (perlVariable instanceof PsiPerlGlobVariable) {
        perlBuiltInVariablesService.processGlobs((element, state) -> {
            // noinspection ConstantConditions
            resultSet.addElement(getGlobLookupElement(((PerlVariableDeclarationElement) element).getName()).withBoldness(true));
            return true;
        });
    }
}
Also used : PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) PerlVariableType(com.perl5.lang.perl.psi.utils.PerlVariableType) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) StringUtil(com.intellij.openapi.util.text.StringUtil) Set(java.util.Set) THashSet(gnu.trove.THashSet) PerlInsertHandlers(com.perl5.lang.perl.idea.completion.PerlInsertHandlers) PerlLexicalScope(com.perl5.lang.perl.psi.properties.PerlLexicalScope) CompletionResultSet(com.intellij.codeInsight.completion.CompletionResultSet) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) PerlCompletionWeighter(com.perl5.lang.perl.idea.PerlCompletionWeighter) PerlIcons(com.perl5.PerlIcons) PsiElement(com.intellij.psi.PsiElement) PerlResolveUtil(com.perl5.lang.perl.psi.utils.PerlResolveUtil) com.perl5.lang.perl.psi(com.perl5.lang.perl.psi) PerlBuiltInVariablesService(com.perl5.lang.perl.psi.references.PerlBuiltInVariablesService) NotNull(org.jetbrains.annotations.NotNull) PerlBuiltInVariablesService(com.perl5.lang.perl.psi.references.PerlBuiltInVariablesService) PsiElement(com.intellij.psi.PsiElement)

Example 14 with PerlVariable

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

the class PerlVariableCompletionUtil method fillWithLExicalVariables.

public static void fillWithLExicalVariables(@NotNull PsiElement variableNameElement, @NotNull final CompletionResultSet resultSet) {
    final PsiElement perlVariable = variableNameElement.getParent();
    PsiScopeProcessor processor = (element, state) -> {
        if (element instanceof PerlVariableDeclarationElement) {
            PerlVariableDeclarationElement variable = (PerlVariableDeclarationElement) element;
            PsiElement declarationStatement = PsiTreeUtil.getParentOfType(variable, PerlStatement.class);
            if (PsiTreeUtil.isAncestor(declarationStatement, perlVariable, false)) {
                return true;
            }
            if (perlVariable instanceof PsiPerlScalarVariable) {
                String variableName = variable.getName();
                if (variableName != null) {
                    if (variable.getActualType() == PerlVariableType.SCALAR) {
                        resultSet.addElement(setLexical(getScalarLookupElement(variableName)));
                    } else if (variable.getActualType() == PerlVariableType.ARRAY) {
                        resultSet.addElement(setLexical(getArrayElementLookupElement(variableName)));
                    } else if (variable.getActualType() == PerlVariableType.HASH) {
                        resultSet.addElement(setLexical(getHashElementLookupElement(variableName)));
                    }
                }
            } else if (perlVariable instanceof PsiPerlArrayVariable) {
                String variableName = variable.getName();
                if (variableName != null) {
                    if (variable.getActualType() == PerlVariableType.ARRAY) {
                        resultSet.addElement(setLexical(getArrayLookupElement(variableName)));
                        resultSet.addElement(setLexical(getArraySliceLookupElement(variableName)));
                    } else if (variable.getActualType() == PerlVariableType.HASH) {
                        resultSet.addElement(setLexical(getHashSliceLookupElement(variableName)));
                    }
                }
            } else if (perlVariable instanceof PsiPerlArrayIndexVariable) {
                String variableName = variable.getName();
                if (variableName != null) {
                    if (variable.getActualType() == PerlVariableType.ARRAY) {
                        resultSet.addElement(setLexical(getArrayLookupElement(variableName)));
                    }
                }
            } else if (perlVariable instanceof PsiPerlHashVariable) {
                String variableName = variable.getName();
                if (variableName != null) {
                    if (variable.getActualType() == PerlVariableType.HASH) {
                        resultSet.addElement(setLexical(getHashLookupElement(variableName)));
                    }
                }
            }
        }
        return true;
    };
    PerlResolveUtil.treeWalkUp(variableNameElement, processor);
    PerlBuiltInVariablesService.getInstance(variableNameElement.getProject()).processVariables(processor);
}
Also used : PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) PerlVariableType(com.perl5.lang.perl.psi.utils.PerlVariableType) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) StringUtil(com.intellij.openapi.util.text.StringUtil) Set(java.util.Set) THashSet(gnu.trove.THashSet) PerlInsertHandlers(com.perl5.lang.perl.idea.completion.PerlInsertHandlers) PerlLexicalScope(com.perl5.lang.perl.psi.properties.PerlLexicalScope) CompletionResultSet(com.intellij.codeInsight.completion.CompletionResultSet) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) PerlCompletionWeighter(com.perl5.lang.perl.idea.PerlCompletionWeighter) PerlIcons(com.perl5.PerlIcons) PsiElement(com.intellij.psi.PsiElement) PerlResolveUtil(com.perl5.lang.perl.psi.utils.PerlResolveUtil) com.perl5.lang.perl.psi(com.perl5.lang.perl.psi) PerlBuiltInVariablesService(com.perl5.lang.perl.psi.references.PerlBuiltInVariablesService) NotNull(org.jetbrains.annotations.NotNull) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) PsiElement(com.intellij.psi.PsiElement)

Example 15 with PerlVariable

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

the class HTMLMasonArgsBlockImpl method getArgumentsListHeavy.

@NotNull
protected List<PerlSubArgument> getArgumentsListHeavy() {
    List<PerlSubArgument> result = new ArrayList<>();
    PsiElement run = getFirstChild();
    while (run != null) {
        if (run instanceof PerlVariableDeclarationElement) {
            PerlVariable variable = ((PerlVariableDeclarationElement) run).getVariable();
            if (variable != null) {
                PsiElement nextSibling = PerlPsiUtil.getNextSignificantSibling(run);
                result.add(PerlSubArgument.create(variable.getActualType(), variable.getName(), nextSibling != null && nextSibling.getNode().getElementType() == FAT_COMMA));
            }
        }
        run = run.getNextSibling();
    }
    return result;
}
Also used : PerlSubArgument(com.perl5.lang.perl.psi.utils.PerlSubArgument) PerlVariableDeclarationElement(com.perl5.lang.perl.psi.PerlVariableDeclarationElement) ArrayList(java.util.ArrayList) PerlVariable(com.perl5.lang.perl.psi.PerlVariable) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

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