From 74d1f9adf591772921ef9ae60663b4b9bd5f2dbe 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, 16 Feb 2022 22:21:57 +0800
Subject: [PATCH] =?UTF-8?q?1.3=20support=20class=20in=20tree,=20constructo?=
=?UTF-8?q?r=20and=20field=20type=20in=20line=20end=20|=20=E6=94=AF?=
=?UTF-8?q?=E6=8C=81=20class=20=E6=A0=91=E8=8A=82=E7=82=B9=E3=80=81?=
=?UTF-8?q?=E6=9E=84=E9=80=A0=E6=96=B9=E6=B3=95=E5=92=8C=E5=AD=97=E6=AE=B5?=
=?UTF-8?q?=E7=9A=84=E8=A1=8C=E6=9C=AB=E6=B3=A8=E9=87=8A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
build.gradle | 3 +-
.../linwancen/plugin/comment/LineEnd.java | 76 ++++++++++++++-----
.../github/linwancen/plugin/comment/Tree.java | 4 +-
.../comment/settings/AppSettingsState.java | 4 +-
.../settings/ProjectSettingsState.java | 1 -
.../utils/PsiMethodCommentFactory.java | 16 +++-
src/main/resources/META-INF/plugin.xml | 12 +--
7 files changed, 81 insertions(+), 35 deletions(-)
diff --git a/build.gradle b/build.gradle
index bf7f115..14bbe86 100644
--- a/build.gradle
+++ b/build.gradle
@@ -4,7 +4,7 @@ plugins {
}
group 'io.github.linwancen'
-version '1.2.0.' + (new Date().format('yyyy.MM.dd_HH.mm'))
+version '1.3.0.' + (new Date().format('yyyy.MM.dd_HH.mm'))
apply plugin: 'java'
@@ -40,6 +40,7 @@ patchPluginXml {
- 1.1 Add end-of-line text color settings 添加行末文本颜色配置
- 1.2 Add end-of-line comment class prefix filter settings 添加行末注释类前缀配置
+
- 1.3 support class in tree, constructor and field type in line end 支持 class 树节点、构造方法和字段的行末注释
"""
}
diff --git a/src/main/java/io/github/linwancen/plugin/comment/LineEnd.java b/src/main/java/io/github/linwancen/plugin/comment/LineEnd.java
index d90edf0..e0b7fd9 100644
--- a/src/main/java/io/github/linwancen/plugin/comment/LineEnd.java
+++ b/src/main/java/io/github/linwancen/plugin/comment/LineEnd.java
@@ -9,7 +9,6 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
-import com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.util.PsiTreeUtil;
import io.github.linwancen.plugin.comment.settings.AppSettingsState;
@@ -65,15 +64,18 @@ public class LineEnd extends EditorLinePainter {
}
return PsiMethodCommentFactory.from(psiMethod);
}
+ if (psiElement instanceof PsiField) {
+ PsiField psiField = (PsiField) psiElement;
+ if (SkipUtils.skip(psiField.getContainingClass(), psiField.getProject())) {
+ return null;
+ }
+ return CommentFactory.fromSrcOrByteCode(psiField);
+ }
return null;
}
protected static final String[] KEYS = {
- "if",
- "for",
- "new",
"=",
- "return",
};
private static @Nullable PsiElement psiElementFrom(FileViewProvider viewProvider, int lineNumber) {
@@ -116,17 +118,35 @@ public class LineEnd extends EditorLinePainter {
startOffset = 0;
}
PsiElement element = viewProvider.findElementAt(offset, JavaLanguage.INSTANCE);
- if (!(element instanceof PsiIdentifier)) {
+ PsiIdentifier psiIdentifier = psiIdentifier(endOffset, element);
+ if (psiIdentifier == null) {
return null;
}
- return parentElementOf(element, startOffset, endOffset);
+ return parentElementOf(psiIdentifier, startOffset, endOffset);
}
@Nullable
- private static PsiElement parentElementOf(PsiElement element, int startOffset, int endOffset) {
+ private static PsiIdentifier psiIdentifier(int endOffset, PsiElement element) {
+ if (element == null) {
+ return null;
+ }
+ while (!(element instanceof PsiIdentifier)) {
+ element = PsiTreeUtil.nextVisibleLeaf(element);
+ if (element == null) {
+ return null;
+ }
+ if (element.getTextRange().getEndOffset() > endOffset) {
+ return null;
+ }
+ }
+ return (PsiIdentifier) element;
+ }
+
+ @Nullable
+ private static PsiElement parentElementOf(PsiElement psiIdentifier, int startOffset, int endOffset) {
// method call
PsiMethodCallExpression call =
- PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression.class, false, startOffset);
+ PsiTreeUtil.getParentOfType(psiIdentifier, PsiMethodCallExpression.class, false, startOffset);
if (call != null) {
// skip double comment when method call in new line:
// someObject // someMethodComment
@@ -141,6 +161,21 @@ public class LineEnd extends EditorLinePainter {
}
}
// new
+ PsiElement newPsiElement = newMethodOrClass(psiIdentifier, startOffset);
+ if (newPsiElement != null) {
+ return newPsiElement;
+ }
+ // ::/class/field
+ PsiReference psiReference = parentPsiReference(psiIdentifier, endOffset);
+ if (psiReference != null) {
+ return psiReference.resolve();
+ }
+ return null;
+ }
+
+ @Nullable
+ private static PsiElement newMethodOrClass(PsiElement element, int startOffset) {
+ // new and Class should same line
PsiNewExpression newExp = PsiTreeUtil.getParentOfType(element, PsiNewExpression.class, false, startOffset);
if (newExp != null) {
PsiMethod psiMethod = newExp.resolveMethod();
@@ -152,17 +187,20 @@ public class LineEnd extends EditorLinePainter {
return classReference.resolve();
}
}
- // ::
- PsiMethodReferenceExpression ref =
- PsiTreeUtil.getParentOfType(element, PsiMethodReferenceExpression.class, false, startOffset);
- if (ref != null) {
- return ref.resolve();
+ return null;
+ }
+
+ @Nullable
+ private static PsiReference parentPsiReference(PsiElement element, int endOffset) {
+ PsiElement parent;
+ while ((parent = element.getParent()) instanceof PsiReference) {
+ if (parent.getTextRange().getEndOffset() > endOffset) {
+ break;
+ }
+ element = parent;
}
- // SomeClass // SomeClassComment
- PsiJavaCodeReferenceElementImpl code =
- PsiTreeUtil.getParentOfType(element, PsiJavaCodeReferenceElementImpl.class, false, startOffset);
- if (code != null) {
- return code.resolve();
+ if (element instanceof PsiReference) {
+ return (PsiReference) element;
}
return null;
}
diff --git a/src/main/java/io/github/linwancen/plugin/comment/Tree.java b/src/main/java/io/github/linwancen/plugin/comment/Tree.java
index eb33199..4455eb0 100644
--- a/src/main/java/io/github/linwancen/plugin/comment/Tree.java
+++ b/src/main/java/io/github/linwancen/plugin/comment/Tree.java
@@ -72,13 +72,13 @@ public class Tree implements ProjectViewNodeDecorator {
if (node instanceof PsiFieldNode) {
// On Show Members
PsiField psiField = ((PsiFieldNode) node).getValue();
- return psiField.getDocComment();
+ return CommentFactory.fromSrcOrByteCode(psiField);
}
if (node instanceof ClassTreeNode) {
// On Packages View, Project Files View, Show Members
PsiClass psiClass = ((ClassTreeNode) node).getValue();
- return psiClass.getDocComment();
+ return CommentFactory.fromSrcOrByteCode(psiClass);
}
if (node instanceof PackageElementNode) {
// On Packages View
diff --git a/src/main/java/io/github/linwancen/plugin/comment/settings/AppSettingsState.java b/src/main/java/io/github/linwancen/plugin/comment/settings/AppSettingsState.java
index c4e9cbb..95ae958 100644
--- a/src/main/java/io/github/linwancen/plugin/comment/settings/AppSettingsState.java
+++ b/src/main/java/io/github/linwancen/plugin/comment/settings/AppSettingsState.java
@@ -5,7 +5,6 @@ import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.editor.markup.TextAttributes;
-import com.intellij.ui.Gray;
import com.intellij.ui.JBColor;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
@@ -24,7 +23,8 @@ public class AppSettingsState implements PersistentStateComponent 3 && (name.startsWith("get") || name.startsWith("set"))) {
name = name.substring(3);
diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml
index 1e32779..2acc4c2 100644
--- a/src/main/resources/META-INF/plugin.xml
+++ b/src/main/resources/META-INF/plugin.xml
@@ -8,12 +8,12 @@
- Show javadoc comments in the Project view Tree structure.
- Show javadoc comments for calling methods at the end of the line.
-
- One of the above features
- can be turned off in the settings -> Tools -> Show Comment Global.
- - The color of end-of-line comments
- can be modified in the settings -> Tools -> Show Comment Global.
- - End-of-line comment class prefix filter
- can be modified in settings -> Tools -> Show Comment Project.
+ - One of the above features can be turned off in
+ settings -> Tools -> Show Comment Global
+ - The color of end-of-line comments can be modified in
+ settings -> Tools -> Show Comment Global
+ - End-of-line comment class prefix filter can be modified in
+ settings -> Tools -> Show Comment Project
Chinese Note: