use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class CStyleCommentPredicate method satisfiedBy.
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof PsiComment)) {
return false;
}
if (element instanceof PsiDocComment) {
return false;
}
final PsiComment comment = (PsiComment) element;
final IElementType type = comment.getTokenType();
if (!GroovyTokenTypes.mML_COMMENT.equals(type)) {
return false;
}
final PsiElement sibling = PsiTreeUtil.nextLeaf(comment);
if (sibling == null) {
return true;
}
if (!(isWhitespace(sibling))) {
return false;
}
final String whitespaceText = sibling.getText();
return whitespaceText.indexOf((int) '\n') >= 0 || whitespaceText.indexOf((int) '\r') >= 0;
}
use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class GroovyGenerationInfo method adjustDocCommentIfExists.
private static void adjustDocCommentIfExists(PsiMember member) {
final PsiElement child = member.getFirstChild();
if (child instanceof PsiDocComment) {
final Project project = member.getProject();
final GrDocComment groovyDoc = GroovyPsiElementFactory.getInstance(project).createDocCommentFromText(child.getText());
child.delete();
CodeStyleManager.getInstance(project).reformat(member);
member.getParent().addBefore(groovyDoc, member);
}
}
use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class MoveGroovyClassHandler method doMoveClass.
@Override
public PsiClass doMoveClass(@NotNull PsiClass aClass, @NotNull PsiDirectory moveDestination) throws IncorrectOperationException {
if (!aClass.getLanguage().equals(GroovyLanguage.INSTANCE))
return null;
PsiFile file = aClass.getContainingFile();
if (!(file instanceof GroovyFile))
return null;
final PsiPackage newPackage = JavaDirectoryService.getInstance().getPackage(moveDestination);
LOG.assertTrue(newPackage != null);
PsiClass newClass = null;
final String newPackageName = newPackage.getQualifiedName();
if (aClass instanceof GroovyScriptClass) {
final PsiClass[] classes = ((GroovyFile) file).getClasses();
if (classes.length == 1) {
if (!moveDestination.equals(file.getContainingDirectory())) {
Project project = file.getProject();
MoveFilesOrDirectoriesUtil.doMoveFile(file, moveDestination);
DumbService.getInstance(project).completeJustSubmittedTasks();
file = moveDestination.findFile(file.getName());
assert file != null;
((PsiClassOwner) file).setPackageName(newPackageName);
}
return ((GroovyFile) file).getScriptClass();
}
//script class is moved the first from the file due to MoveClassOrPackageProcessor:88 (element sort)
correctSelfReferences(aClass, newPackage);
final GroovyFile newFile = generateNewScript((GroovyFile) file, newPackage);
for (PsiElement child : file.getChildren()) {
if (!(child instanceof GrTopStatement || child instanceof PsiComment))
continue;
if (child instanceof PsiClass || child instanceof GrImportStatement || child instanceof GrPackageDefinition)
continue;
if (child instanceof GrDocComment) {
final GrDocCommentOwner owner = GrDocCommentUtil.findDocOwner((GrDocComment) child);
if (owner instanceof PsiClass)
continue;
}
child.delete();
}
if (!moveDestination.equals(file.getContainingDirectory())) {
moveDestination.add(newFile);
//aClass.getManager().moveFile(newFile, moveDestination);
}
newClass = newFile.getClasses()[0];
correctOldClassReferences(newClass, aClass);
} else {
if (!moveDestination.equals(file.getContainingDirectory()) && moveDestination.findFile(file.getName()) != null) {
// moving second of two classes which were in the same file to a different directory (IDEADEV-3089)
correctSelfReferences(aClass, newPackage);
PsiFile newFile = moveDestination.findFile(file.getName());
final FileASTNode fileNode = newFile.getNode();
fileNode.addChild(Factory.createSingleLeafElement(GroovyTokenTypes.mNLS, "\n\n", 0, 2, null, aClass.getManager()));
final PsiDocComment docComment = aClass.getDocComment();
if (docComment != null) {
newFile.add(docComment);
fileNode.addChild(Factory.createSingleLeafElement(GroovyTokenTypes.mNLS, "\n", 0, 1, null, aClass.getManager()));
}
newClass = (GrTypeDefinition) newFile.add(aClass);
correctOldClassReferences(newClass, aClass);
aClass.delete();
} else if (((GroovyFile) file).getClasses().length > 1) {
correctSelfReferences(aClass, newPackage);
Project project = aClass.getProject();
PsiFileFactory fileFactory = PsiFileFactory.getInstance(project);
GroovyFile newFile = (GroovyFile) moveDestination.add(fileFactory.createFileFromText(aClass.getName() + "." + GroovyFileType.DEFAULT_EXTENSION, GroovyLanguage.INSTANCE, "class XXX {}"));
final PsiClass created = newFile.getClasses()[0];
PsiDocComment docComment = aClass.getDocComment();
if (docComment != null) {
newFile.addBefore(docComment, created);
docComment.delete();
}
newClass = (PsiClass) created.replace(aClass);
setPackageDefinition((GroovyFile) file, newFile, newPackageName);
correctOldClassReferences(newClass, aClass);
aClass.delete();
}
}
return newClass;
}
use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class MoveGroovyMemberHandler method doMove.
@Override
@NotNull
public PsiMember doMove(@NotNull MoveMembersOptions options, @NotNull PsiMember member, PsiElement anchor, @NotNull PsiClass targetClass) {
GroovyChangeContextUtil.encodeContextInfo(member);
final PsiDocComment docComment;
if (member instanceof PsiDocCommentOwner) {
docComment = ((PsiDocCommentOwner) member).getDocComment();
} else {
docComment = null;
}
PsiMember moved;
if (options.makeEnumConstant() && member instanceof GrVariable && EnumConstantsUtil.isSuitableForEnumConstant(((PsiVariable) member).getType(), targetClass)) {
final GrEnumConstant prototype = createEnumConstant(member.getName(), ((GrVariable) member).getInitializerGroovy(), member.getProject());
moved = (PsiMember) addEnumConstant(targetClass, prototype, anchor);
member.delete();
} else if (member instanceof GrEnumConstant) {
moved = (PsiMember) addEnumConstant(targetClass, (GrEnumConstant) member, null);
} else if (member instanceof GrField) {
if (anchor != null)
anchor = anchor.getParent();
final GrVariableDeclaration parent = (GrVariableDeclaration) member.getParent();
GrVariableDeclaration movedDeclaration = (GrVariableDeclaration) targetClass.addAfter(parent, anchor);
int number = ArrayUtil.find(parent.getMembers(), member);
final GrMember[] members = movedDeclaration.getMembers();
for (int i = 0; i < number; i++) {
members[i].delete();
}
for (int i = number + 1; i < members.length; i++) {
members[i].delete();
}
if (member.getContainingClass().isInterface() && !targetClass.isInterface()) {
//might need to make modifiers explicit, see IDEADEV-11416
final PsiModifierList list = movedDeclaration.getModifierList();
VisibilityUtil.setVisibility(list, VisibilityUtil.getVisibilityModifier(member.getModifierList()));
list.setModifierProperty(PsiModifier.STATIC, member.hasModifierProperty(PsiModifier.STATIC));
list.setModifierProperty(PsiModifier.FINAL, member.hasModifierProperty(PsiModifier.FINAL));
}
moved = movedDeclaration.getMembers()[0];
} else if (member instanceof GrMethod) {
moved = (PsiMember) targetClass.addAfter(member, anchor);
if (member.getContainingClass().isInterface() && !targetClass.isInterface()) {
//might need to make modifiers explicit, see IDEADEV-11416
final PsiModifierList list = moved.getModifierList();
assert list != null;
list.setModifierProperty(PsiModifier.STATIC, member.hasModifierProperty(PsiModifier.STATIC));
list.setModifierProperty(PsiModifier.FINAL, member.hasModifierProperty(PsiModifier.FINAL));
VisibilityUtil.setVisibility(list, VisibilityUtil.getVisibilityModifier(member.getModifierList()));
}
} else {
moved = (PsiMember) targetClass.addAfter(member, anchor);
}
if (docComment != null) {
PsiElement insertedDocComment = targetClass.addBefore(docComment, moved);
PsiElement prevSibling = insertedDocComment.getPrevSibling();
addLineFeedIfNeeded(prevSibling);
docComment.delete();
}
member.delete();
return moved;
}
use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class JavaPushDownDelegate method pushDownToClass.
@Override
public void pushDownToClass(PsiElement targetElement, PushDownData<MemberInfo, PsiMember> pushDownData) {
final PsiElementFactory factory = JavaPsiFacade.getInstance(pushDownData.getSourceClass().getProject()).getElementFactory();
final PsiClass targetClass = targetElement instanceof PsiClass ? (PsiClass) targetElement : null;
if (targetClass == null) {
return;
}
final PsiClass sourceClass = (PsiClass) pushDownData.getSourceClass();
final PsiSubstitutor substitutor = TypeConversionUtil.getSuperClassSubstitutor(sourceClass, targetClass, PsiSubstitutor.EMPTY);
for (MemberInfoBase<? extends PsiElement> memberInfo : pushDownData.getMembersToMove()) {
PsiMember member = (PsiMember) memberInfo.getMember();
final List<PsiReference> refsToRebind = new ArrayList<>();
final PsiModifierList list = member.getModifierList();
LOG.assertTrue(list != null);
if (list.hasModifierProperty(PsiModifier.STATIC)) {
for (final PsiReference reference : ReferencesSearch.search(member)) {
final PsiElement element = reference.getElement();
if (element instanceof PsiReferenceExpression) {
final PsiExpression qualifierExpression = ((PsiReferenceExpression) element).getQualifierExpression();
if (qualifierExpression instanceof PsiReferenceExpression && !(((PsiReferenceExpression) qualifierExpression).resolve() instanceof PsiClass)) {
continue;
}
}
refsToRebind.add(reference);
}
}
member = (PsiMember) member.copy();
RefactoringUtil.replaceMovedMemberTypeParameters(member, PsiUtil.typeParametersIterable(sourceClass), substitutor, factory);
PsiMember newMember = null;
if (member instanceof PsiField) {
((PsiField) member).normalizeDeclaration();
if (sourceClass.isInterface() && !targetClass.isInterface()) {
PsiUtil.setModifierProperty(member, PsiModifier.PUBLIC, true);
PsiUtil.setModifierProperty(member, PsiModifier.STATIC, true);
PsiUtil.setModifierProperty(member, PsiModifier.FINAL, true);
}
newMember = (PsiMember) targetClass.add(member);
} else if (member instanceof PsiMethod) {
PsiMethod method = (PsiMethod) member;
PsiMethod methodBySignature = MethodSignatureUtil.findMethodBySuperSignature(targetClass, method.getSignature(substitutor), false);
if (methodBySignature == null) {
newMember = (PsiMethod) targetClass.add(method);
final PsiMethod oldMethod = (PsiMethod) memberInfo.getMember();
if (sourceClass.isInterface() && !targetClass.isInterface()) {
PsiUtil.setModifierProperty(newMember, PsiModifier.PUBLIC, true);
if (oldMethod.hasModifierProperty(PsiModifier.ABSTRACT)) {
RefactoringUtil.makeMethodAbstract(targetClass, (PsiMethod) newMember);
} else {
PsiUtil.setModifierProperty(newMember, PsiModifier.DEFAULT, false);
}
}
if (memberInfo.isToAbstract()) {
if (newMember.hasModifierProperty(PsiModifier.PRIVATE)) {
PsiUtil.setModifierProperty(newMember, PsiModifier.PROTECTED, true);
}
pushDownData.getCommentPolicy().processNewJavaDoc(((PsiMethod) newMember).getDocComment());
OverrideImplementUtil.annotateOnOverrideImplement((PsiMethod) newMember, targetClass, (PsiMethod) memberInfo.getMember());
}
} else {
//abstract method: remove @Override
if (!memberInfo.isToAbstract()) {
final PsiAnnotation annotation = AnnotationUtil.findAnnotation(methodBySignature, "java.lang.Override");
if (annotation != null && !leaveOverrideAnnotation(sourceClass, substitutor, method)) {
annotation.delete();
}
}
final PsiDocComment oldDocComment = method.getDocComment();
if (oldDocComment != null) {
final PsiDocComment docComment = methodBySignature.getDocComment();
final int policy = pushDownData.getCommentPolicy().getJavaDocPolicy();
if (policy == DocCommentPolicy.COPY || policy == DocCommentPolicy.MOVE) {
if (docComment != null) {
docComment.replace(oldDocComment);
} else {
methodBySignature.getParent().addBefore(oldDocComment, methodBySignature);
}
}
}
}
} else if (member instanceof PsiClass) {
if (sourceClass.isInterface() && !targetClass.isInterface()) {
PsiUtil.setModifierProperty(member, PsiModifier.PUBLIC, true);
}
if (Boolean.FALSE.equals(memberInfo.getOverrides())) {
final PsiClass psiClass = (PsiClass) memberInfo.getMember();
PsiClassType classType = null;
if (!targetClass.isInheritor(psiClass, false)) {
final PsiClassType[] types = ((MemberInfo) memberInfo).getSourceReferenceList().getReferencedTypes();
for (PsiClassType type : types) {
if (type.resolve() == psiClass) {
classType = (PsiClassType) substitutor.substitute(type);
}
}
PsiJavaCodeReferenceElement classRef = classType != null ? factory.createReferenceElementByType(classType) : factory.createClassReferenceElement(psiClass);
if (psiClass.isInterface()) {
targetClass.getImplementsList().add(classRef);
} else {
targetClass.getExtendsList().add(classRef);
}
}
} else {
newMember = (PsiMember) targetClass.add(member);
}
}
if (newMember != null) {
decodeRefs(sourceClass, newMember, targetClass);
//rebind imports first
Collections.sort(refsToRebind, Comparator.comparing(PsiReference::getElement, PsiUtil.BY_POSITION));
for (PsiReference psiReference : refsToRebind) {
JavaCodeStyleManager.getInstance(sourceClass.getProject()).shortenClassReferences(psiReference.bindToElement(newMember));
}
final JavaRefactoringListenerManager listenerManager = JavaRefactoringListenerManager.getInstance(newMember.getProject());
((JavaRefactoringListenerManagerImpl) listenerManager).fireMemberMoved(sourceClass, newMember);
}
}
}
Aggregations