use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project android by JetBrains.
the class GradleSettingsFile method addModule.
/**
* Adds a reference to the module to the settings file, if there is not already one. The module path must be colon separated, with a
* leading colon, e.g. ":project:subproject". Must be run inside a write action.
*
* If the file does not match the default module location, this method will override the location.
*/
public void addModule(@NotNull String modulePath, @NotNull File location) {
checkInitialized();
commitDocumentChanges();
for (GrMethodCall includeStatement : getMethodCalls(myGroovyFile, INCLUDE_METHOD)) {
for (GrLiteral lit : getLiteralArguments(includeStatement)) {
if (modulePath.equals(lit.getValue())) {
return;
}
}
}
GrMethodCall includeStatement = getMethodCall(myGroovyFile, INCLUDE_METHOD);
if (includeStatement != null) {
GrArgumentList argList = includeStatement.getArgumentList();
GrLiteral literal = GroovyPsiElementFactory.getInstance(myProject).createLiteralFromValue(modulePath);
argList.addAfter(literal, argList.getLastChild());
} else {
GrStatement statement = GroovyPsiElementFactory.getInstance(myProject).createStatementFromText(INCLUDE_METHOD + " '" + modulePath + "'");
myGroovyFile.add(statement);
}
// We get location relative to this file parent
VirtualFile parent = getFile().getParent();
File defaultLocation = GradleUtil.getModuleDefaultPath(parent, modulePath);
if (!FileUtil.filesEqual(defaultLocation, location)) {
final String path;
File parentFile = VfsUtilCore.virtualToIoFile(parent);
if (FileUtil.isAncestor(parentFile, location, true)) {
path = PathUtil.toSystemIndependentName(FileUtil.getRelativePath(parentFile, location));
} else {
path = PathUtil.toSystemIndependentName(location.getAbsolutePath());
}
String locationAssignment = String.format(CUSTOM_LOCATION_FORMAT, modulePath, path);
GrStatement locationStatement = GroovyPsiElementFactory.getInstance(myProject).createStatementFromText(locationAssignment);
myGroovyFile.add(locationStatement);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project android by JetBrains.
the class GradleDslElement method deleteIfEmpty.
protected static void deleteIfEmpty(@Nullable PsiElement element) {
if (element == null || !element.isValid()) {
return;
}
PsiElement parent = element.getParent();
if (element instanceof GrAssignmentExpression) {
if (((GrAssignmentExpression) element).getRValue() == null) {
element.delete();
}
} else if (element instanceof GrApplicationStatement) {
if (((GrApplicationStatement) element).getArgumentList() == null) {
element.delete();
}
} else if (element instanceof GrClosableBlock) {
final Boolean[] isEmpty = new Boolean[] { true };
((GrClosableBlock) element).acceptChildren(new GroovyElementVisitor() {
@Override
public void visitElement(GroovyPsiElement child) {
if (child instanceof GrParameterList) {
if (((GrParameterList) child).getParameters().length == 0) {
// Ignore the empty parameter list.
return;
}
}
isEmpty[0] = false;
}
});
if (isEmpty[0]) {
element.delete();
}
} else if (element instanceof GrMethodCallExpression) {
GrMethodCallExpression call = ((GrMethodCallExpression) element);
GrArgumentList argumentList;
try {
argumentList = call.getArgumentList();
} catch (AssertionError e) {
// We will get this exception if the argument list is already deleted.
argumentList = null;
}
GrClosableBlock[] closureArguments = call.getClosureArguments();
if ((argumentList == null || argumentList.getAllArguments().length == 0) && closureArguments.length == 0) {
element.delete();
}
} else if (element instanceof GrCommandArgumentList) {
GrCommandArgumentList commandArgumentList = (GrCommandArgumentList) element;
if (commandArgumentList.getAllArguments().length == 0) {
commandArgumentList.delete();
}
} else if (element instanceof GrListOrMap) {
GrListOrMap listOrMap = (GrListOrMap) element;
if ((listOrMap.isMap() && listOrMap.getNamedArguments().length == 0) || (!listOrMap.isMap() && listOrMap.getInitializers().length == 0)) {
listOrMap.delete();
}
}
if (!element.isValid()) {
// If this element is deleted, also delete the parent if it is empty.
deleteIfEmpty(parent);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project android by JetBrains.
the class GradleDslExpressionList method createNamedArgumentList.
@Nullable
private GroovyPsiElement createNamedArgumentList() {
assert myParent instanceof GradleDslExpressionMap;
GroovyPsiElement parentPsiElement = myParent.create();
if (parentPsiElement == null) {
return null;
}
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentPsiElement.getProject());
GrExpression expressionFromText = factory.createExpressionFromText("[]");
if (expressionFromText instanceof GrListOrMap) {
// Elements need to be added to the list before adding the list to the named argument.
GrListOrMap list = (GrListOrMap) expressionFromText;
for (GradleDslExpression expression : myToBeAddedExpressions) {
expression.setPsiElement(list);
expression.applyChanges();
myExpressions.add(expression);
}
myToBeAddedExpressions.clear();
}
GrNamedArgument namedArgument = factory.createNamedArgument(myName, expressionFromText);
PsiElement added;
if (parentPsiElement instanceof GrArgumentList) {
added = ((GrArgumentList) parentPsiElement).addNamedArgument(namedArgument);
} else {
added = parentPsiElement.addAfter(namedArgument, parentPsiElement.getLastChild());
}
if (added instanceof GrNamedArgument) {
GrNamedArgument addedNameArgument = (GrNamedArgument) added;
setPsiElement(addedNameArgument.getExpression());
return getPsiElement();
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project android by JetBrains.
the class GradleDslExpressionMap method create.
@Override
@Nullable
public GroovyPsiElement create() {
GroovyPsiElement psiElement = super.create();
if (psiElement == null) {
return null;
}
if (psiElement instanceof GrListOrMap || psiElement instanceof GrArgumentList) {
return psiElement;
}
if (psiElement instanceof GrApplicationStatement) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(psiElement.getProject());
GrArgumentList argumentList = factory.createArgumentListFromText("xyz");
// Workaround to get an empty argument list.
argumentList.getFirstChild().delete();
PsiElement added = psiElement.addAfter(argumentList, psiElement.getLastChild());
if (added instanceof GrArgumentList) {
GrArgumentList addedArgumentList = (GrArgumentList) added;
setPsiElement(addedArgumentList);
return addedArgumentList;
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.
the class GrIntroduceClosureParameterProcessor method processExternalUsage.
private static void processExternalUsage(UsageInfo usage, GrIntroduceParameterSettings settings, PsiElement expression) {
final PsiElement element = usage.getElement();
GrCall callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(element);
if (callExpression == null) {
final PsiElement parent = element.getParent();
if (parent instanceof GrReferenceExpression && element == ((GrReferenceExpression) parent).getQualifier() && "call".equals(((GrReferenceExpression) parent).getReferenceName())) {
callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(parent);
}
}
if (callExpression == null)
return;
//check for x.getFoo()(args)
if (callExpression instanceof GrMethodCall) {
final GrExpression invoked = ((GrMethodCall) callExpression).getInvokedExpression();
if (invoked instanceof GrReferenceExpression) {
final GroovyResolveResult result = ((GrReferenceExpression) invoked).advancedResolve();
final PsiElement resolved = result.getElement();
if (resolved instanceof GrAccessorMethod && !result.isInvokedOnProperty()) {
PsiElement actualCallExpression = callExpression.getParent();
if (actualCallExpression instanceof GrCall) {
callExpression = (GrCall) actualCallExpression;
}
}
}
}
GrArgumentList argList = callExpression.getArgumentList();
LOG.assertTrue(argList != null);
GrExpression[] oldArgs = argList.getExpressionArguments();
GrClosableBlock toReplaceIn = (GrClosableBlock) settings.getToReplaceIn();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(settings.getProject());
final GrExpression anchor = getAnchorForArgument(oldArgs, toReplaceIn.isVarArgs(), toReplaceIn.getParameterList());
GrClosureSignature signature = GrClosureSignatureUtil.createSignature(callExpression);
if (signature == null)
signature = GrClosureSignatureUtil.createSignature(toReplaceIn);
final GrClosureSignatureUtil.ArgInfo<PsiElement>[] actualArgs = GrClosureSignatureUtil.mapParametersToArguments(signature, callExpression.getNamedArguments(), callExpression.getExpressionArguments(), callExpression.getClosureArguments(), callExpression, true, true);
if (PsiTreeUtil.isAncestor(toReplaceIn, callExpression, false)) {
argList.addAfter(factory.createExpressionFromText(settings.getName()), anchor);
} else {
PsiElement initializer = ExpressionConverter.getExpression(expression, GroovyLanguage.INSTANCE, settings.getProject());
LOG.assertTrue(initializer instanceof GrExpression);
GrExpression newArg = GroovyIntroduceParameterUtil.addClosureToCall(initializer, argList);
if (newArg == null) {
final PsiElement dummy = argList.addAfter(factory.createExpressionFromText("1"), anchor);
newArg = ((GrExpression) dummy).replaceWithExpression((GrExpression) initializer, true);
}
new OldReferencesResolver(callExpression, newArg, toReplaceIn, settings.replaceFieldsWithGetters(), initializer, signature, actualArgs, toReplaceIn.getParameters()).resolve();
ChangeContextUtil.clearContextInfo(initializer);
//newarg can be replaced by OldReferenceResolve
if (newArg.isValid()) {
JavaCodeStyleManager.getInstance(newArg.getProject()).shortenClassReferences(newArg);
CodeStyleManager.getInstance(settings.getProject()).reformat(newArg);
}
}
if (actualArgs == null) {
GroovyIntroduceParameterUtil.removeParamsFromUnresolvedCall(callExpression, toReplaceIn.getParameters(), settings.parametersToRemove());
} else {
GroovyIntroduceParameterUtil.removeParametersFromCall(actualArgs, settings.parametersToRemove());
}
if (argList.getAllArguments().length == 0 && PsiImplUtil.hasClosureArguments(callExpression)) {
final GrArgumentList emptyArgList = ((GrMethodCallExpression) factory.createExpressionFromText("foo{}")).getArgumentList();
argList.replace(emptyArgList);
}
}
Aggregations