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);
}
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;
}
Aggregations