Search in sources :

Example 31 with PsiDocTag

use of com.intellij.psi.javadoc.PsiDocTag in project intellij-community by JetBrains.

the class JavadocParamTagsTest method testAddTag1.

public void testAddTag1() throws Exception {
    final PsiElementFactory factory = getFactory();
    final PsiMethod method = factory.createMethodFromText("/**\n" + " * Javadoc\n" + " * @param p1\n" + " */\n" + "void m();", null);
    final PsiDocComment docComment = method.getDocComment();
    assertNotNull(docComment);
    final PsiDocTag[] tags = docComment.getTags();
    final PsiDocTag tag2 = factory.createParamTag("p2", "");
    docComment.addAfter(tag2, tags[0]);
    assertEquals("/**\n" + " * Javadoc\n" + " * @param p1\n" + " * @param p2\n" + " */", docComment.getText());
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag)

Example 32 with PsiDocTag

use of com.intellij.psi.javadoc.PsiDocTag in project intellij-community by JetBrains.

the class JavadocParamTagsTest method testDeleteTag1.

public void testDeleteTag1() throws Exception {
    final PsiElementFactory factory = getFactory();
    final PsiMethod method = factory.createMethodFromText("/**\n" + " * Javadoc\n" + " * @param p1\n" + " * @param p2\n" + " */" + "  void m() {}", null);
    final PsiDocComment docComment = method.getDocComment();
    assertNotNull(docComment);
    final PsiDocTag[] tags = docComment.getTags();
    WriteCommandAction.runWriteCommandAction(null, () -> tags[0].delete());
    assertEquals("/**\n" + " * Javadoc\n" + " * @param p2\n" + " */", docComment.getText());
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag)

Example 33 with PsiDocTag

use of com.intellij.psi.javadoc.PsiDocTag in project intellij-community by JetBrains.

the class JavadocParamTagsTest method testDeleteTag3.

public void testDeleteTag3() throws Exception {
    final PsiElementFactory factory = getFactory();
    final PsiMethod method = factory.createMethodFromText("/**\n" + " * Javadoc\n" + " * @param p1\n" + " * @param p2\n" + " * @param p3\n" + " */" + "  void m() {}", null);
    final PsiDocComment docComment = method.getDocComment();
    assertNotNull(docComment);
    final PsiDocTag[] tags = docComment.getTags();
    ApplicationManager.getApplication().runWriteAction(() -> tags[1].delete());
    assertEquals("/**\n" + " * Javadoc\n" + " * @param p1\n" + " * @param p3\n" + " */", docComment.getText());
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag)

Example 34 with PsiDocTag

use of com.intellij.psi.javadoc.PsiDocTag in project intellij-community by JetBrains.

the class JavadocParamTagsTest method testAddTag4.

public void testAddTag4() throws Exception {
    final PsiElementFactory factory = getFactory();
    final PsiMethod method = factory.createMethodFromText("/**\n" + " * Javadoc\n" + " */\n" + "void m();", null);
    final PsiDocComment docComment = method.getDocComment();
    assertNotNull(docComment);
    final PsiDocTag tag2 = factory.createParamTag("p2", "");
    docComment.add(tag2);
    assertEquals("/**\n" + " * Javadoc\n" + " * @param p2\n" + " */", docComment.getText());
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag)

Example 35 with PsiDocTag

use of com.intellij.psi.javadoc.PsiDocTag in project intellij-community by JetBrains.

the class JavaDocCommentFixer method fixCommonProblems.

/**
   * This fixer is based on existing javadoc inspections - there are two of them. One detects invalid references (to nonexistent
   * method parameter or non-declared checked exception). Another one handles all other cases (parameter documentation is missing;
   * parameter doesn't have a description etc). This method handles result of the second exception
   * 
   * @param problems  detected problems
   * @param comment   target comment to fix
   * @param document  target document which contains text of the comment being fixed
   * @param project   current project
   */
@SuppressWarnings("unchecked")
private static void fixCommonProblems(@NotNull ProblemDescriptor[] problems, @NotNull PsiComment comment, @NotNull final Document document, @NotNull Project project) {
    List<PsiElement> toRemove = new ArrayList<>();
    for (ProblemDescriptor problem : problems) {
        PsiElement element = problem.getPsiElement();
        if (element == null) {
            continue;
        }
        if ((!(element instanceof PsiDocToken) || !JavaDocTokenType.DOC_COMMENT_START.equals(((PsiDocToken) element).getTokenType())) && comment.getTextRange().contains(element.getTextRange())) {
            // Unnecessary element like '@return' at the void method's javadoc.
            for (PsiElement e = element; e != null; e = e.getParent()) {
                if (e instanceof PsiDocTag) {
                    toRemove.add(e);
                    break;
                }
            }
        } else {
            // Problems like 'missing @param'.
            QuickFix[] fixes = problem.getFixes();
            if (fixes != null && fixes.length > 0) {
                fixes[0].applyFix(project, problem);
            }
        }
    }
    if (toRemove.isEmpty()) {
        return;
    }
    if (toRemove.size() > 1) {
        Collections.sort(toRemove, COMPARATOR);
    }
    PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
    psiDocumentManager.doPostponedOperationsAndUnblockDocument(document);
    CharSequence text = document.getCharsSequence();
    for (PsiElement element : toRemove) {
        int startOffset = element.getTextRange().getStartOffset();
        int startLine = document.getLineNumber(startOffset);
        int i = CharArrayUtil.shiftBackward(text, startOffset - 1, " \t");
        if (i >= 0) {
            char c = text.charAt(i);
            if (c == '*') {
                i = CharArrayUtil.shiftBackward(text, i - 1, " \t");
            }
        }
        if (i >= 0 && text.charAt(i) == '\n') {
            startOffset = Math.max(i, document.getLineStartOffset(startLine) - 1);
        }
        int endOffset = element.getTextRange().getEndOffset();
        // Javadoc PSI is awkward, it includes next line text before the next tag. That's why we need to strip it.
        i = CharArrayUtil.shiftBackward(text, endOffset - 1, " \t*");
        if (i > 0 && text.charAt(i) == '\n') {
            endOffset = i;
        }
        document.deleteString(startOffset, endOffset);
    }
    psiDocumentManager.commitDocument(document);
}
Also used : QuickFix(com.intellij.codeInspection.QuickFix) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) PsiDocToken(com.intellij.psi.javadoc.PsiDocToken)

Aggregations

PsiDocTag (com.intellij.psi.javadoc.PsiDocTag)40 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)14 PsiDocTagValue (com.intellij.psi.javadoc.PsiDocTagValue)7 ASTNode (com.intellij.lang.ASTNode)5 ArrayList (java.util.ArrayList)5 MethodJavaDocHelper (com.intellij.refactoring.util.javadoc.MethodJavaDocHelper)4 NotNull (org.jetbrains.annotations.NotNull)4 PsiDocParamRef (com.intellij.psi.impl.source.javadoc.PsiDocParamRef)3 PsiDocToken (com.intellij.psi.javadoc.PsiDocToken)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 TextRange (com.intellij.openapi.util.TextRange)2 PsiInlineDocTag (com.intellij.psi.javadoc.PsiInlineDocTag)2 HashMap (com.intellij.util.containers.HashMap)2 TIntProcedure (gnu.trove.TIntProcedure)2 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1 QuickFix (com.intellij.codeInspection.QuickFix)1 Annotation (com.intellij.lang.annotation.Annotation)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1