use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList in project intellij-community by JetBrains.
the class GrReferenceElementImpl method getTypeArguments.
@Override
@NotNull
public PsiType[] getTypeArguments() {
final GrTypeArgumentList typeArgsList = getTypeArgumentList();
if (typeArgsList == null)
return PsiType.EMPTY_ARRAY;
final GrTypeElement[] args = typeArgsList.getTypeArgumentElements();
if (args.length == 0)
return PsiType.EMPTY_ARRAY;
PsiType[] result = PsiType.createArray(args.length);
for (int i = 0; i < result.length; i++) {
result[i] = args[i].getType();
}
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList in project intellij-community by JetBrains.
the class GrCreateSubclassAction method startTemplate.
private static void startTemplate(GrTypeParameterList oldTypeParameterList, final Project project, final GrTypeDefinition psiClass, final GrTypeDefinition targetClass, boolean includeClassName) {
PsiElementFactory jfactory = JavaPsiFacade.getElementFactory(project);
final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(project);
GrCodeReferenceElement stubRef = elementFactory.createCodeReferenceElementFromClass(psiClass);
try {
GrReferenceList clause = psiClass.isInterface() ? targetClass.getImplementsClause() : targetClass.getExtendsClause();
if (clause == null) {
GrReferenceList stubRefList = psiClass.isInterface() ? elementFactory.createImplementsClause() : elementFactory.createExtendsClause();
clause = (GrExtendsClause) targetClass.addAfter(stubRefList, targetClass.getNameIdentifierGroovy());
}
GrCodeReferenceElement ref = (GrCodeReferenceElement) clause.add(stubRef);
if (psiClass.hasTypeParameters() || includeClassName) {
final Editor editor = CodeInsightUtil.positionCursorAtLBrace(project, targetClass.getContainingFile(), targetClass);
final TemplateBuilderImpl templateBuilder = editor == null || ApplicationManager.getApplication().isUnitTestMode() ? null : (TemplateBuilderImpl) TemplateBuilderFactory.getInstance().createTemplateBuilder(targetClass);
if (includeClassName && templateBuilder != null) {
templateBuilder.replaceElement(targetClass.getNameIdentifier(), targetClass.getName());
}
if (oldTypeParameterList != null && oldTypeParameterList.getTypeParameters().length > 0) {
GrTypeArgumentList existingList = ref.getTypeArgumentList();
final GrTypeParameterList typeParameterList = (GrTypeParameterList) targetClass.addAfter(elementFactory.createTypeParameterList(), targetClass.getNameIdentifierGroovy());
GrTypeArgumentList argList;
if (existingList == null) {
GrCodeReferenceElement codeRef = elementFactory.createCodeReferenceElementFromText("A<T>");
argList = ((GrTypeArgumentList) ref.add(codeRef.getTypeArgumentList()));
argList.getTypeArgumentElements()[0].delete();
} else {
argList = existingList;
}
for (PsiTypeParameter parameter : oldTypeParameterList.getTypeParameters()) {
final PsiElement param = argList.add(elementFactory.createTypeElement(jfactory.createType(parameter)));
if (templateBuilder != null) {
templateBuilder.replaceElement(param, param.getText());
}
typeParameterList.add(elementFactory.createTypeParameter(parameter.getName(), parameter.getExtendsListTypes()));
}
}
if (templateBuilder != null) {
templateBuilder.setEndVariableBefore(ref);
final Template template = templateBuilder.buildTemplate();
template.addEndVariable();
final PsiFile containingFile = targetClass.getContainingFile();
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
final TextRange textRange = targetClass.getTextRange();
final RangeMarker startClassOffset = editor.getDocument().createRangeMarker(textRange.getStartOffset(), textRange.getEndOffset());
startClassOffset.setGreedyToLeft(true);
startClassOffset.setGreedyToRight(true);
editor.getDocument().deleteString(textRange.getStartOffset(), textRange.getEndOffset());
CreateFromUsageBaseFix.startTemplate(editor, template, project, new TemplateEditingAdapter() {
@Override
public void templateFinished(Template template, boolean brokenOff) {
chooseAndImplement(psiClass, project, PsiTreeUtil.getParentOfType(containingFile.findElementAt(startClassOffset.getStartOffset()), GrTypeDefinition.class), editor);
}
}, getTitle(psiClass));
}
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList in project intellij-community by JetBrains.
the class ImportStaticIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull final Project project, final Editor editor) throws IncorrectOperationException {
final PsiElement resolved = resolve(element);
if (!(resolved instanceof PsiMember))
return;
final PsiClass containingClass = ((PsiMember) resolved).getContainingClass();
if (containingClass == null)
return;
String originalName = ((PsiMember) resolved).getName();
final String name = resolved instanceof PsiMethod && GroovyPropertyUtils.isSimplePropertyAccessor((PsiMethod) resolved) ? GroovyPropertyUtils.getPropertyName((PsiMethod) resolved) : originalName;
final String qname = containingClass.getQualifiedName();
if (name == null)
return;
final PsiFile containingFile = element.getContainingFile();
if (!(containingFile instanceof GroovyFile))
return;
final GroovyFile file = (GroovyFile) containingFile;
file.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression expression) {
super.visitReferenceExpression(expression);
if (name.equals(expression.getReferenceName())) {
PsiElement resolved = expression.resolve();
if (resolved != null) {
expression.putUserData(TEMP_REFERENT_USER_DATA, resolved);
}
}
}
});
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
final GrImportStatement tempImport = factory.createImportStatementFromText(qname + "." + name, true, false, null);
final GrImportStatement importStatement = file.addImport(tempImport);
boolean isAnythingShortened = shortenUsages(resolved, containingFile);
if (!isAnythingShortened) {
importStatement.delete();
return;
}
file.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression expression) {
super.visitReferenceExpression(expression);
GrTypeArgumentList typeArgumentList = expression.getTypeArgumentList();
if (typeArgumentList != null && typeArgumentList.getFirstChild() != null) {
expression.putUserData(TEMP_REFERENT_USER_DATA, null);
return;
}
if (name.equals(expression.getReferenceName())) {
if (expression.isQualified()) {
GrExpression qualifier = expression.getQualifierExpression();
if (qualifier instanceof GrReferenceExpression) {
PsiElement aClass = ((GrReferenceExpression) qualifier).resolve();
if (aClass == ((PsiMember) resolved).getContainingClass()) {
GrReferenceAdjuster.shortenReference(expression);
}
}
} else {
PsiElement referent = expression.getUserData(TEMP_REFERENT_USER_DATA);
if (referent instanceof PsiMember && ((PsiMember) referent).hasModifierProperty(PsiModifier.STATIC) && referent != expression.resolve()) {
expression.bindToElement(referent);
}
}
}
expression.putUserData(TEMP_REFERENT_USER_DATA, null);
}
});
}
Aggregations