Search in sources :

Example 1 with GoBinaryExpr

use of com.goide.psi.GoBinaryExpr in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoSimplifyBoolExprQuickFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    if (!(startElement instanceof GoBinaryExpr))
        return;
    GoBinaryExpr o = (GoBinaryExpr) startElement;
    boolean and = o instanceof GoAndExpr;
    List<GoExpression> elements = GoBoolExpressionsInspection.collect(o, and);
    List<GoExpression> toRemove = ContainerUtil.newSmartList();
    for (int i = 0; i < elements.size(); i++) {
        GoExpression l = elements.get(i);
        if (l instanceof GoReferenceExpression && (l.textMatches("true") || l.textMatches("false")) && GoPsiImplUtil.builtin(((GoReferenceExpression) l).resolve())) {
            boolean trueExpr = l.textMatches("true");
            if (and ^ !trueExpr) {
                toRemove.add(l);
            } else {
                replaceExpressionByBoolConst(o, project, !and);
                return;
            }
        }
        for (int j = i + 1; j < elements.size(); j++) {
            GoExpression r = elements.get(j);
            if (GoBoolExpressionsInspection.isEqualsWithNot(l, r) || GoBoolExpressionsInspection.isEqualsWithNot(r, l)) {
                replaceExpressionByBoolConst(o, project, !and);
            }
            if (GoExpressionUtil.identical(l, r))
                toRemove.add(l);
        // todo expr evaluating! x != c1 || x != c2 (c1, c2 const, c1 != c2)
        }
    }
    if (!toRemove.isEmpty()) {
        removeRedundantExpressions(o, project, elements, toRemove, and);
    }
}
Also used : GoBinaryExpr(com.goide.psi.GoBinaryExpr) GoReferenceExpression(com.goide.psi.GoReferenceExpression) GoExpression(com.goide.psi.GoExpression) GoAndExpr(com.goide.psi.GoAndExpr)

Aggregations

GoAndExpr (com.goide.psi.GoAndExpr)1 GoBinaryExpr (com.goide.psi.GoBinaryExpr)1 GoExpression (com.goide.psi.GoExpression)1 GoReferenceExpression (com.goide.psi.GoReferenceExpression)1