Search in sources :

Example 16 with PerlFileImpl

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

the class StatementToCompoundIntention method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
    PsiPerlStatementImpl statement = getStatement(element);
    if (statement == null) {
        return;
    }
    PsiPerlStatementModifier modifier = statement.getModifier();
    if (modifier == null) {
        return;
    }
    PsiElement keyword = modifier.getFirstChild();
    if (keyword == null) {
        return;
    }
    PsiPerlExpr modifierExpression = PsiTreeUtil.getChildOfType(modifier, PsiPerlExpr.class);
    PsiPerlExpr valueExpression = modifierExpression instanceof PsiPerlParenthesisedExpr ? ((PsiPerlParenthesisedExpr) modifierExpression).getExpr() : modifierExpression;
    String conditionText = valueExpression == null ? "" : valueExpression.getText();
    TextRange modifierRangeInStatement = TextRange.from(modifier.getStartOffsetInParent(), modifier.getTextLength());
    String statementText = modifierRangeInStatement.replace(statement.getText(), "");
    StringBuilder newCode = new StringBuilder();
    newCode.append(keyword.getText()).append("(").append(conditionText).append("){\n").append(statementText).append("\n}");
    PerlFileImpl fakeFile = PerlElementFactory.createFile(project, newCode.toString());
    PsiElement[] children = fakeFile.getChildren();
    assert children.length == 1;
    statement.replace(children[0]);
}
Also used : PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) PsiPerlStatementImpl(com.perl5.lang.perl.psi.impl.PsiPerlStatementImpl) PsiPerlStatementModifier(com.perl5.lang.perl.psi.PsiPerlStatementModifier) PsiPerlParenthesisedExpr(com.perl5.lang.perl.psi.PsiPerlParenthesisedExpr) TextRange(com.intellij.openapi.util.TextRange) PsiPerlExpr(com.perl5.lang.perl.psi.PsiPerlExpr) PsiElement(com.intellij.psi.PsiElement)

Example 17 with PerlFileImpl

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

the class PerlUseVarsQuickFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    Collection<PerlVariableDeclarationElement> declarations = myVariablesProvider.getValue();
    if (declarations.isEmpty()) {
        startElement.delete();
        return;
    }
    StringBuilder newCode = new StringBuilder("our");
    if (declarations.size() > 1) {
        newCode.append("(");
    }
    newCode.append(StringUtil.join(ContainerUtil.map(declarations, PsiElement::getText), ","));
    if (declarations.size() > 1) {
        newCode.append(")");
    }
    newCode.append(";");
    PerlFileImpl fakeFile = PerlElementFactory.createFile(myStartElement.getProject(), newCode.toString());
    PsiPerlStatement newElement = PsiTreeUtil.findChildOfType(fakeFile, PsiPerlStatement.class);
    if (newElement != null) {
        startElement.replace(newElement);
    }
}
Also used : PerlVariableDeclarationElement(com.perl5.lang.perl.psi.PerlVariableDeclarationElement) PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) PsiPerlStatement(com.perl5.lang.perl.psi.PsiPerlStatement) LocalQuickFixOnPsiElement(com.intellij.codeInspection.LocalQuickFixOnPsiElement) PsiElement(com.intellij.psi.PsiElement)

Example 18 with PerlFileImpl

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

the class PerlRenameFileProcessor method getPostRenameCallback.

@Nullable
@Override
public Runnable getPostRenameCallback(final PsiElement element, String newName, RefactoringElementListener elementListener) {
    if (newName.endsWith(".pm")) {
        final Project project = element.getProject();
        final String currentPackageName = ((PerlFileImpl) element).getFilePackageName();
        if (currentPackageName != null) {
            String[] nameChunks = currentPackageName.split(PerlPackageUtil.PACKAGE_SEPARATOR);
            nameChunks[nameChunks.length - 1] = newName.replaceFirst("\\.pm$", "");
            final String newPackageName = StringUtils.join(nameChunks, PerlPackageUtil.PACKAGE_SEPARATOR);
            final String newFileName = ((PerlFileImpl) element).getVirtualFile().getParent().getPath() + '/' + newName;
            return () -> {
                VirtualFile newFile = LocalFileSystem.getInstance().findFileByPath(newFileName);
                if (newFile != null) {
                    PsiFile psiFile = PsiManager.getInstance(project).findFile(newFile);
                    if (psiFile != null) {
                        final RenameRefactoring[] refactoring = { null };
                        for (PerlNamespaceDefinitionElement namespaceDefinition : PsiTreeUtil.findChildrenOfType(psiFile, PerlNamespaceDefinitionElement.class)) {
                            if (currentPackageName.equals(namespaceDefinition.getPackageName())) {
                                if (refactoring[0] == null) {
                                    refactoring[0] = RefactoringFactory.getInstance(psiFile.getProject()).createRename(namespaceDefinition, newPackageName);
                                } else {
                                    refactoring[0].addElement(namespaceDefinition, newPackageName);
                                }
                            }
                        }
                        if (refactoring[0] != null) {
                            ApplicationManager.getApplication().invokeLater(refactoring[0]::run);
                        }
                    }
                }
            };
        }
    }
    return super.getPostRenameCallback(element, newName, elementListener);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement) PsiFile(com.intellij.psi.PsiFile) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with PerlFileImpl

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

the class PerlFormattingScalarDerefCollapse method apply.

@Override
public int apply() {
    int delta = 0;
    if (myScalarElement.isValid() && myIndexElement.isValid()) {
        String newCode = "$" + myScalarElement.getNode().getText() + myIndexElement.getNode().getText() + ";";
        PerlFileImpl newFile = PerlElementFactory.createFile(myScalarElement.getProject(), newCode);
        PsiPerlStatement statement = PsiTreeUtil.findChildOfType(newFile, PsiPerlStatement.class);
        if (statement != null) {
            delta = new PerlFormattingReplace(myScalarElement, myIndexElement, statement.getFirstChild(), statement.getFirstChild()).apply();
        }
    }
    return delta;
}
Also used : PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) PsiPerlStatement(com.perl5.lang.perl.psi.PsiPerlStatement)

Example 20 with PerlFileImpl

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

the class PerlFormattingSimpleDereferenceUnwrap method apply.

@Override
public int apply() {
    int delta = 0;
    if (myCastElement.isValid() && myVariableElement.isValid()) {
        String newCode = getCode();
        PerlFileImpl newFile = PerlElementFactory.createFile(myCastElement.getProject(), newCode);
        PerlCastExpression newCastExpression = PsiTreeUtil.findChildOfType(newFile, PerlCastExpression.class);
        if (newCastExpression != null) {
            delta = new PerlFormattingReplace(myCastElement, newCastExpression).apply();
        }
    }
    return delta;
}
Also used : PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) PerlCastExpression(com.perl5.lang.perl.psi.PerlCastExpression)

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