|
6 | 6 | import com.intellij.openapi.application.ApplicationManager; |
7 | 7 | import com.intellij.openapi.fileEditor.FileEditorManager; |
8 | 8 | import com.intellij.openapi.fileEditor.OpenFileDescriptor; |
| 9 | +import com.intellij.openapi.progress.ProgressIndicator; |
| 10 | +import com.intellij.openapi.progress.ProgressManager; |
| 11 | +import com.intellij.openapi.progress.Task; |
9 | 12 | import com.intellij.openapi.project.Project; |
10 | 13 | import com.intellij.openapi.vfs.LocalFileSystem; |
11 | 14 | import com.intellij.openapi.vfs.VirtualFile; |
|
14 | 17 | import com.shuzijun.leetcode.plugin.setting.ProjectConfig; |
15 | 18 | import com.shuzijun.leetcode.plugin.utils.*; |
16 | 19 | import org.apache.commons.lang.StringUtils; |
| 20 | +import org.jetbrains.annotations.NotNull; |
17 | 21 |
|
18 | 22 | import java.io.File; |
19 | 23 | import java.math.BigDecimal; |
20 | | -import java.util.concurrent.ExecutorService; |
21 | | -import java.util.concurrent.Executors; |
22 | 24 |
|
23 | 25 | /** |
24 | 26 | * @author shuzijun |
25 | 27 | */ |
26 | 28 | public class CodeManager { |
27 | 29 |
|
28 | | - private static ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); |
29 | | - |
30 | 30 | public static void openCode(Question question, Project project) { |
31 | 31 | Config config = PersistentConfig.getInstance().getInitConfig(); |
32 | 32 | String codeType = config.getCodeType(); |
@@ -173,7 +173,7 @@ public static void SubmitCode(Question question, Project project) { |
173 | 173 | if (response != null && response.getStatusCode() == 200) { |
174 | 174 | String body = response.getBody(); |
175 | 175 | JSONObject returnObj = JSONObject.parseObject(body); |
176 | | - cachedThreadPool.execute(new SubmitCheckTask(returnObj, codeTypeEnum, question, project)); |
| 176 | + ProgressManager.getInstance().run(new SubmitCheckTask(returnObj, codeTypeEnum, question, project)); |
177 | 177 | MessageUtils.getInstance(project).showInfoMsg("info", PropertiesUtils.getInfo("request.pending")); |
178 | 178 | } else if (response != null && response.getStatusCode() == 429) { |
179 | 179 | MessageUtils.getInstance(project).showInfoMsg("info", PropertiesUtils.getInfo("request.pending")); |
@@ -218,7 +218,7 @@ public static void RunCodeCode(Question question, Project project) { |
218 | 218 |
|
219 | 219 | String body = response.getBody(); |
220 | 220 | JSONObject returnObj = JSONObject.parseObject(body); |
221 | | - cachedThreadPool.execute(new RunCodeCheckTask(returnObj, project)); |
| 221 | + ProgressManager.getInstance().run(new RunCodeCheckTask(returnObj, project)); |
222 | 222 | MessageUtils.getInstance(project).showInfoMsg("info", PropertiesUtils.getInfo("request.pending")); |
223 | 223 | } else { |
224 | 224 | LogUtils.LOG.error("RuncodeCode failure " + response.getBody()); |
@@ -336,24 +336,29 @@ private static String getContent(JSONObject jsonObject) { |
336 | 336 | return sb.toString(); |
337 | 337 | } |
338 | 338 |
|
339 | | - private static class SubmitCheckTask implements Runnable { |
| 339 | + private static class SubmitCheckTask extends Task.Backgroundable { |
340 | 340 |
|
341 | 341 | private Question question; |
342 | 342 | private JSONObject returnObj; |
343 | 343 | private CodeTypeEnum codeTypeEnum; |
344 | 344 | private Project project; |
345 | 345 |
|
346 | 346 | public SubmitCheckTask(JSONObject returnObj, CodeTypeEnum codeTypeEnum, Question question, Project project) { |
| 347 | + super(project,"leetcode.editor.submitCheckTask",true); |
347 | 348 | this.returnObj = returnObj; |
348 | 349 | this.codeTypeEnum = codeTypeEnum; |
349 | 350 | this.question = question; |
350 | 351 | this.project = project; |
351 | 352 | } |
352 | 353 |
|
353 | 354 | @Override |
354 | | - public void run() { |
| 355 | + public void run(@NotNull ProgressIndicator progressIndicator) { |
355 | 356 | String key = returnObj.getString("submission_id"); |
356 | 357 | for (int i = 0; i < 50; i++) { |
| 358 | + if(progressIndicator.isCanceled()){ |
| 359 | + MessageUtils.getInstance(project).showWarnMsg("error", PropertiesUtils.getInfo("request.cancel")); |
| 360 | + return; |
| 361 | + } |
357 | 362 | try { |
358 | 363 | HttpRequest httpRequest = HttpRequest.get(URLUtils.getLeetcodeSubmissions() + key + "/check/"); |
359 | 364 | HttpResponse response = HttpRequestUtils.executeGet(httpRequest); |
@@ -424,22 +429,27 @@ private static String buildErrorMsg(JSONObject errorBody) { |
424 | 429 | } |
425 | 430 |
|
426 | 431 |
|
427 | | - private static class RunCodeCheckTask implements Runnable { |
| 432 | + private static class RunCodeCheckTask extends Task.Backgroundable { |
428 | 433 | private JSONObject returnObj; |
429 | 434 | private Project project; |
430 | 435 |
|
431 | 436 | public RunCodeCheckTask(JSONObject returnObj, Project project) { |
| 437 | + super(project,"leetcode.editor.runCodeCheckTask",true); |
432 | 438 | this.returnObj = returnObj; |
433 | 439 | this.project = project; |
434 | 440 | } |
435 | 441 |
|
436 | 442 | @Override |
437 | | - public void run() { |
| 443 | + public void run(@NotNull ProgressIndicator progressIndicator) { |
438 | 444 | String key = returnObj.getString("interpret_expected_id"); |
439 | 445 | if (StringUtils.isBlank(key)) { |
440 | 446 | key = returnObj.getString("interpret_id"); |
441 | 447 | } |
442 | 448 | for (int i = 0; i < 50; i++) { |
| 449 | + if(progressIndicator.isCanceled()){ |
| 450 | + MessageUtils.getInstance(project).showWarnMsg("error", PropertiesUtils.getInfo("request.cancel")); |
| 451 | + return; |
| 452 | + } |
443 | 453 | String body = null; |
444 | 454 | try { |
445 | 455 | HttpRequest httpRequest = HttpRequest.get(URLUtils.getLeetcodeSubmissions() + key + "/check/"); |
|
0 commit comments