Search in sources :

Example 1 with PerlVariableDeclarationSearcher

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

Document (com.intellij.openapi.editor.Document)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 ResolveState (com.intellij.psi.ResolveState)1 XSourcePosition (com.intellij.xdebugger.XSourcePosition)1 PerlVariable (com.perl5.lang.perl.psi.PerlVariable)1 PerlVariableDeclarationElement (com.perl5.lang.perl.psi.PerlVariableDeclarationElement)1 PerlFileImpl (com.perl5.lang.perl.psi.impl.PerlFileImpl)1 PerlVariableDeclarationSearcher (com.perl5.lang.perl.psi.references.scopes.PerlVariableDeclarationSearcher)1 PerlVariableType (com.perl5.lang.perl.psi.utils.PerlVariableType)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1