Search in sources :

Example 1 with GoRangeClause

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

Example 2 with GoRangeClause

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

the class GoRangeIterationOnIllegalTypeInspection method buildGoVisitor.

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

        @Override
        public void visitRangeClause(@NotNull GoRangeClause o) {
            super.visitRangeClause(o);
            GoExpression expression = o.getRangeExpression();
            GoType type = expression != null ? expression.getGoType(null) : null;
            if (type != null && !GoTypeUtil.isIterable(type)) {
                holder.registerProblem(expression, "Cannot range over data (type " + GoPsiImplUtil.getText(type) + ")", ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
            }
        }
    };
}
Also used : GoRangeClause(com.goide.psi.GoRangeClause) GoExpression(com.goide.psi.GoExpression) NotNull(org.jetbrains.annotations.NotNull) GoVisitor(com.goide.psi.GoVisitor) GoType(com.goide.psi.GoType) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GoExpression (com.goide.psi.GoExpression)2 GoRangeClause (com.goide.psi.GoRangeClause)2 GoAssignmentStatement (com.goide.psi.GoAssignmentStatement)1 GoRecvStatement (com.goide.psi.GoRecvStatement)1 GoType (com.goide.psi.GoType)1 GoVisitor (com.goide.psi.GoVisitor)1 NotNull (org.jetbrains.annotations.NotNull)1