use of com.intellij.psi.javadoc.PsiDocTag in project intellij-community by JetBrains.
the class JavadocTypedHandler method isAppropriatePlace.
private static boolean isAppropriatePlace(Editor editor, PsiFile file) {
FileViewProvider provider = file.getViewProvider();
int offset = editor.getCaretModel().getOffset();
final PsiElement elementAtCaret;
if (offset < editor.getDocument().getTextLength()) {
elementAtCaret = provider.findElementAt(offset);
} else {
elementAtCaret = provider.findElementAt(editor.getDocument().getTextLength() - 1);
}
PsiElement element = elementAtCaret;
while (element instanceof PsiWhiteSpace || element != null && containsOnlyWhiteSpaces(element.getText())) {
element = element.getPrevSibling();
}
if (element == null) {
return false;
}
if (element instanceof PsiDocParamRef) {
element = element.getParent();
}
if (element instanceof PsiDocTag) {
PsiDocTag tag = (PsiDocTag) element;
if ("param".equals(tag.getName()) && isTypeParamBracketClosedAfterParamTag(tag, offset)) {
return false;
}
}
// The contents of inline tags is not HTML, so the paired tag completion isn't appropriate there.
if (PsiTreeUtil.getParentOfType(element, PsiInlineDocTag.class, false) != null) {
return false;
}
ASTNode node = element.getNode();
return node != null && (JavaDocTokenType.ALL_JAVADOC_TOKENS.contains(node.getElementType()) || JavaDocElementType.ALL_JAVADOC_ELEMENTS.contains(node.getElementType()));
}
use of com.intellij.psi.javadoc.PsiDocTag in project intellij-community by JetBrains.
the class JavadocHelper method parse.
/**
* Returns information about all lines that contain javadoc parameters and are adjacent to the one that holds given offset.
*
* @param psiFile PSI holder for the document exposed the given editor
* @param editor target editor
* @param offset target offset that identifies anchor line to check
* @return pair like (javadoc info for the line identified by the given offset; list of javadoc parameter infos for
* adjacent lines if any
*/
@SuppressWarnings("MethodMayBeStatic")
@NotNull
public Pair<JavadocParameterInfo, List<JavadocParameterInfo>> parse(@NotNull PsiFile psiFile, @NotNull Editor editor, int offset) {
List<JavadocParameterInfo> result = new ArrayList<>();
PsiDocumentManager.getInstance(psiFile.getProject()).commitDocument(editor.getDocument());
final PsiElement elementAtCaret = psiFile.findElementAt(offset);
if (elementAtCaret == null) {
return EMPTY;
}
PsiDocTag tag = PsiTreeUtil.getParentOfType(elementAtCaret, PsiDocTag.class);
if (tag == null) {
// Due to javadoc PSI specifics.
if (elementAtCaret instanceof PsiWhiteSpace) {
for (PsiElement e = elementAtCaret.getPrevSibling(); e != null && tag == null; e = e.getPrevSibling()) {
tag = PsiTreeUtil.getParentOfType(e, PsiDocTag.class, false);
if (e instanceof PsiWhiteSpace || (e instanceof PsiDocToken && ((PsiDocToken) e).getTokenType() == JavaDocTokenType.DOC_COMMENT_LEADING_ASTERISKS)) {
continue;
}
break;
}
}
}
if (tag == null) {
return EMPTY;
}
JavadocParameterInfo anchorInfo = parse(tag, editor);
if (anchorInfo == null) {
return EMPTY;
}
// Parse previous parameters.
for (PsiElement e = tag.getPrevSibling(); e != null; e = e.getPrevSibling()) {
JavadocParameterInfo info = parse(e, editor);
if (info == null) {
break;
}
result.add(0, info);
}
result.add(anchorInfo);
// Parse subsequent parameters.
for (PsiElement e = tag.getNextSibling(); e != null; e = e.getNextSibling()) {
JavadocParameterInfo info = parse(e, editor);
if (info == null) {
break;
}
result.add(info);
}
return Pair.create(anchorInfo, result);
}
use of com.intellij.psi.javadoc.PsiDocTag in project intellij-community by JetBrains.
the class JavaDocumentationProvider method generateParametersTakingDocFromSuperMethods.
public static void generateParametersTakingDocFromSuperMethods(Project project, StringBuilder builder, CodeDocumentationAwareCommenter commenter, PsiMethod psiMethod) {
final PsiParameter[] parameters = psiMethod.getParameterList().getParameters();
final Map<String, String> param2Description = new HashMap<>();
final PsiMethod[] superMethods = psiMethod.findSuperMethods();
for (PsiMethod superMethod : superMethods) {
final PsiDocComment comment = superMethod.getDocComment();
if (comment != null) {
final PsiDocTag[] params = comment.findTagsByName("param");
for (PsiDocTag param : params) {
final PsiElement[] dataElements = param.getDataElements();
if (dataElements != null) {
String paramName = null;
for (PsiElement dataElement : dataElements) {
if (dataElement instanceof PsiDocParamRef) {
//noinspection ConstantConditions
paramName = dataElement.getReference().getCanonicalText();
break;
}
}
if (paramName != null) {
param2Description.put(paramName, param.getText());
}
}
}
}
}
for (PsiParameter parameter : parameters) {
String description = param2Description.get(parameter.getName());
if (description != null) {
builder.append(CodeDocumentationUtil.createDocCommentLine("", project, commenter));
if (description.indexOf('\n') > -1)
description = description.substring(0, description.lastIndexOf('\n'));
builder.append(description);
} else {
builder.append(CodeDocumentationUtil.createDocCommentLine(PARAM_TAG, project, commenter));
builder.append(parameter.getName());
}
builder.append(LINE_SEPARATOR);
}
}
use of com.intellij.psi.javadoc.PsiDocTag in project intellij-community by JetBrains.
the class PsiDocCommentImpl method findTagByName.
@Override
public PsiDocTag findTagByName(String name) {
if (getFirstChildNode().getElementType() == JavaDocElementType.DOC_COMMENT) {
if (getFirstChildNode().getText().indexOf(name) < 0)
return null;
}
for (ASTNode child = getFirstChildNode(); child != null; child = child.getTreeNext()) {
if (child.getElementType() == DOC_TAG) {
PsiDocTag tag = (PsiDocTag) SourceTreeToPsiMap.treeElementToPsi(child);
final CharSequence nameText = ((LeafElement) tag.getNameElement()).getChars();
if (nameText.length() > 0 && nameText.charAt(0) == '@' && CharArrayUtil.regionMatches(nameText, 1, name)) {
return tag;
}
}
}
return null;
}
use of com.intellij.psi.javadoc.PsiDocTag in project intellij-community by JetBrains.
the class PsiDocCommentImpl method deleteChildInternal.
@Override
public void deleteChildInternal(@NotNull ASTNode child) {
if (child.getElementType() == DOC_TAG) {
if (child.getTreeNext() == null || child.getTreeNext().getElementType() != DOC_TAG) {
ASTNode prev = child.getTreePrev();
while (prev != null && prev.getElementType() == DOC_COMMENT_DATA) {
prev = prev.getTreePrev();
}
ASTNode next = child.getTreeNext();
while (next != null && (next.getElementType() == DOC_COMMENT_DATA || next.getElementType() == WHITE_SPACE)) {
next = next.getTreeNext();
}
if (prev != null && prev.getElementType() == DOC_COMMENT_LEADING_ASTERISKS && !(next instanceof PsiDocTag)) {
ASTNode leadingAsterisk = prev;
if (leadingAsterisk.getTreePrev() != null) {
super.deleteChildInternal(leadingAsterisk.getTreePrev());
super.deleteChildInternal(leadingAsterisk);
}
} else if (prev != null && prev.getElementType() == DOC_TAG) {
final CompositeElement compositePrev = (CompositeElement) prev;
final ASTNode lastPrevChild = compositePrev.getLastChildNode();
ASTNode prevChild = lastPrevChild;
while (prevChild != null && prevChild.getElementType() == DOC_COMMENT_DATA) {
prevChild = prevChild.getTreePrev();
}
if (prevChild != null && prevChild.getElementType() == DOC_COMMENT_LEADING_ASTERISKS) {
ASTNode current = prevChild;
while (current != null) {
final ASTNode nextChild = current.getTreeNext();
compositePrev.deleteChildInternal(current);
current = nextChild;
}
}
} else {
next = child.getTreeNext();
if (next != null && next.getElementType() == WHITE_SPACE) {
next.getTreeParent().removeChild(next);
}
}
}
}
super.deleteChildInternal(child);
}
Aggregations