Search in sources :

Example 1 with PsiDocTagValue

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

the class GroovyMethodSignatureInsertHandler method handleInsert.

@Override
public void handleInsert(InsertionContext context, LookupElement item) {
    if (!(item.getObject() instanceof PsiMethod)) {
        return;
    }
    PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getEditor().getDocument());
    final Editor editor = context.getEditor();
    final PsiMethod method = (PsiMethod) item.getObject();
    final PsiParameter[] parameters = method.getParameterList().getParameters();
    final StringBuilder buffer = new StringBuilder();
    final CharSequence chars = editor.getDocument().getCharsSequence();
    int endOffset = editor.getCaretModel().getOffset();
    final Project project = context.getProject();
    int afterSharp = CharArrayUtil.shiftBackwardUntil(chars, endOffset - 1, "#") + 1;
    int signatureOffset = afterSharp;
    PsiElement element = context.getFile().findElementAt(signatureOffset - 1);
    final CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(element.getProject());
    PsiDocTag tag = PsiTreeUtil.getParentOfType(element, PsiDocTag.class);
    if (context.getCompletionChar() == Lookup.REPLACE_SELECT_CHAR) {
        final PsiDocTagValue value = tag.getValueElement();
        endOffset = value.getTextRange().getEndOffset();
    }
    editor.getDocument().deleteString(afterSharp, endOffset);
    editor.getCaretModel().moveToOffset(signatureOffset);
    editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
    editor.getSelectionModel().removeSelection();
    buffer.append(method.getName()).append("(");
    final int afterParenth = afterSharp + buffer.length();
    for (int i = 0; i < parameters.length; i++) {
        final PsiType type = TypeConversionUtil.erasure(parameters[i].getType());
        buffer.append(type.getCanonicalText());
        if (i < parameters.length - 1) {
            buffer.append(",");
            if (styleSettings.SPACE_AFTER_COMMA)
                buffer.append(" ");
        }
    }
    buffer.append(")");
    if (!(tag instanceof PsiInlineDocTag)) {
        buffer.append(" ");
    } else {
        final int currentOffset = editor.getCaretModel().getOffset();
        if (chars.charAt(currentOffset) == '}') {
            afterSharp++;
        } else {
            buffer.append("} ");
        }
    }
    String insertString = buffer.toString();
    EditorModificationUtil.insertStringAtCaret(editor, insertString);
    editor.getCaretModel().moveToOffset(afterSharp + buffer.length());
    editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
    PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
    shortenReferences(project, editor, context, afterParenth);
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) Project(com.intellij.openapi.project.Project) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) PsiInlineDocTag(com.intellij.psi.javadoc.PsiInlineDocTag) Editor(com.intellij.openapi.editor.Editor)

Example 2 with PsiDocTagValue

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

the class ConvertToInstanceMethodProcessor method findUsages.

@NotNull
protected UsageInfo[] findUsages() {
    LOG.assertTrue(myTargetParameter.getDeclarationScope() == myMethod);
    final Project project = myMethod.getProject();
    final PsiReference[] methodReferences = ReferencesSearch.search(myMethod, GlobalSearchScope.projectScope(project), false).toArray(PsiReference.EMPTY_ARRAY);
    List<UsageInfo> result = new ArrayList<>();
    for (final PsiReference ref : methodReferences) {
        final PsiElement element = ref.getElement();
        if (element instanceof PsiReferenceExpression) {
            if (element.getParent() instanceof PsiMethodCallExpression) {
                result.add(new MethodCallUsageInfo((PsiMethodCallExpression) element.getParent()));
            }
        } else if (element instanceof PsiDocTagValue) {
            //TODO:!!!
            result.add(new JavaDocUsageInfo(ref));
        }
    }
    for (final PsiReference ref : ReferencesSearch.search(myTargetParameter, new LocalSearchScope(myMethod), false)) {
        final PsiElement element = ref.getElement();
        if (element instanceof PsiReferenceExpression || element instanceof PsiDocParamRef) {
            result.add(new ParameterUsageInfo(ref));
        }
    }
    if (myTargetClass.isInterface()) {
        PsiClass[] implementingClasses = RefactoringHierarchyUtil.findImplementingClasses(myTargetClass);
        for (final PsiClass implementingClass : implementingClasses) {
            result.add(new ImplementingClassUsageInfo(implementingClass));
        }
    }
    return result.toArray(new UsageInfo[result.size()]);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) Project(com.intellij.openapi.project.Project) PsiDocParamRef(com.intellij.psi.impl.source.javadoc.PsiDocParamRef) UsageInfo(com.intellij.usageView.UsageInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PsiDocTagValue

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

the class DocValuesIterator method advanceToNext.

// since doctag value may be inside doc comment we specially skip that nodes from list
static PsiElement advanceToNext(PsiElement e) {
    PsiElement nextSibling = e.getNextSibling();
    if (nextSibling instanceof PsiDocTagValue)
        e = nextSibling;
    nextSibling = e.getNextSibling();
    if (nextSibling instanceof PsiDocToken && ((PsiDocToken) nextSibling).getTokenType() == tokenType) {
        e = nextSibling;
    }
    return e;
}
Also used : PsiDocToken(com.intellij.psi.javadoc.PsiDocToken) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) PsiElement(com.intellij.psi.PsiElement)

Example 4 with PsiDocTagValue

use of com.intellij.psi.javadoc.PsiDocTagValue 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 5 with PsiDocTagValue

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

PsiDocTagValue (com.intellij.psi.javadoc.PsiDocTagValue)14 PsiDocTag (com.intellij.psi.javadoc.PsiDocTag)7 NotNull (org.jetbrains.annotations.NotNull)4 Project (com.intellij.openapi.project.Project)3 PsiDocToken (com.intellij.psi.javadoc.PsiDocToken)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 UsageInfo (com.intellij.usageView.UsageInfo)3 ASTNode (com.intellij.lang.ASTNode)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiElement (com.intellij.psi.PsiElement)2 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)2 Annotation (com.intellij.lang.annotation.Annotation)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 Pair (com.intellij.openapi.util.Pair)1 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)1 PsiDocParamRef (com.intellij.psi.impl.source.javadoc.PsiDocParamRef)1 CandidateInfo (com.intellij.psi.infos.CandidateInfo)1 PsiInlineDocTag (com.intellij.psi.javadoc.PsiInlineDocTag)1 PsiScopeProcessor (com.intellij.psi.scope.PsiScopeProcessor)1