Search in sources :

Example 1 with GoAssignmentStatement

use of com.goide.psi.GoAssignmentStatement 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 GoAssignmentStatement

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

the class GoReplaceAssignmentWithDeclarationQuickFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    if (startElement instanceof GoAssignmentStatement) {
        GoAssignmentStatement statement = (GoAssignmentStatement) startElement;
        String leftSide = statement.getLeftHandExprList().getText();
        String rightSide = GoPsiImplUtil.joinPsiElementText(statement.getExpressionList());
        statement.replace(GoElementFactory.createShortVarDeclarationStatement(project, leftSide, rightSide));
    } else if (startElement instanceof GoRangeClause) {
        GoRangeClause rangeClause = (GoRangeClause) startElement;
        String leftSide = GoPsiImplUtil.joinPsiElementText(rangeClause.getLeftExpressionsList());
        GoExpression rangeExpression = rangeClause.getRangeExpression();
        String rightSide = rangeExpression != null ? rangeExpression.getText() : "";
        rangeClause.replace(GoElementFactory.createRangeClause(project, leftSide, rightSide));
    } else if (startElement instanceof GoRecvStatement) {
        GoRecvStatement recvStatement = (GoRecvStatement) startElement;
        String leftSide = GoPsiImplUtil.joinPsiElementText(recvStatement.getLeftExpressionsList());
        GoExpression recvExpression = recvStatement.getRecvExpression();
        String rightSide = recvExpression != null ? recvExpression.getText() : "";
        recvStatement.replace(GoElementFactory.createRecvStatement(project, leftSide, rightSide));
    }
}
Also used : GoAssignmentStatement(com.goide.psi.GoAssignmentStatement) GoRecvStatement(com.goide.psi.GoRecvStatement) GoRangeClause(com.goide.psi.GoRangeClause) GoExpression(com.goide.psi.GoExpression)

Aggregations

GoAssignmentStatement (com.goide.psi.GoAssignmentStatement)2 GoExpression (com.goide.psi.GoExpression)1 GoIfStatement (com.goide.psi.GoIfStatement)1 GoRangeClause (com.goide.psi.GoRangeClause)1 GoRecvStatement (com.goide.psi.GoRecvStatement)1 GoShortVarDeclaration (com.goide.psi.GoShortVarDeclaration)1 GoVisitor (com.goide.psi.GoVisitor)1 PsiElement (com.intellij.psi.PsiElement)1 NotNull (org.jetbrains.annotations.NotNull)1