feat(BaseLangDoc): multi RefClass to support Ref in javadoc and kotlin doc

This commit is contained in:
林万程
2024-01-26 19:59:17 +08:00
parent b0cd5ad4e1
commit 25caa79a68
10 changed files with 52 additions and 24 deletions

View File

@@ -19,6 +19,8 @@ import io.github.linwancen.plugin.show.lang.base.DocFilter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class JavaLangDoc extends BaseTagLangDoc<PsiDocComment> {
public static final JavaLangDoc INSTANCE = new JavaLangDoc();
@@ -28,8 +30,8 @@ public class JavaLangDoc extends BaseTagLangDoc<PsiDocComment> {
}
@Override
public @NotNull Class<? extends PsiElement> getRefClass() {
return PsiJavaCodeReferenceElement.class;
public @NotNull List<Class<? extends PsiElement>> getRefClass() {
return List.of(PsiJavaCodeReferenceElement.class, PsiDocTagValue.class);
}
@Override

View File

@@ -11,6 +11,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.idea.KotlinLanguage;
import org.jetbrains.kotlin.kdoc.psi.api.KDoc;
import org.jetbrains.kotlin.kdoc.psi.impl.KDocName;
import org.jetbrains.kotlin.kdoc.psi.impl.KDocSection;
import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag;
import org.jetbrains.kotlin.psi.KtNameReferenceExpression;
@@ -24,8 +25,8 @@ public class KotlinLangDoc extends BaseTagLangDoc<KDocSection> {
}
@Override
public @NotNull Class<? extends PsiElement> getRefClass() {
return KtNameReferenceExpression.class;
public @NotNull List<Class<? extends PsiElement>> getRefClass() {
return List.of(KtNameReferenceExpression.class, KDocName.class);
}
@Override

View File

@@ -20,8 +20,8 @@ public class GoLangDoc extends BaseLangDoc {
}
@Override
public @NotNull Class<? extends PsiElement> getRefClass() {
return GoReferenceExpressionBase.class;
public @NotNull List<Class<? extends PsiElement>> getRefClass() {
return List.of(GoReferenceExpressionBase.class);
}
@Override

View File

@@ -11,6 +11,8 @@ import io.github.linwancen.plugin.show.lang.base.BaseLangDoc;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class JsLangDoc extends BaseLangDoc {
static {
@@ -18,8 +20,8 @@ public class JsLangDoc extends BaseLangDoc {
}
@Override
public @NotNull Class<? extends PsiElement> getRefClass() {
return JSPsiReferenceElement.class;
public @NotNull List<Class<? extends PsiElement>> getRefClass() {
return List.of(JSPsiReferenceElement.class);
}
@Override

View File

@@ -30,8 +30,8 @@ public class JsonLangDoc extends BaseLangDoc {
public static final JsonLangDoc INSTANCE = new JsonLangDoc();
@Override
public @NotNull Class<? extends PsiElement> getRefClass() {
return JsonProperty.class;
public @NotNull List<Class<? extends PsiElement>> getRefClass() {
return List.of(JsonProperty.class);
}
@Override

View File

@@ -15,6 +15,8 @@ import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class PythonLangDoc extends BaseTagLangDoc<StructuredDocString> {
static {
@@ -22,8 +24,8 @@ public class PythonLangDoc extends BaseTagLangDoc<StructuredDocString> {
}
@Override
public @NotNull Class<? extends PsiElement> getRefClass() {
return PyReferenceExpression.class;
public @NotNull List<Class<? extends PsiElement>> getRefClass() {
return List.of(PyReferenceExpression.class);
}
@Override

View File

@@ -12,6 +12,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.Method;
import java.util.List;
public class SqlLangDoc extends BaseLangDoc {
@@ -20,8 +21,8 @@ public class SqlLangDoc extends BaseLangDoc {
}
@Override
public @NotNull Class<? extends PsiElement> getRefClass() {
return SqlReferenceExpression.class;
public @NotNull List<Class<? extends PsiElement>> getRefClass() {
return List.of(SqlReferenceExpression.class);
}
@Override

View File

@@ -29,7 +29,7 @@ import java.util.Map;
public abstract class BaseLangDoc extends EditorLinePainter {
public static final Map<String, BaseLangDoc> LANG_DOC_MAP = new LinkedHashMap<>();
public abstract @Nullable Class<? extends PsiElement> getRefClass();
public abstract @NotNull List<Class<? extends PsiElement>> getRefClass();
public abstract boolean show(@NotNull LineInfo info);
@@ -75,10 +75,7 @@ public abstract class BaseLangDoc extends EditorLinePainter {
@Nullable
public String findRefDoc(@NotNull LineInfo info, @NotNull FileViewProvider viewProvider,
@NotNull PsiElement element) {
@Nullable Class<? extends PsiElement> refClass = getRefClass();
if (refClass == null) {
return null;
}
@NotNull List<Class<? extends PsiElement>> refClass = getRefClass();
@Nullable String doc = null;
@Nullable String text = null;
@Nullable PsiElement refElement = element;

View File

@@ -10,6 +10,7 @@ import io.github.linwancen.plugin.show.bean.SettingsInfo;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.regex.Pattern;
public class Prev {
@@ -18,7 +19,7 @@ public class Prev {
@Nullable
public static PsiElement prevRefChild(@NotNull LineInfo info, @NotNull PsiElement element,
@NotNull Class<? extends PsiElement> refClass) {
@NotNull List<Class<? extends PsiElement>> refClass) {
@Nullable PsiElement prevParent = refClassParent(element, refClass);
while ((element = PsiTreeUtil.prevVisibleLeaf(element)) != null) {
if (element.getTextRange().getEndOffset() < info.startOffset) {
@@ -29,7 +30,7 @@ public class Prev {
// skip b in a.b.c
if (prevParent != null
&& prevParent.getTextRange().getEndOffset() < info.endOffset
&& PsiTreeUtil.findChildOfType(prevParent, refClass) == parent) {
&& prevParentChildEqParent(prevParent, refClass, parent)) {
prevParent = parent;
} else {
if (!(element instanceof PsiComment)) {
@@ -41,6 +42,17 @@ public class Prev {
return null;
}
private static boolean prevParentChildEqParent(@NotNull PsiElement prevParent,
@NotNull List<Class<? extends PsiElement>> refClass,
@NotNull PsiElement parent) {
for (@NotNull Class<? extends PsiElement> c : refClass) {
if (PsiTreeUtil.findChildOfType(prevParent, c) == parent) {
return true;
}
}
return false;
}
private static final Pattern SYMBOL_PATTERN = Pattern.compile("[^" +
":-@" +
"\\[-`" +
@@ -49,7 +61,7 @@ public class Prev {
@Nullable
private static PsiElement refClassParent(@NotNull PsiElement element,
@NotNull Class<? extends PsiElement> refClass) {
@NotNull List<Class<? extends PsiElement>> refClass) {
String text = element.getText();
if (!SYMBOL_PATTERN.matcher(text).find()) {
return null;
@@ -58,18 +70,23 @@ public class Prev {
if (parent == null) {
return null;
}
if (!refClass.isAssignableFrom(parent.getClass())) {
if (notAssignableFrom(refClass, parent)) {
parent = parent.getParent();
if (parent == null) {
return null;
}
}
if (!refClass.isAssignableFrom(parent.getClass())) {
if (notAssignableFrom(refClass, parent)) {
return null;
}
return parent;
}
private static boolean notAssignableFrom(@NotNull List<Class<? extends PsiElement>> refClass,
@NotNull PsiElement parent) {
return refClass.stream().noneMatch(it -> it.isAssignableFrom(parent.getClass()));
}
public static @Nullable <T extends SettingsInfo> PsiElement prevCompactElement(
@SuppressWarnings("unused") @NotNull T info, @NotNull PsiElement resolve, @NotNull Document document) {
@Nullable PsiElement element = PsiTreeUtil.prevVisibleLeaf(resolve);

View File

@@ -4,6 +4,12 @@ import io.github.linwancen.plugin.show.demo.java.obj.Child
import io.github.linwancen.plugin.show.demo.java.obj.Parent
/**
* Kotlin
*
* [call]
* [InDoc.field]
* [InDoc.method1] [InDoc.method2]
*
* @author l
*/
object Kotlin : Parent() {