use of com.intellij.psi.javadoc.PsiDocTagValue in project intellij-community by JetBrains.
the class DocDataHandler method getTextFromNode.
// since doctag value may be inside doc comment we specially build text including skipped nodes
private static String getTextFromNode(final PsiElement node, String text1) {
PsiElement nextSibling = node.getNextSibling();
if (nextSibling instanceof PsiDocTagValue) {
text1 += nextSibling.getText();
nextSibling = nextSibling.getNextSibling();
if (nextSibling instanceof PsiDocToken && ((PsiDocToken) nextSibling).getTokenType() == JavaDocTokenType.DOC_COMMENT_DATA) {
text1 += nextSibling.getText();
}
}
return text1;
}
use of com.intellij.psi.javadoc.PsiDocTagValue in project intellij-community by JetBrains.
the class JavaDocCommentFixer method ensureContentOrdered.
private static void ensureContentOrdered(@NotNull PsiDocComment comment, @NotNull Document document) {
//region Parse existing doc comment parameters.
List<String> current = new ArrayList<>();
Map<String, Pair<TextRange, String>> tagInfoByName = new HashMap<>();
for (PsiDocTag tag : comment.getTags()) {
if (!PARAM_TAG_NAME.equals(tag.getName())) {
continue;
}
PsiDocTagValue valueElement = tag.getValueElement();
if (valueElement == null) {
continue;
}
String paramName = valueElement.getText();
if (paramName != null) {
current.add(paramName);
tagInfoByName.put(paramName, parseTagValue(tag, document));
}
}
//endregion
//region Calculate desired parameters order
List<String> ordered = new ArrayList<>();
PsiJavaDocumentedElement owner = comment.getOwner();
if ((owner instanceof PsiMethod)) {
PsiParameter[] parameters = ((PsiMethod) owner).getParameterList().getParameters();
for (PsiParameter parameter : parameters) {
ordered.add(parameter.getName());
}
}
if (owner instanceof PsiTypeParameterListOwner) {
PsiTypeParameter[] typeParameters = ((PsiTypeParameterListOwner) owner).getTypeParameters();
for (PsiTypeParameter parameter : typeParameters) {
ordered.add(String.format("<%s>", parameter.getName()));
}
}
//region Fix order if necessary.
if (current.size() != ordered.size()) {
// Something is wrong, stop the processing.
return;
}
boolean changed = false;
for (int i = current.size() - 1; i >= 0; i--) {
String newTag = ordered.get(i);
String oldTag = current.get(i);
if (newTag.equals(oldTag)) {
continue;
}
TextRange range = tagInfoByName.get(oldTag).first;
document.replaceString(range.getStartOffset(), range.getEndOffset(), tagInfoByName.get(newTag).second);
changed = true;
}
if (changed) {
PsiDocumentManager manager = PsiDocumentManager.getInstance(comment.getProject());
manager.commitDocument(document);
}
//endregion
}
use of com.intellij.psi.javadoc.PsiDocTagValue in project intellij-community by JetBrains.
the class JavaDocCommentFixer method parseTagValue.
@NotNull
private static Pair<TextRange, String> parseTagValue(@NotNull PsiDocTag tag, @NotNull Document document) {
PsiDocTagValue valueElement = tag.getValueElement();
assert valueElement != null;
int startOffset = valueElement.getTextRange().getStartOffset();
int endOffset = tag.getTextRange().getEndOffset();
// Javadoc PSI is rather weird...
CharSequence text = document.getCharsSequence();
int i = CharArrayUtil.shiftBackward(text, endOffset - 1, " \t*");
if (i > 0 && text.charAt(i) == '\n') {
endOffset = i;
}
return Pair.create(TextRange.create(startOffset, endOffset), text.subSequence(startOffset, endOffset).toString());
}
use of com.intellij.psi.javadoc.PsiDocTagValue in project intellij-community by JetBrains.
the class JavadocHelper method parse.
@Nullable
private static JavadocParameterInfo parse(@NotNull PsiElement element, @NotNull Editor editor) {
final PsiDocTag tag = PsiTreeUtil.getParentOfType(element, PsiDocTag.class, false);
if (tag == null || !PARAM_TEXT.equals(tag.getName())) {
return null;
}
final PsiDocTagValue paramRef = PsiTreeUtil.getChildOfType(tag, PsiDocTagValue.class);
if (paramRef == null) {
return null;
}
for (PsiElement e = paramRef.getNextSibling(); e != null; e = e.getNextSibling()) {
final ASTNode node = e.getNode();
if (node == null) {
break;
}
final IElementType elementType = node.getElementType();
if (elementType == JavaDocTokenType.DOC_COMMENT_DATA) {
return new JavadocParameterInfo(editor.offsetToLogicalPosition(paramRef.getTextRange().getEndOffset()), editor.offsetToLogicalPosition(e.getTextRange().getStartOffset()), editor.getDocument().getLineNumber(e.getTextRange().getEndOffset()));
} else if (elementType == JavaDocTokenType.DOC_COMMENT_LEADING_ASTERISKS) {
break;
}
}
return new JavadocParameterInfo(editor.offsetToLogicalPosition(paramRef.getTextRange().getEndOffset()), null, editor.getDocument().getLineNumber(paramRef.getTextRange().getEndOffset()));
}
Aggregations