use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class GrCharConverter method isConvertibleEx.
@Nullable
@Override
public ConversionResult isConvertibleEx(@NotNull PsiType lType, @NotNull PsiType rType, @NotNull GroovyPsiElement context, @NotNull ApplicableTo currentPosition) {
if (!PsiType.CHAR.equals(TypesUtil.unboxPrimitiveTypeWrapper(lType)))
return null;
if (PsiType.CHAR.equals(TypesUtil.unboxPrimitiveTypeWrapper(rType)))
return ConversionResult.OK;
// can assign numeric types to char
if (TypesUtil.isNumericType(rType)) {
if (rType instanceof PsiPrimitiveType || TypesUtil.unboxPrimitiveTypeWrapper(rType) instanceof PsiPrimitiveType) {
return PsiType.CHAR.equals(lType) ? ConversionResult.OK : ConversionResult.ERROR;
} else {
// BigDecimal && BigInteger
return ConversionResult.ERROR;
}
}
{
// special case 'c = []' will throw RuntimeError
final GrExpression rValue;
if (context instanceof GrAssignmentExpression) {
final GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) context;
rValue = assignmentExpression.getRValue();
} else if (context instanceof GrVariable) {
final GrVariable assignmentExpression = (GrVariable) context;
rValue = assignmentExpression.getInitializerGroovy();
} else {
rValue = null;
}
if (rValue instanceof GrListOrMap && ((GrListOrMap) rValue).isEmpty()) {
return ConversionResult.WARNING;
}
}
if (PsiType.BOOLEAN.equals(TypesUtil.unboxPrimitiveTypeWrapper(rType))) {
switch(currentPosition) {
case ASSIGNMENT:
case RETURN_VALUE:
return ConversionResult.WARNING;
default:
return null;
}
}
// one-symbol string-to-char conversion doesn't work with return value
if (currentPosition == ApplicableTo.RETURN_VALUE) {
return null;
}
// can cast and assign one-symbol strings to char
if (!TypesUtil.isClassType(rType, CommonClassNames.JAVA_LANG_STRING))
return null;
return checkSingleSymbolLiteral(context) ? ConversionResult.OK : ConversionResult.ERROR;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class GroovyConfigSlurperCompletionProvider method getPrefix.
@Nullable
public static List<String> getPrefix(GrReferenceExpression ref) {
List<String> res = new ArrayList<>();
GrExpression qualifier = ref.getQualifierExpression();
while (qualifier != null) {
if (!(qualifier instanceof GrReferenceExpression))
return null;
GrReferenceExpression r = (GrReferenceExpression) qualifier;
String name = r.getReferenceName();
if (name == null)
return null;
res.add(name);
qualifier = r.getQualifierExpression();
}
PsiElement e = ref.getParent();
if (e instanceof GrAssignmentExpression) {
GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) e;
if (assignmentExpression.getLValue() != ref)
return null;
e = assignmentExpression.getParent();
}
while (true) {
if (e instanceof PsiFile) {
break;
} else if (e instanceof GrClosableBlock) {
PsiElement eCall = e.getParent();
if (!(eCall instanceof GrMethodCall))
return null;
GrMethodCall call = (GrMethodCall) eCall;
if (!isPropertyCall(call))
return null;
String name = extractPropertyName(call);
if (name == null)
return null;
res.add(name);
e = call.getParent();
} else if (e instanceof GrBlockStatement || e instanceof GrOpenBlock || e instanceof GrIfStatement || e instanceof GrForStatement || e instanceof GrWhileStatement || e instanceof GrTryCatchStatement) {
e = e.getParent();
} else {
return null;
}
}
Collections.reverse(res);
return res;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class GroovyConfigSlurperCompletionProvider method addCompletions.
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
PsiFile file = parameters.getOriginalFile();
if (!(file instanceof GroovyFile))
return;
GroovyFile groovyFile = (GroovyFile) file;
if (!groovyFile.isScript())
return;
GrReferenceExpression ref = (GrReferenceExpression) parameters.getPosition().getParent();
if (ref == null)
return;
final Map<String, Boolean> variants = new HashMap<>();
collectVariants((s, isFinal) -> variants.put(s, isFinal), ref, groovyFile);
if (variants.isEmpty())
return;
// Remove existing variants.
PsiElement parent = ref.getParent();
if (parent instanceof GrAssignmentExpression) {
parent = parent.getParent();
}
if (parent == null)
return;
Set<String> processedPrefixes = new HashSet<>();
Set<String> prefixesInMethodCall = new HashSet<>();
for (PsiElement e = parent.getFirstChild(); e != null; e = e.getNextSibling()) {
if (e instanceof GrAssignmentExpression) {
PsiElement left = ((GrAssignmentExpression) e).getLValue();
if (left instanceof GrReferenceExpression) {
String s = refToString((GrReferenceExpression) left);
if (s == null)
continue;
int dotIndex = s.indexOf('.');
if (dotIndex > 0) {
processedPrefixes.add(s.substring(0, dotIndex));
}
variants.remove(s);
}
} else if (e instanceof GrMethodCall) {
GrMethodCall call = (GrMethodCall) e;
if (isPropertyCall(call)) {
String name = extractPropertyName(call);
if (name == null)
continue;
processedPrefixes.add(name);
prefixesInMethodCall.add(name);
variants.remove(name);
}
}
}
// Process variants.
for (Map.Entry<String, Boolean> entry : variants.entrySet()) {
String variant = entry.getKey();
int dotIndex = variant.indexOf('.');
if (dotIndex > 0 && dotIndex < variant.length() - 1) {
String p = variant.substring(0, dotIndex);
if (prefixesInMethodCall.contains(p))
continue;
if (myAddPrefixes && processedPrefixes.add(p)) {
result.addElement(LookupElementBuilder.create(p));
}
}
LookupElement lookupElement = LookupElementBuilder.create(variant);
if (entry.getValue()) {
lookupElement = TailTypeDecorator.withTail(lookupElement, TailType.EQ);
}
result.addElement(lookupElement);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class IndexedExpressionConversionIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrIndexProperty arrayIndexExpression = (GrIndexProperty) element;
final GrArgumentList argList = (GrArgumentList) arrayIndexExpression.getLastChild();
assert argList != null;
final GrExpression[] arguments = argList.getExpressionArguments();
final PsiElement parent = element.getParent();
final GrExpression arrayExpression = arrayIndexExpression.getInvokedExpression();
if (!(parent instanceof GrAssignmentExpression)) {
rewriteAsGetAt(arrayIndexExpression, arrayExpression, arguments[0]);
return;
}
final GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) parent;
final GrExpression rhs = assignmentExpression.getRValue();
if (rhs.equals(element)) {
rewriteAsGetAt(arrayIndexExpression, arrayExpression, arguments[0]);
} else {
rewriteAsSetAt(assignmentExpression, arrayExpression, arguments[0], rhs);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class IndexedExpressionConversionPredicate method satisfiedBy.
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof GrIndexProperty))
return false;
if (ErrorUtil.containsError(element))
return false;
final GrIndexProperty arrayIndexExpression = (GrIndexProperty) element;
final PsiElement lastChild = arrayIndexExpression.getLastChild();
if (!(lastChild instanceof GrArgumentList))
return false;
final GrArgumentList argList = (GrArgumentList) lastChild;
final GrExpression[] arguments = argList.getExpressionArguments();
if (arguments.length != 1)
return false;
final PsiElement parent = element.getParent();
if (!(parent instanceof GrAssignmentExpression)) {
return true;
}
final GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) parent;
final GrExpression rvalue = assignmentExpression.getRValue();
if (rvalue == null)
return false;
if (rvalue.equals(element))
return true;
final IElementType operator = assignmentExpression.getOperationTokenType();
return GroovyTokenTypes.mASSIGN.equals(operator);
}
Aggregations