use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class GroovyStaticTypeCheckVisitor method processTupleAssignment.
@Override
protected void processTupleAssignment(@NotNull GrTupleExpression tupleExpression, @NotNull GrExpression initializer) {
if (initializer instanceof GrListOrMap && !((GrListOrMap) initializer).isMap()) {
final GrListOrMap initializerList = (GrListOrMap) initializer;
final GrExpression[] vars = tupleExpression.getExpressions();
final GrExpression[] expressions = initializerList.getInitializers();
if (vars.length > expressions.length) {
registerError(initializer, GroovyBundle.message("incorrect.number.of.values", vars.length, expressions.length), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR);
} else {
for (int i = 0; i < vars.length; i++) {
processAssignmentWithinMultipleAssignment(vars[i], expressions[i], tupleExpression);
}
}
} else {
registerError(initializer, GroovyBundle.message("multiple.assignments.without.list.expr"), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class GrChangeSignatureUsageProcessor method processMethodUsage.
private static void processMethodUsage(PsiElement element, JavaChangeInfo changeInfo, boolean toChangeArguments, boolean toCatchExceptions, GrClosureSignatureUtil.ArgInfo<PsiElement>[] map, PsiSubstitutor substitutor) {
if (map == null)
return;
if (changeInfo.isNameChanged()) {
if (element instanceof GrReferenceElement) {
element = ((GrReferenceElement) element).handleElementRename(changeInfo.getNewName());
}
}
if (toChangeArguments) {
JavaParameterInfo[] parameters = changeInfo.getNewParameters();
GrArgumentList argumentList = PsiUtil.getArgumentsList(element);
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject());
if (argumentList == null) {
if (element instanceof GrEnumConstant) {
argumentList = factory.createArgumentList();
argumentList = (GrArgumentList) element.add(argumentList);
} else {
return;
}
}
Set<PsiElement> argsToDelete = new HashSet<>(map.length * 2);
for (GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo : map) {
argsToDelete.addAll(argInfo.args);
}
GrExpression[] values = new GrExpression[parameters.length];
for (int i = 0; i < parameters.length; i++) {
JavaParameterInfo parameter = parameters[i];
int index = parameter.getOldIndex();
if (index >= 0) {
argsToDelete.removeAll(map[index].args);
} else {
values[i] = createDefaultValue(factory, changeInfo, parameter, argumentList, substitutor);
}
}
for (PsiElement arg : argsToDelete) {
arg.delete();
}
boolean skipOptionals = false;
//PsiTreeUtil.getChildOfAnyType(argumentList, GrExpression.class, GrNamedArgument.class);
PsiElement anchor = null;
for (int i = 0; i < parameters.length; i++) {
JavaParameterInfo parameter = parameters[i];
int index = parameter.getOldIndex();
if (index >= 0) {
GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo = map[index];
List<PsiElement> arguments = argInfo.args;
if (argInfo.isMultiArg) {
//arguments for Map and varArg
if ((i != 0 || !(!arguments.isEmpty() && arguments.iterator().next() instanceof GrNamedArgument)) && (i != parameters.length - 1 || !parameter.isVarargType())) {
final PsiType type = parameter.createType(changeInfo.getMethod().getParameterList(), argumentList.getManager());
final GrExpression arg = GroovyRefactoringUtil.generateArgFromMultiArg(substitutor, arguments, type, element.getProject());
for (PsiElement argument : arguments) {
argument.delete();
}
anchor = argumentList.addAfter(arg, anchor);
JavaCodeStyleManager.getInstance(anchor.getProject()).shortenClassReferences(anchor);
}
} else {
//arguments for simple parameters
if (arguments.size() == 1) {
//arg exists
PsiElement arg = arguments.iterator().next();
if (i == parameters.length - 1 && parameter.isVarargType()) {
if (arg instanceof GrSafeCastExpression) {
PsiElement expr = ((GrSafeCastExpression) arg).getOperand();
if (expr instanceof GrListOrMap && !((GrListOrMap) expr).isMap()) {
final PsiElement copy = expr.copy();
PsiElement[] newVarargs = ((GrListOrMap) copy).getInitializers();
for (PsiElement vararg : newVarargs) {
anchor = argumentList.addAfter(vararg, anchor);
}
arg.delete();
continue;
}
}
}
PsiElement curArg = getNextOfType(argumentList, anchor, GrExpression.class);
if (curArg == arg) {
anchor = arg;
} else {
final PsiElement copy = arg.copy();
anchor = argumentList.addAfter(copy, anchor);
arg.delete();
}
} else {
//arg is skipped. Parameter is optional
skipOptionals = true;
}
}
} else {
if (skipOptionals && isParameterOptional(parameter))
continue;
if (forceOptional(parameter)) {
skipOptionals = true;
continue;
}
try {
final GrExpression value = values[i];
if (i > 0 && (value == null || anchor == null)) {
PsiElement comma = Factory.createSingleLeafElement(GroovyTokenTypes.mCOMMA, ",", 0, 1, SharedImplUtil.findCharTableByTree(argumentList.getNode()), argumentList.getManager()).getPsi();
if (anchor == null)
anchor = argumentList.getLeftParen();
anchor = argumentList.addAfter(comma, anchor);
}
if (value != null) {
anchor = argumentList.addAfter(value, anchor);
}
} catch (IncorrectOperationException e) {
LOG.error(e.getMessage());
}
}
}
for (PsiElement arg : argsToDelete) {
arg.delete();
}
GrCall call = GroovyRefactoringUtil.getCallExpressionByMethodReference(element);
if (argumentList.getText().trim().isEmpty() && (call == null || !PsiImplUtil.hasClosureArguments(call))) {
argumentList = argumentList.replaceWithArgumentList(factory.createArgumentList());
}
CodeStyleManager.getInstance(argumentList.getProject()).reformat(argumentList);
}
if (toCatchExceptions) {
final ThrownExceptionInfo[] exceptionInfos = changeInfo.getNewExceptions();
PsiClassType[] exceptions = getExceptions(exceptionInfos, element, element.getManager());
fixExceptions(element, exceptions);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap 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.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class PsiUtil method getCallByNamedParameter.
@Nullable
public static GrCall getCallByNamedParameter(GrNamedArgument namedArgument) {
PsiElement parent = namedArgument.getParent();
PsiElement eMethodCall;
if (parent instanceof GrArgumentList) {
eMethodCall = parent.getParent();
} else {
if (!(parent instanceof GrListOrMap))
return null;
PsiElement eArgumentList = parent.getParent();
if (!(eArgumentList instanceof GrArgumentList))
return null;
GrArgumentList argumentList = (GrArgumentList) eArgumentList;
if (argumentList.getNamedArguments().length > 0)
return null;
if (argumentList.getExpressionArgumentIndex((GrListOrMap) parent) != 0)
return null;
eMethodCall = eArgumentList.getParent();
}
if (!(eMethodCall instanceof GrCall))
return null;
return (GrCall) eMethodCall;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class NamedArgumentInsertHandler method handleInsert.
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
int tailOffset = context.getTailOffset();
PsiElement argumentList = context.getFile().findElementAt(tailOffset - 1);
while (argumentList != null && !(argumentList instanceof GrArgumentList) && !(argumentList instanceof GrListOrMap)) {
argumentList = argumentList.getParent();
}
final Editor editor = context.getEditor();
if (argumentList != null) {
CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(context.getProject()).getCurrentSettings();
GroovyCodeStyleSettings codeStyleSettings = settings.getCustomSettings(GroovyCodeStyleSettings.class);
CommonCodeStyleSettings commonCodeStyleSettings = settings.getCommonSettings(GroovyLanguage.INSTANCE);
boolean insertSpace = codeStyleSettings.SPACE_IN_NAMED_ARGUMENT;
if (context.getCompletionChar() == ':' || (insertSpace && context.getCompletionChar() == ' ')) {
context.setAddCompletionChar(false);
}
String argumentListText = argumentList.getText();
String s = argumentListText.substring(tailOffset - argumentList.getTextOffset());
s = StringUtil.trimEnd(s, ")");
if (s.trim().isEmpty()) {
String toInsert = insertSpace ? ": " : ":";
editor.getDocument().insertString(tailOffset, toInsert);
editor.getCaretModel().moveToOffset(tailOffset + toInsert.length());
} else {
if (context.getCompletionChar() == Lookup.REPLACE_SELECT_CHAR) {
char a = s.charAt(0);
if (Character.isLetterOrDigit(a)) {
return;
}
}
Matcher m = Pattern.compile("([ \\t]*):([ \\t]*)(.*)", Pattern.DOTALL).matcher(s);
if (m.matches()) {
int caret = tailOffset + m.end(2);
if (m.group(2).isEmpty()) {
editor.getDocument().insertString(caret, " ");
caret++;
}
editor.getCaretModel().moveToOffset(caret);
} else {
m = Pattern.compile("([ \\t]*)([\\n \\t]*)[\\],](.*)", Pattern.DOTALL).matcher(s);
if (m.matches()) {
String toInsert = insertSpace ? ": " : ":";
editor.getDocument().replaceString(tailOffset, tailOffset + m.start(2), toInsert);
editor.getCaretModel().moveToOffset(tailOffset + toInsert.length());
} else {
m = Pattern.compile("([ \\t]*)(.*)", Pattern.DOTALL).matcher(s);
if (!m.matches())
throw new RuntimeException("This pattern must match any non-empty string! (" + s + ")");
StringBuilder sb = new StringBuilder(3);
sb.append(':');
int shiftCaret = 1;
if (insertSpace) {
sb.append(' ');
shiftCaret++;
}
if (!m.group(2).startsWith("\n") && commonCodeStyleSettings.SPACE_AFTER_COMMA) {
sb.append(' ');
}
editor.getDocument().replaceString(tailOffset, tailOffset + m.start(2), sb);
editor.getCaretModel().moveToOffset(tailOffset + shiftCaret);
}
}
}
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
editor.getSelectionModel().removeSelection();
}
}
Aggregations