Search in sources :

Example 1 with PsiPerlStatement

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

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

Aggregations

PsiPerlStatement (com.perl5.lang.perl.psi.PsiPerlStatement)2 PerlFileImpl (com.perl5.lang.perl.psi.impl.PerlFileImpl)2 LocalQuickFixOnPsiElement (com.intellij.codeInspection.LocalQuickFixOnPsiElement)1 PsiElement (com.intellij.psi.PsiElement)1 PerlVariableDeclarationElement (com.perl5.lang.perl.psi.PerlVariableDeclarationElement)1