use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyShellLanguageConsoleView method generateClosure.
@NotNull
private GrClosableBlock generateClosure(@NotNull GrMethod method) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
StringBuilder buffer = new StringBuilder();
buffer.append('{');
GrParameter[] parameters = method.getParameters();
for (GrParameter parameter : parameters) {
buffer.append(parameter.getText());
buffer.append(',');
}
if (parameters.length > 0)
buffer.delete(buffer.length() - 1, buffer.length());
buffer.append("->}");
return factory.createClosureFromText(buffer.toString(), getFile());
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrPullUpHelper method addClassToSupers.
private void addClassToSupers(MemberInfo info, PsiClass aClass, PsiSubstitutor substitutor, GrTypeDefinition targetSuperClass) {
final PsiReferenceList sourceReferenceList = info.getSourceReferenceList();
LOG.assertTrue(sourceReferenceList != null);
PsiQualifiedReferenceElement ref = mySourceClass.equals(sourceReferenceList.getParent()) ? removeFromReferenceList(sourceReferenceList, aClass) : findReferenceToClass(sourceReferenceList, aClass);
if (ref != null && !targetSuperClass.isInheritor(aClass, false)) {
GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(myProject);
replaceMovedMemberTypeParameters(ref, PsiUtil.typeParametersIterable(mySourceClass), substitutor, elementFactory);
GrReferenceList referenceList;
if (targetSuperClass.isInterface()) {
referenceList = targetSuperClass.getExtendsClause();
if (referenceList == null) {
GrExtendsClause newClause = GroovyPsiElementFactory.getInstance(myProject).createExtendsClause();
PsiElement anchor = targetSuperClass.getTypeParameterList() != null ? targetSuperClass.getTypeParameterList() : targetSuperClass.getNameIdentifierGroovy();
referenceList = (GrReferenceList) targetSuperClass.addAfter(newClause, anchor);
addSpacesAround(referenceList);
}
} else {
referenceList = targetSuperClass.getImplementsClause();
if (referenceList == null) {
GrImplementsClause newClause = GroovyPsiElementFactory.getInstance(myProject).createImplementsClause();
PsiElement anchor = targetSuperClass.getExtendsClause() != null ? targetSuperClass.getExtendsClause() : targetSuperClass.getTypeParameterList() != null ? targetSuperClass.getTypeParameterList() : targetSuperClass.getNameIdentifierGroovy();
referenceList = (GrReferenceList) targetSuperClass.addAfter(newClause, anchor);
addSpacesAround(referenceList);
}
}
assert referenceList != null;
referenceList.add(ref);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrPullUpHelper method fixReferencesToStatic.
private void fixReferencesToStatic(GroovyPsiElement classMember) throws IncorrectOperationException {
final StaticReferencesCollector collector = new StaticReferencesCollector(myMembersToMove);
classMember.accept(collector);
ArrayList<GrReferenceElement> refs = collector.getReferences();
ArrayList<PsiElement> members = collector.getReferees();
ArrayList<PsiClass> classes = collector.getRefereeClasses();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
for (int i = 0; i < refs.size(); i++) {
GrReferenceElement ref = refs.get(i);
PsiElement namedElement = members.get(i);
PsiClass aClass = classes.get(i);
if (namedElement instanceof PsiNamedElement) {
GrReferenceExpression newRef = (GrReferenceExpression) factory.createExpressionFromText("a." + ((PsiNamedElement) namedElement).getName(), null);
GrExpression qualifier = newRef.getQualifierExpression();
assert qualifier != null;
qualifier = (GrExpression) qualifier.replace(factory.createReferenceExpressionFromText(aClass.getQualifiedName()));
qualifier.putCopyableUserData(PRESERVE_QUALIFIER, ref.isQualified());
PsiElement replaced = ref.replace(newRef);
JavaCodeStyleManager.getInstance(myProject).shortenClassReferences(replaced);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrPullUpHelper method doMoveMethod.
private void doMoveMethod(PsiSubstitutor substitutor, MemberInfo info) {
GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(myProject);
GrMethod method = (GrMethod) info.getMember();
PsiMethod sibling = method;
PsiMethod anchor = null;
while (sibling != null) {
sibling = PsiTreeUtil.getNextSiblingOfType(sibling, PsiMethod.class);
if (sibling != null) {
anchor = MethodSignatureUtil.findMethodInSuperClassBySignatureInDerived(method.getContainingClass(), myTargetSuperClass, sibling.getSignature(PsiSubstitutor.EMPTY), false);
if (anchor != null) {
break;
}
}
}
GrMethod methodCopy = (GrMethod) method.copy();
if (method.findSuperMethods(myTargetSuperClass).length == 0) {
deleteOverrideAnnotationIfFound(methodCopy);
}
final boolean isOriginalMethodAbstract = method.hasModifierProperty(PsiModifier.ABSTRACT) || method.hasModifierProperty(PsiModifier.DEFAULT);
if (myTargetSuperClass.isInterface() || info.isToAbstract()) {
GroovyChangeContextUtil.clearContextInfo(method);
RefactoringUtil.makeMethodAbstract(myTargetSuperClass, methodCopy);
if (myTargetSuperClass.isInterface()) {
PsiUtil.setModifierProperty(methodCopy, PsiModifier.ABSTRACT, false);
}
replaceMovedMemberTypeParameters(methodCopy, PsiUtil.typeParametersIterable(mySourceClass), substitutor, elementFactory);
final GrMethod movedElement = anchor != null ? (GrMethod) myTargetSuperClass.addBefore(methodCopy, anchor) : (GrMethod) myTargetSuperClass.add(methodCopy);
CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(method.getProject());
if (styleSettings.INSERT_OVERRIDE_ANNOTATION) {
if (PsiUtil.isLanguageLevel5OrHigher(mySourceClass) && !myTargetSuperClass.isInterface() || PsiUtil.isLanguageLevel6OrHigher(mySourceClass)) {
new AddAnnotationFix(CommonClassNames.JAVA_LANG_OVERRIDE, method).invoke(method.getProject(), null, mySourceClass.getContainingFile());
}
}
GrDocComment oldDoc = method.getDocComment();
if (oldDoc != null) {
GrDocCommentUtil.setDocComment(movedElement, oldDoc);
}
myDocCommentPolicy.processCopiedJavaDoc(methodCopy.getDocComment(), oldDoc, isOriginalMethodAbstract);
myMembersAfterMove.add(movedElement);
if (isOriginalMethodAbstract) {
deleteMemberWithDocComment(method);
}
} else {
if (isOriginalMethodAbstract) {
PsiUtil.setModifierProperty(myTargetSuperClass, PsiModifier.ABSTRACT, true);
}
fixReferencesToStatic(methodCopy);
replaceMovedMemberTypeParameters(methodCopy, PsiUtil.typeParametersIterable(mySourceClass), substitutor, elementFactory);
final PsiMethod superClassMethod = myTargetSuperClass.findMethodBySignature(methodCopy, false);
Language language = myTargetSuperClass.getLanguage();
final PsiMethod movedElement;
if (superClassMethod != null && superClassMethod.hasModifierProperty(PsiModifier.ABSTRACT)) {
movedElement = (PsiMethod) superClassMethod.replace(convertMethodToLanguage(methodCopy, language));
} else {
movedElement = anchor != null ? (PsiMethod) myTargetSuperClass.addBefore(convertMethodToLanguage(methodCopy, language), anchor) : (PsiMethod) myTargetSuperClass.add(convertMethodToLanguage(methodCopy, language));
myMembersAfterMove.add(movedElement);
}
if (movedElement instanceof GrMethod) {
GrDocCommentUtil.setDocComment((GrDocCommentOwner) movedElement, method.getDocComment());
}
deleteMemberWithDocComment(method);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyMoveClassToInnerHandler method moveClass.
@Override
public PsiClass moveClass(@NotNull PsiClass aClass, @NotNull PsiClass targetClass) {
if (!(aClass instanceof GrTypeDefinition))
return null;
GroovyChangeContextUtil.encodeContextInfo(aClass);
PsiDocComment doc = aClass.getDocComment();
PsiElement brace = targetClass.getRBrace();
PsiClass newClass = (PsiClass) targetClass.addBefore(aClass, brace);
PsiElement sibling = newClass.getPrevSibling();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(targetClass.getProject());
if (!org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isNewLine(sibling)) {
targetClass.addBefore(factory.createLineTerminator("\n "), newClass);
} else if (doc != null) {
LOG.assertTrue(sibling != null);
sibling.replace(factory.createLineTerminator(sibling.getText() + " "));
}
if (doc != null) {
targetClass.addBefore(doc, newClass);
targetClass.addBefore(factory.createLineTerminator("\n"), newClass);
}
if (targetClass.isInterface()) {
PsiUtil.setModifierProperty(newClass, PsiModifier.PUBLIC, true);
} else {
PsiUtil.setModifierProperty(newClass, PsiModifier.STATIC, true);
}
GroovyChangeContextUtil.decodeContextInfo(newClass, null, null);
return newClass;
}
Aggregations