Skip to content

Commit 4f2b71d

Browse files
committed
Upload missing files
1 parent 280f910 commit 4f2b71d

File tree

3 files changed

+238
-0
lines changed

3 files changed

+238
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dev.rollczi.litecommands.intellijplugin.util;
2+
3+
import com.intellij.openapi.project.Project;
4+
import java.util.concurrent.CompletableFuture;
5+
import java.util.function.Supplier;
6+
7+
public interface IdeaEmptyTask {
8+
9+
IdeaEmptyTask then(IdeaTaskType type, Runnable runnable);
10+
11+
IdeaEmptyTask then(Runnable runnable);
12+
13+
<R> IdeaTask<R> map(IdeaTaskType type, Supplier<R> function);
14+
15+
<R> IdeaTask<R> flatMap(IdeaTaskType type, Supplier<IdeaTask<R>> function);
16+
17+
<R> IdeaTask<R> flatMap(Supplier<IdeaTask<R>> function);
18+
19+
<R> IdeaTask<R> ui(Supplier<R> supplier);
20+
21+
<R> IdeaTask<R> read(Supplier<R> supplier);
22+
23+
<R> IdeaTask<R> write(Supplier<R> supplier);
24+
25+
<R> IdeaTask<R> async(Supplier<R> supplier);
26+
27+
<R> IdeaTask<R> waitForSmart(Project project, Supplier<R> supplier);
28+
29+
IdeaEmptyTask waitForSmart(Project project, Runnable supplier);
30+
31+
IdeaEmptyTask waitForSmart(Project project);
32+
33+
CompletableFuture<?> asFuture();
34+
35+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package dev.rollczi.litecommands.intellijplugin.util;
2+
3+
import com.intellij.openapi.project.Project;
4+
import java.util.concurrent.CompletableFuture;
5+
import java.util.function.Consumer;
6+
import java.util.function.Function;
7+
import java.util.function.Supplier;
8+
9+
public class IdeaTask<T> implements IdeaEmptyTask {
10+
11+
private final CompletableFuture<T> future;
12+
13+
public IdeaTask(CompletableFuture<T> future) {
14+
this.future = future;
15+
}
16+
17+
public IdeaTask<T> filter(IdeaTaskType type, Function<T, Boolean> function) {
18+
return flatMap(type, t -> function.apply(t) ? this : notCompleted());
19+
}
20+
21+
public IdeaTask<T> then(IdeaTaskType type, Consumer<T> consumer) {
22+
return map(type, t -> {
23+
consumer.accept(t);
24+
return t;
25+
});
26+
}
27+
28+
@Override
29+
public IdeaTask<T> then(IdeaTaskType type, Runnable runnable) {
30+
return then(type, unused -> runnable.run());
31+
}
32+
33+
public IdeaTask<T> then(Consumer<T> consumer) {
34+
return map(t -> {
35+
consumer.accept(t);
36+
return t;
37+
});
38+
}
39+
40+
@Override
41+
public IdeaTask<T> then(Runnable runnable) {
42+
return then(unused -> runnable.run());
43+
}
44+
45+
public <R> IdeaTask<R> map(IdeaTaskType type, Function<T, R> function) {
46+
return new IdeaTask<>(future.thenApplyAsync(function, type.getExecutor()));
47+
}
48+
49+
@Override
50+
public <R> IdeaTask<R> map(IdeaTaskType type, Supplier<R> function) {
51+
return map(type, unused -> function.get());
52+
}
53+
54+
public <R> IdeaTask<R> map(Function<T, R> supplier) {
55+
return new IdeaTask<>(future.thenApply(supplier));
56+
}
57+
58+
public <R> IdeaTask<R> flatMap(Function<T, IdeaTask<R>> function) {
59+
return new IdeaTask<>(future.thenCompose(t -> function.apply(t).asFuture()));
60+
}
61+
62+
public <R> IdeaTask<R> flatMap(IdeaTaskType type, Function<T, IdeaTask<R>> function) {
63+
return new IdeaTask<>(future.thenCompose(t -> {
64+
IdeaTask<IdeaTask<R>> task = this.map(type, function);
65+
CompletableFuture<IdeaTask<R>> future = task.asFuture();
66+
67+
return future.thenCompose(ideaTask -> ideaTask.asFuture());
68+
}));
69+
}
70+
71+
@Override
72+
public <R> IdeaTask<R> flatMap(IdeaTaskType type, Supplier<IdeaTask<R>> function) {
73+
return flatMap(type, unused -> function.get());
74+
}
75+
76+
@Override
77+
public <R> IdeaTask<R> flatMap(Supplier<IdeaTask<R>> function) {
78+
return flatMap(unused -> function.get());
79+
}
80+
81+
@Override
82+
public <R> IdeaTask<R> ui(Supplier<R> supplier) {
83+
return ui(unused -> supplier.get());
84+
}
85+
86+
public <R> IdeaTask<R> ui(Function<T, R> supplier) {
87+
return map(IdeaTaskType.UI, supplier);
88+
}
89+
90+
@Override
91+
public <R> IdeaTask<R> read(Supplier<R> supplier) {
92+
return read(unused -> supplier.get());
93+
}
94+
95+
public <R> IdeaTask<R> read(Function<T, R> supplier) {
96+
return map(IdeaTaskType.READ, supplier);
97+
}
98+
99+
@Override
100+
public <R> IdeaTask<R> write(Supplier<R> supplier) {
101+
return write(unused -> supplier.get());
102+
}
103+
104+
public <R> IdeaTask<R> write(Function<T, R> supplier) {
105+
return map(IdeaTaskType.WRITE, supplier);
106+
}
107+
108+
@Override
109+
public <R> IdeaTask<R> async(Supplier<R> supplier) {
110+
return async(unused -> supplier.get());
111+
}
112+
113+
public <R> IdeaTask<R> async(Function<T, R> supplier) {
114+
return map(IdeaTaskType.ASYNC, supplier);
115+
}
116+
117+
@Override
118+
public <R> IdeaTask<R> waitForSmart(Project project, Supplier<R> supplier) {
119+
return waitForSmart(project, unused -> supplier.get());
120+
}
121+
122+
public <R> IdeaTask<R> waitForSmart(Project project, Function<T, R> supplier) {
123+
return map(IdeaTaskType.smartMode(project), supplier);
124+
}
125+
126+
@Override
127+
public IdeaTask<T> waitForSmart(Project project, Runnable runnable) {
128+
return waitForSmart(project, unused -> {
129+
runnable.run();
130+
return null;
131+
});
132+
}
133+
134+
@Override
135+
public IdeaTask<T> waitForSmart(Project project) {
136+
return waitForSmart(project, () -> {});
137+
}
138+
139+
public CompletableFuture<T> asFuture() {
140+
return future;
141+
}
142+
143+
public static IdeaEmptyTask start() {
144+
return new IdeaTask<>(CompletableFuture.completedFuture(null));
145+
}
146+
147+
public static IdeaEmptyTask startInSmart(Project project) {
148+
return start().waitForSmart(project);
149+
}
150+
151+
public static <T> IdeaTask<T> supply(IdeaTaskType type, Supplier<T> supplier) {
152+
return start().map(type, supplier);
153+
}
154+
155+
public static <T> IdeaTask<T> notCompleted() {
156+
return new IdeaTask<>(new CompletableFuture<>());
157+
}
158+
159+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package dev.rollczi.litecommands.intellijplugin.util;
2+
3+
import com.intellij.openapi.application.ApplicationManager;
4+
import com.intellij.openapi.command.CommandProcessor;
5+
import com.intellij.openapi.command.WriteCommandAction;
6+
import com.intellij.openapi.project.DumbService;
7+
import com.intellij.openapi.project.Project;
8+
import java.util.concurrent.Executor;
9+
10+
public class IdeaTaskType {
11+
12+
public static final IdeaTaskType UI = new IdeaTaskType(command -> ApplicationManager.getApplication().invokeLater(command));
13+
public static final IdeaTaskType READ = new IdeaTaskType(command -> ApplicationManager.getApplication().runReadAction(command));
14+
public static final IdeaTaskType WRITE = new IdeaTaskType(command -> ApplicationManager.getApplication().runWriteAction(command));
15+
public static final IdeaTaskType WRITE_INTENT = new IdeaTaskType(command -> ApplicationManager.getApplication().runWriteIntentReadAction(() -> {
16+
ApplicationManager.getApplication().runWriteAction(command);
17+
return null;
18+
}));
19+
20+
public static final IdeaTaskType ASYNC = new IdeaTaskType(command -> ApplicationManager.getApplication().executeOnPooledThread(command));
21+
22+
public static IdeaTaskType smartMode(Project project) {
23+
return new IdeaTaskType(command -> DumbService.getInstance(project).runWhenSmart(command));
24+
}
25+
26+
public static IdeaTaskType writeCommand(Project project) {
27+
return new IdeaTaskType(command -> WriteCommandAction.runWriteCommandAction(project, command));
28+
}
29+
30+
public static IdeaTaskType command(Project project) {
31+
return new IdeaTaskType(command -> CommandProcessor.getInstance().executeCommand(project, command, null, null));
32+
}
33+
34+
private final Executor executor;
35+
36+
Executor getExecutor() {
37+
return executor;
38+
}
39+
40+
public IdeaTaskType(Executor executor) {
41+
this.executor = executor;
42+
}
43+
44+
}

0 commit comments

Comments
 (0)