2.04 json dict doc from (key).tsv | 从 键名.tsv 中读取 json 的字典注释 fixed #12

This commit is contained in:
林万程
2022-10-30 18:34:23 +08:00
parent 82d0ade99b
commit c6318f88a2
22 changed files with 184 additions and 94 deletions

View File

@@ -1,14 +1,24 @@
package io.github.linwancen.plugin.show.lang;
import com.intellij.json.psi.JsonArray;
import com.intellij.json.psi.JsonObject;
import com.intellij.json.psi.JsonProperty;
import com.intellij.json.psi.JsonValue;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import io.github.linwancen.plugin.show.bean.SettingsInfo;
import io.github.linwancen.plugin.show.lang.base.BaseLangDoc;
import com.intellij.psi.search.FilenameIndex;
import com.intellij.psi.search.GlobalSearchScope;
import io.github.linwancen.plugin.show.bean.LineInfo;
import io.github.linwancen.plugin.show.ext.conf.ConfCacheGetUtils;
import io.github.linwancen.plugin.show.ext.conf.TsvLoader;
import io.github.linwancen.plugin.show.lang.base.BaseLangDoc;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class JsonLangDoc extends BaseLangDoc {
public static final JsonLangDoc INSTANCE = new JsonLangDoc();
@@ -37,11 +47,15 @@ public class JsonLangDoc extends BaseLangDoc {
}
@Override
protected @Nullable <T extends SettingsInfo> String refDoc(@NotNull T lineInfo, @NotNull PsiElement ref) {
protected @Nullable String refDoc(@NotNull LineInfo lineInfo, @NotNull PsiElement ref) {
if (!(ref instanceof JsonProperty)) {
return null;
}
@NotNull JsonProperty jsonProperty = (JsonProperty) ref;
@Nullable String dictDoc = dictDoc(lineInfo, jsonProperty);
if (dictDoc != null) {
return dictDoc;
}
@NotNull PsiReference[] references = jsonProperty.getNameElement().getReferences();
for (@NotNull PsiReference reference : references) {
@Nullable PsiElement resolve = null;
@@ -60,4 +74,48 @@ public class JsonLangDoc extends BaseLangDoc {
}
return null;
}
@Nullable
private static String dictDoc(@NotNull LineInfo lineInfo, @NotNull JsonProperty prop) {
@Nullable JsonValue value = prop.getValue();
if (value == null || value instanceof JsonArray || value instanceof JsonObject) {
return null;
}
@NotNull GlobalSearchScope scope = GlobalSearchScope.allScope(lineInfo.project);
String jsonKey = prop.getName();
String jsonValue = value.getText();
// Read the json.path before if needed
return jsonDictDoc(lineInfo, scope, jsonKey, jsonValue);
}
@Nullable
private static String jsonDictDoc(@NotNull LineInfo lineInfo, @NotNull GlobalSearchScope scope, String fileName, String jsonValue) {
@NotNull Collection<VirtualFile> files = FilenameIndex.getVirtualFilesByName(lineInfo.project, fileName + ".tsv", scope);
// one file
if (files.size() < 2) {
for (@NotNull VirtualFile file : files) {
@NotNull Map<String, List<String>> map = TsvLoader.buildMap(file, false);
List<String> list = map.get(jsonValue);
if (list != null && list.size() > 1) {
return list.get(1);
}
}
return null;
}
// multi file
@NotNull Map<VirtualFile, Map<String, List<String>>> fileMap = new ConcurrentHashMap<>();
for (@NotNull VirtualFile file : files) {
@NotNull Map<String, List<String>> map = TsvLoader.buildMap(file, false);
fileMap.put(file, map);
}
@NotNull String path = lineInfo.file.getPath();
@NotNull SortedMap<String, Map<String, List<String>>> treeMap = ConfCacheGetUtils.filterPath(fileMap, path);
for (@NotNull Map.Entry<String, Map<String, List<String>>> entry : treeMap.entrySet()) {
List<String> list = entry.getValue().get(jsonValue);
if (list != null && list.size() > 1) {
return list.get(1);
}
}
return null;
}
}

View File

@@ -5,10 +5,9 @@ import com.intellij.psi.PsiElement;
import com.intellij.sql.psi.SqlLanguage;
import com.intellij.sql.psi.SqlReferenceExpression;
import com.intellij.util.containers.JBIterable;
import io.github.linwancen.plugin.show.bean.SettingsInfo;
import io.github.linwancen.plugin.show.bean.LineInfo;
import io.github.linwancen.plugin.show.lang.base.BaseLangDoc;
import io.github.linwancen.plugin.show.lang.base.DocSkip;
import io.github.linwancen.plugin.show.bean.LineInfo;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -32,8 +31,8 @@ public class SqlLangDoc extends BaseLangDoc {
@Override
protected @Nullable <T extends SettingsInfo> String refElementDoc(@NotNull T lineInfo,
@NotNull PsiElement ref) {
protected @Nullable String refElementDoc(@NotNull LineInfo lineInfo,
@NotNull PsiElement ref) {
JBIterable<DbElement> relatedDbElements;
Class<?> clazz;
try {
@@ -48,7 +47,7 @@ public class SqlLangDoc extends BaseLangDoc {
}
}
try {
Method method = clazz.getMethod("findRelatedDbElements", PsiElement.class, boolean.class);
@NotNull Method method = clazz.getMethod("findRelatedDbElements", PsiElement.class, boolean.class);
//noinspection unchecked
relatedDbElements = (JBIterable<DbElement>) method.invoke(null, ref, false);
} catch (Throwable e) {

View File

@@ -116,8 +116,8 @@ public abstract class BaseLangDoc extends EditorLinePainter {
* Override like SQL
*/
@Nullable
protected <T extends SettingsInfo> String refElementDoc(@NotNull T lineInfo,
@NotNull PsiElement refElement) {
protected String refElementDoc(@NotNull LineInfo lineInfo,
@NotNull PsiElement refElement) {
@Nullable String refDoc = refDoc(lineInfo, refElement);
if (refDoc != null && !DocSkip.skipDoc(lineInfo.appSettings, lineInfo.projectSettings, refDoc)) {
return refDoc;
@@ -129,7 +129,7 @@ public abstract class BaseLangDoc extends EditorLinePainter {
* Override like Java/Json
*/
@Nullable
protected <T extends SettingsInfo> String refDoc(@NotNull T lineInfo, @NotNull PsiElement ref) {
protected String refDoc(@NotNull LineInfo lineInfo, @NotNull PsiElement ref) {
// kotlin ref.getReference() == null but ref.getReferences().length == 2
@NotNull PsiReference[] references = ref.getReferences();
if (references.length < 1) {