Search in sources :

Example 36 with PsiDocComment

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());
    }
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment)

Example 37 with PsiDocComment

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);
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment)

Example 38 with PsiDocComment

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));
    }
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment)

Example 39 with PsiDocComment

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());
    });
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) MemberChooser(com.intellij.ide.util.MemberChooser) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 40 with PsiDocComment

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;
    }
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)54 PsiDocTag (com.intellij.psi.javadoc.PsiDocTag)14 Project (com.intellij.openapi.project.Project)8 TextRange (com.intellij.openapi.util.TextRange)6 PsiElement (com.intellij.psi.PsiElement)6 IElementType (com.intellij.psi.tree.IElementType)6 NotNull (org.jetbrains.annotations.NotNull)5 ASTNode (com.intellij.lang.ASTNode)4 PsiComment (com.intellij.psi.PsiComment)4 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)4 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 ArrayList (java.util.ArrayList)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiField (com.intellij.psi.PsiField)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)2 PsiDocParamRef (com.intellij.psi.impl.source.javadoc.PsiDocParamRef)2 HashMap (com.intellij.util.containers.HashMap)2 HashSet (java.util.HashSet)2 Matcher (java.util.regex.Matcher)2