use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class GrClosableBlockImpl method getOwner.
private PsiVariable getOwner() {
return CachedValuesManager.getCachedValue(this, () -> {
final GroovyPsiElement context = PsiTreeUtil.getParentOfType(this, GrTypeDefinition.class, GrClosableBlock.class, GroovyFile.class);
final PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
PsiType type = null;
if (context instanceof GrTypeDefinition) {
type = factory.createType((PsiClass) context);
} else if (context instanceof GrClosableBlock) {
type = GrClosureType.create((GrClosableBlock) context, true);
} else if (context instanceof GroovyFile) {
final PsiClass scriptClass = ((GroovyFile) context).getScriptClass();
if (scriptClass != null && GroovyNamesUtil.isIdentifier(scriptClass.getName()))
type = factory.createType(scriptClass);
}
if (type == null) {
type = TypesUtil.getJavaLangObject(this);
}
PsiVariable owner = new GrLightVariable(getManager(), OWNER_NAME, type, this);
return CachedValueProvider.Result.create(owner, PsiModificationTracker.MODIFICATION_COUNT);
});
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class ResolveUtil method getCallVariants.
public static GroovyResolveResult[] getCallVariants(GroovyPsiElement place) {
final PsiElement parent = place.getParent();
GroovyResolveResult[] variants = GroovyResolveResult.EMPTY_ARRAY;
if (parent instanceof GrCallExpression) {
variants = ((GrCallExpression) parent).getCallVariants(place instanceof GrExpression ? (GrExpression) place : null);
} else if (parent instanceof GrConstructorInvocation) {
final PsiClass clazz = ((GrConstructorInvocation) parent).getDelegatedClass();
if (clazz != null) {
final PsiMethod[] constructors = clazz.getConstructors();
variants = getConstructorResolveResult(constructors, place);
}
} else if (parent instanceof GrAnonymousClassDefinition) {
final PsiElement element = ((GrAnonymousClassDefinition) parent).getBaseClassReferenceGroovy().resolve();
if (element instanceof PsiClass) {
final PsiMethod[] constructors = ((PsiClass) element).getConstructors();
variants = getConstructorResolveResult(constructors, place);
}
} else if (place instanceof GrReferenceExpression) {
variants = ((GrReferenceExpression) place).getSameNameVariants();
}
return variants;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project android by JetBrains.
the class GradleDslElement method delete.
/**
* Deletes this element and all it's children from the .gradle file.
*/
protected void delete() {
for (GradleDslElement element : getChildren()) {
element.delete();
}
GroovyPsiElement psiElement = getPsiElement();
if (psiElement == null || !psiElement.isValid()) {
return;
}
PsiElement parent = psiElement.getParent();
psiElement.delete();
if (parent != null) {
deleteIfEmpty(parent);
}
setPsiElement(null);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement 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.GroovyPsiElement 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;
}
Aggregations