use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GrAnnotationImpl method getQualifiedName.
@Override
@Nullable
@NonNls
public String getQualifiedName() {
final GrAnnotationStub stub = getStub();
if (stub != null) {
return stub.getPsiElement().getQualifiedName();
}
final GrCodeReferenceElement nameRef = getClassReference();
final PsiElement resolved = nameRef.resolve();
if (resolved instanceof PsiClass)
return ((PsiClass) resolved).getQualifiedName();
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GroovyPsiElementFactoryImpl method createCodeReferenceElementFromText.
@NotNull
@Override
public GrCodeReferenceElement createCodeReferenceElementFromText(@NotNull String text) {
GroovyFile file = createGroovyFileChecked("class X extends " + text + "{}");
PsiClass[] classes = file.getClasses();
if (classes.length != 1)
throw new IncorrectOperationException("cannot create code reference element for class" + text);
GrExtendsClause extendsClause = ((GrTypeDefinition) classes[0]).getExtendsClause();
if (extendsClause == null)
throw new IncorrectOperationException("cannot create code reference element for class" + text);
GrCodeReferenceElement[] refElements = extendsClause.getReferenceElementsGroovy();
if (refElements.length != 1)
throw new IncorrectOperationException("cannot create code reference element for class" + text);
return refElements[0];
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GrAnonymousClassType method getJavaClassName.
@NotNull
@Override
protected String getJavaClassName() {
final GrCodeReferenceElement ref = myAnonymous.getBaseClassReferenceGroovy();
final PsiElement resolved = ref.resolve();
if (resolved instanceof PsiClass) {
String qname = ((PsiClass) resolved).getQualifiedName();
return qname != null ? qname : ((PsiClass) resolved).getName();
} else {
return ref.getClassNameText();
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement 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.GrCodeReferenceElement in project intellij-community by JetBrains.
the class AnnotationAttributeCompletionResultProcessor method process.
public void process(@NotNull Consumer<LookupElement> consumer, @NotNull PrefixMatcher matcher) {
GrCodeReferenceElement ref = myAnnotation.getClassReference();
PsiElement resolved = ref.resolve();
if (resolved instanceof PsiClass) {
final PsiAnnotation annotationCollector = GrAnnotationCollector.findAnnotationCollector((PsiClass) resolved);
if (annotationCollector != null) {
final ArrayList<GrAnnotation> annotations = ContainerUtil.newArrayList();
GrAnnotationCollector.collectAnnotations(annotations, myAnnotation, annotationCollector);
Set<String> usedNames = ContainerUtil.newHashSet();
for (GrAnnotation annotation : annotations) {
final PsiElement resolvedAliased = annotation.getClassReference().resolve();
if (resolvedAliased instanceof PsiClass && ((PsiClass) resolvedAliased).isAnnotationType()) {
for (PsiMethod method : ((PsiClass) resolvedAliased).getMethods()) {
if (usedNames.add(method.getName())) {
for (LookupElement element : GroovyCompletionUtil.createLookupElements(new GroovyResolveResultImpl(method, true), false, matcher, null)) {
consumer.consume(element);
}
}
}
}
}
} else if (((PsiClass) resolved).isAnnotationType()) {
for (PsiMethod method : ((PsiClass) resolved).getMethods()) {
for (LookupElement element : GroovyCompletionUtil.createLookupElements(new GroovyResolveResultImpl(method, true), false, matcher, null)) {
consumer.consume(element);
}
}
}
}
}
Aggregations