Skip to content

Commit b2079e5

Browse files
committed
feat: compatible with other plugins
1 parent b600701 commit b2079e5

16 files changed

+252
-455
lines changed

build.gradle

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group 'org.linyimin'
7-
version '1.0.0'
7+
version '1.0.1'
88

99
repositories {
1010
maven { url 'https://maven.aliyun.com/repository/public/' }
@@ -28,14 +28,22 @@ intellij {
2828
}
2929
patchPluginXml {
3030
changeNotes = """
31-
<ul>
32-
<li>navigate from the code to mapper and from the mapper back to code</li>
33-
<li>The xml statement/method in the mapper file convert to sql statement, which supports parameters mock, data source configuration, and sql execution</li>
34-
</ul>
35-
<ul>
36-
<li>快速从代码跳转到mapper及从mapper返回代码</li>
37-
<li>mapper文件中的xml语句/方法转换成sql语句,支持参数mock、数据源配置、sql执行</li>
38-
</ul>
31+
<h4>1.0.1</h4>
32+
<ul>
33+
<li>Compatible with other plugins</li>
34+
</ul>
35+
<ul>
36+
<li>兼容其他插件</li>
37+
</ul>
38+
<h4>1.0.0</h4>
39+
<ul>
40+
<li>navigate from the code to mapper and from the mapper back to code</li>
41+
<li>The xml statement/method in the mapper file convert to sql statement, which supports parameters mock, data source configuration, and sql execution</li>
42+
</ul>
43+
<ul>
44+
<li>快速从代码跳转到mapper及从mapper返回代码</li>
45+
<li>mapper文件中的xml语句/方法转换成sql语句,支持参数mock、数据源配置、sql执行</li>
46+
</ul>
3947
"""
4048
}
4149

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package io.github.linyimin.plugin.cache;
2+
3+
import com.google.common.collect.Lists;
4+
import com.intellij.openapi.project.Project;
5+
import com.intellij.openapi.roots.ProjectRootManager;
6+
import com.intellij.openapi.vfs.VirtualFile;
7+
import com.intellij.psi.PsiFile;
8+
import com.intellij.psi.PsiFileSystemItem;
9+
import com.intellij.psi.util.PsiUtil;
10+
import com.intellij.psi.xml.XmlAttribute;
11+
import com.intellij.psi.xml.XmlFile;
12+
import com.intellij.psi.xml.XmlTag;
13+
import io.github.linyimin.plugin.utils.MapperDomUtils;
14+
15+
import java.util.*;
16+
17+
/**
18+
* @author banzhe
19+
* @date 2022/08/10 23:28
20+
**/
21+
public class MybatisXmlContentCache {
22+
23+
private static final List<String> SUB_TAGS = Lists.newArrayList("insert", "update", "delete", "select");
24+
25+
private static final Map<Project, Map<String /* path */, String /* configuration */>> projectMybatisConfigurationMap = new HashMap<>();
26+
27+
private static final Map<Project, Map<String /* namespace */, List<String> /* method name list */>> projectMybatisMapperMap = new HashMap<>();
28+
29+
private static final Map<Project, Map<String /* namespace */, List<XmlTag>>> projectMapperNamespaceMap = new HashMap<>();
30+
31+
private static final Map<Project, Map<String /* method qualified name */, List<XmlTag>>> projectMapperMethodMap = new HashMap<>();
32+
33+
34+
public static List<String> acquireConfigurations(Project project) {
35+
36+
addXmlCache(project);
37+
38+
Map<String, String> cacheMap = projectMybatisConfigurationMap.getOrDefault(project, Collections.emptyMap());
39+
40+
return new ArrayList<>(cacheMap.values());
41+
}
42+
43+
public static List<String> acquireByNamespace(Project project) {
44+
45+
addXmlCache(project);
46+
47+
Set<String> namespaces = projectMybatisMapperMap.getOrDefault(project, new HashMap<>()).keySet();
48+
return new ArrayList<>(namespaces);
49+
}
50+
51+
public static List<XmlTag> acquireByNamespace(Project project, String namespace) {
52+
53+
addXmlCache(project);
54+
55+
Map<String /* namespace */, List<XmlTag>> cache = projectMapperNamespaceMap.getOrDefault(project, new HashMap<>());
56+
57+
return cache.getOrDefault(namespace, new ArrayList<>());
58+
}
59+
60+
public static List<XmlTag> acquireByMethodName(Project project, String methodQualifiedName) {
61+
62+
addXmlCache(project);
63+
64+
Map<String /* namespace */, List<XmlTag>> cache = projectMapperMethodMap.getOrDefault(project, new HashMap<>());
65+
66+
return cache.getOrDefault(methodQualifiedName, new ArrayList<>());
67+
}
68+
69+
private static void addXmlCache(Project project) {
70+
71+
ProjectRootManager.getInstance(project).getFileIndex().iterateContent(fileOrDir -> {
72+
73+
PsiFileSystemItem item = PsiUtil.findFileSystemItem(project, fileOrDir);
74+
75+
if (item == null) {
76+
return true;
77+
}
78+
79+
PsiFile psiFile = item.getContainingFile();
80+
81+
if (MapperDomUtils.isMybatisConfigurationFile(psiFile)) {
82+
addConfigurationCache(project, fileOrDir, psiFile);
83+
}
84+
85+
if (MapperDomUtils.isMybatisMapperFile(psiFile)) {
86+
addMapperCache(project, psiFile);
87+
}
88+
89+
return true;
90+
});
91+
}
92+
93+
private static void addMapperCache(Project project, PsiFile psiFile) {
94+
95+
XmlTag rootTag = ((XmlFile) psiFile).getRootTag();
96+
97+
if (rootTag == null || rootTag.getAttribute("namespace") == null) {
98+
return;
99+
}
100+
101+
102+
XmlAttribute namespaceAttribute = rootTag.getAttribute("namespace");
103+
104+
if (namespaceAttribute == null) {
105+
return;
106+
}
107+
108+
String namespace = namespaceAttribute.getValue();
109+
110+
addNamespaceXmlTagCache(project, namespace, rootTag);
111+
112+
XmlTag[] subTags = rootTag.getSubTags();
113+
114+
for (XmlTag subTag : subTags) {
115+
if (!SUB_TAGS.contains(subTag.getName())) {
116+
continue;
117+
}
118+
119+
XmlAttribute subAttribute = subTag.getAttribute("id");
120+
121+
if (subAttribute == null) {
122+
continue;
123+
}
124+
125+
String id = subAttribute.getValue();
126+
127+
addMethodXmlTagCache(project, namespace, id, subTag);
128+
129+
addNamespaceCache(project, namespace, id);
130+
131+
}
132+
133+
}
134+
135+
private static void addNamespaceXmlTagCache(Project project, String namespace, XmlTag xmlTag) {
136+
137+
Map<String, List<XmlTag>> namespaceCacheMap = projectMapperNamespaceMap.getOrDefault(project, new HashMap<>());
138+
139+
List<XmlTag> tags = namespaceCacheMap.getOrDefault(namespace, new ArrayList<>());
140+
tags.add(xmlTag);
141+
142+
namespaceCacheMap.put(namespace, tags);
143+
144+
projectMapperNamespaceMap.put(project, namespaceCacheMap);
145+
146+
}
147+
148+
private static void addMethodXmlTagCache(Project project, String namespace, String id, XmlTag xmlTag) {
149+
150+
Map<String, List<XmlTag>> methodCacheMap = projectMapperMethodMap.getOrDefault(project, new HashMap<>());
151+
152+
String methodQualifiedName = namespace + "." + id;
153+
154+
List<XmlTag> tags = methodCacheMap.getOrDefault(methodQualifiedName, new ArrayList<>());
155+
tags.add(xmlTag);
156+
157+
methodCacheMap.put(methodQualifiedName, tags);
158+
159+
projectMapperMethodMap.put(project, methodCacheMap);
160+
}
161+
162+
private static void addNamespaceCache(Project project, String namespace, String id) {
163+
164+
Map<String, List<String>> cacheMap = projectMybatisMapperMap.getOrDefault(project, new HashMap<>());
165+
166+
List<String> ids = cacheMap.getOrDefault(namespace, new ArrayList<>());
167+
ids.add(id);
168+
169+
cacheMap.put(namespace, ids);
170+
171+
projectMybatisMapperMap.put(project, cacheMap);
172+
}
173+
174+
private static void addConfigurationCache(Project project, VirtualFile fileOrDir, PsiFile psiFile) {
175+
Map<String, String> cacheMap = projectMybatisConfigurationMap.getOrDefault(project, new HashMap<>());
176+
177+
String path = fileOrDir.getPath();
178+
String configuration = psiFile.getText();
179+
180+
cacheMap.put(path, configuration);
181+
projectMybatisConfigurationMap.put(project, cacheMap);
182+
}
183+
}

src/main/java/io/github/linyimin/plugin/description/MapperConfigurationDescription.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/main/java/io/github/linyimin/plugin/description/MapperDescription.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/main/java/io/github/linyimin/plugin/dom/model/IdDomElement.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/main/java/io/github/linyimin/plugin/dom/model/Mapper.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/main/java/io/github/linyimin/plugin/dom/model/MybatisConfiguration.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)