use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition in project intellij-community by JetBrains.
the class GroovyDirectInheritorsSearcher method getDerivingClassCandidates.
@NotNull
private static List<PsiClass> getDerivingClassCandidates(PsiClass clazz, GlobalSearchScope scope, boolean includeAnonymous) {
final String name = clazz.getName();
if (name == null)
return Collections.emptyList();
final ArrayList<PsiClass> inheritors = new ArrayList<>();
for (GrReferenceList list : StubIndex.getElements(GrDirectInheritorsIndex.KEY, name, clazz.getProject(), scope, GrReferenceList.class)) {
final PsiElement parent = list.getParent();
if (parent instanceof GrTypeDefinition) {
inheritors.add((PsiClass) parent);
}
}
if (includeAnonymous) {
final Collection<GrAnonymousClassDefinition> classes = StubIndex.getElements(GrAnonymousClassIndex.KEY, name, clazz.getProject(), scope, GrAnonymousClassDefinition.class);
for (GrAnonymousClassDefinition aClass : classes) {
inheritors.add(aClass);
}
}
return inheritors;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition in project intellij-community by JetBrains.
the class MoveClassToNewFileIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrTypeDefinition psiClass = (GrTypeDefinition) element.getParent();
final String name = psiClass.getName();
final PsiFile file = psiClass.getContainingFile();
final String fileExtension = FileUtilRt.getExtension(file.getName());
final String newFileName = name + "." + fileExtension;
final PsiDirectory dir = file.getParent();
if (dir != null) {
if (dir.findFile(newFileName) != null) {
if (!ApplicationManager.getApplication().isUnitTestMode()) {
final String message = GroovyIntentionsBundle.message("file.exists", newFileName, dir.getName());
CommonRefactoringUtil.showErrorHint(project, editor, message, getFamilyName(), null);
}
return;
}
}
final GroovyFile newFile = (GroovyFile) GroovyTemplatesFactory.createFromTemplate(dir, name, newFileName, GroovyTemplates.GROOVY_CLASS, true);
final GrTypeDefinition template = newFile.getTypeDefinitions()[0];
final PsiElement newClass = template.replace(psiClass);
final GrDocComment docComment = psiClass.getDocComment();
if (newClass instanceof GrTypeDefinition && docComment != null) {
final GrDocComment newDoc = ((GrTypeDefinition) newClass).getDocComment();
if (newDoc != null) {
newDoc.replace(docComment);
} else {
final PsiElement parent = newClass.getParent();
parent.addBefore(docComment, psiClass);
parent.getNode().addLeaf(GroovyTokenTypes.mNLS, "\n", psiClass.getNode());
}
docComment.delete();
}
psiClass.delete();
IntentionUtils.positionCursor(project, newClass.getContainingFile(), newClass.getNavigationElement());
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition in project intellij-community by JetBrains.
the class MyPredicate method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull final Project project, Editor editor) throws IncorrectOperationException {
final GrListOrMap map = (GrListOrMap) element;
final GrNamedArgument[] namedArguments = map.getNamedArguments();
LOG.assertTrue(map.getInitializers().length == 0);
final PsiFile file = map.getContainingFile();
final String packageName = file instanceof GroovyFileBase ? ((GroovyFileBase) file).getPackageName() : "";
final CreateClassDialog dialog = new CreateClassDialog(project, GroovyBundle.message("create.class.family.name"), "", packageName, GrCreateClassKind.CLASS, true, ModuleUtilCore.findModuleForPsiElement(element));
dialog.show();
if (dialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
return;
boolean replaceReturnType = checkForReturnFromMethod(map);
boolean variableDeclaration = checkForVariableDeclaration(map);
final GrParameter methodParameter = checkForMethodParameter(map);
final String qualifiedClassName = dialog.getClassName();
final String selectedPackageName = StringUtil.getPackageName(qualifiedClassName);
final String shortName = StringUtil.getShortName(qualifiedClassName);
final GrTypeDefinition typeDefinition = createClass(project, namedArguments, selectedPackageName, shortName);
final PsiClass generatedClass = CreateClassActionBase.createClassByType(dialog.getTargetDirectory(), typeDefinition.getName(), PsiManager.getInstance(project), map, GroovyTemplates.GROOVY_CLASS, true);
final PsiClass replaced = (PsiClass) generatedClass.replace(typeDefinition);
replaceMapWithClass(project, map, replaced, replaceReturnType, variableDeclaration, methodParameter);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition in project intellij-community by JetBrains.
the class ConvertMapToClassTest method doTest.
protected void doTest(boolean exists) {
myFixture.configureByFile(getTestName(true) + "/Test.groovy");
String hint = GroovyIntentionsBundle.message("convert.map.to.class.intention.name");
final List<IntentionAction> list = myFixture.filterAvailableIntentions(hint);
if (!exists) {
assertEmpty(list);
return;
}
assertOneElement(list);
final PsiElement element = myFixture.getFile().findElementAt(myFixture.getEditor().getCaretModel().getOffset());
final GrListOrMap map = PsiTreeUtil.getParentOfType(element, GrListOrMap.class);
assertNotNull(map);
final GrTypeDefinition foo = ConvertMapToClassIntention.createClass(getProject(), map.getNamedArguments(), "", "Foo");
myFixture.addFileToProject(getTestName(true) + "/Foo.groovy", foo.getContainingFile().getText());
final PsiClass psiClass = myFixture.findClass("Foo");
WriteCommandAction.runWriteCommandAction(null, () -> ConvertMapToClassIntention.replaceMapWithClass(getProject(), map, psiClass, ConvertMapToClassIntention.checkForReturnFromMethod(map), ConvertMapToClassIntention.checkForVariableDeclaration(map), ConvertMapToClassIntention.checkForMethodParameter(map)));
myFixture.checkResultByFile(getTestName(true) + "/Foo.groovy", getTestName(true) + "/Expected.groovy", true);
myFixture.checkResultByFile(getTestName(true) + "/Test_after.groovy", true);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition in project intellij-community by JetBrains.
the class ExternalBuilderStrategySupport method applyTransformation.
@Override
public void applyTransformation(@NotNull TransformationContext context) {
PsiAnnotation annotation = PsiImplUtil.getAnnotation(context.getCodeClass(), BUILDER_FQN);
if (!isApplicable(annotation, EXTERNAL_STRATEGY_NAME))
return;
final PsiClass constructedClass = GrAnnotationUtil.inferClassAttribute(annotation, "forClass");
if (constructedClass == null || "groovy.transform.Undefined.CLASS".equals(constructedClass.getQualifiedName()))
return;
if (constructedClass instanceof GrTypeDefinition) {
for (GrField field : ((GrTypeDefinition) constructedClass).getCodeFields()) {
context.addMethod(DefaultBuilderStrategySupport.createFieldSetter(context.getCodeClass(), field, annotation));
}
} else {
for (PsiMethod setter : PropertyUtil.getAllProperties(constructedClass, true, false).values()) {
final PsiMethod builderSetter = createFieldSetter(context.getCodeClass(), setter, annotation);
if (builderSetter != null)
context.addMethod(builderSetter);
}
}
context.addMethod(createBuildMethod(annotation, createType(constructedClass)));
}
Aggregations