From 1122637819037ad37cc702bc6f2b0e3b09012700 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=9E=97=E4=B8=87=E7=A8=8B?= <1498425439@qq.com>
Date: Wed, 9 Mar 2022 03:28:12 +0800
Subject: [PATCH] =?UTF-8?q?1.9=20Add=20project-view-tree-comment=20=20for?=
=?UTF-8?q?=20"xx=20ClassNameOrSimpleName.json"=20and=20SPI=20file=20|=20?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0=20"xx=20=E7=B1=BB=E5=85=A8=E5=90=8D=E6=88=96?=
=?UTF-8?q?=E7=AE=80=E5=90=8D.json"=20=E5=92=8C=20SPI=20=E9=A1=B9=E7=9B=AE?=
=?UTF-8?q?=E5=AF=BC=E8=88=AA=E6=A0=8F=E6=B3=A8=E9=87=8A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 6 +-
build.gradle | 4 +-
.../linwancen/plugin/show/doc/DocUtils.java | 17 +++-
.../plugin/show/doc/JsonDocUtils.java | 99 +++++++++----------
.../plugin/show/doc/PsiClassUtils.java | 54 ++++++++++
src/main/resources/META-INF/plugin.xml | 4 +-
6 files changed, 124 insertions(+), 60 deletions(-)
create mode 100644 src/main/java/io/github/linwancen/plugin/show/doc/PsiClassUtils.java
diff --git a/README.md b/README.md
index 9d860a7..242a844 100644
--- a/README.md
+++ b/README.md
@@ -5,7 +5,7 @@ English Notes:
- Show javadoc comments at the Project view Tree structure.
- Show javadoc comments at the end-of-line.
-
- Show javadoc comments at "xx ClassNameOrSimpleName.json" end-of-line.
+
- Show javadoc comments at "xx ClassNameOrSimpleName.json".
- One of the above features can be turned off in
settings -> Tools -> Show Comment Global
- Font color of end-of-line comments can be modified in
@@ -17,7 +17,7 @@ Chinese Notes:
- 在结构树显示 文档注释。
- 在行末尾显示 文档注释。
-
- 行末注释支持 "xx 类全名或简名.json"。
+
- 支持 "xx 类全名或简名.json"。
- 可以在设置中 关闭 上面其中一个功能。
- 可以在设置中 修改 行末注释 的字体颜色。
- 可以在设置中 修改 行末注释 的类名前缀过滤。
@@ -25,6 +25,7 @@ Chinese Notes:
English Change Notes:
+- 1.9 Add project-view-tree-comment for "xx ClassNameOrSimpleName.json" and SPI file
- 1.8 Add line-end-comment for "xx ClassNameOrSimpleName.json"
- 1.7 Add line-end-comment setting for prefix and count
- 1.6 Add line-end-comment independent switch for call, new, ref
@@ -37,6 +38,7 @@ English Change Notes:
Chinese Change Notes:
+- 1.9 增加 "xx 类全名或简名.json" 和 SPI 项目导航栏注释
- 1.8 增加 "xx 类全名或简名.json" 行末注释
- 1.7 增加 行末注释前缀和对象数设置
- 1.6 增加 行末调用,new,引用注释独立开关
diff --git a/build.gradle b/build.gradle
index 1b25494..f905dad 100644
--- a/build.gradle
+++ b/build.gradle
@@ -4,7 +4,7 @@ plugins {
}
group 'io.github.linwancen'
-version '1.8.0.' + (new Date().format('yyyy.MM.dd_HH.mm'))
+version '1.9.0.' + (new Date().format('yyyy.MM.dd_HH.mm'))
apply plugin: 'java'
@@ -39,6 +39,7 @@ patchPluginXml {
changeNotes = """
English Change Notes:
+- 1.9 Add project-view-tree-comment for "xx ClassNameOrSimpleName.json" and SPI file
- 1.8 Add line-end-comment for "xx ClassNameOrSimpleName.json"
- 1.7 Add line-end-comment setting for prefix and count
- 1.6 Add line-end-comment independent switch for call, new, ref
@@ -51,6 +52,7 @@ English Change Notes:
Chinese Change Notes:
+- 1.9 增加 "xx 类全名或简名.json" 和 SPI 项目导航栏注释
- 1.8 增加 "xx 类全名或简名.json" 行末注释
- 1.7 增加 行末注释前缀和对象数设置
- 1.6 增加 行末调用,new,引用注释独立开关
diff --git a/src/main/java/io/github/linwancen/plugin/show/doc/DocUtils.java b/src/main/java/io/github/linwancen/plugin/show/doc/DocUtils.java
index becc2e7..227c05b 100644
--- a/src/main/java/io/github/linwancen/plugin/show/doc/DocUtils.java
+++ b/src/main/java/io/github/linwancen/plugin/show/doc/DocUtils.java
@@ -1,5 +1,6 @@
package io.github.linwancen.plugin.show.doc;
+import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.javadoc.PsiDocComment;
import org.jetbrains.annotations.Nullable;
@@ -36,12 +37,26 @@ public class DocUtils {
@Nullable
public static PsiDocComment fileDoc(PsiFile psiFile) {
if (!(psiFile instanceof PsiClassOwner)) {
+ // for SPI
+ PsiClass[] psiClasses = PsiClassUtils.nameToClass(psiFile.getName(), psiFile.getProject());
+ // for "xxx ClassName.xxx"
+ if (psiClasses.length == 0) {
+ VirtualFile virtualFile = psiFile.getVirtualFile();
+ psiClasses = PsiClassUtils.encClass(virtualFile, psiFile.getProject());
+ }
+ for (PsiClass psiClass : psiClasses) {
+ PsiDocComment docComment = srcOrByteCodeDoc(psiClass);
+ if (docComment != null) {
+ // Inaccurate when there are classes with the same name
+ return docComment;
+ }
+ }
return null;
}
- PsiClassOwner psiClassOwner = (PsiClassOwner) psiFile;
if (PsiPackage.PACKAGE_INFO_FILE.equals(psiFile.getName())) {
return PackageDocUtils.fromPackageInfoFile(psiFile);
}
+ PsiClassOwner psiClassOwner = (PsiClassOwner) psiFile;
PsiClass[] classes = psiClassOwner.getClasses();
if (classes.length == 0) {
return null;
diff --git a/src/main/java/io/github/linwancen/plugin/show/doc/JsonDocUtils.java b/src/main/java/io/github/linwancen/plugin/show/doc/JsonDocUtils.java
index 28bcfb2..7a6861f 100644
--- a/src/main/java/io/github/linwancen/plugin/show/doc/JsonDocUtils.java
+++ b/src/main/java/io/github/linwancen/plugin/show/doc/JsonDocUtils.java
@@ -2,22 +2,17 @@ package io.github.linwancen.plugin.show.doc;
import com.intellij.json.psi.JsonProperty;
import com.intellij.openapi.project.Project;
-import com.intellij.psi.JavaPsiFacade;
-import com.intellij.psi.PsiClass;
-import com.intellij.psi.PsiElement;
-import com.intellij.psi.PsiField;
+import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.psi.*;
import com.intellij.psi.javadoc.PsiDocComment;
-import com.intellij.psi.search.GlobalSearchScope;
-import com.intellij.psi.search.PsiShortNamesCache;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
+import java.util.ArrayList;
+import java.util.List;
public class JsonDocUtils {
- private static final Pattern JSON_PATTERN = Pattern.compile("[\\w.]*+$");
private JsonDocUtils() {}
@@ -27,60 +22,56 @@ public class JsonDocUtils {
if (jsonProp == null || jsonProp.getNameElement().getTextRange().getEndOffset() > endOffset) {
return null;
}
- String fileName = element.getContainingFile().getVirtualFile().getNameWithoutExtension();
- Matcher matcher = JSON_PATTERN.matcher(fileName);
- if (!matcher.find()) {
- return null;
- }
- String className = matcher.group();
- PsiClass[] psiClasses = classByName(className, element.getProject());
- PsiField psiField = psiField(psiClasses, element.getProject(), jsonProp);
- if (psiField != null) {
- return DocUtils.srcOrByteCodeDoc(psiField);
- }
- return null;
+ VirtualFile virtualFile = element.getContainingFile().getVirtualFile();
+ PsiClass[] psiClasses = PsiClassUtils.encClass(virtualFile, element.getProject());
+ List jsonPath = jsonPath(jsonProp);
+ return doc(psiClasses, element.getProject(), jsonPath, jsonPath.size() - 1);
}
@NotNull
- private static PsiClass[] classByName(String className, @NotNull Project project) {
- int i = className.indexOf('.');
- if (i > 0) {
- return classByFullName(className, project);
- }
- PsiShortNamesCache namesCache = PsiShortNamesCache.getInstance(project);
- return namesCache.getClassesByName(className, GlobalSearchScope.allScope(project));
- }
-
- @NotNull
- private static PsiClass[] classByFullName(String className, @NotNull Project project) {
- JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(project);
- return javaPsiFacade.findClasses(className, GlobalSearchScope.allScope(project));
+ public static List jsonPath(JsonProperty jsonProp) {
+ ArrayList jsonPath = new ArrayList<>();
+ do {
+ jsonPath.add(jsonProp.getName());
+ } while ((jsonProp = PsiTreeUtil.getParentOfType(jsonProp, JsonProperty.class)) != null);
+ return jsonPath;
}
@Nullable
- private static PsiField psiField(PsiClass[] rootClasses, Project project, JsonProperty jsonProp) {
- JsonProperty parentJsonProp = PsiTreeUtil.getParentOfType(jsonProp, JsonProperty.class);
- if (parentJsonProp == null) {
- for (PsiClass c : rootClasses) {
- PsiField field = c.findFieldByName(jsonProp.getName(), true);
- if (field != null) {
- return field;
- }
+ private static PsiDocComment doc(PsiClass[] psiClasses, Project project, List jsonPath, int level) {
+ String name = jsonPath.get(level);
+ for (PsiClass psiClass : psiClasses) {
+ PsiField psiField = psiClass.findFieldByName(name, true);
+ if (psiField == null) {
+ return null;
}
- return null;
- }
- PsiField psiField = psiField(rootClasses, project, parentJsonProp);
- if (psiField == null) {
- return null;
- }
- String classFullName = psiField.getType().getCanonicalText();
- @NotNull PsiClass[] psiClasses = classByFullName(classFullName, project);
- for (PsiClass c : psiClasses) {
- PsiField field = c.findFieldByName(jsonProp.getName(), true);
- if (field != null) {
- return field;
+ if (level == 0) {
+ return DocUtils.srcOrByteCodeDoc(psiField);
+ }
+ String classFullName = toClassFullName(psiField);
+ PsiClass[] classes = PsiClassUtils.fullNameToClass(classFullName, project);
+ PsiDocComment docComment = doc(classes, project, jsonPath, level - 1);
+ if (docComment != null) {
+ return docComment;
}
}
return null;
}
+
+ @NotNull
+ private static String toClassFullName(PsiField psiField) {
+ PsiTypeElement typeElement = psiField.getTypeElement();
+ if (typeElement != null) {
+ PsiJavaCodeReferenceElement code = typeElement.getInnermostComponentReferenceElement();
+ if (code != null) {
+ PsiType[] types = code.getTypeParameters();
+ if (types.length > 0) {
+ // List
+ return types[types.length - 1].getCanonicalText();
+ }
+ }
+ }
+ // Array
+ return psiField.getType().getDeepComponentType().getCanonicalText();
+ }
}
diff --git a/src/main/java/io/github/linwancen/plugin/show/doc/PsiClassUtils.java b/src/main/java/io/github/linwancen/plugin/show/doc/PsiClassUtils.java
new file mode 100644
index 0000000..46d823d
--- /dev/null
+++ b/src/main/java/io/github/linwancen/plugin/show/doc/PsiClassUtils.java
@@ -0,0 +1,54 @@
+package io.github.linwancen.plugin.show.doc;
+
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.psi.JavaPsiFacade;
+import com.intellij.psi.PsiClass;
+import com.intellij.psi.search.GlobalSearchScope;
+import com.intellij.psi.search.PsiShortNamesCache;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class PsiClassUtils {
+
+ private PsiClassUtils() {}
+
+ private static final Pattern JSON_PATTERN = Pattern.compile("[\\w.]*+$");
+
+ @NotNull
+ public static PsiClass[] encClass(@NotNull VirtualFile virtualFile, @NotNull Project project) {
+ String fileName = virtualFile.getNameWithoutExtension();
+ Matcher matcher = JSON_PATTERN.matcher(fileName);
+ if (!matcher.find()) {
+ return new PsiClass[0];
+ }
+ String className = matcher.group();
+ return nameToClass(className, project);
+ }
+
+ @NotNull
+ public static PsiClass[] nameToClass(@Nullable String className, @NotNull Project project) {
+ if (className == null) {
+ return new PsiClass[0];
+ }
+ int i = className.indexOf('.');
+ return i > 0
+ ? fullNameToClass(className, project)
+ : simpleNameToClass(className, project);
+ }
+
+ @NotNull
+ public static PsiClass[] simpleNameToClass(@NotNull String className, @NotNull Project project) {
+ PsiShortNamesCache namesCache = PsiShortNamesCache.getInstance(project);
+ return namesCache.getClassesByName(className, GlobalSearchScope.allScope(project));
+ }
+
+ @NotNull
+ public static PsiClass[] fullNameToClass(@NotNull String className, @NotNull Project project) {
+ JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(project);
+ return javaPsiFacade.findClasses(className, GlobalSearchScope.allScope(project));
+ }
+}
diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml
index 7dc044d..7c169a7 100644
--- a/src/main/resources/META-INF/plugin.xml
+++ b/src/main/resources/META-INF/plugin.xml
@@ -8,7 +8,7 @@ English Notes:
- Show javadoc comments at the Project view Tree structure.
- Show javadoc comments at the end-of-line.
-
- Show javadoc comments at "xx ClassNameOrSimpleName.json" end-of-line.
+
- Show javadoc comments at "xx ClassNameOrSimpleName.json".
- One of the above features can be turned off in
settings -> Tools -> Show Comment Global
- Font color of end-of-line comments can be modified in
@@ -20,7 +20,7 @@ Chinese Notes:
- 在结构树显示 文档注释。
- 在行末尾显示 文档注释。
-
- 行末注释支持 "xx 类全名或简名.json"。
+
- 支持 "xx 类全名或简名.json"。
- 可以在设置中 关闭 上面其中一个功能。
- 可以在设置中 修改 行末注释 的字体颜色。
- 可以在设置中 修改 行末注释 的类名前缀过滤。