Search in sources :

Example 6 with PsiDocToken

use of com.intellij.psi.javadoc.PsiDocToken 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)

Example 7 with PsiDocToken

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

the class DocCommentSelectioner method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    List<TextRange> result = super.select(e, editorText, cursorOffset, editor);
    PsiElement[] children = e.getChildren();
    int startOffset = e.getTextRange().getStartOffset();
    int endOffset = e.getTextRange().getEndOffset();
    for (PsiElement child : children) {
        if (child instanceof PsiDocToken) {
            PsiDocToken token = (PsiDocToken) child;
            if (token.getTokenType() == JavaDocTokenType.DOC_COMMENT_DATA) {
                char[] chars = token.getText().toCharArray();
                if (CharArrayUtil.shiftForward(chars, 0, " *\n\t\r") != chars.length) {
                    break;
                }
            }
        }
        startOffset = child.getTextRange().getEndOffset();
    }
    for (PsiElement child : children) {
        if (child instanceof PsiDocToken) {
            PsiDocToken token = (PsiDocToken) child;
            if (token.getTokenType() == JavaDocTokenType.DOC_COMMENT_DATA) {
                char[] chars = token.getText().toCharArray();
                if (CharArrayUtil.shiftForward(chars, 0, " *\n\t\r") != chars.length) {
                    endOffset = child.getTextRange().getEndOffset();
                }
            }
        }
    }
    startOffset = CharArrayUtil.shiftBackward(editorText, startOffset - 1, "* \t") + 1;
    result.add(new TextRange(startOffset, endOffset));
    return result;
}
Also used : PsiDocToken(com.intellij.psi.javadoc.PsiDocToken) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiDocToken (com.intellij.psi.javadoc.PsiDocToken)7 PsiElement (com.intellij.psi.PsiElement)4 TextRange (com.intellij.openapi.util.TextRange)3 PsiDocTag (com.intellij.psi.javadoc.PsiDocTag)3 PsiDocTagValue (com.intellij.psi.javadoc.PsiDocTagValue)3 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1 QuickFix (com.intellij.codeInspection.QuickFix)1 ASTNode (com.intellij.lang.ASTNode)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 CandidateInfo (com.intellij.psi.infos.CandidateInfo)1 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)1 PsiScopeProcessor (com.intellij.psi.scope.PsiScopeProcessor)1 IElementType (com.intellij.psi.tree.IElementType)1 CharTable (com.intellij.util.CharTable)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 HashSet (java.util.HashSet)1