Search in sources :

Example 1 with PsiPerlNamespaceDefinition

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

the class PerlUsePackageQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement newStatementContainer = descriptor.getPsiElement();
    if (!FileModificationService.getInstance().prepareFileForWrite(newStatementContainer.getContainingFile())) {
        return;
    }
    PsiElement newStatement = PerlElementFactory.createUseStatement(project, myPackageName);
    PsiElement afterAnchor = null;
    PsiElement beforeAnchor = null;
    PsiElement baseUseStatement = PsiTreeUtil.findChildOfType(newStatementContainer, PerlUseStatement.class);
    if (baseUseStatement != null) {
        if (// pragma or version
        ((PerlUseStatement) baseUseStatement).isPragmaOrVersion()) {
            while (true) {
                // trying to find next use statement
                PsiElement nextStatement = baseUseStatement;
                while ((nextStatement = nextStatement.getNextSibling()) != null && PerlParserDefinition.WHITE_SPACE_AND_COMMENTS.contains(nextStatement.getNode().getElementType())) {
                }
                if (nextStatement instanceof PerlUseStatement && // found more use pragma/version
                ((PerlUseStatement) nextStatement).isPragmaOrVersion()) {
                    baseUseStatement = nextStatement;
                } else {
                    afterAnchor = baseUseStatement;
                    // we've got last pragma statement
                    break;
                }
            }
        } else // not a pragma
        {
            beforeAnchor = baseUseStatement;
        }
    } else // no uses found
    {
        PsiPerlNamespaceDefinition baseNamespace = PsiTreeUtil.findChildOfType(newStatementContainer, PsiPerlNamespaceDefinition.class);
        if (// got a namespace definition
        baseNamespace != null && baseNamespace.getBlock() != null) {
            newStatementContainer = baseNamespace.getBlock();
            if (newStatementContainer != null && !(newStatementContainer instanceof PsiPerlNamespaceContent)) {
                afterAnchor = newStatementContainer.getFirstChild();
            } else if (newStatementContainer != null && newStatementContainer.getFirstChild() != null) {
                beforeAnchor = newStatementContainer.getFirstChild();
            }
        } else {
            PsiElement anchor = newStatementContainer.getFirstChild();
            if (anchor instanceof PsiComment) {
                while (anchor.getNextSibling() != null && PerlPsiUtil.isCommentOrSpace(anchor.getNextSibling())) {
                    anchor = anchor.getNextSibling();
                }
                afterAnchor = anchor;
            } else if (anchor != null) {
                beforeAnchor = anchor;
            }
        }
    }
    if (afterAnchor != null) {
        newStatementContainer = afterAnchor.getParent();
        newStatement = newStatementContainer.addAfter(newStatement, afterAnchor);
    } else if (beforeAnchor != null) {
        newStatementContainer = beforeAnchor.getParent();
        newStatement = newStatementContainer.addBefore(newStatement, beforeAnchor);
    } else if (newStatementContainer != null) {
        newStatement = newStatementContainer.add(newStatement);
    }
    if (newStatement != null) {
        PsiElement newLineElement = PerlElementFactory.createNewLine(project);
        PsiElement nextSibling = newStatement.getNextSibling();
        PsiElement preveSibling = newStatement.getPrevSibling();
        newStatementContainer = newStatement.getParent();
        if (nextSibling == null || !(nextSibling instanceof PsiWhiteSpace) || !StringUtil.equals(nextSibling.getText(), "\n")) {
            newStatementContainer.addAfter(newLineElement, newStatement);
        }
        if ((preveSibling == null && !(newStatementContainer instanceof PsiFile)) || !(preveSibling instanceof PsiWhiteSpace) || !StringUtil.equals(preveSibling.getText(), "\n")) {
            newStatementContainer.addBefore(newLineElement, newStatement);
        }
    }
}
Also used : PsiComment(com.intellij.psi.PsiComment) PsiPerlNamespaceDefinition(com.perl5.lang.perl.psi.PsiPerlNamespaceDefinition) PsiFile(com.intellij.psi.PsiFile) PerlUseStatement(com.perl5.lang.perl.psi.PerlUseStatement) PsiElement(com.intellij.psi.PsiElement) PsiPerlNamespaceContent(com.perl5.lang.perl.psi.PsiPerlNamespaceContent) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Aggregations

PsiComment (com.intellij.psi.PsiComment)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 PerlUseStatement (com.perl5.lang.perl.psi.PerlUseStatement)1 PsiPerlNamespaceContent (com.perl5.lang.perl.psi.PsiPerlNamespaceContent)1 PsiPerlNamespaceDefinition (com.perl5.lang.perl.psi.PsiPerlNamespaceDefinition)1