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