Need help on PsiDocComment

Any idea as to how to create a new PsiDocComment object and replace the exsiting PsiDocComment object with the new one.

Any help would be appreciated
Regards
Ruchir Awadhawal

2 comments
Comment actions Permalink

Ruchir Awadhawal wrote:

Any idea as to how to create a new PsiDocComment object and replace the
exsiting PsiDocComment object with the new one.


Well, I am afraid there is no direct way at the moment, however the
following should work:

1. Create bogus method with your doc comment using
PsiElementFactory.createMethodFromText():

2. Use bogusMethod.getDocComment() to obtain a PsiDocComment
3. Add it/replace with it wherever you want

Hope this helps,
Cheers,
Dmitry
--
Dmitry Lomov
Software Developer
JetBrains Inc.
http://www.jetbrains.com
"Develop with pleasure!"

0
Comment actions Permalink

I am the writer of the to-string plugin and it has a feature to add/replace javadoc on the toString method.

Here is some code from my plugin. You can download the source from http://www.intellij.org/twiki/bin/view/Main/ToStringPlugin

/Claus

not be replaced.
     * @throws IncorrectOperationException is thrown if error adding/replacing the javadoc comment.
     * @since 2.14
     */
    public PsiComment addOrReplaceJavadoc(PsiElementFactory factory, PsiMethod method, String javadoc, boolean replace) throws IncorrectOperationException {
        PsiComment comment = factory.createCommentFromText(javadoc, null);

        // does a method already exists?
        if (method.getDocComment() != null) {
            if (replace) {
                // javadoc already exists, so replace
                method.getDocComment().replace(comment);
                return comment;
            } else {
                // do not relace existing javadoc
                return null;
            }
        } else {
            // add new javadoc
            method.addBefore(comment, method.getFirstChild());
            return comment;
        }
    }
]]>



0

Please sign in to leave a comment.