use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.
the class PsiUtil method getArgumentTypes.
@Nullable
public static PsiType[] getArgumentTypes(@Nullable PsiElement place, boolean nullAsBottom, @Nullable GrExpression stopAt) {
PsiElement parent = place instanceof GrEnumConstant ? place : place != null ? place.getParent() : null;
if (parent instanceof GrIndexProperty) {
GrIndexProperty index = (GrIndexProperty) parent;
PsiType[] argTypes = getArgumentTypes(index.getNamedArguments(), index.getExpressionArguments(), index.getClosureArguments(), nullAsBottom, stopAt);
if (isLValue(index) && argTypes != null) {
PsiType rawInitializer = TypeInferenceHelper.getInitializerTypeFor(index);
PsiType initializer = notNullizeType(rawInitializer, nullAsBottom, index);
return ArrayUtil.append(argTypes, initializer);
} else {
return argTypes;
}
}
if (parent instanceof GrCall) {
GrCall call = (GrCall) parent;
GrNamedArgument[] namedArgs = call.getNamedArguments();
GrExpression[] expressions = call.getExpressionArguments();
GrClosableBlock[] closures = call.getClosureArguments();
return getArgumentTypes(namedArgs, expressions, closures, nullAsBottom, stopAt);
} else if (parent instanceof GrAnonymousClassDefinition) {
final GrArgumentList argList = ((GrAnonymousClassDefinition) parent).getArgumentListGroovy();
if (argList == null) {
return getArgumentTypes(GrNamedArgument.EMPTY_ARRAY, GrExpression.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, nullAsBottom, stopAt);
} else {
return getArgumentTypes(argList.getNamedArguments(), argList.getExpressionArguments(), GrClosableBlock.EMPTY_ARRAY, nullAsBottom, stopAt);
}
} else if (parent instanceof GrBinaryExpression) {
GrExpression right = ((GrBinaryExpression) parent).getRightOperand();
PsiType type = right != null ? right.getType() : null;
return new PsiType[] { notNullizeType(type, nullAsBottom, parent) };
} else if (parent instanceof GrAssignmentExpression) {
PsiType type = ((GrAssignmentExpression) parent).getType();
return new PsiType[] { notNullizeType(type, nullAsBottom, parent) };
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.
the class PsiUtil method getAllArguments.
/**
* Returns all arguments passed to method. First argument is null if Named Arguments is present.
*/
public static GrExpression[] getAllArguments(@NotNull GrCall call) {
GrArgumentList argumentList = call.getArgumentList();
if (argumentList == null)
return GrExpression.EMPTY_ARRAY;
GrClosableBlock[] closureArguments = call.getClosureArguments();
GrExpression[] expressionArguments = argumentList.getExpressionArguments();
GrNamedArgument[] namedArguments = argumentList.getNamedArguments();
int length = expressionArguments.length + closureArguments.length;
int k = 0;
if (namedArguments.length > 0) {
length++;
k = 1;
}
GrExpression[] res = new GrExpression[length];
for (GrExpression expressionArgument : expressionArguments) {
res[k++] = expressionArgument;
}
for (GrClosableBlock closureArgument : closureArguments) {
res[k++] = closureArgument;
}
return res;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList 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.statements.arguments.GrArgumentList in project intellij-community by JetBrains.
the class ResolveUtil method getSelfOrWithQualifier.
@Nullable
public static GrExpression getSelfOrWithQualifier(GrReferenceExpression ref) {
final GrExpression qualifier = ref.getQualifierExpression();
if (qualifier != null) {
return qualifier;
}
PsiElement place = ref;
while (true) {
final GrClosableBlock closure = PsiTreeUtil.getParentOfType(place, GrClosableBlock.class, true, GrMember.class, GroovyFile.class);
if (closure == null)
break;
place = closure;
PsiElement clParent = closure.getParent();
if (clParent instanceof GrArgumentList)
clParent = clParent.getParent();
if (!(clParent instanceof GrMethodCall))
continue;
final GrExpression expression = ((GrMethodCall) clParent).getInvokedExpression();
if (expression instanceof GrReferenceExpression && GdkMethodUtil.isWithName(((GrReferenceExpression) expression).getReferenceName()) && ((GrReferenceExpression) expression).resolve() instanceof GrGdkMethod) {
final GrExpression withQualifier = ((GrReferenceExpression) expression).getQualifierExpression();
if (withQualifier != null) {
return withQualifier;
}
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList 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