use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class GrSortMapKeysIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
PsiElement parent = element.getParent();
if (parent instanceof GrArgumentLabel) {
PsiElement pparent = parent.getParent().getParent();
if (pparent instanceof GrListOrMap && !ErrorUtil.containsError(pparent)) {
GrListOrMap map = (GrListOrMap) pparent;
if (map.getInitializers().length == 0) {
GrNamedArgument[] namedArgs = map.getNamedArguments();
if (isLiteralKeys(namedArgs)) {
GrListOrMap newMap = constructNewMap(namedArgs, project);
map.replace(newMap);
}
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class GrSortMapKeysIntention method constructNewMap.
@NotNull
private static GrListOrMap constructNewMap(@NotNull GrNamedArgument[] args, Project project) {
StringBuilder builder = new StringBuilder();
builder.append("[");
Arrays.sort(args, (o1, o2) -> {
final String l1 = o1.getLabelName();
final String l2 = o2.getLabelName();
assert l1 != null && l2 != null;
return l1.compareTo(l2);
});
for (GrNamedArgument arg : args) {
builder.append(arg.getText()).append(",\n");
}
builder.replace(builder.length() - 2, builder.length(), "]");
return (GrListOrMap) GroovyPsiElementFactory.getInstance(project).createExpressionFromText(builder);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class SpockUtils method createVariableMap.
// See org.spockframework.compiler.WhereBlockRewriter
public static Map<String, SpockVariableDescriptor> createVariableMap(GrMethod method) {
GrOpenBlock block = method.getBlock();
if (block == null)
return Collections.emptyMap();
PsiElement elementUnderLabel = null;
PsiElement elementAfterLabel = null;
main: for (PsiElement e = block.getFirstChild(); e != null; e = e.getNextSibling()) {
if (e instanceof GrLabeledStatement) {
GrLabeledStatement l = (GrLabeledStatement) e;
elementAfterLabel = l.getNextSibling();
while (true) {
GrStatement statement = l.getStatement();
if ("where".equals(l.getName())) {
elementUnderLabel = statement;
break main;
}
if (statement instanceof GrLabeledStatement) {
l = (GrLabeledStatement) statement;
continue;
}
break;
}
}
}
if (elementUnderLabel == null)
return Collections.emptyMap();
Map<String, SpockVariableDescriptor> res = new HashMap<>();
PsiElement e = elementUnderLabel;
while (e != null) {
if (e instanceof GrBinaryExpression && ((GrBinaryExpression) e).getOperationTokenType() == GroovyElementTypes.COMPOSITE_LSHIFT_SIGN) {
GrBinaryExpression shift = (GrBinaryExpression) e;
GrExpression leftOperand = shift.getLeftOperand();
GrExpression rightOperand = shift.getRightOperand();
if (leftOperand instanceof GrReferenceExpression) {
String name = getNameByReference(leftOperand);
if (name != null) {
SpockVariableDescriptor descriptor = new SpockVariableDescriptor(leftOperand, name);
descriptor.addExpressionOfCollection(rightOperand);
res.put(name, descriptor);
}
} else if (leftOperand instanceof GrListOrMap) {
GrExpression[] variableDefinitions = ((GrListOrMap) leftOperand).getInitializers();
SpockVariableDescriptor[] variables = createVariables(res, Arrays.asList(variableDefinitions));
if (rightOperand instanceof GrListOrMap) {
for (GrExpression expression : ((GrListOrMap) rightOperand).getInitializers()) {
if (expression instanceof GrListOrMap) {
add(variables, Arrays.asList(((GrListOrMap) expression).getInitializers()));
} else {
for (SpockVariableDescriptor variable : variables) {
if (variable != null) {
variable.addExpressionOfCollection(expression);
}
}
}
}
}
}
} else if (e instanceof GrAssignmentExpression) {
GrAssignmentExpression assExpr = (GrAssignmentExpression) e;
GrExpression lValue = assExpr.getLValue();
String name = getNameByReference(lValue);
if (name != null) {
res.put(name, new SpockVariableDescriptor(lValue, name).addExpression(assExpr.getRValue()));
}
} else if (isOrStatement(e)) {
// See org.spockframework.compiler.WhereBlockRewriter#rewriteTableLikeParameterization()
List<GrExpression> variableDefinitions = new ArrayList<>();
splitOr(variableDefinitions, (GrExpression) e);
SpockVariableDescriptor[] variables = createVariables(res, variableDefinitions);
List<GrExpression> row = new ArrayList<>();
PsiElement rowElement = getNext(e, elementUnderLabel, elementAfterLabel);
while (isOrStatement(rowElement)) {
row.clear();
splitOr(row, (GrExpression) rowElement);
add(variables, row);
rowElement = getNext(rowElement, elementUnderLabel, elementAfterLabel);
}
e = rowElement;
continue;
}
e = getNext(e, elementUnderLabel, elementAfterLabel);
}
return res;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class GroovyTypeCheckVisitor method visitVariable.
@Override
public void visitVariable(@NotNull GrVariable variable) {
super.visitVariable(variable);
final PsiType varType = variable.getType();
final PsiElement parent = variable.getParent();
if (variable instanceof GrParameter && ((GrParameter) variable).getDeclarationScope() instanceof GrMethod || parent instanceof GrForInClause) {
return;
} else if (parent instanceof GrVariableDeclaration && ((GrVariableDeclaration) parent).isTuple()) {
//check tuple assignment: def (int x, int y) = foo()
final GrVariableDeclaration tuple = (GrVariableDeclaration) parent;
final GrExpression initializer = tuple.getTupleInitializer();
if (initializer == null)
return;
if (!(initializer instanceof GrListOrMap)) {
PsiType type = initializer.getType();
if (type == null)
return;
PsiType valueType = extractIterableTypeParameter(type, false);
processAssignment(varType, valueType, tuple, variable.getNameIdentifierGroovy());
return;
}
}
GrExpression initializer = variable.getInitializerGroovy();
if (initializer == null)
return;
processAssignment(varType, initializer, variable.getNameIdentifierGroovy(), "cannot.assign", variable, ApplicableTo.ASSIGNMENT);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class GroovyTypeCheckVisitor method processAssignment.
private void processAssignment(@NotNull PsiType expectedType, @NotNull GrExpression expression, @NotNull PsiElement toHighlight, @NotNull @PropertyKey(resourceBundle = GroovyBundle.BUNDLE) String messageKey, @NotNull PsiElement context, @NotNull ApplicableTo position) {
{
// check if current assignment is constructor call
final GrListOrMap initializer = getTupleInitializer(expression);
if (initializer != null) {
processConstructorCall(new GrListOrMapInfo(initializer));
return;
}
}
if (PsiUtil.isRawClassMemberAccess(expression))
return;
if (checkForImplicitEnumAssigning(expectedType, expression, expression))
return;
final PsiType actualType = expression.getType();
if (actualType == null)
return;
final ConversionResult result = TypesUtil.canAssign(expectedType, actualType, context, position);
if (result == ConversionResult.OK)
return;
final List<LocalQuickFix> fixes = ContainerUtil.newArrayList();
{
fixes.add(new GrCastFix(expectedType, expression));
final String varName = getLValueVarName(toHighlight);
if (varName != null) {
fixes.add(new GrChangeVariableType(actualType, varName));
}
}
final String message = GroovyBundle.message(messageKey, actualType.getPresentableText(), expectedType.getPresentableText());
registerError(toHighlight, message, fixes.toArray(new LocalQuickFix[fixes.size()]), result == ConversionResult.ERROR ? ProblemHighlightType.GENERIC_ERROR : ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
Aggregations