use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement in project intellij-community by JetBrains.
the class ApplicationStatementUtil method convertToMethodCallExpression.
public static GrExpression convertToMethodCallExpression(GrExpression expr) {
final Project project = expr.getProject();
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
boolean copied = false;
if (expr instanceof GrApplicationStatement) {
expr = convertAppInternal(factory, (GrApplicationStatement) expr);
copied = true;
}
if (expr instanceof GrReferenceExpression && ((GrReferenceExpression) expr).getDotToken() == null && ((GrReferenceExpression) expr).getQualifier() != null) {
expr = convertRefInternal(factory, ((GrReferenceExpression) expr));
copied = true;
}
if (!shouldManage(expr))
return expr;
if (!copied)
expr = (GrExpression) expr.copy();
for (PsiElement child = expr.getFirstChild(); child != null; child = child.getFirstChild()) {
if (child instanceof GrApplicationStatement) {
child = child.replace(convertAppInternal(factory, (GrApplicationStatement) child));
} else if (child instanceof GrReferenceExpression && ((GrReferenceExpression) child).getDotToken() == null && ((GrReferenceExpression) child).getQualifier() != null) {
child = child.replace(convertRefInternal(factory, ((GrReferenceExpression) child)));
}
}
return expr;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement in project intellij-community by JetBrains.
the class RemoveParenthesesFromMethodPredicate method satisfiedBy.
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof GrMethodCallExpression))
return false;
if (!PsiUtil.isExpressionStatement(element))
return false;
final GrMethodCallExpression expression = (GrMethodCallExpression) element;
if (PsiImplUtil.hasClosureArguments(expression))
return false;
final StringBuilder newStatementText = new StringBuilder();
newStatementText.append(expression.getInvokedExpression().getText()).append(' ');
GrArgumentList argumentList = expression.getArgumentList();
final GroovyPsiElement[] allArguments = argumentList.getAllArguments();
argumentList = (GrArgumentList) argumentList.copy();
final PsiElement leftParen = argumentList.getLeftParen();
final PsiElement rightParen = argumentList.getRightParen();
if (leftParen != null)
leftParen.delete();
if (rightParen != null)
rightParen.delete();
newStatementText.append(argumentList.getText());
final GrStatement newStatement;
try {
newStatement = GroovyPsiElementFactory.getInstance(element.getProject()).createStatementFromText(newStatementText.toString());
} catch (IncorrectOperationException e) {
return false;
}
if (newStatement instanceof GrApplicationStatement) {
final GrCommandArgumentList newArgList = ((GrApplicationStatement) newStatement).getArgumentList();
if (newArgList.getAllArguments().length == allArguments.length) {
return true;
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement in project intellij-community by JetBrains.
the class ImportMavenRepositoriesTask method findMavenRemoteRepositories.
@NotNull
private Collection<? extends MavenRemoteRepository> findMavenRemoteRepositories(@Nullable GrClosableBlock repositoriesBlock) {
Set<MavenRemoteRepository> myRemoteRepositories = ContainerUtil.newHashSet();
for (GrMethodCall repo : PsiTreeUtil.getChildrenOfTypeAsList(repositoriesBlock, GrMethodCall.class)) {
final String expressionText = repo.getInvokedExpression().getText();
if ("mavenCentral".equals(expressionText)) {
myRemoteRepositories.add(mavenCentralRemoteRepository);
} else if ("mavenRepo".equals(expressionText)) {
for (GrNamedArgument namedArgument : repo.getNamedArguments()) {
if ("url".equals(namedArgument.getLabelName())) {
URI urlArgumentValue = resolveUriFromSimpleExpression(namedArgument.getExpression());
if (urlArgumentValue != null) {
String textUri = urlArgumentValue.toString();
myRemoteRepositories.add(new MavenRemoteRepository(textUri, null, textUri, null, null, null));
}
break;
}
}
} else if ("maven".equals(expressionText) && repo.getClosureArguments().length > 0) {
List<GrApplicationStatement> applicationStatementList = PsiTreeUtil.getChildrenOfTypeAsList(repo.getClosureArguments()[0], GrApplicationStatement.class);
if (!applicationStatementList.isEmpty()) {
GrApplicationStatement statement = applicationStatementList.get(0);
if (statement == null)
continue;
GrExpression expression = statement.getInvokedExpression();
if ("url".equals(expression.getText())) {
URI urlArgumentValue = resolveUriFromSimpleExpression(statement.getExpressionArguments()[0]);
if (urlArgumentValue != null) {
String textUri = urlArgumentValue.toString();
myRemoteRepositories.add(new MavenRemoteRepository(textUri, null, textUri, null, null, null));
}
}
}
List<GrAssignmentExpression> assignmentExpressionList = PsiTreeUtil.getChildrenOfTypeAsList(repo.getClosureArguments()[0], GrAssignmentExpression.class);
if (!assignmentExpressionList.isEmpty()) {
GrAssignmentExpression statement = assignmentExpressionList.get(0);
if (statement == null)
continue;
GrExpression expression = statement.getLValue();
if ("url".equals(expression.getText())) {
URI urlArgumentValue = resolveUriFromSimpleExpression(statement.getRValue());
if (urlArgumentValue != null) {
String textUri = urlArgumentValue.toString();
myRemoteRepositories.add(new MavenRemoteRepository(textUri, null, textUri, null, null, null));
}
}
}
}
}
return myRemoteRepositories;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement in project android by JetBrains.
the class GradleEditorModelParserFacade method fillContext.
/**
* Processes given PSI file and fills given context
* by {@link GradleEditorModelParseContext#getAssignments(Variable) corresponding assignments}.
*
* @param context context to fill
* @param psiFile psi file to parse
*/
private static void fillContext(@NotNull final GradleEditorModelParseContext context, @NotNull PsiFile psiFile) {
psiFile.acceptChildren(new GroovyPsiElementVisitor(new GroovyRecursiveElementVisitor() {
@Override
public void visitMethodCallExpression(GrMethodCallExpression methodCallExpression) {
Pair<String, TextRange> pair = GradleEditorValueExtractor.extractMethodName(methodCallExpression);
GrClosableBlock[] closureArguments = methodCallExpression.getClosureArguments();
if (pair == null || closureArguments.length > 1) {
super.visitMethodCallExpression(methodCallExpression);
return;
}
if (closureArguments.length == 0) {
if (methodCallExpression.getArgumentList().getAllArguments().length == 0) {
// This is a no-args method, so, we just register it for cases like 'mavenCentral()' or 'jcenter()'.
context.addCachedValue(NO_ARGS_METHOD_ASSIGNMENT_VALUE, TextRange.create(pair.second.getEndOffset(), methodCallExpression.getTextRange().getEndOffset()));
context.registerAssignmentFromCachedData(pair.first, pair.second, methodCallExpression);
}
return;
}
context.onMethodEnter(pair.getFirst());
try {
super.visitClosure(closureArguments[0]);
} finally {
context.onMethodExit();
}
}
@Override
public void visitApplicationStatement(GrApplicationStatement applicationStatement) {
Pair<String, TextRange> methodName = GradleEditorValueExtractor.extractMethodName(applicationStatement);
if (methodName == null) {
return;
}
GroovyPsiElement[] allArguments = applicationStatement.getArgumentList().getAllArguments();
if (allArguments.length == 1) {
context.resetCaches();
extractValueOrVariable(allArguments[0], context);
context.registerAssignmentFromCachedData(methodName.getFirst(), methodName.getSecond(), applicationStatement.getArgumentList());
}
}
@Override
public void visitAssignmentExpression(GrAssignmentExpression expression) {
// General idea is to try to extract variable from the given expression and, in case of success, try to extract rvalue and
// register corresponding assignment with them.
context.resetCaches();
extractValueOrVariable(expression.getLValue(), context);
Multimap<Variable, Location> vars = context.getCachedVariables();
if (vars.size() != 1) {
context.resetCaches();
return;
}
Map.Entry<Variable, Location> entry = vars.entries().iterator().next();
Variable lVariable = entry.getKey();
Location lVariableLocation = entry.getValue();
context.resetCaches();
GrExpression rValue = expression.getRValue();
if (rValue == null) {
return;
}
extractValueOrVariable(rValue, context);
if (context.getCachedValues().size() > 1) {
Value value = new Value("", new Location(context.getCurrentFile(), GradleEditorModelUtil.interestedRange(rValue)));
context.setCachedValues(Collections.singletonList(value));
}
context.registerAssignmentFromCachedData(lVariable, lVariableLocation, rValue);
context.resetCaches();
}
@Override
public void visitVariable(GrVariable variable) {
TextRange nameRange = null;
boolean lookForInitializer = false;
ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.findSingle(GroovyLanguage.INSTANCE);
for (PsiElement e = variable.getFirstChild(); e != null; e = e.getNextSibling()) {
ASTNode node = e.getNode();
if (node == null) {
continue;
}
if (!lookForInitializer) {
if (node.getElementType() == GroovyTokenTypes.mIDENT) {
nameRange = e.getTextRange();
} else if (node.getElementType() == GroovyTokenTypes.mASSIGN) {
if (nameRange == null) {
return;
}
lookForInitializer = true;
}
continue;
}
if (node.getElementType() == GroovyTokenTypes.mNLS || node.getElementType() == GroovyTokenTypes.mSEMI) {
break;
}
if (parserDefinition.getWhitespaceTokens().contains(node.getElementType())) {
continue;
}
extractValueOrVariable(e, context);
if (context.getCachedValues().size() > 1) {
Value value = new Value("", new Location(context.getCurrentFile(), GradleEditorModelUtil.interestedRange(e)));
context.setCachedValues(Collections.singletonList(value));
}
if (context.registerAssignmentFromCachedData(variable.getName(), nameRange, e)) {
return;
}
}
}
}));
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement 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);
}
}
Aggregations