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