Search in sources :

Example 1 with PsiPerlParenthesisedExpr

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

the class PerlFormattingStatementModifierUnwrap method apply.

@Override
public int apply() {
    int delta = 0;
    if (getMyModifier().isValid()) {
        PsiPerlExpr expression = PsiTreeUtil.getChildOfType(getMyModifier(), PsiPerlExpr.class);
        if (expression != null && expression instanceof PsiPerlParenthesisedExpr) {
            PsiPerlExpr nestedExpression = ((PsiPerlParenthesisedExpr) expression).getExpr();
            if (nestedExpression != null) {
                delta = nestedExpression.getNode().getTextLength() - expression.getNode().getTextLength();
                expression.replace(nestedExpression.copy());
            }
        }
    }
    return delta;
}
Also used : PsiPerlParenthesisedExpr(com.perl5.lang.perl.psi.PsiPerlParenthesisedExpr) PsiPerlExpr(com.perl5.lang.perl.psi.PsiPerlExpr)

Example 2 with PsiPerlParenthesisedExpr

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

the class PerlFormattingStatementModifierWrap method apply.

@Override
public int apply() {
    int delta = 0;
    if (getMyModifier().isValid()) {
        PsiPerlExpr expression = PsiTreeUtil.getChildOfType(getMyModifier(), PsiPerlExpr.class);
        if (expression != null && !(expression instanceof PsiPerlParenthesisedExpr)) {
            PsiPerlParenthesisedExpr parenthesisedExpression = PerlElementFactory.createParenthesisedExpression(getMyModifier().getProject());
            parenthesisedExpression.addAfter(expression.copy(), parenthesisedExpression.getFirstChild());
            delta = expression.getNode().getTextLength() - parenthesisedExpression.getNode().getTextLength();
            expression.replace(parenthesisedExpression);
        }
    }
    return delta;
}
Also used : PsiPerlParenthesisedExpr(com.perl5.lang.perl.psi.PsiPerlParenthesisedExpr) PsiPerlExpr(com.perl5.lang.perl.psi.PsiPerlExpr)

Example 3 with PsiPerlParenthesisedExpr

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

the class PerlElementFactory method createParenthesisedExpression.

public static PsiPerlParenthesisedExpr createParenthesisedExpression(Project project) {
    PerlFileImpl file = createFile(project, "();");
    PsiPerlParenthesisedExpr result = PsiTreeUtil.findChildOfType(file, PsiPerlParenthesisedExpr.class);
    assert result != null : "While creating PsiPerlParenthesisedExpr";
    return result;
}
Also used : PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl)

Example 4 with PsiPerlParenthesisedExpr

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

Aggregations

PsiPerlExpr (com.perl5.lang.perl.psi.PsiPerlExpr)3 PsiPerlParenthesisedExpr (com.perl5.lang.perl.psi.PsiPerlParenthesisedExpr)3 PerlFileImpl (com.perl5.lang.perl.psi.impl.PerlFileImpl)2 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 PsiPerlStatementModifier (com.perl5.lang.perl.psi.PsiPerlStatementModifier)1 PsiPerlStatementImpl (com.perl5.lang.perl.psi.impl.PsiPerlStatementImpl)1