Skip to content
This repository was archived by the owner on Apr 10, 2024. It is now read-only.

Commit 6502f02

Browse files
committed
opt: 优化源代码文件路径获取
1 parent 909686b commit 6502f02

File tree

4 files changed

+67
-38
lines changed

4 files changed

+67
-38
lines changed

Plugin/Plugin.zip

408 Bytes
Binary file not shown.

Plugin/src/com/ciy/plugin/ShowInputDialogAction.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.intellij.openapi.application.ApplicationManager
1616
import com.intellij.openapi.module.Module
1717
import com.intellij.openapi.project.Project
1818
import com.intellij.openapi.project.rootManager
19+
import com.intellij.openapi.roots.ModuleRootManager
1920
import com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl
2021
import java.io.File
2122

@@ -69,8 +70,7 @@ class ShowInputDialogAction : AnAction() {
6970
fun analysisApiList(module: Module, packName: String, apiList: List<ApiBean>) {
7071
AnalysisApiListProgressDialog(apiList, AnalysisApiListProgressDialog.AnalysisApiListProgressDialogListener {
7172
// 找到模块源代码存放的根目录
72-
val javaSrc: VirtualDirectoryImpl? =
73-
module.rootManager.getSourceRoots(false).find { it2 -> it2.name == "java" } as? VirtualDirectoryImpl
73+
val javaSrc: VirtualDirectoryImpl? = ModuleRootManager.getInstance(module).sourceRoots.find { it2 -> it2.path.endsWith("src/main/java") } as? VirtualDirectoryImpl
7474
if (javaSrc != null) {
7575
val rootDir = File(javaSrc.path)
7676
if (!rootDir.exists()) {

Plugin/src/com/ciy/plugin/ui/SelectApiDialog.java

+64-35
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.intellij.openapi.module.Module;
1111
import com.intellij.openapi.module.ModuleManager;
1212
import com.intellij.openapi.project.Project;
13+
import com.intellij.openapi.roots.ModuleRootManager;
1314
import com.intellij.openapi.vfs.VirtualFile;
1415
import okhttp3.*;
1516
import org.jetbrains.annotations.NotNull;
@@ -192,43 +193,48 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
192193
private void readSelectApi() {
193194
Module selectModule = moduleList.get(cbModule.getSelectedIndex());
194195
if (selectModule != null) {
195-
String pack = tfPack.getText().replace(".", "/");
196-
VirtualFile apiServiceFile = selectModule.getModuleFile().getParent().findFileByRelativePath("src/main/java/" +
197-
pack + "/" + ShowInputDialogAction.Companion.getUrlConstantClassName() + ".kt");
198-
if (apiServiceFile != null && apiServiceFile.exists()) {
199-
// URLConstant 存在读取里面的url
200-
try {
201-
BufferedReader br = new BufferedReader(new InputStreamReader(apiServiceFile.getInputStream()));
202-
StringBuilder sb = new StringBuilder();
203-
String len = "";
204-
while ((len = br.readLine()) != null) {
205-
sb.append(len);
206-
}
207-
String sourceText = sb.toString();
208-
if (!sourceText.isEmpty()) {
209-
Pattern pattern = Pattern.compile("\\$" + URLConstantGenerate.INSTANCE.getPrefixPropertyName() +
210-
"(.+?)\\$" + URLConstantGenerate.INSTANCE.getSuffixPropertyName());
211-
Matcher matcher = pattern.matcher(sourceText);
212-
List<String> urlList = new ArrayList<>();
213-
while (matcher.find()) {
214-
if (matcher.group(1) != null) {
215-
urlList.add(matcher.group(1));
196+
VirtualFile sourcePath = getSourcePath(selectModule);
197+
if (sourcePath == null) {
198+
lb.setText("无法找到源代码文件");
199+
} else {
200+
String pack = tfPack.getText().replace(".", "/");
201+
VirtualFile apiServiceFile = sourcePath.findFileByRelativePath(pack + "/" +
202+
ShowInputDialogAction.Companion.getUrlConstantClassName() + ".kt");
203+
if (apiServiceFile != null && apiServiceFile.exists()) {
204+
// URLConstant 存在读取里面的url
205+
try {
206+
BufferedReader br = new BufferedReader(new InputStreamReader(apiServiceFile.getInputStream()));
207+
StringBuilder sb = new StringBuilder();
208+
String len = "";
209+
while ((len = br.readLine()) != null) {
210+
sb.append(len);
211+
}
212+
String sourceText = sb.toString();
213+
if (!sourceText.isEmpty()) {
214+
Pattern pattern = Pattern.compile("\\$" + URLConstantGenerate.INSTANCE.getPrefixPropertyName() +
215+
"(.+?)\\$" + URLConstantGenerate.INSTANCE.getSuffixPropertyName());
216+
Matcher matcher = pattern.matcher(sourceText);
217+
List<String> urlList = new ArrayList<>();
218+
while (matcher.find()) {
219+
if (matcher.group(1) != null) {
220+
urlList.add(matcher.group(1));
221+
}
216222
}
223+
apiList.stream().forEach(it -> {
224+
it.setSelect(urlList.contains(it.getPath()));
225+
});
217226
}
218-
apiList.stream().forEach(it -> {
219-
it.setSelect(urlList.contains(it.getPath()));
220-
});
227+
} catch (IOException e) {
228+
e.printStackTrace();
221229
}
222-
} catch (IOException e) {
223-
e.printStackTrace();
230+
} else {
231+
// URLConstant 不存在则全选
232+
apiList.forEach(it -> {
233+
it.setSelect(false);
234+
});
224235
}
225-
} else {
226-
// URLConstant 不存在则全选
227-
apiList.forEach(it -> {
228-
it.setSelect(false);
229-
});
236+
refreshListData();
230237
}
231-
refreshListData();
232238
}
233239
}
234240

@@ -277,15 +283,38 @@ private void loadModule() {
277283
private void onOK() {
278284
if (cbModule.getSelectedIndex() < moduleList.size() && !tfPack.getText().isEmpty()) {
279285
Module selectModule = moduleList.get(cbModule.getSelectedIndex());
280-
if (listener != null) {
281-
this.listener.onOk(selectModule, tfPack.getText(), apiList.stream().filter(ApiBean::getSelect).collect(Collectors.toList()));
286+
VirtualFile sourcePath = getSourcePath(selectModule);
287+
if (sourcePath == null) {
288+
lb.setText("无法找到源代码文件");
289+
} else {
290+
if (listener != null) {
291+
this.listener.onOk(selectModule, tfPack.getText(), apiList.stream().filter(ApiBean::getSelect).collect(Collectors.toList()));
292+
}
293+
dispose();
282294
}
283-
dispose();
284295
} else {
285296
lb.setText("模块或者包名格式错误");
286297
}
287298
}
288299

300+
/**
301+
* 获取模块源代码文件
302+
*
303+
* @param module 模块
304+
* @return 源代码文件
305+
*/
306+
private VirtualFile getSourcePath(Module module) {
307+
VirtualFile[] sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots();
308+
if (sourceRoots != null && sourceRoots.length > 0) {
309+
for (VirtualFile sourceRoot : sourceRoots) {
310+
if (sourceRoot.getPath().endsWith("src/main/java")) {
311+
return sourceRoot;
312+
}
313+
}
314+
}
315+
return null;
316+
}
317+
289318
private void onCancel() {
290319
// add your code here if necessary
291320
dispose();

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
plugins {
22
id 'java'
3-
id 'org.jetbrains.kotlin.jvm' version '1.3.11'
3+
id 'org.jetbrains.kotlin.jvm' version '1.3.50'
44
}
55

66
/*

0 commit comments

Comments
 (0)