feat: 2.27 use 1st or 2nd comment | 用第 1 或第 2 行注释当文件注释

This commit is contained in:
林万程
2025-10-05 20:32:02 +08:00
parent f3762a1c91
commit cb7b979243
12 changed files with 193 additions and 143 deletions

View File

@@ -4,7 +4,7 @@ plugins {
}
group 'io.github.linwancen'
version '2.26.0.' + (new Date().format('yyyy.MM.dd_HH.mm'))
version '2.27.0.' + (new Date().format('yyyy.MM.dd_HH.mm'))
patchPluginXml {
// The performance of 2019.3 has been greatly improved.
@@ -15,6 +15,7 @@ patchPluginXml {
changeNotes = """
<h2>English Change Notes:</h2>
<ul>
<li>2.27 Add project-view-tree use 1st or 2nd line to file comment
<li>2.26 Add External Comment load *.sql ddl comment
<li>2.25 Add project-view-tree use xml/html/vue 1st or 2nd line comment
<li>2.24 Add line-end-comment Maven pom.xml \${} doc
@@ -70,6 +71,7 @@ patchPluginXml {
<h2>中文更新说明:</h2>
<ul>
<li>2.27 增加 文件树注释 用第 1 或第 2 行注释当文件注释
<li>2.26 增加 外部注释 读取 *.sql ddl 注释
<li>2.25 增加 文件树注释 用 xml/html/vue 第 1 或第 2 行注释当文件注释
<li>2.24 增加 行末注释 Maven pom.xml \${} 注释

View File

@@ -19,6 +19,7 @@ import io.github.linwancen.plugin.show.ext.TreeExt;
import io.github.linwancen.plugin.show.lang.base.BaseLangDoc;
import io.github.linwancen.plugin.show.settings.AppSettingsState;
import io.github.linwancen.plugin.show.tree.RelFileDoc;
import io.github.linwancen.plugin.show.tree.first.FirstDoc;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
@@ -104,7 +105,7 @@ public class Tree implements ProjectViewNodeDecorator {
return s;
}
}
return null;
return FirstDoc.firstDoc(node, info);
}
@Override

View File

@@ -1,21 +1,13 @@
package io.github.linwancen.plugin.show.lang;
import com.intellij.ide.projectView.ProjectViewNode;
import com.intellij.lang.xml.XMLLanguage;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiManager;
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.bean.SettingsInfo;
import io.github.linwancen.plugin.show.lang.base.BaseLangDoc;
import io.github.linwancen.plugin.show.lang.base.PsiUnSaveUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -37,53 +29,6 @@ public class XmlLangDoc extends BaseLangDoc {
return true;
}
@Override
public @Nullable <T extends SettingsInfo> String treeDoc(@NotNull T info, @NotNull ProjectViewNode<?> node,
@NotNull Project project) {
@Nullable VirtualFile virtualFile = node.getVirtualFile();
if (virtualFile == null || virtualFile.isDirectory()) {
return null;
}
@Nullable FileViewProvider viewProvider = PsiManager.getInstance(project).findViewProvider(virtualFile);
if (viewProvider == null) {
return null;
}
@Nullable PsiElement psiElement = viewProvider.findElementAt(0);
if (psiElement == null || !"<!--".equals(PsiUnSaveUtils.getText(psiElement))) {
@Nullable Document document = viewProvider.getDocument();
if (document == null) {
return null;
}
int lineCount = document.getLineCount();
// lineNumber start 0, as 1 <= 1 should return
if (lineCount <= 1) {
return null;
}
// because in html 1st line must <!DOCTYPE html>
int i = document.getLineStartOffset(1);
psiElement = viewProvider.findElementAt(i);
}
if (psiElement == null || !"<!--".equals(PsiUnSaveUtils.getText(psiElement))) {
return null;
}
PsiElement parent = psiElement.getParent();
if (!(parent instanceof PsiComment)) {
return null;
}
@NotNull PsiElement[] children = parent.getChildren();
if (children.length < 2) {
return null;
}
@Nullable String doc = PsiUnSaveUtils.getText(children[1]);
// Copyright or copyright
//noinspection SpellCheckingInspection
if (doc == null || doc.contains("opyright")) {
return null;
}
XmlCache.indexDocToDirDoc(virtualFile, doc);
return doc;
}
@Nullable
@Override
public String findRefDoc(@NotNull LineInfo info, @NotNull FileViewProvider viewProvider, @NotNull PsiElement element) {

View File

@@ -25,7 +25,10 @@ public class DocFilter {
// #### xx python and sh start
"|^ *#++ *+" +
// -- xx SQL
"|^ *--++ *+"
"|^ *--++ *+" +
// <!-- xml -->
"|.*<!--\\s*" +
"|\\s*-->.*"
);
private static final Pattern LINE_SEPARATOR_PATTERN = Pattern.compile("[\r\n]");

View File

@@ -18,6 +18,7 @@ public class AppSettingsComponent {
private final JBCheckBox showTreeComment = new JBCheckBox(ShowBundle.message("show.tree.comment"));
private final JBCheckBox compact = new JBCheckBox(ShowBundle.message("compact"));
private final JBCheckBox treeCache = new JBCheckBox(ShowBundle.message("tree.cache"));
private final JBCheckBox treeFirst = new JBCheckBox(ShowBundle.message("tree.first"));
private final JBCheckBox showLineEndComment = new JBCheckBox(ShowBundle.message("show.line.end.comment"));
private final JBCheckBox lineEndCache = new JBCheckBox(ShowBundle.message("line.end.cache"));
private final JBCheckBox showLineEndCommentJava = new JBCheckBox(" Java ");
@@ -57,86 +58,7 @@ public class AppSettingsComponent {
private final ColorPanel lineEndJsonColor = new ColorPanel();
private final JBTextField lineEndPrefix = new JBTextField();
public AppSettingsComponent() {
@NotNull JButton resetDefault = new JButton(ShowBundle.message("reset.default"));
resetDefault.addActionListener(e -> AppSettingsConfigurable.reset(AppSettingsState.DEFAULT_SETTING, this));
myMainPanel = FormBuilder.createFormBuilder()
.addComponent(JPanelFactory.of(resetDefault), 1)
.addComponent(showPanel(), 1)
.addComponent(lineEndFilterPanel(), 1)
.addComponentFillVertically(new JPanel(), 0)
.getPanel();
}
@NotNull
private JPanel showPanel() {
JPanel comment = FormBuilder.createFormBuilder()
.addComponent(JPanelFactory.of(showTreeComment, treeCache, compact), 1)
.addComponent(JPanelFactory.of(showLineEndComment, lineEndCache), 1)
.addComponent(JPanelFactory.of(
showLineEndCommentJava,
showLineEndCommentKotlin,
showLineEndCommentScala,
showLineEndCommentGroovy
), 1)
.addComponent(JPanelFactory.of(
showLineEndCommentJavaBase,
showLineEndCommentKotlinBase,
showLineEndCommentScalaBase,
showLineEndCommentGroovyBase
), 1)
.addComponent(JPanelFactory.of(
showLineEndCommentJs,
showLineEndCommentPhp,
showLineEndCommentPy,
showLineEndCommentGo,
showLineEndCommentSql,
showLineEndCommentJson,
showLineEndCommentYaml
), 1)
.addComponent(JPanelFactory.of(
showLineEndCommentJsBase,
showLineEndCommentPhpBase,
showLineEndCommentPyBase,
showLineEndCommentRustBase,
showLineEndCommentCBase,
showLineEndCommentSwiftBase,
showLineEndCommentHtml
), 1)
.addLabeledComponent(new JBLabel(ShowBundle.message("tree.tags")), treeTags, 1, true)
.addLabeledComponent(new JBLabel(ShowBundle.message("line.tags")), lineTags, 1, true)
.getPanel();
comment.setBorder(IdeBorderFactory.createTitledBorder(ShowBundle.message("show")));
return comment;
}
@NotNull
protected JPanel lineEndFilterPanel() {
@NotNull JPanel text = JPanelFactory.of(
new JBLabel(ShowBundle.message("text.color")), lineEndColor,
new JBLabel(ShowBundle.message("text.color.json")), lineEndJsonColor,
new JBLabel(ShowBundle.message("prefix")), lineEndPrefix);
FormBuilder formBuilder = FormBuilder.createFormBuilder()
.addSeparator()
.addComponent(JPanelFactory.of(skipAscii, skipBlank, skipAnnotation, getToSet), 1)
.addComponent(JPanelFactory.of(fromNew, fromParam, enumDoc), 1)
.addSeparator()
.addComponent(text)
.addSeparator();
return formBuilder.getPanel();
}
public JPanel getPanel() {
return myMainPanel;
}
@NotNull
public JComponent getPreferredFocusedComponent() {
return showTreeComment;
}
// move get set near field for AI
public boolean getShowTreeComment() {
return showTreeComment.isSelected();
@@ -162,6 +84,14 @@ public class AppSettingsComponent {
treeCache.setSelected(newStatus);
}
public boolean getTreeFirst() {
return treeFirst.isSelected();
}
public void setTreeFirst(boolean newStatus) {
treeFirst.setSelected(newStatus);
}
@NotNull
public String getTreeTags() {
return treeTags.getText();
@@ -473,4 +403,85 @@ public class AppSettingsComponent {
public void setLineEndPrefix(@NotNull String newText) {
lineEndPrefix.setText(newText);
}
public AppSettingsComponent() {
@NotNull JButton resetDefault = new JButton(ShowBundle.message("reset.default"));
resetDefault.addActionListener(e -> AppSettingsConfigurable.reset(AppSettingsState.DEFAULT_SETTING, this));
myMainPanel = FormBuilder.createFormBuilder()
.addComponent(JPanelFactory.of(resetDefault), 1)
.addComponent(showPanel(), 1)
.addComponent(lineEndFilterPanel(), 1)
.addComponentFillVertically(new JPanel(), 0)
.getPanel();
}
@NotNull
private JPanel showPanel() {
JPanel comment = FormBuilder.createFormBuilder()
.addComponent(JPanelFactory.of(showTreeComment, treeCache, compact, treeFirst), 1)
.addComponent(JPanelFactory.of(showLineEndComment, lineEndCache), 1)
.addComponent(JPanelFactory.of(
showLineEndCommentJava,
showLineEndCommentKotlin,
showLineEndCommentScala,
showLineEndCommentGroovy
), 1)
.addComponent(JPanelFactory.of(
showLineEndCommentJavaBase,
showLineEndCommentKotlinBase,
showLineEndCommentScalaBase,
showLineEndCommentGroovyBase
), 1)
.addComponent(JPanelFactory.of(
showLineEndCommentJs,
showLineEndCommentPhp,
showLineEndCommentPy,
showLineEndCommentGo,
showLineEndCommentSql,
showLineEndCommentJson,
showLineEndCommentYaml
), 1)
.addComponent(JPanelFactory.of(
showLineEndCommentJsBase,
showLineEndCommentPhpBase,
showLineEndCommentPyBase,
showLineEndCommentRustBase,
showLineEndCommentCBase,
showLineEndCommentSwiftBase,
showLineEndCommentHtml
), 1)
.addLabeledComponent(new JBLabel(ShowBundle.message("tree.tags")), treeTags, 1, true)
.addLabeledComponent(new JBLabel(ShowBundle.message("line.tags")), lineTags, 1, true)
.getPanel();
comment.setBorder(IdeBorderFactory.createTitledBorder(ShowBundle.message("show")));
return comment;
}
@NotNull
protected JPanel lineEndFilterPanel() {
@NotNull JPanel text = JPanelFactory.of(
new JBLabel(ShowBundle.message("text.color")), lineEndColor,
new JBLabel(ShowBundle.message("text.color.json")), lineEndJsonColor,
new JBLabel(ShowBundle.message("prefix")), lineEndPrefix);
FormBuilder formBuilder = FormBuilder.createFormBuilder()
.addSeparator()
.addComponent(JPanelFactory.of(skipAscii, skipBlank, skipAnnotation, getToSet), 1)
.addComponent(JPanelFactory.of(fromNew, fromParam, enumDoc), 1)
.addSeparator()
.addComponent(text)
.addSeparator();
return formBuilder.getPanel();
}
public JPanel getPanel() {
return myMainPanel;
}
@NotNull
public JComponent getPreferredFocusedComponent() {
return showTreeComment;
}
}

View File

@@ -38,6 +38,7 @@ public class AppSettingsConfigurable implements Configurable {
boolean modified = mySettingsComponent.getShowTreeComment() != settings.showTreeComment;
modified |= mySettingsComponent.getCompact() != settings.compact;
modified |= mySettingsComponent.getTreeCache() != settings.treeCache;
modified |= mySettingsComponent.getTreeFirst() != settings.treeFirst;
modified |= mySettingsComponent.getShowLineEndComment() != settings.showLineEndComment;
modified |= mySettingsComponent.getLineEndCache() != settings.lineEndCache;
@@ -90,6 +91,7 @@ public class AppSettingsConfigurable implements Configurable {
settings.showTreeComment = mySettingsComponent.getShowTreeComment();
settings.compact = mySettingsComponent.getCompact();
settings.treeCache = mySettingsComponent.getTreeCache();
settings.treeFirst = mySettingsComponent.getTreeFirst();
settings.showLineEndComment = mySettingsComponent.getShowLineEndComment();
settings.lineEndCache = mySettingsComponent.getLineEndCache();
@@ -145,6 +147,7 @@ public class AppSettingsConfigurable implements Configurable {
mySettingsComponent.setShowTreeComment(settings.showTreeComment);
mySettingsComponent.setCompact(settings.compact);
mySettingsComponent.setTreeCache(settings.treeCache);
mySettingsComponent.setTreeFirst(settings.treeFirst);
mySettingsComponent.setShowLineEndComment(settings.showLineEndComment);
mySettingsComponent.setLineEndCache(settings.lineEndCache);

View File

@@ -26,6 +26,7 @@ public class AppSettingsState implements PersistentStateComponent<AppSettingsSta
public boolean showTreeComment = true;
public boolean compact = true;
public boolean treeCache = false;
public boolean treeFirst = true;
public boolean showLineEndComment = true;
public boolean lineEndCache = true;

View File

@@ -0,0 +1,82 @@
package io.github.linwancen.plugin.show.tree.first;
import com.intellij.ide.projectView.ProjectViewNode;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiManager;
import io.github.linwancen.plugin.show.bean.SettingsInfo;
import io.github.linwancen.plugin.show.lang.base.DocFilter;
import io.github.linwancen.plugin.show.lang.base.PsiUnSaveUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FirstDoc {
private FirstDoc() {}
@Nullable
public static String firstDoc(ProjectViewNode<?> node, @NotNull SettingsInfo info) {
if (!info.appSettings.treeFirst) {
return null;
}
@Nullable VirtualFile virtualFile = node.getVirtualFile();
if (virtualFile == null || virtualFile.isDirectory()) {
return null;
}
Project project = node.getProject();
if (project == null) {
return null;
}
@Nullable FileViewProvider viewProvider = PsiManager.getInstance(project).findViewProvider(virtualFile);
if (viewProvider == null) {
return null;
}
@Nullable PsiElement psiElement = viewProvider.findElementAt(0);
if (psiElement == null || notDoc(psiElement)) {
@Nullable Document document = viewProvider.getDocument();
if (document == null) {
return null;
}
int lineCount = document.getLineCount();
// lineNumber start 0, as 1 <= 1 should return
if (lineCount <= 1) {
return null;
}
// because in HTML 1st line must <!DOCTYPE HTML>
int i = document.getLineStartOffset(1);
psiElement = viewProvider.findElementAt(i);
if (psiElement == null || notDoc(psiElement)) {
return null;
}
}
if (!(psiElement instanceof PsiComment)) {
psiElement = psiElement.getParent();
if (!(psiElement instanceof PsiComment)) {
return null;
}
}
@NotNull String doc = PsiUnSaveUtils.getText(psiElement);
// Copyright or copyright
//noinspection SpellCheckingInspection
if (doc.contains("opyright")) {
return null;
}
String cutDoc = DocFilter.cutDoc(doc, info, true);
FirstDocToDirCache.indexDocToDirDoc(virtualFile, cutDoc);
return cutDoc;
}
private static final Pattern COMMENT_PATTERN = Pattern.compile("<!--|/\\*|//|#(?!!)");
private static boolean notDoc(@NotNull PsiElement psiElement) {
String text = PsiUnSaveUtils.getText(psiElement);
Matcher matcher = COMMENT_PATTERN.matcher(text);
return !matcher.find();
}
}

View File

@@ -1,4 +1,4 @@
package io.github.linwancen.plugin.show.lang;
package io.github.linwancen.plugin.show.tree.first;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
@@ -7,15 +7,15 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* for html, in here because {@code <!--} is based XML
* index.html index.vue index.js
*/
public class XmlCache extends FileLoader {
public class FirstDocToDirCache extends FileLoader {
public static void indexDocToDirDoc(@NotNull VirtualFile virtualFile, @Nullable String doc) {
if (doc != null && "index".equals(virtualFile.getNameWithoutExtension())) {
VirtualFile parent = virtualFile.getParent();
if (parent != null) {
@Nullable XmlCache extension = FileLoader.EPN.findExtension(XmlCache.class);
@Nullable FirstDocToDirCache extension = FileLoader.EPN.findExtension(FirstDocToDirCache.class);
if (extension != null) {
extension.fileDoc.put(parent, doc);
}

View File

@@ -170,7 +170,7 @@ Show doc comment in the Project view Tree, line End, json, other
<extensions defaultExtensionNs="io.github.linwancen.show-comment">
<fileLoader implementation="io.github.linwancen.plugin.show.ext.conf.ConfCache"/>
<fileLoader implementation="io.github.linwancen.plugin.show.ext.sql.SqlCache"/>
<fileLoader implementation="io.github.linwancen.plugin.show.lang.XmlCache"/>
<fileLoader implementation="io.github.linwancen.plugin.show.tree.first.FirstDocToDirCache"/>
<fileLoader implementation="io.github.linwancen.plugin.show.ext.ollama.OllamaModelsCache"/>
</extensions>

View File

@@ -5,6 +5,7 @@ show=Show
show.tree.comment=Show tree comment
compact=Support compact
tree.cache=Tree Comment Cache
tree.first=Tree Comment From 1st/2nd Line
show.line.end.comment=Show line end comment
line.end.cache=Line End Comment Cache for 2023.3 Slow EDT

View File

@@ -5,6 +5,7 @@ show=\u663E\u793A
show.tree.comment=\u663E\u793A\u6587\u4EF6\u6811\u6CE8\u91CA
compact=\u652F\u6301\u538B\u7F29\u7A7A\u7684\u4E2D\u95F4\u5305
tree.cache=\u6587\u4EF6\u6811\u6CE8\u91CA\u7F13\u5B58
tree.first=\u6587\u4EF6\u6811\u6CE8\u91CA\u4ECE\u4E00\u4E8C\u884C\u83B7\u53D6
show.line.end.comment=\u663E\u793A\u884C\u672B\u6CE8\u91CA
line.end.cache=\u884C\u672B\u6CE8\u91CA\u7F13\u5B58\u7528\u4E8E 2023.3 Slow EDT