1.3 support class in tree, constructor and field type in line end | 支持 class 树节点、构造方法和字段的行末注释
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<AppSettingsSta
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public Color lineEndColorBright = new Color(98, 151, 85);
|
||||
public Color lineEndColorDark = Gray._140;
|
||||
@SuppressWarnings("all")
|
||||
public Color lineEndColorDark = new Color(98, 151, 85);
|
||||
public final TextAttributes lineEndTextAttr = new TextAttributes(new JBColor(lineEndColorBright, lineEndColorDark),
|
||||
null, null, null, Font.ITALIC);
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import com.intellij.openapi.components.ServiceManager;
|
||||
import com.intellij.openapi.components.State;
|
||||
import com.intellij.openapi.components.Storage;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.project.ProjectManager;
|
||||
import com.intellij.util.xmlb.XmlSerializerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -30,8 +30,19 @@ public class PsiMethodCommentFactory {
|
||||
return superDoc;
|
||||
}
|
||||
|
||||
|
||||
PsiClass clazz = psiMethod.getContainingClass();
|
||||
if (clazz == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// constructor
|
||||
if (psiMethod.isConstructor()) {
|
||||
return clazz.getDocComment();
|
||||
}
|
||||
|
||||
// get/set/is - PropertyDescriptor getReadMethod() getWriteMethod()
|
||||
return propMethodComment(psiMethod, psiMethod.getContainingClass());
|
||||
return propMethodComment(psiMethod, clazz);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -48,9 +59,6 @@ public class PsiMethodCommentFactory {
|
||||
|
||||
@Nullable
|
||||
public static PsiDocComment propMethodComment(PsiMethod psiMethod, PsiClass psiClass) {
|
||||
if (psiClass == null) {
|
||||
return null;
|
||||
}
|
||||
String name = psiMethod.getName();
|
||||
if (name.length() > 3 && (name.startsWith("get") || name.startsWith("set"))) {
|
||||
name = name.substring(3);
|
||||
|
||||
Reference in New Issue
Block a user