Search in sources :

Example 1 with PerlVariableType

use of com.perl5.lang.perl.psi.utils.PerlVariableType 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 2 with PerlVariableType

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

Example 3 with PerlVariableType

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

the class PerlVariableMixin method getGlobalDeclarations.

// fixme this need to be moved to PerlResolveUtil or Resolver
@Override
public List<PerlVariableDeclarationElement> getGlobalDeclarations() {
    List<PerlVariableDeclarationElement> result = new ArrayList<>();
    PerlVariableType myType = getActualType();
    // wrapper if any
    PsiElement parent = getParent();
    if (myType == PerlVariableType.SCALAR) {
        for (PerlVariableDeclarationElement variable : PerlScalarUtil.getGlobalScalarDefinitions(getProject(), getCanonicalName())) {
            if (!variable.equals(parent)) {
                result.add(variable);
            }
        }
    } else if (myType == PerlVariableType.ARRAY) {
        for (PerlVariableDeclarationElement variable : PerlArrayUtil.getGlobalArrayDefinitions(getProject(), getCanonicalName())) {
            if (!variable.equals(parent)) {
                result.add(variable);
            }
        }
    } else if (myType == PerlVariableType.HASH) {
        for (PerlVariableDeclarationElement variable : PerlHashUtil.getGlobalHashDefinitions(getProject(), getCanonicalName())) {
            if (!variable.equals(parent)) {
                result.add(variable);
            }
        }
    }
    return result;
}
Also used : PerlVariableType(com.perl5.lang.perl.psi.utils.PerlVariableType) ArrayList(java.util.ArrayList) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 4 with PerlVariableType

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

the class PerlBuiltInSubsService method readArgument.

@Nullable
private PerlSubArgument readArgument(@NotNull Element element, boolean isOptional, String subName) {
    String variableName = element.getAttribute("name").getValue();
    if (StringUtil.isEmpty(variableName)) {
        LOG.warn("Missing argument name for " + subName);
        return null;
    }
    PerlVariableType variableType;
    String type = element.getAttribute("type").getValue();
    if (type == null || type.length() != 1 || (variableType = PerlVariableType.bySigil(type.charAt(0))) == null) {
        LOG.warn("Unknown type modifier for argument: " + variableName + " in " + subName);
        return null;
    }
    return PerlSubArgument.create(variableType, variableName, isOptional);
}
Also used : PerlVariableType(com.perl5.lang.perl.psi.utils.PerlVariableType) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with PerlVariableType

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

the class PerlDocUtil method getPerlVarDoc.

@Nullable
public static PsiElement getPerlVarDoc(PerlVariable variable) {
    final Project project = variable.getProject();
    PerlVariableType actualType = variable.getActualType();
    String variableName = variable.getName();
    if (actualType != null && StringUtil.isNotEmpty(variableName)) {
        String text = actualType.getSigil() + variableName;
        if (myVariablesRedirections.containsKey(text)) {
            return resolveDocLink(myVariablesRedirections.get(text), variable);
        }
        if (variable.isBuiltIn()) {
            PodDocumentPattern pattern = PodDocumentPattern.itemPattern(text);
            if (text.matches("\\$[123456789]")) {
                pattern.setItemPattern("$<digits>");
            }
            return searchPodElementInFile(project, PodSearchHelper.PERL_VAR_FILE_NAME, pattern);
        }
    }
    return null;
}
Also used : Project(com.intellij.openapi.project.Project) PerlVariableType(com.perl5.lang.perl.psi.utils.PerlVariableType) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PerlVariableType (com.perl5.lang.perl.psi.utils.PerlVariableType)6 Project (com.intellij.openapi.project.Project)3 PsiElement (com.intellij.psi.PsiElement)3 PerlVariable (com.perl5.lang.perl.psi.PerlVariable)2 PerlVariableDeclarationElement (com.perl5.lang.perl.psi.PerlVariableDeclarationElement)2 ArrayList (java.util.ArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 Document (com.intellij.openapi.editor.Document)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElementResolveResult (com.intellij.psi.PsiElementResolveResult)1 PsiFile (com.intellij.psi.PsiFile)1 ResolveResult (com.intellij.psi.ResolveResult)1 ResolveState (com.intellij.psi.ResolveState)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 XSourcePosition (com.intellij.xdebugger.XSourcePosition)1 PerlExportDescriptor (com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor)1 PerlGlobVariable (com.perl5.lang.perl.psi.PerlGlobVariable)1 PerlNamespaceDefinitionElement (com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement)1 PerlFileImpl (com.perl5.lang.perl.psi.impl.PerlFileImpl)1 PerlImplicitVariableDeclaration (com.perl5.lang.perl.psi.impl.PerlImplicitVariableDeclaration)1