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());
}
}
}
};
}
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);
}
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;
}
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);
}
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);
}
Aggregations