|
10 | 10 | import com.intellij.openapi.module.Module;
|
11 | 11 | import com.intellij.openapi.module.ModuleManager;
|
12 | 12 | import com.intellij.openapi.project.Project;
|
| 13 | +import com.intellij.openapi.roots.ModuleRootManager; |
13 | 14 | import com.intellij.openapi.vfs.VirtualFile;
|
14 | 15 | import okhttp3.*;
|
15 | 16 | import org.jetbrains.annotations.NotNull;
|
@@ -192,43 +193,48 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
|
192 | 193 | private void readSelectApi() {
|
193 | 194 | Module selectModule = moduleList.get(cbModule.getSelectedIndex());
|
194 | 195 | 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 | + } |
216 | 222 | }
|
| 223 | + apiList.stream().forEach(it -> { |
| 224 | + it.setSelect(urlList.contains(it.getPath())); |
| 225 | + }); |
217 | 226 | }
|
218 |
| - apiList.stream().forEach(it -> { |
219 |
| - it.setSelect(urlList.contains(it.getPath())); |
220 |
| - }); |
| 227 | + } catch (IOException e) { |
| 228 | + e.printStackTrace(); |
221 | 229 | }
|
222 |
| - } catch (IOException e) { |
223 |
| - e.printStackTrace(); |
| 230 | + } else { |
| 231 | + // URLConstant 不存在则全选 |
| 232 | + apiList.forEach(it -> { |
| 233 | + it.setSelect(false); |
| 234 | + }); |
224 | 235 | }
|
225 |
| - } else { |
226 |
| - // URLConstant 不存在则全选 |
227 |
| - apiList.forEach(it -> { |
228 |
| - it.setSelect(false); |
229 |
| - }); |
| 236 | + refreshListData(); |
230 | 237 | }
|
231 |
| - refreshListData(); |
232 | 238 | }
|
233 | 239 | }
|
234 | 240 |
|
@@ -277,15 +283,38 @@ private void loadModule() {
|
277 | 283 | private void onOK() {
|
278 | 284 | if (cbModule.getSelectedIndex() < moduleList.size() && !tfPack.getText().isEmpty()) {
|
279 | 285 | 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(); |
282 | 294 | }
|
283 |
| - dispose(); |
284 | 295 | } else {
|
285 | 296 | lb.setText("模块或者包名格式错误");
|
286 | 297 | }
|
287 | 298 | }
|
288 | 299 |
|
| 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 | + |
289 | 318 | private void onCancel() {
|
290 | 319 | // add your code here if necessary
|
291 | 320 | dispose();
|
|
0 commit comments