use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement in project intellij-community by JetBrains.
the class JavaStylePropertiesInvocationInspection method buildVisitor.
@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
return new BaseInspectionVisitor() {
@Override
public void visitMethodCallExpression(@NotNull GrMethodCallExpression methodCallExpression) {
super.visitMethodCallExpression(methodCallExpression);
visitMethodCall(methodCallExpression);
}
@Override
public void visitApplicationStatement(@NotNull GrApplicationStatement applicationStatement) {
super.visitApplicationStatement(applicationStatement);
visitMethodCall(applicationStatement);
}
private void visitMethodCall(GrMethodCall methodCall) {
if (JavaStylePropertiesUtil.isPropertyAccessor(methodCall)) {
final String message = GroovyInspectionBundle.message("java.style.property.access");
final GrExpression expression = methodCall.getInvokedExpression();
if (expression instanceof GrReferenceExpression) {
PsiElement referenceNameElement = ((GrReferenceExpression) expression).getReferenceNameElement();
registerError(referenceNameElement, message, myFixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
}
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement in project android by JetBrains.
the class AndroidGradleSpellcheckingStrategy method isPrint.
private static boolean isPrint(PsiElement element) {
PsiElement parent0 = element.getParent();
if (parent0 == null) {
return false;
}
PsiElement parent1 = parent0.getParent();
if (parent1 == null) {
return false;
}
PsiElement parent2 = parent1.getParent();
if (parent2 == null) {
return false;
}
if (parent2 instanceof GrCommandArgumentList) {
parent2 = parent2.getParent();
}
if (parent2 instanceof GrApplicationStatement) {
GrApplicationStatement call = (GrApplicationStatement) parent2;
GrExpression propertyExpression = call.getInvokedExpression();
if (propertyExpression instanceof GrReferenceExpression) {
GrReferenceExpression propertyRef = (GrReferenceExpression) propertyExpression;
String property = propertyRef.getReferenceName();
if ("print".equals(property) || "println".equals(property)) {
return true;
}
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement in project android by JetBrains.
the class GradleDslElement method create.
/**
* Creates the {@link GroovyPsiElement} by adding this element to the .gradle file.
*
* <p>It creates a new {@link GroovyPsiElement} only when {@link #getPsiElement()} return {@code null}.
*
* <p>Returns the final {@link GroovyPsiElement} corresponds to this element or {@code null} when failed to create the
* {@link GroovyPsiElement}.
*/
@Nullable
public GroovyPsiElement create() {
GroovyPsiElement psiElement = getPsiElement();
if (psiElement != null) {
return psiElement;
}
if (myParent == null) {
return null;
}
GroovyPsiElement parentPsiElement = myParent.create();
if (parentPsiElement == null) {
return null;
}
Project project = parentPsiElement.getProject();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
if (isNewEmptyBlockElement()) {
// Avoid creation of an empty block statement.
return null;
}
String statementText = myName + (isBlockElement() ? " {\n}\n" : " \"abc\", \"xyz\"");
GrStatement statement = factory.createStatementFromText(statementText);
if (statement instanceof GrApplicationStatement) {
// Workaround to create an application statement.
((GrApplicationStatement) statement).getArgumentList().delete();
}
PsiElement lineTerminator = factory.createLineTerminator(1);
PsiElement addedElement;
if (parentPsiElement instanceof GroovyFile) {
addedElement = parentPsiElement.addAfter(statement, parentPsiElement.getLastChild());
parentPsiElement.addBefore(lineTerminator, addedElement);
} else {
addedElement = parentPsiElement.addBefore(statement, parentPsiElement.getLastChild());
parentPsiElement.addAfter(lineTerminator, addedElement);
}
if (isBlockElement()) {
GrClosableBlock closableBlock = getClosableBlock(addedElement);
if (closableBlock != null) {
setPsiElement(closableBlock);
}
} else {
if (addedElement instanceof GrApplicationStatement) {
setPsiElement((GrApplicationStatement) addedElement);
}
}
return getPsiElement();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement in project android by JetBrains.
the class GradleDslExpressionList method create.
@Override
@Nullable
public GroovyPsiElement create() {
GroovyPsiElement psiElement = getPsiElement();
if (psiElement == null) {
if (myParent instanceof GradleDslExpressionMap) {
// This is a list in the map element and we need to create a named argument for it.
return createNamedArgumentList();
}
psiElement = super.create();
}
if (psiElement == null) {
return null;
}
if (psiElement instanceof GrListOrMap) {
return psiElement;
}
if (psiElement instanceof GrArgumentList) {
if (!myToBeAddedExpressions.isEmpty() && ((GrArgumentList) psiElement).getAllArguments().length == 1 && !myAppendToArgumentListWithOneElement) {
// Sometimes it's not possible to append to the arguments list with one item. eg. proguardFile "xyz".
// Set the psiElement to null and create a new psiElement of an empty application statement.
setPsiElement(null);
psiElement = super.create();
} else {
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.expressions.GrApplicationStatement in project android by JetBrains.
the class GradleDslMethodCall method create.
@Override
@Nullable
public GroovyPsiElement create() {
GroovyPsiElement psiElement = getPsiElement();
if (psiElement != null) {
return psiElement;
}
if (myParent == null) {
return null;
}
GroovyPsiElement parentPsiElement = myParent.create();
if (parentPsiElement == null) {
return null;
}
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentPsiElement.getProject());
String statementText = (myStatementName != null ? myStatementName + " " : "") + myName + "()";
GrStatement statement = factory.createStatementFromText(statementText);
PsiElement addedElement = parentPsiElement.addBefore(statement, parentPsiElement.getLastChild());
if (addedElement instanceof GrApplicationStatement) {
GrExpression[] expressionArguments = ((GrApplicationStatement) addedElement).getArgumentList().getExpressionArguments();
if (expressionArguments.length == 1 && expressionArguments[0] instanceof GrMethodCallExpression) {
setPsiElement(expressionArguments[0]);
return getPsiElement();
}
}
if (addedElement instanceof GrMethodCallExpression) {
setPsiElement((GrMethodCallExpression) addedElement);
return getPsiElement();
}
return null;
}
Aggregations