Search in sources :

Example 1 with GoIfStatement

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

the class GoUsedAsValueInCondition method buildGoVisitor.

@NotNull
@Override
protected GoVisitor buildGoVisitor(@NotNull ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
    return new GoVisitor() {

        @Override
        public void visitAssignmentStatement(@NotNull GoAssignmentStatement o) {
            if (o.getParent() != null && o.getParent() instanceof GoIfStatement && ((GoIfStatement) o.getParent()).getExpression() == null) {
                String left = GoPsiImplUtil.joinPsiElementText(o.getLeftHandExprList().getExpressionList());
                String right = GoPsiImplUtil.joinPsiElementText(o.getExpressionList());
                holder.registerProblem(o, left + " = " + right + " used as value", GENERIC_ERROR_OR_WARNING, new GoAssignmentToComparisonQuickFix());
            }
        }

        @Override
        public void visitShortVarDeclaration(@NotNull GoShortVarDeclaration o) {
            PsiElement parent = o.getParent();
            if (parent != null) {
                PsiElement gradParent = parent.getParent();
                if (gradParent instanceof GoIfStatement && ((GoIfStatement) gradParent).getExpression() == null) {
                    String left = GoPsiImplUtil.joinPsiElementText(o.getVarDefinitionList());
                    String right = GoPsiImplUtil.joinPsiElementText(o.getRightExpressionsList());
                    holder.registerProblem(o, left + " := " + right + " used as value", GENERIC_ERROR_OR_WARNING, new GoAssignmentToComparisonQuickFix());
                }
            }
        }
    };
}
Also used : GoShortVarDeclaration(com.goide.psi.GoShortVarDeclaration) GoAssignmentStatement(com.goide.psi.GoAssignmentStatement) GoIfStatement(com.goide.psi.GoIfStatement) NotNull(org.jetbrains.annotations.NotNull) GoVisitor(com.goide.psi.GoVisitor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with GoIfStatement

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

the class GoBoolExpressionSurrounderBase method surroundExpressionWithIfElse.

@Nullable
protected TextRange surroundExpressionWithIfElse(@NotNull PsiElement[] elements, boolean withElse) {
    GoExpression expression = getExpression(elements);
    if (expression == null)
        return null;
    String condition = expression.getText();
    GoIfStatement ifStatement = GoElementFactory.createIfStatement(expression.getProject(), condition, "", withElse ? "" : null);
    PsiElement replace = expression.replace(ifStatement);
    int offset = replace.getTextRange().getEndOffset();
    return TextRange.create(offset, offset);
}
Also used : GoIfStatement(com.goide.psi.GoIfStatement) GoExpression(com.goide.psi.GoExpression) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with GoIfStatement

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

the class GoStatementsSurrounder method surroundStatementsWithIfElse.

@Nullable
protected TextRange surroundStatementsWithIfElse(@NotNull Project project, @NotNull PsiElement container, @NotNull PsiElement[] statements, boolean withElse) {
    PsiElement first = ArrayUtil.getFirstElement(statements);
    PsiElement last = ArrayUtil.getLastElement(statements);
    String block = StringUtil.join(statements, PsiElement::getText, "\n");
    GoIfStatement ifStatement = GoElementFactory.createIfStatement(project, "", block, withElse ? "" : null);
    ifStatement = (GoIfStatement) container.addAfter(ifStatement, last);
    container.deleteChildRange(first, last);
    int offset = getOffsetLBraceOfBlock(ifStatement.getBlock());
    return offset > -1 ? new TextRange(offset, offset) : null;
}
Also used : GoIfStatement(com.goide.psi.GoIfStatement) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with GoIfStatement

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

the class GoIfUnwrapper method doUnwrap.

@Override
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
    GoIfStatement ifStatement = (GoIfStatement) element;
    context.extractFromBlock(ifStatement.getBlock(), ifStatement);
    context.delete(ifStatement);
}
Also used : GoIfStatement(com.goide.psi.GoIfStatement)

Example 5 with GoIfStatement

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

the class GoElseUnwrapper method doUnwrap.

@Override
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
    GoElseStatement elseStatement = (GoElseStatement) element;
    GoIfStatement elseIf = elseStatement.getIfStatement();
    context.extractNewLine(elseStatement);
    if (elseIf != null) {
        context.extractElement(elseIf, elseStatement);
    }
    context.extractFromBlock(elseStatement.getBlock(), elseStatement);
    context.delete(elseStatement);
}
Also used : GoElseStatement(com.goide.psi.GoElseStatement) GoIfStatement(com.goide.psi.GoIfStatement)

Aggregations

GoIfStatement (com.goide.psi.GoIfStatement)5 PsiElement (com.intellij.psi.PsiElement)3 Nullable (org.jetbrains.annotations.Nullable)2 GoAssignmentStatement (com.goide.psi.GoAssignmentStatement)1 GoElseStatement (com.goide.psi.GoElseStatement)1 GoExpression (com.goide.psi.GoExpression)1 GoShortVarDeclaration (com.goide.psi.GoShortVarDeclaration)1 GoVisitor (com.goide.psi.GoVisitor)1 TextRange (com.intellij.openapi.util.TextRange)1 NotNull (org.jetbrains.annotations.NotNull)1