use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterList in project intellij-community by JetBrains.
the class GrCreateSubclassAction method createSubclassGroovy.
@Nullable
public static PsiClass createSubclassGroovy(final GrTypeDefinition psiClass, final PsiDirectory targetDirectory, final String className) {
final Project project = psiClass.getProject();
final Ref<GrTypeDefinition> targetClass = new Ref<>();
new WriteCommandAction(project, getTitle(psiClass), getTitle(psiClass)) {
@Override
protected void run(@NotNull Result result) throws Throwable {
IdeDocumentHistory.getInstance(project).includeCurrentPlaceAsChangePlace();
final GrTypeParameterList oldTypeParameterList = psiClass.getTypeParameterList();
try {
targetClass.set(CreateClassActionBase.createClassByType(targetDirectory, className, PsiManager.getInstance(project), psiClass, GroovyTemplates.GROOVY_CLASS, true));
} catch (final IncorrectOperationException e) {
ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog(project, CodeInsightBundle.message("intention.error.cannot.create.class.message", className) + "\n" + e.getLocalizedMessage(), CodeInsightBundle.message("intention.error.cannot.create.class.title")));
return;
}
startTemplate(oldTypeParameterList, project, psiClass, targetClass.get(), false);
}
}.execute();
if (targetClass.get() == null)
return null;
if (!ApplicationManager.getApplication().isUnitTestMode() && !psiClass.hasTypeParameters()) {
final Editor editor = CodeInsightUtil.positionCursorAtLBrace(project, targetClass.get().getContainingFile(), targetClass.get());
if (editor == null)
return targetClass.get();
chooseAndImplement(psiClass, project, targetClass.get(), editor);
}
return targetClass.get();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterList in project intellij-community by JetBrains.
the class GrMethodBaseImpl method setReturnType.
@Override
@Nullable
public GrTypeElement setReturnType(@Nullable PsiType newReturnType) {
GrTypeElement typeElement = getReturnTypeElementGroovy();
if (newReturnType == null || newReturnType == PsiType.NULL) {
if (typeElement != null)
typeElement.delete();
insertPlaceHolderToModifierList();
return null;
}
final GrTypeElement stub = GroovyPsiElementFactory.getInstance(getProject()).createTypeElement(newReturnType);
GrTypeElement newTypeElement;
if (typeElement == null) {
final GrTypeParameterList typeParemeterList = getTypeParameterList();
PsiElement anchor = typeParemeterList != null ? typeParemeterList : getModifierList();
newTypeElement = (GrTypeElement) addAfter(stub, anchor);
} else {
newTypeElement = (GrTypeElement) typeElement.replace(stub);
}
return newTypeElement;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterList 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);
}
}
Aggregations