1.19 support doc tag like @author | 支持 @author 等注释标签

This commit is contained in:
林万程
2022-06-26 15:04:36 +08:00
parent d388c355cd
commit db6c1963bf
8 changed files with 80 additions and 18 deletions

View File

@@ -59,7 +59,7 @@ public class Tree implements ProjectViewNodeDecorator {
if (docComment == null) {
return null;
}
return PsiDocToStrDoc.text(docComment);
return PsiDocToStrDoc.text(docComment, true);
}
@Nullable

View File

@@ -1,10 +1,10 @@
package io.github.linwancen.plugin.show.doc;
import com.intellij.psi.PsiElement;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.javadoc.PsiDocToken;
import com.intellij.psi.javadoc.PsiInlineDocTag;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.javadoc.*;
import io.github.linwancen.plugin.show.settings.AppSettingsState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.regex.Pattern;
@@ -14,7 +14,7 @@ public class PsiDocToStrDoc {
private PsiDocToStrDoc() {}
@Nullable
public static String text(@Nullable PsiDocComment psiDocComment) {
public static String text(@Nullable PsiDocComment psiDocComment, boolean isTree) {
if (psiDocComment == null) {
return null;
}
@@ -32,33 +32,61 @@ public class PsiDocToStrDoc {
break;
}
}
StringBuilder tags = tags(psiDocComment, isTree, appSettings);
if (tags.length() > 0) {
if (sb.length() > 0) {
sb.append("@ ");
}
sb.append(tags);
}
if (sb.length() == 0) {
return null;
}
return sb.toString();
}
@NotNull
private static StringBuilder tags(@NotNull PsiDocComment psiDocComment, boolean isTree,
AppSettingsState appSettings) {
StringBuilder sb = new StringBuilder();
PsiDocTag[] tags = psiDocComment.getTags();
for (PsiDocTag tag : tags) {
String name = tag.getName();
if (isTree ? appSettings.treeTags.contains(name) : appSettings.lineTags.contains(name)) {
// @see @param should use getDataElements()
PsiDocTagValue value = tag.getValueElement();
if (value != null) {
addHtml(sb, value.getText());
}
}
}
return sb;
}
/**
* @return is new line
*/
private static boolean appendElementText(StringBuilder sb, PsiElement element) {
if (element instanceof PsiDocToken) {
PsiDocToken psiDocToken = (PsiDocToken) element;
sb.append(deleteHtml(psiDocToken.getText()));
sb.append(" ");
return true;
addHtml(sb, psiDocToken.getText());
}
if (element instanceof PsiInlineDocTag) {
PsiInlineDocTag psiInlineDocTag = (PsiInlineDocTag) element;
PsiElement[] children = psiInlineDocTag.getChildren();
if (children.length > 2) {
sb.append(deleteHtml(children[2].getText()));
sb.append(" ");
if (children.length > 3) {
addHtml(sb, children[3].getText());
}
}
return false;
return element instanceof PsiWhiteSpace && sb.length() > 0;
}
private static final Pattern HTML_PATTERN = Pattern.compile("<[^>]++>");
private static String deleteHtml(String s) {
return HTML_PATTERN.matcher(s.trim()).replaceAll("");
private static void addHtml(StringBuilder sb, String s) {
String deleteHtml = HTML_PATTERN.matcher(s.trim()).replaceAll("");
if (deleteHtml.length() > 0) {
sb.append(deleteHtml).append(" ");
}
}
}

View File

@@ -47,7 +47,7 @@ public class FileViewToDocStrUtils {
PsiDocComment docComment = setting.findElementRightToLeft
? FileViewToPsiDocRightToLeft.rightDoc(viewProvider, startOffset, endOffset)
: FileViewToPsiDocLeftToRight.leftDoc(viewProvider, document, startOffset, endOffset);
String strDoc = PsiDocToStrDoc.text(docComment);
String strDoc = PsiDocToStrDoc.text(docComment, false);
if (strDoc == null) {
return null;
}

View File

@@ -15,7 +15,9 @@ public class AppSettingsComponent extends AbstractSettingsComponent {
private final JPanel myMainPanel;
private final JBCheckBox showTreeComment = new JBCheckBox("Show tree comment ");
private final JBTextField treeTags = new JBTextField();
private final JBCheckBox showLineEndComment = new JBCheckBox("Show line end comment ");
private final JBTextField lineTags = new JBTextField();
private final JBCheckBox fromCall = new JBCheckBox("call ");
private final JBCheckBox fromNew = new JBCheckBox("new ");
private final JBCheckBox fromRef = new JBCheckBox("ref ");
@@ -26,8 +28,8 @@ public class AppSettingsComponent extends AbstractSettingsComponent {
private final ColorPanel lineEndColor = new ColorPanel();
private final ColorPanel lineEndJsonColor = new ColorPanel();
private final JBCheckBox findElementRightToLeft = new JBCheckBox("Find element right to left");
protected final JBTextField lineEndPrefix = new JBTextField();
protected final JBTextField lineEndCount = new JBTextField();
private final JBTextField lineEndPrefix = new JBTextField();
private final JBTextField lineEndCount = new JBTextField();
public AppSettingsComponent() {
myMainPanel = FormBuilder.createFormBuilder()
@@ -42,6 +44,8 @@ public class AppSettingsComponent extends AbstractSettingsComponent {
JPanel comment = FormBuilder.createFormBuilder()
.addComponent(showTreeComment, 1)
.addComponent(showLineEndComment, 1)
.addLabeledComponent(new JBLabel("tree tags"), treeTags, 1, true)
.addLabeledComponent(new JBLabel("line tags"), lineTags, 1, true)
.getPanel();
comment.setBorder(IdeBorderFactory.createTitledBorder("Show"));
return comment;
@@ -81,6 +85,15 @@ public class AppSettingsComponent extends AbstractSettingsComponent {
showTreeComment.setSelected(newStatus);
}
@NotNull
public String getTreeTags() {
return treeTags.getText();
}
public void setTreeTags(@NotNull String newText) {
treeTags.setText(newText);
}
public boolean getShowLineEndComment() {
return showLineEndComment.isSelected();
}
@@ -89,6 +102,15 @@ public class AppSettingsComponent extends AbstractSettingsComponent {
showLineEndComment.setSelected(newStatus);
}
@NotNull
public String getLineTags() {
return lineTags.getText();
}
public void setLineTags(@NotNull String newText) {
lineTags.setText(newText);
}
public boolean getFromCall() {
return fromCall.isSelected();
}

View File

@@ -32,7 +32,9 @@ public class AppSettingsConfigurable implements Configurable {
public boolean isModified() {
AppSettingsState settings = AppSettingsState.getInstance();
boolean modified = mySettingsComponent.getShowTreeComment() != settings.showTreeComment;
modified |= !mySettingsComponent.getTreeTags().equals(settings.treeTags);
modified |= mySettingsComponent.getShowLineEndComment() != settings.showLineEndComment;
modified |= !mySettingsComponent.getLineTags().equals(settings.lineTags);
modified |= !mySettingsComponent.getLineEndCount().equals(String.valueOf(settings.lineEndCount));
modified |= !mySettingsComponent.getLineEndColor().equals(settings.lineEndTextAttr.getForegroundColor());
@@ -58,7 +60,9 @@ public class AppSettingsConfigurable implements Configurable {
public void apply() {
AppSettingsState settings = AppSettingsState.getInstance();
settings.showTreeComment = mySettingsComponent.getShowTreeComment();
settings.treeTags = mySettingsComponent.getTreeTags();
settings.showLineEndComment = mySettingsComponent.getShowLineEndComment();
settings.lineTags = mySettingsComponent.getLineTags();
try {
settings.lineEndCount = Integer.parseInt(mySettingsComponent.getLineEndCount());
@@ -86,7 +90,9 @@ public class AppSettingsConfigurable implements Configurable {
public void reset() {
AppSettingsState settings = AppSettingsState.getInstance();
mySettingsComponent.setShowTreeComment(settings.showTreeComment);
mySettingsComponent.setTreeTags(settings.treeTags);
mySettingsComponent.setShowLineEndComment(settings.showLineEndComment);
mySettingsComponent.setLineTags(settings.lineTags);
mySettingsComponent.setLineEndCount(String.valueOf(settings.lineEndCount));
mySettingsComponent.setLineEndColor(settings.lineEndTextAttr.getForegroundColor());

View File

@@ -21,7 +21,9 @@ import java.math.BigInteger;
public class AppSettingsState extends AbstractSettingsState implements PersistentStateComponent<AppSettingsState> {
public boolean showTreeComment = true;
public String treeTags = "author";
public boolean showLineEndComment = true;
public String lineTags = "author";
public final TextAttributes lineEndTextAttr = new TextAttributes(
new JBColor(new Color(98, 151, 85), new Color(98, 151, 85)),