Search in sources :

Example 6 with GoExpression

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

the class GoSimplifyBoolExprQuickFix method removeRedundantExpressions.

private static void removeRedundantExpressions(@NotNull GoBinaryExpr binaryExpr, @NotNull Project project, @NotNull List<GoExpression> expressions, @NotNull List<GoExpression> toRemove, boolean and) {
    for (GoExpression e : toRemove) {
        expressions.remove(e);
    }
    String separator = and ? " && " : " || ";
    String text = StringUtil.join(expressions, PsiElement::getText, separator);
    GoExpression expression = GoElementFactory.createExpression(project, text);
    binaryExpr.replace(expression);
}
Also used : GoExpression(com.goide.psi.GoExpression) LocalQuickFixOnPsiElement(com.intellij.codeInspection.LocalQuickFixOnPsiElement) PsiElement(com.intellij.psi.PsiElement)

Example 7 with GoExpression

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

the class GoExpressionSurrounder method surroundWithParenthesis.

@Nullable
protected TextRange surroundWithParenthesis(@NotNull PsiElement[] elements, boolean withNot) {
    GoExpression expression = getExpression(elements);
    if (expression == null)
        return null;
    String text = (withNot ? "!" : "") + "(" + expression.getText() + ")";
    GoExpression parenthExprNode = GoElementFactory.createExpression(expression.getProject(), text);
    PsiElement replace = expression.replace(parenthExprNode);
    int endOffset = replace.getTextRange().getEndOffset();
    return TextRange.create(endOffset, endOffset);
}
Also used : GoExpression(com.goide.psi.GoExpression) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with GoExpression

use of com.goide.psi.GoExpression 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 9 with GoExpression

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

the class GoConstSpecStub method getExpressionList.

@NotNull
public List<GoExpression> getExpressionList() {
    if (myList == null) {
        String text = getExpressionsText();
        if (!StringUtil.isNotEmpty(text))
            return myList = ContainerUtil.emptyList();
        Project project = getPsi().getProject();
        List<String> split = StringUtil.split(text, ";");
        myList = ContainerUtil.map(split, s -> GoElementFactory.createExpression(project, s));
    }
    return myList;
}
Also used : StringUtil(com.intellij.openapi.util.text.StringUtil) IStubElementType(com.intellij.psi.stubs.IStubElementType) StringRef(com.intellij.util.io.StringRef) GoConstSpec(com.goide.psi.GoConstSpec) ContainerUtil(com.intellij.util.containers.ContainerUtil) StubBase(com.intellij.psi.stubs.StubBase) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) GoExpression(com.goide.psi.GoExpression) Project(com.intellij.openapi.project.Project) GoElementFactory(com.goide.psi.impl.GoElementFactory) StubElement(com.intellij.psi.stubs.StubElement) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with GoExpression

use of com.goide.psi.GoExpression 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)11 PsiElement (com.intellij.psi.PsiElement)5 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 GoRangeClause (com.goide.psi.GoRangeClause)2 GoAndExpr (com.goide.psi.GoAndExpr)1 GoAssignmentStatement (com.goide.psi.GoAssignmentStatement)1 GoBinaryExpr (com.goide.psi.GoBinaryExpr)1 GoCallExpr (com.goide.psi.GoCallExpr)1 GoConstSpec (com.goide.psi.GoConstSpec)1 GoIfStatement (com.goide.psi.GoIfStatement)1 GoRecvStatement (com.goide.psi.GoRecvStatement)1 GoReferenceExpression (com.goide.psi.GoReferenceExpression)1 GoType (com.goide.psi.GoType)1 GoVisitor (com.goide.psi.GoVisitor)1 GoElementFactory (com.goide.psi.impl.GoElementFactory)1 LocalQuickFixOnPsiElement (com.intellij.codeInspection.LocalQuickFixOnPsiElement)1 Project (com.intellij.openapi.project.Project)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 PsiFile (com.intellij.psi.PsiFile)1