Search in sources :

Example 6 with PsiDocTag

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

the class RemoveSuppressWarningAction method removeFromJavaDoc.

private void removeFromJavaDoc(PsiDocComment docComment) throws IncorrectOperationException {
    PsiDocTag tag = docComment.findTagByName(SuppressionUtilCore.SUPPRESS_INSPECTIONS_TAG_NAME);
    if (tag == null)
        return;
    String newText = removeFromElementText(tag.getDataElements());
    if (newText != null && newText.isEmpty()) {
        tag.delete();
    } else if (newText != null) {
        newText = "@" + SuppressionUtilCore.SUPPRESS_INSPECTIONS_TAG_NAME + " " + newText;
        PsiDocTag newTag = JavaPsiFacade.getInstance(tag.getProject()).getElementFactory().createDocTagFromText(newText);
        tag.replace(newTag);
    }
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag)

Example 7 with PsiDocTag

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

the class MethodJavaDocHelper method appendParameter.

public PsiDocTag appendParameter(String name) throws IncorrectOperationException {
    if (!myDoCorrectJavaDoc)
        return null;
    final PsiDocTag[] paramTags = myDocComment.findTagsByName("param");
    final PsiDocTag newTag = JavaPsiFacade.getInstance(myMethod.getProject()).getElementFactory().createParamTag(name, "");
    if (paramTags.length > 0) {
        return (PsiDocTag) myDocComment.addAfter(newTag, paramTags[paramTags.length - 1]);
    } else {
        return (PsiDocTag) myDocComment.add(newTag);
    }
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag)

Example 8 with PsiDocTag

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

the class MethodJavaDocHelper method addParameterAfter.

public PsiDocTag addParameterAfter(String name, PsiDocTag anchor) throws IncorrectOperationException {
    if (!myDoCorrectJavaDoc)
        return null;
    if (anchor == null)
        return prependParameter(name);
    LOG.assertTrue(anchor.getParent() == myDocComment);
    final PsiDocTag paramTag = JavaPsiFacade.getInstance(myMethod.getProject()).getElementFactory().createParamTag(name, "");
    return (PsiDocTag) myDocComment.addAfter(paramTag, anchor);
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag)

Example 9 with PsiDocTag

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

the class RefactoringUtil method fixJavadocsForParams.

public static void fixJavadocsForParams(PsiMethod method, Set<PsiParameter> newParameters, Condition<Pair<PsiParameter, String>> eqCondition, Condition<String> matchedToOldParam) throws IncorrectOperationException {
    final PsiDocComment docComment = method.getDocComment();
    if (docComment == null)
        return;
    final PsiParameter[] parameters = method.getParameterList().getParameters();
    final PsiDocTag[] paramTags = docComment.findTagsByName("param");
    if (parameters.length > 0 && newParameters.size() < parameters.length && paramTags.length == 0)
        return;
    Map<PsiParameter, PsiDocTag> tagForParam = new HashMap<>();
    for (PsiParameter parameter : parameters) {
        boolean found = false;
        for (PsiDocTag paramTag : paramTags) {
            if (parameter.getName().equals(getNameOfReferencedParameter(paramTag))) {
                tagForParam.put(parameter, paramTag);
                found = true;
                break;
            }
        }
        if (!found) {
            for (PsiDocTag paramTag : paramTags) {
                final String paramName = getNameOfReferencedParameter(paramTag);
                if (eqCondition.value(Pair.create(parameter, paramName))) {
                    tagForParam.put(parameter, paramTag);
                    found = true;
                    break;
                }
            }
        }
        if (!found && !newParameters.contains(parameter)) {
            tagForParam.put(parameter, null);
        }
    }
    List<PsiDocTag> newTags = new ArrayList<>();
    for (PsiDocTag paramTag : paramTags) {
        final String paramName = getNameOfReferencedParameter(paramTag);
        if (!tagForParam.containsValue(paramTag) && !matchedToOldParam.value(paramName)) {
            newTags.add((PsiDocTag) paramTag.copy());
        }
    }
    for (PsiParameter parameter : parameters) {
        if (tagForParam.containsKey(parameter)) {
            final PsiDocTag psiDocTag = tagForParam.get(parameter);
            if (psiDocTag != null) {
                final PsiDocTag copy = (PsiDocTag) psiDocTag.copy();
                final PsiDocTagValue valueElement = copy.getValueElement();
                if (valueElement != null) {
                    valueElement.replace(createParamTag(parameter).getValueElement());
                }
                newTags.add(copy);
            }
        } else {
            newTags.add(createParamTag(parameter));
        }
    }
    PsiElement anchor = paramTags.length > 0 ? paramTags[0].getPrevSibling() : null;
    for (PsiDocTag paramTag : paramTags) {
        paramTag.delete();
    }
    for (PsiDocTag psiDocTag : newTags) {
        anchor = anchor != null && anchor.isValid() ? docComment.addAfter(psiDocTag, anchor) : docComment.add(psiDocTag);
    }
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) HashMap(com.intellij.util.containers.HashMap) THashMap(gnu.trove.THashMap) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue)

Example 10 with PsiDocTag

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

the class JavaDocCommentFixer method locateCaret.

private static void locateCaret(@NotNull PsiDocComment comment, @NotNull Editor editor, @NotNull PsiFile file) {
    Document document = editor.getDocument();
    int lineToNavigate = -1;
    for (PsiDocTag tag : comment.getTags()) {
        PsiElement nameElement = tag.getNameElement();
        if (nameElement == null || !CARET_ANCHOR_TAGS.contains(nameElement.getText())) {
            continue;
        }
        boolean good = false;
        PsiElement[] dataElements = tag.getDataElements();
        if (dataElements != null) {
            PsiDocTagValue valueElement = tag.getValueElement();
            for (PsiElement element : dataElements) {
                if (element == valueElement) {
                    continue;
                }
                if (!StringUtil.isEmptyOrSpaces(element.getText())) {
                    good = true;
                    break;
                }
            }
        }
        if (!good) {
            int offset = tag.getTextRange().getEndOffset();
            CharSequence text = document.getCharsSequence();
            int i = CharArrayUtil.shiftBackward(text, offset - 1, " \t*");
            if (i > 0 && text.charAt(i) == '\n') {
                offset = i - 1;
            }
            lineToNavigate = document.getLineNumber(offset);
            break;
        }
    }
    if (lineToNavigate >= 0) {
        editor.getCaretModel().moveToOffset(document.getLineEndOffset(lineToNavigate));
        JavadocNavigationDelegate.navigateToLineEnd(editor, file);
    }
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) Document(com.intellij.openapi.editor.Document)

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