feat: 2.19 ★ support HTML(Vue) Tag/Attr doc since 2022.3.1 | 支持 HTML(Vue) 标签/属性 注释从 2022.3.1 起

This commit is contained in:
林万程
2025-01-25 01:53:08 +08:00
parent 369b316834
commit 035d8d590a
15 changed files with 242 additions and 4 deletions

View File

@@ -79,6 +79,7 @@ public class LineEndCacheUtils {
AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(() -> {
try {
cacheUpdate();
} catch (ProcessCanceledException ignored) {
} catch (Throwable e) {
LOG.info("LineEndCacheUtils checkScheduleAndInit catch Throwable but log to record.", e);
}

View File

@@ -1,17 +1,106 @@
package io.github.linwancen.plugin.show.lang;
import com.intellij.lang.html.HTMLLanguage;
import com.intellij.lang.javascript.psi.JSPsiReferenceElement;
import com.intellij.model.psi.PsiSymbolReference;
import com.intellij.model.psi.PsiSymbolReferenceService;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.psi.PsiElement;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlTag;
import io.github.linwancen.plugin.show.bean.LineInfo;
import io.github.linwancen.plugin.show.lang.base.DocFilter;
import io.github.linwancen.plugin.show.lang.base.DocSkip;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class HtmlLangDoc extends JsLangDoc {
private static final Logger LOG = LoggerFactory.getLogger(HtmlLangDoc.class);
public static final Method WEB_DOC_METHOD;
public static final Method REF_METHOD;
static {
LANG_DOC_MAP.put(HTMLLanguage.INSTANCE.getID(), new HtmlLangDoc());
Method method = null;
Method refMethod = null;
try {
Class<?> clazz = Class.forName("com.intellij.webSymbols.WebSymbol");
method = clazz.getMethod("getDescription");
// noinspection UnstableApiUsage
refMethod = PsiSymbolReferenceService.class.getMethod("getReferences", PsiElement.class);
} catch (Exception e) {
LOG.warn("Web Tag Attr Doc is support since 2022.3, {}", e.getLocalizedMessage());
}
WEB_DOC_METHOD = method;
REF_METHOD = refMethod;
}
@Override
public @NotNull List<Class<? extends PsiElement>> getRefClass() {
return List.of(JSPsiReferenceElement.class, XmlAttribute.class, XmlTag.class);
}
@Override
public boolean show(@NotNull LineInfo info) {
return info.appSettings.showLineEndCommentJs;
return info.appSettings.showLineEndCommentJs || info.appSettings.showLineEndCommentHtml;
}
/**
* Override like Java/Json/Html
*/
@SuppressWarnings("UnstableApiUsage")
@Nullable
protected String refDoc(@NotNull LineInfo info, @NotNull PsiElement ref) {
if (DocSkip.skipTagAttr(info, ref)) {
return null;
}
if (WEB_DOC_METHOD == null || !info.appSettings.showLineEndCommentHtml) {
return super.refDoc(info, ref);
}
PsiSymbolReferenceService service = PsiSymbolReferenceService.getService();
ArrayList<PsiSymbolReference> references = new ArrayList<>();
try {
Object object = REF_METHOD.invoke(service, ref);
if (object instanceof Iterable) {
Iterable<?> objects = (Iterable<?>) object;
for (Object o : objects) {
if (o instanceof PsiSymbolReference) {
references.add(((PsiSymbolReference) o));
}
}
}
} catch (ProcessCanceledException ignored) {
return super.refDoc(info, ref);
} catch (Exception e) {
LOG.warn("Web Tag Attr getReferences fail: ", e);
}
if (references.isEmpty()) {
return super.refDoc(info, ref);
}
try {
// v-model:visible should only visible
for (Object symbol : references.get(references.size() - 1).resolveReference()) {
try {
Object doc = WEB_DOC_METHOD.invoke(symbol);
if (doc instanceof String) {
return DocFilter.html2Text((String) doc);
}
} catch (ProcessCanceledException ignored) {
} catch (Exception e) {
LOG.warn("Get Web Tag Attr Doc fail: ", e);
}
}
} catch (ProcessCanceledException ignored) {
} catch (Throwable e) {
LOG.warn("Web Tag Attr Doc resolveReference fail: ", e);
}
return super.refDoc(info, ref);
}
}

View File

@@ -113,6 +113,9 @@ public class DocFilter {
@NotNull
public static String html2Text(@NotNull String s) {
return HTML_PATTERN.matcher(s).replaceAll(" ").trim();
return HTML_PATTERN.matcher(s).replaceAll(" ")
.replace("&lt;", "<")
.replace("&gt;", ">")
.trim();
}
}

View File

@@ -1,5 +1,8 @@
package io.github.linwancen.plugin.show.lang.base;
import com.intellij.psi.PsiElement;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlTag;
import io.github.linwancen.plugin.show.bean.SettingsInfo;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -27,6 +30,21 @@ public class DocSkip {
info.projectSettings.docInclude, info.projectSettings.docExclude);
}
public static <T extends SettingsInfo> boolean skipTagAttr(@NotNull T info, @NotNull PsiElement ref) {
if (ref instanceof XmlTag) {
String text = ((XmlTag) ref).getName();
return skipText(info, text,
info.globalSettings.tagInclude, info.globalSettings.tagExclude,
info.projectSettings.tagInclude, info.projectSettings.tagExclude);
} else if (ref instanceof XmlAttribute) {
String text = ((XmlAttribute) ref).getName();
return skipText(info, text,
info.globalSettings.attrInclude, info.globalSettings.attrExclude,
info.projectSettings.attrInclude, info.projectSettings.attrExclude);
}
return false;
}
static <T extends SettingsInfo> boolean skipText(@NotNull T info, @Nullable String text,
@NotNull Pattern appDocInclude, @NotNull Pattern appDocExclude,
@NotNull Pattern projectDocInclude, @NotNull Pattern projectDocExclude

View File

@@ -23,6 +23,12 @@ public abstract class AbstractSettingsComponent {
private final JBTextField docInclude = new JBTextField();
private final JBTextField docExclude = new JBTextField();
private final JBTextField tagInclude = new JBTextField();
private final JBTextField tagExclude = new JBTextField();
private final JBTextField attrInclude = new JBTextField();
private final JBTextField attrExclude = new JBTextField();
private final JBCheckBox docGetEffect = new JBCheckBox("");
private final JBTextField docGet = new JBTextField();
private final JBCheckBox annoDocEffect = new JBCheckBox("");
@@ -52,6 +58,11 @@ public abstract class AbstractSettingsComponent {
.addSeparator()
.addLabeledComponent(new JBLabel(ShowBundle.message("comment.include.regexp")), docInclude, 1, true)
.addLabeledComponent(new JBLabel(ShowBundle.message("comment.exclude.regexp")), docExclude, 1, true)
.addSeparator()
.addLabeledComponent(new JBLabel(ShowBundle.message("tag.include.regexp")), tagInclude, 1, true)
.addLabeledComponent(new JBLabel(ShowBundle.message("tag.exclude.regexp")), tagExclude, 1, true)
.addLabeledComponent(new JBLabel(ShowBundle.message("attr.include.regexp")), attrInclude, 1, true)
.addLabeledComponent(new JBLabel(ShowBundle.message("attr.exclude.regexp")), attrExclude, 1, true)
.addSeparator();
Border border = docGet.getBorder();
annoDoc.setBorder(border);
@@ -132,6 +143,42 @@ public abstract class AbstractSettingsComponent {
docExclude.setText(newText);
}
@NotNull
public String getTagInclude() {
return tagInclude.getText();
}
public void setTagInclude(@NotNull String newText) {
tagInclude.setText(newText);
}
@NotNull
public String getTagExclude() {
return tagExclude.getText();
}
public void setTagExclude(@NotNull String newText) {
tagExclude.setText(newText);
}
@NotNull
public String getAttrInclude() {
return attrInclude.getText();
}
public void setAttrInclude(@NotNull String newText) {
attrInclude.setText(newText);
}
@NotNull
public String getAttrExclude() {
return attrExclude.getText();
}
public void setAttrExclude(@NotNull String newText) {
attrExclude.setText(newText);
}
public boolean getDocGetEffect() {
return docGetEffect.isSelected();

View File

@@ -14,6 +14,10 @@ public class AbstractSettingsConfigurable {
modified |= !component.getLineExclude().equals(settings.getLineExclude());
modified |= !component.getDocInclude().equals(settings.getDocInclude());
modified |= !component.getDocExclude().equals(settings.getDocExclude());
modified |= !component.getTagInclude().equals(settings.getTagInclude());
modified |= !component.getTagExclude().equals(settings.getTagExclude());
modified |= !component.getAttrInclude().equals(settings.getAttrInclude());
modified |= !component.getAttrExclude().equals(settings.getAttrExclude());
modified |= component.getDocGetEffect() != settings.docGetEffect;
modified |= !component.getDocGet().equals(settings.getDocGet());
modified |= component.getAnnoDocEffect() != settings.annoDocEffect;
@@ -36,6 +40,10 @@ public class AbstractSettingsConfigurable {
settings.setLineExclude(component.getLineExclude());
settings.setDocInclude(component.getDocInclude());
settings.setDocExclude(component.getDocExclude());
settings.setTagInclude(component.getTagInclude());
settings.setTagExclude(component.getTagExclude());
settings.setAttrInclude(component.getAttrInclude());
settings.setAttrExclude(component.getAttrExclude());
settings.docGetEffect = component.getDocGetEffect();
settings.setDocGet(component.getDocGet());
settings.annoDocEffect = component.getAnnoDocEffect();
@@ -53,6 +61,10 @@ public class AbstractSettingsConfigurable {
component.setLineExclude(settings.getLineExclude());
component.setDocInclude(settings.getDocInclude());
component.setDocExclude(settings.getDocExclude());
component.setTagInclude(settings.getTagInclude());
component.setTagExclude(settings.getTagExclude());
component.setAttrInclude(settings.getAttrInclude());
component.setAttrExclude(settings.getAttrExclude());
component.setDocGetEffect(settings.docGetEffect);
component.setDocGet(settings.getDocGet());
component.setAnnoDocEffect(settings.annoDocEffect);

View File

@@ -22,6 +22,14 @@ public abstract class AbstractSettingsState {
public transient Pattern docInclude = Pattern.compile("");
@NotNull
public transient Pattern docExclude = Pattern.compile("");
@NotNull
public transient Pattern tagInclude = Pattern.compile("");
@NotNull
public transient Pattern tagExclude = Pattern.compile("^(template|script|style)$");
@NotNull
public transient Pattern attrInclude = Pattern.compile("");
@NotNull
public transient Pattern attrExclude = Pattern.compile("^(v-[\\w-]*|ref|:?style|:?class|:key|@click|@change|@input)$");
public boolean docGetEffect = false;
@NotNull
@@ -92,6 +100,38 @@ public abstract class AbstractSettingsState {
this.docExclude = Pattern.compile(docExclude);
}
public String getTagInclude() {
return tagInclude.pattern();
}
public void setTagInclude(@NotNull String tagInclude) {
this.tagInclude = Pattern.compile(tagInclude);
}
public String getTagExclude() {
return tagExclude.pattern();
}
public void setTagExclude(@NotNull String tagExclude) {
this.tagExclude = Pattern.compile(tagExclude);
}
public String getAttrInclude() {
return attrInclude.pattern();
}
public void setAttrInclude(@NotNull String attrInclude) {
this.attrInclude = Pattern.compile(attrInclude);
}
public String getAttrExclude() {
return attrExclude.pattern();
}
public void setAttrExclude(@NotNull String attrExclude) {
this.attrExclude = Pattern.compile(attrExclude);
}
public String getDocGet() {
return docGet.pattern();

View File

@@ -43,6 +43,7 @@ public class AppSettingsComponent {
private final JBCheckBox showLineEndCommentSql = new JBCheckBox(" SQL ");
private final JBCheckBox showLineEndCommentJson = new JBCheckBox(" JSON ");
private final JBCheckBox showLineEndCommentYaml = new JBCheckBox(" YAML ");
private final JBCheckBox showLineEndCommentHtml = new JBCheckBox(" HTML ");
private final JBTextField treeTags = new JBTextField();
private final JBTextField lineTags = new JBTextField();
private final JBCheckBox skipAscii = new JBCheckBox(ShowBundle.message("skip.english"));
@@ -101,7 +102,8 @@ public class AppSettingsComponent {
showLineEndCommentRustBase,
showLineEndCommentCBase,
showLineEndCommentSwiftBase
showLineEndCommentSwiftBase,
showLineEndCommentHtml
), 1)
.addLabeledComponent(new JBLabel(ShowBundle.message("tree.tags")), treeTags, 1, true)
.addLabeledComponent(new JBLabel(ShowBundle.message("line.tags")), lineTags, 1, true)
@@ -370,6 +372,14 @@ public class AppSettingsComponent {
showLineEndCommentYaml.setSelected(newStatus);
}
public boolean getShowLineEndCommentHtml() {
return showLineEndCommentHtml.isSelected();
}
public void setShowLineEndCommentHtml(boolean newStatus) {
showLineEndCommentHtml.setSelected(newStatus);
}
// endregion line end
@NotNull

View File

@@ -64,6 +64,7 @@ public class AppSettingsConfigurable implements Configurable {
modified |= mySettingsComponent.getShowLineEndCommentSql() != settings.showLineEndCommentSql;
modified |= mySettingsComponent.getShowLineEndCommentJson() != settings.showLineEndCommentJson;
modified |= mySettingsComponent.getShowLineEndCommentYaml() != settings.showLineEndCommentYaml;
modified |= mySettingsComponent.getShowLineEndCommentHtml() != settings.showLineEndCommentHtml;
modified |= !mySettingsComponent.getTreeTags().equals(String.join("|", settings.treeTags));
modified |= !mySettingsComponent.getLineTags().equals(String.join("|", settings.lineTags));
@@ -115,6 +116,7 @@ public class AppSettingsConfigurable implements Configurable {
settings.showLineEndCommentSql = mySettingsComponent.getShowLineEndCommentSql();
settings.showLineEndCommentJson = mySettingsComponent.getShowLineEndCommentJson();
settings.showLineEndCommentYaml = mySettingsComponent.getShowLineEndCommentYaml();
settings.showLineEndCommentHtml = mySettingsComponent.getShowLineEndCommentHtml();
settings.treeTags = Splitter.on('|').splitToList(mySettingsComponent.getTreeTags()).toArray(new String[0]);
settings.lineTags = Splitter.on('|').splitToList(mySettingsComponent.getLineTags()).toArray(new String[0]);
@@ -169,6 +171,7 @@ public class AppSettingsConfigurable implements Configurable {
mySettingsComponent.setShowLineEndCommentSql(settings.showLineEndCommentSql);
mySettingsComponent.setShowLineEndCommentJson(settings.showLineEndCommentJson);
mySettingsComponent.setShowLineEndCommentYaml(settings.showLineEndCommentYaml);
mySettingsComponent.setShowLineEndCommentHtml(settings.showLineEndCommentHtml);
mySettingsComponent.setTreeTags(String.join("|", settings.treeTags));
mySettingsComponent.setLineTags(String.join("|", settings.lineTags));

View File

@@ -52,6 +52,7 @@ public class AppSettingsState implements PersistentStateComponent<AppSettingsSta
public boolean showLineEndCommentSql = true;
public boolean showLineEndCommentJson = true;
public boolean showLineEndCommentYaml = true;
public boolean showLineEndCommentHtml = true;
@NotNull
public String[] treeTags = {"author"};

View File

@@ -20,6 +20,8 @@ public class ProjectSettingsState extends AbstractSettingsState implements Persi
public ProjectSettingsState() {
this.lineEndCount = 0;
this.lineExclude = Pattern.compile("");
this.tagExclude = Pattern.compile("");
this.attrExclude = Pattern.compile("");
this.annoDocEffect = false;
this.annoDoc = new String[][]{};
this.dirDocEffect = false;

View File

@@ -29,6 +29,10 @@ sign.include.regexp=className#memberName include Regexp:
sign.exclude.regexp=className#memberName exclude Regexp:
comment.include.regexp=comment include Regexp:
comment.exclude.regexp=comment exclude Regexp:
tag.include.regexp=tag include Regexp:
tag.exclude.regexp=tag exclude Regexp:
attr.include.regexp=attr include Regexp:
attr.exclude.regexp=attr exclude Regexp:
get.doc.regexp=get doc Regexp, last () when had, default is first sentence .+?(?:[\u3002\r\n]|. ) :
anno.doc=anno doc, class/method/field/!doc/all#AnnoFullName#methodName#methodName2, Separated by \\n:
dir.doc.regexp=dir doc, fileName||Regexp1||Regexp2\\n:

View File

@@ -29,6 +29,10 @@ sign.include.regexp=\u7C7B#\u65B9\u6CD5 \u5305\u542B \u6B63\u5219\uFF1A
sign.exclude.regexp=\u7C7B#\u65B9\u6CD5 \u6392\u9664 \u6B63\u5219\uFF1A
comment.include.regexp=\u6CE8\u91CA \u5305\u542B \u6B63\u5219\uFF1A
comment.exclude.regexp=\u6CE8\u91CA \u6392\u9664 \u6B63\u5219\uFF1A
tag.include.regexp=\u6807\u7B7E \u5305\u542B \u6B63\u5219\uFF1A
tag.exclude.regexp=\u6807\u7B7E \u6392\u9664 \u6B63\u5219\uFF1A
attr.include.regexp=\u5C5E\u6027 \u5305\u542B \u6B63\u5219\uFF1A
attr.exclude.regexp=\u5C5E\u6027 \u6392\u9664 \u6B63\u5219\uFF1A
get.doc.regexp=\u6CE8\u91CA\u63D0\u53D6\u6B63\u5219\uFF0C\u82E5\u6709 () \u5219\u53D6\u6700\u540E\u4E00\u4E2A\uFF0C\u9ED8\u8BA4\u6B63\u5219\u662F\u7B2C\u4E00\u53E5 .+?(?:[\u3002\r\n]|. )\uFF1A
anno.doc=\u6CE8\u89E3\u6CE8\u91CA\uFF0Cclass/method/field/!doc/all#\u6CE8\u89E3\u5168\u540D#\u65B9\u6CD5\u540D1#\u65B9\u6CD5\u540D2 \u6362\u884C\u5206\u9694\uFF1A
dir.doc.regexp=\u76EE\u5F55\u6CE8\u91CA\uFF0C\u6587\u4EF6\u540D||\u6B63\u52191||\u6B63\u52192\\n\uFF1A