|
1 | 1 | package io.snyk.languageserver.protocolextension; |
2 | 2 |
|
3 | | -import io.snyk.eclipse.plugin.utils.SnykLogger; |
4 | | -import org.apache.commons.lang3.tuple.ImmutablePair; |
5 | | -import org.apache.commons.lang3.tuple.Pair; |
6 | | -import org.eclipse.core.runtime.IProgressMonitor; |
7 | | -import org.eclipse.core.runtime.IStatus; |
8 | | -import org.eclipse.core.runtime.Status; |
9 | | -import org.eclipse.core.runtime.jobs.Job; |
10 | | -import org.eclipse.lsp4j.ProgressParams; |
11 | | -import org.eclipse.lsp4j.WorkDoneProgressBegin; |
12 | | -import org.eclipse.lsp4j.WorkDoneProgressCreateParams; |
13 | | -import org.eclipse.lsp4j.WorkDoneProgressEnd; |
14 | | -import org.eclipse.lsp4j.WorkDoneProgressNotification; |
15 | | -import org.eclipse.lsp4j.WorkDoneProgressReport; |
16 | | -import org.eclipse.lsp4j.jsonrpc.messages.Either; |
| 3 | +import static java.util.Collections.synchronizedSet; |
17 | 4 |
|
18 | | -import java.util.concurrent.CompletableFuture; |
19 | | -import java.util.concurrent.ConcurrentHashMap; |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.Set; |
20 | 7 |
|
21 | | -@SuppressWarnings("BusyWait") |
22 | 8 | public class ProgressManager { |
23 | | - final ConcurrentHashMap<String, Pair<SnykBackgroundJob, IProgressMonitor>> progresses = new ConcurrentHashMap<>(); |
24 | | - final ConcurrentHashMap<IProgressMonitor, Integer> currentPercentage = new ConcurrentHashMap<>(); |
25 | | - SnykBackgroundJobBuilder jobBuilder = new SnykBackgroundJobBuilder(); |
26 | | - |
27 | | - ProgressManager(SnykBackgroundJobBuilder jobBuilder) { |
28 | | - this.jobBuilder = jobBuilder; |
29 | | - } |
30 | | - |
31 | | - ProgressManager() { |
32 | | - } |
33 | | - |
34 | | - class SnykBackgroundJobBuilder { |
35 | | - SnykBackgroundJob build(WorkDoneProgressCreateParams param) { |
36 | | - return new SnykBackgroundJob(param); |
37 | | - } |
38 | | - } |
39 | | - |
40 | | - class SnykBackgroundJob extends Job { |
41 | | - private final WorkDoneProgressCreateParams param; |
42 | | - |
43 | | - public SnykBackgroundJob(WorkDoneProgressCreateParams param) { |
44 | | - super("Snyk Background Job"); |
45 | | - this.param = param; |
46 | | - } |
47 | | - |
48 | | - protected IStatus run(IProgressMonitor monitor) { |
49 | | - IStatus cancelStatus = doWork(progresses.get(getToken(param.getToken())).getKey(), monitor, param); |
50 | | - if (cancelStatus != null) |
51 | | - return cancelStatus; |
52 | | - return Status.OK_STATUS; |
53 | | - } |
54 | | - } |
55 | | - |
56 | | - public CompletableFuture<Void> createProgress(WorkDoneProgressCreateParams param) { |
57 | | - synchronized (progresses) { |
58 | | - var job = jobBuilder.build(param); |
59 | | - progresses.put(getToken(param.getToken()), new ImmutablePair<>(job, null)); |
60 | | - job.schedule(); |
61 | | - return CompletableFuture.completedFuture(null); |
62 | | - } |
63 | | - } |
64 | | - |
65 | | - IStatus doWork(SnykBackgroundJob job, IProgressMonitor monitor, WorkDoneProgressCreateParams param) { |
66 | | - var token = getToken(param.getToken()); |
67 | | - progresses.put(token, new ImmutablePair<>(job, monitor)); |
68 | | - currentPercentage.put(monitor, 0); |
69 | | - while (progresses.containsKey(token)) { |
70 | | - try { |
71 | | - if (monitor.isCanceled()) { |
72 | | - return Status.CANCEL_STATUS; |
73 | | - } |
74 | | - Thread.sleep(100); |
75 | | - } catch (InterruptedException e) { |
76 | | - SnykLogger.logError(e); |
77 | | - Thread.currentThread().interrupt(); |
78 | | - } |
79 | | - } |
80 | | - return null; |
81 | | - } |
82 | | - |
83 | | - String getToken(Either<String, Integer> param) { |
84 | | - return param.getLeft(); |
85 | | - } |
86 | | - |
87 | | - public void updateProgress(ProgressParams param) { |
88 | | - String progressToken = getToken(param.getToken()); |
89 | | - while (!progresses.containsKey(progressToken) || progresses.get(progressToken).getValue() == null) { |
90 | | - try { |
91 | | - Thread.sleep(1000); |
92 | | - } catch (InterruptedException e) { |
93 | | - SnykLogger.logError(e); |
94 | | - Thread.currentThread().interrupt(); |
95 | | - } |
96 | | - } |
97 | | - synchronized (progresses) { |
98 | | - WorkDoneProgressNotification notification = param.getValue().getLeft(); |
99 | | - IProgressMonitor monitor = progresses.get(progressToken).getValue(); |
100 | | - switch (notification.getKind()) { |
101 | | - case begin: |
102 | | - begin((WorkDoneProgressBegin) notification, monitor); |
103 | | - break; |
104 | | - case report: |
105 | | - report((WorkDoneProgressReport) notification, monitor); |
106 | | - break; |
107 | | - case end: |
108 | | - end((WorkDoneProgressEnd) notification, monitor); |
109 | | - progresses.remove(progressToken); |
110 | | - currentPercentage.remove(monitor); |
111 | | - break; |
112 | | - } |
113 | | - } |
114 | | - } |
115 | | - |
116 | | - private void end(WorkDoneProgressEnd end, IProgressMonitor monitor) { |
117 | | - if (null == end || null == monitor) |
118 | | - return; |
119 | | - monitor.subTask(end.getMessage()); |
120 | | - monitor.done(); |
121 | | - } |
122 | | - |
123 | | - private void report(WorkDoneProgressReport report, IProgressMonitor monitor) { |
124 | | - if (report.getPercentage() == null) return; |
125 | | - var worked = currentPercentage.get(monitor) != null |
126 | | - ? Math.min(currentPercentage.get(monitor), report.getPercentage()) |
127 | | - : 0; |
128 | | - |
129 | | - if (null != report.getMessage() && !report.getMessage().isBlank()) { |
130 | | - monitor.subTask(report.getMessage()); |
131 | | - } |
132 | | - |
133 | | - monitor.worked(report.getPercentage() - worked); |
134 | | - currentPercentage.put(monitor, report.getPercentage()); |
135 | | - } |
136 | | - |
137 | | - private void begin(WorkDoneProgressBegin begin, IProgressMonitor monitor) { |
138 | | - if (null == begin || null == monitor) |
139 | | - return; |
140 | | - if (begin.getPercentage() == null) { |
141 | | - monitor.beginTask(begin.getTitle(), IProgressMonitor.UNKNOWN); |
142 | | - } else { |
143 | | - monitor.beginTask(begin.getTitle(), 100); |
144 | | - } |
145 | | - String message = begin.getMessage(); |
146 | | - if (message != null && !message.isBlank()) |
147 | | - monitor.subTask(message); |
148 | | - } |
| 9 | + SnykExtendedLanguageClient lc = SnykExtendedLanguageClient.getInstance(); |
| 10 | + Set<String> progresses = synchronizedSet(new HashSet<>()); |
| 11 | + |
| 12 | + ProgressManager(SnykExtendedLanguageClient lc) { |
| 13 | + this.lc = lc; |
| 14 | + } |
| 15 | + |
| 16 | + public void cancelAll() { |
| 17 | + HashSet<String> copy = new HashSet<>(progresses); |
| 18 | + for (String token : copy) { |
| 19 | + cancelProgress(token); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public void cancelProgress(String token) { |
| 24 | + lc.cancelProgress(token); |
| 25 | + progresses.remove(token); |
| 26 | + } |
| 27 | + |
| 28 | + public void addProgress(String token) { |
| 29 | + progresses.add(token); |
| 30 | + } |
| 31 | + |
| 32 | + public void removeProgress(String token) { |
| 33 | + progresses.remove(token); |
| 34 | + } |
149 | 35 |
|
150 | 36 | } |
0 commit comments