Search in sources :

Example 6 with PerlFileImpl

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

the class PerlElementFactory method createHereDocElements.

public static List<PsiElement> createHereDocElements(Project project, char quoteSymbol, String markerText, String contentText) {
    PerlFileImpl file = createFile(project, String.format("<<%c%s%c\n%s\n%s\n;", quoteSymbol, markerText, quoteSymbol, contentText, markerText));
    PsiElement heredocOpener = PsiTreeUtil.findChildOfType(file, PsiPerlHeredocOpener.class);
    @SuppressWarnings("ConstantConditions") PsiElement headingNewLine = heredocOpener.getNextSibling();
    PsiElement tailingNewLine = headingNewLine.getNextSibling().getNextSibling().getNextSibling();
    return new ArrayList<>(Arrays.asList(heredocOpener, headingNewLine, tailingNewLine));
}
Also used : PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement)

Example 7 with PerlFileImpl

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

the class CompoundToStatementIntention method replaceWithStatement.

/**
 * Generating new code, extracting declarations from control expression and replacing compound statement if possible.
 *
 * @param compound      compound statement
 * @param statementText statement text
 * @param controlExpr   control expression (condition or iterable)
 */
private static void replaceWithStatement(@NotNull PerlConvertableCompound compound, @NotNull String statementText, @NotNull PsiPerlExpr controlExpr) {
    List<PsiElement> declarations = new ArrayList<>();
    controlExpr.accept(new PerlRecursiveVisitor() {

        @Override
        public void visitPerlVariableDeclarationExpr(@NotNull PerlVariableDeclarationExpr o) {
            declarations.add(o);
            super.visitPerlVariableDeclarationExpr(o);
        }
    });
    String controlExprText = controlExpr.getText();
    StringBuilder sb = new StringBuilder();
    if (!declarations.isEmpty()) {
        // extracting declarations from control statement
        TextRange controlExprTextRange = controlExpr.getTextRange();
        for (int i = declarations.size() - 1; i >= 0; i--) {
            PsiElement declaration = declarations.get(i);
            sb.append(declaration.getText()).append(";\n");
            PsiElement keywordElement = declaration.getFirstChild();
            if (keywordElement instanceof LeafPsiElement) {
                // removing keyword from control expression my $var => $var, my ($var1, $var2) => ($var1, $var2)
                controlExprText = keywordElement.getTextRange().shiftRight(-controlExprTextRange.getStartOffset()).replace(controlExprText, "");
            }
        }
    }
    sb.append(statementText).append(" ").append(compound.getFirstChild().getText()).append(" ").append(controlExprText).append(";");
    PerlFileImpl perlFile = PerlElementFactory.createFile(compound.getProject(), sb.toString());
    PsiPerlStatementImpl[] statements = PsiTreeUtil.getChildrenOfType(perlFile, PsiPerlStatementImpl.class);
    if (statements == null || statements.length == 0) {
        return;
    }
    PsiElement container = compound.getParent();
    if (container == null) {
        return;
    }
    container.addRangeBefore(statements[0], statements[statements.length - 1], compound);
    compound.delete();
}
Also used : PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) ArrayList(java.util.ArrayList) PsiPerlStatementImpl(com.perl5.lang.perl.psi.impl.PsiPerlStatementImpl) TextRange(com.intellij.openapi.util.TextRange) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiElement(com.intellij.psi.PsiElement)

Example 8 with PerlFileImpl

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

the class PerlDebuggerEditorsProvider method createExpressionCodeFragment.

@Override
protected PsiFile createExpressionCodeFragment(@NotNull Project project, @NotNull String text, @Nullable PsiElement context, boolean isPhysical) {
    PsiFile fileFromText = PsiFileFactory.getInstance(project).createFileFromText("file.dummy", getFileType(), text, 0, isPhysical);
    ((PerlFileImpl) fileFromText).setFileContext(context);
    return fileFromText;
}
Also used : PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) PsiFile(com.intellij.psi.PsiFile)

Example 9 with PerlFileImpl

use of com.perl5.lang.perl.psi.impl.PerlFileImpl 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 10 with PerlFileImpl

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

the class PerlElementFactory method createUseStatement.

public static PerlUseStatement createUseStatement(Project project, String packageName) {
    assert packageName != null;
    PerlFileImpl file = createFile(project, String.format("use %s;", packageName));
    PerlUseStatement def = PsiTreeUtil.findChildOfType(file, PerlUseStatement.class);
    assert def != null;
    return def;
}
Also used : PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl)

Aggregations

PerlFileImpl (com.perl5.lang.perl.psi.impl.PerlFileImpl)21 PsiElement (com.intellij.psi.PsiElement)8 PsiFile (com.intellij.psi.PsiFile)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 ArrayList (java.util.ArrayList)3 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2 PerlNamespaceDefinitionElement (com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement)2 PerlVariableDeclarationElement (com.perl5.lang.perl.psi.PerlVariableDeclarationElement)2 PsiPerlStatement (com.perl5.lang.perl.psi.PsiPerlStatement)2 PsiPerlStatementImpl (com.perl5.lang.perl.psi.impl.PsiPerlStatementImpl)2 LocalQuickFixOnPsiElement (com.intellij.codeInspection.LocalQuickFixOnPsiElement)1 Document (com.intellij.openapi.editor.Document)1 PsiReference (com.intellij.psi.PsiReference)1 ResolveResult (com.intellij.psi.ResolveResult)1 ResolveState (com.intellij.psi.ResolveState)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 IElementType (com.intellij.psi.tree.IElementType)1 XSourcePosition (com.intellij.xdebugger.XSourcePosition)1 PerlCastExpression (com.perl5.lang.perl.psi.PerlCastExpression)1