Search in sources :

Example 1 with GoExpression

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

the class GoExpectedTypesTest method doTopLevelTest.

private void doTopLevelTest(@NotNull String text, @NotNull String expectedTypeText) {
    myFixture.configureByText("a.go", "package a;" + text);
    PsiElement elementAt = findElementAtCaretOrInSelection();
    GoExpression typeOwner = PsiTreeUtil.getNonStrictParentOfType(elementAt, GoExpression.class);
    assertNotNull("Cannot find type owner. Context element: " + elementAt.getText(), typeOwner);
    assertEquals(expectedTypeText, StringUtil.join(GoTypeUtil.getExpectedTypes(typeOwner), PsiElement::getText, "; "));
}
Also used : GoExpression(com.goide.psi.GoExpression) PsiElement(com.intellij.psi.PsiElement)

Example 2 with GoExpression

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

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

the class GoExpressionSurroundDescriptor method getElementsToSurround.

@NotNull
@Override
public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) {
    GoExpression expr = GoIntroduceVariableBase.findExpressionInSelection(file, startOffset, endOffset);
    if (expr == null)
        return PsiElement.EMPTY_ARRAY;
    UsageTrigger.trigger("go.surroundwith.expression");
    FeatureUsageTracker.getInstance().triggerFeatureUsed("codeassists.surroundwith.expression");
    return new PsiElement[] { expr };
}
Also used : GoExpression(com.goide.psi.GoExpression) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with GoExpression

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

the class GoSimplifyBoolExprQuickFix method replaceExpressionByBoolConst.

private static void replaceExpressionByBoolConst(@NotNull GoBinaryExpr binaryExpr, @NotNull Project project, boolean value) {
    GoExpression expression = GoElementFactory.createExpression(project, value ? "true" : "false");
    binaryExpr.replace(expression);
}
Also used : GoExpression(com.goide.psi.GoExpression)

Example 5 with GoExpression

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

the class GoSimplifyBoolExprQuickFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    if (!(startElement instanceof GoBinaryExpr))
        return;
    GoBinaryExpr o = (GoBinaryExpr) startElement;
    boolean and = o instanceof GoAndExpr;
    List<GoExpression> elements = GoBoolExpressionsInspection.collect(o, and);
    List<GoExpression> toRemove = ContainerUtil.newSmartList();
    for (int i = 0; i < elements.size(); i++) {
        GoExpression l = elements.get(i);
        if (l instanceof GoReferenceExpression && (l.textMatches("true") || l.textMatches("false")) && GoPsiImplUtil.builtin(((GoReferenceExpression) l).resolve())) {
            boolean trueExpr = l.textMatches("true");
            if (and ^ !trueExpr) {
                toRemove.add(l);
            } else {
                replaceExpressionByBoolConst(o, project, !and);
                return;
            }
        }
        for (int j = i + 1; j < elements.size(); j++) {
            GoExpression r = elements.get(j);
            if (GoBoolExpressionsInspection.isEqualsWithNot(l, r) || GoBoolExpressionsInspection.isEqualsWithNot(r, l)) {
                replaceExpressionByBoolConst(o, project, !and);
            }
            if (GoExpressionUtil.identical(l, r))
                toRemove.add(l);
        // todo expr evaluating! x != c1 || x != c2 (c1, c2 const, c1 != c2)
        }
    }
    if (!toRemove.isEmpty()) {
        removeRedundantExpressions(o, project, elements, toRemove, and);
    }
}
Also used : GoBinaryExpr(com.goide.psi.GoBinaryExpr) GoReferenceExpression(com.goide.psi.GoReferenceExpression) GoExpression(com.goide.psi.GoExpression) GoAndExpr(com.goide.psi.GoAndExpr)

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