use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class JavaMatchingVisitor method visitMethod.
@Override
public void visitMethod(PsiMethod method) {
final PsiIdentifier methodNameNode = method.getNameIdentifier();
final boolean isTypedVar = myMatchingVisitor.getMatchContext().getPattern().isTypedVar(methodNameNode);
final PsiMethod method2 = (PsiMethod) myMatchingVisitor.getElement();
myMatchingVisitor.getMatchContext().pushResult();
try {
final PsiDocComment docComment = method.getDocComment();
if (docComment != null) {
myMatchingVisitor.setResult(myMatchingVisitor.match(docComment, method2));
if (!myMatchingVisitor.getResult())
return;
}
if (method.hasTypeParameters()) {
myMatchingVisitor.setResult(myMatchingVisitor.match(method.getTypeParameterList(), ((PsiMethod) myMatchingVisitor.getElement()).getTypeParameterList()));
if (!myMatchingVisitor.getResult())
return;
}
if (!checkHierarchy(method2, method)) {
myMatchingVisitor.setResult(false);
return;
}
myMatchingVisitor.setResult((myMatchingVisitor.matchText(method.getNameIdentifier(), method2.getNameIdentifier()) || isTypedVar) && myMatchingVisitor.match(method.getModifierList(), method2.getModifierList()) && myMatchingVisitor.matchSons(method.getParameterList(), method2.getParameterList()) && myMatchingVisitor.match(method.getReturnTypeElement(), method2.getReturnTypeElement()) && matchInAnyOrder(method.getThrowsList(), method2.getThrowsList()) && myMatchingVisitor.matchSonsOptionally(method.getBody(), method2.getBody()));
} finally {
saveOrDropResult(methodNameNode, isTypedVar, method2.getNameIdentifier());
}
}
use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class JavaMatchingVisitor method visitField.
@Override
public void visitField(PsiField psiField) {
final PsiDocComment comment = psiField.getDocComment();
final PsiField other = (PsiField) myMatchingVisitor.getElement();
if (comment != null) {
myMatchingVisitor.setResult(myMatchingVisitor.match(comment, other));
if (!myMatchingVisitor.getResult())
return;
}
if (!checkHierarchy(other, psiField)) {
myMatchingVisitor.setResult(false);
return;
}
super.visitField(psiField);
}
use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class JavaMatchingVisitor method visitClass.
@Override
public void visitClass(PsiClass clazz) {
PsiClass clazz2 = (PsiClass) myMatchingVisitor.getElement();
if (clazz.hasTypeParameters()) {
myMatchingVisitor.setResult(myMatchingVisitor.match(clazz.getTypeParameterList(), clazz2.getTypeParameterList()));
if (!myMatchingVisitor.getResult())
return;
}
final PsiDocComment comment = clazz.getDocComment();
if (comment != null) {
myMatchingVisitor.setResult(myMatchingVisitor.match(comment, clazz2));
if (!myMatchingVisitor.getResult())
return;
}
final boolean isTypedVar = myMatchingVisitor.getMatchContext().getPattern().isTypedVar(clazz.getNameIdentifier());
if (clazz.getModifierList().getTextLength() > 0) {
if (!myMatchingVisitor.match(clazz.getModifierList(), clazz2.getModifierList())) {
myMatchingVisitor.setResult(false);
return;
}
}
myMatchingVisitor.setResult((myMatchingVisitor.matchText(clazz.getNameIdentifier(), clazz2.getNameIdentifier()) || isTypedVar) && compareClasses(clazz, clazz2));
if (myMatchingVisitor.getResult() && isTypedVar) {
PsiElement id = clazz2.getNameIdentifier();
if (id == null)
id = clazz2;
myMatchingVisitor.setResult(myMatchingVisitor.handleTypedElement(clazz.getNameIdentifier(), id));
}
}
use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class CreateConstructorMatchingSuperFix method chooseConstructor2Delegate.
public static void chooseConstructor2Delegate(final Project project, final Editor editor, PsiSubstitutor substitutor, List<PsiMethodMember> baseConstructors, PsiMethod[] baseConstrs, final PsiClass targetClass) {
PsiMethodMember[] constructors = baseConstructors.toArray(new PsiMethodMember[baseConstructors.size()]);
if (constructors.length == 0) {
constructors = new PsiMethodMember[baseConstrs.length];
for (int i = 0; i < baseConstrs.length; i++) {
constructors[i] = new PsiMethodMember(baseConstrs[i], substitutor);
}
}
// Otherwise we won't have been messing with all this stuff
LOG.assertTrue(constructors.length >= 1);
boolean isCopyJavadoc = true;
if (constructors.length > 1 && !ApplicationManager.getApplication().isUnitTestMode()) {
MemberChooser<PsiMethodMember> chooser = new MemberChooser<>(constructors, false, true, project);
chooser.setTitle(QuickFixBundle.message("super.class.constructors.chooser.title"));
chooser.show();
if (chooser.getExitCode() != DialogWrapper.OK_EXIT_CODE)
return;
constructors = chooser.getSelectedElements(new PsiMethodMember[0]);
isCopyJavadoc = chooser.isCopyJavadoc();
}
final PsiMethodMember[] constructors1 = constructors;
final boolean isCopyJavadoc1 = isCopyJavadoc;
ApplicationManager.getApplication().runWriteAction(() -> {
try {
if (targetClass.getLBrace() == null) {
PsiClass psiClass = JavaPsiFacade.getInstance(targetClass.getProject()).getElementFactory().createClass("X");
targetClass.addRangeAfter(psiClass.getLBrace(), psiClass.getRBrace(), targetClass.getLastChild());
}
JVMElementFactory factory = JVMElementFactories.getFactory(targetClass.getLanguage(), project);
CodeStyleManager formatter = CodeStyleManager.getInstance(project);
PsiMethod derived = null;
for (PsiMethodMember candidate : constructors1) {
PsiMethod base = candidate.getElement();
derived = GenerateMembersUtil.substituteGenericMethod(base, candidate.getSubstitutor(), targetClass);
if (!isCopyJavadoc1) {
final PsiDocComment docComment = derived.getDocComment();
if (docComment != null) {
docComment.delete();
}
}
final String targetClassName = targetClass.getName();
LOG.assertTrue(targetClassName != null, targetClass);
derived.setName(targetClassName);
ConstructorBodyGenerator generator = ConstructorBodyGenerator.INSTANCE.forLanguage(derived.getLanguage());
if (generator != null) {
StringBuilder buffer = new StringBuilder();
generator.start(buffer, derived.getName(), PsiParameter.EMPTY_ARRAY);
generator.generateSuperCallIfNeeded(buffer, derived.getParameterList().getParameters());
generator.finish(buffer);
PsiMethod stub = factory.createMethodFromText(buffer.toString(), targetClass);
derived.getBody().replace(stub.getBody());
}
derived = (PsiMethod) formatter.reformat(derived);
derived = (PsiMethod) JavaCodeStyleManager.getInstance(project).shortenClassReferences(derived);
PsiGenerationInfo<PsiMethod> info = OverrideImplementUtil.createGenerationInfo(derived);
info.insert(targetClass, null, true);
derived = info.getPsiMember();
}
if (derived != null) {
editor.getCaretModel().moveToOffset(derived.getTextRange().getStartOffset());
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
UndoUtil.markPsiFileForUndo(targetClass.getContainingFile());
});
}
use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class PsiAdapter method addOrReplaceJavadoc.
/**
* Add or replaces the javadoc comment to the given method.
*
* @param method the method the javadoc should be added/set to.
* @param javadoc the javadoc comment.
* @param replace true if any existing javadoc should be replaced. false will not replace any existing javadoc and thus leave the javadoc untouched.
* @return the added/replace javadoc comment, null if the was an existing javadoc and it should <b>not</b> be replaced.
* @throws IncorrectOperationException is thrown if error adding/replacing the javadoc comment.
*/
@Nullable
public static PsiComment addOrReplaceJavadoc(PsiMethod method, String javadoc, boolean replace) {
final Project project = method.getProject();
PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
PsiComment comment = factory.createCommentFromText(javadoc, null);
// does a method already exists?
PsiDocComment doc = method.getDocComment();
if (doc != null) {
if (replace) {
// javadoc already exists, so replace
doc.replace(comment);
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
// to reformat javadoc
codeStyleManager.reformat(method);
return comment;
} else {
// do not replace existing javadoc
return null;
}
} else {
// add new javadoc
method.addBefore(comment, method.getFirstChild());
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
// to reformat javadoc
codeStyleManager.reformat(method);
return comment;
}
}
Aggregations