/
ExternalEditorType.java
261 lines (218 loc) · 10.1 KB
/
ExternalEditorType.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package io.xpipe.app.prefs;
import io.xpipe.app.ext.PrefsChoiceValue;
import io.xpipe.app.issue.ErrorEvent;
import io.xpipe.app.util.LocalShell;
import io.xpipe.app.util.WindowsRegistry;
import io.xpipe.core.process.CommandBuilder;
import io.xpipe.core.process.OsType;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.function.Supplier;
public interface ExternalEditorType extends PrefsChoiceValue {
ExternalEditorType NOTEPAD = new WindowsType("app.notepad", "notepad", false) {
@Override
protected Optional<Path> determineInstallation() {
return Optional.of(Path.of(System.getenv("SystemRoot") + "\\System32\\notepad.exe"));
}
};
ExternalEditorType VSCODIUM_WINDOWS = new WindowsType("app.vscodium", "codium.cmd", false) {
@Override
protected Optional<Path> determineInstallation() {
return Optional.of(Path.of(System.getenv("LOCALAPPDATA"))
.resolve("Programs")
.resolve("VSCodium")
.resolve("bin")
.resolve("codium.cmd"));
}
};
ExternalEditorType VSCODE_WINDOWS = new WindowsType("app.vscode", "code.cmd", false) {
@Override
protected Optional<Path> determineInstallation() {
return Optional.of(Path.of(System.getenv("LOCALAPPDATA"))
.resolve("Programs")
.resolve("Microsoft VS Code")
.resolve("bin")
.resolve("code.cmd"));
}
};
ExternalEditorType VSCODE_INSIDERS_WINDOWS = new WindowsType("app.vscodeInsiders", "code-insiders.cmd", false) {
@Override
protected Optional<Path> determineInstallation() {
return Optional.of(Path.of(System.getenv("LOCALAPPDATA"))
.resolve("Programs")
.resolve("Microsoft VS Code Insiders")
.resolve("bin")
.resolve("code-insiders.cmd"));
}
};
ExternalEditorType NOTEPADPLUSPLUS = new WindowsType("app.notepad++", "notepad++", false) {
@Override
protected Optional<Path> determineInstallation() {
var found = WindowsRegistry.readString(WindowsRegistry.HKEY_LOCAL_MACHINE, "SOFTWARE\\Notepad++", null);
// Check 32 bit install
if (found.isEmpty()) {
found = WindowsRegistry.readString(
WindowsRegistry.HKEY_LOCAL_MACHINE, "WOW6432Node\\SOFTWARE\\Notepad++", null);
}
return found.map(p -> p + "\\notepad++.exe").map(Path::of);
}
};
LinuxPathType VSCODE_LINUX = new LinuxPathType("app.vscode", "code");
LinuxPathType VSCODIUM_LINUX = new LinuxPathType("app.vscodium", "codium");
LinuxPathType GNOME = new LinuxPathType("app.gnomeTextEditor", "gnome-text-editor");
LinuxPathType KATE = new LinuxPathType("app.kate", "kate");
LinuxPathType GEDIT = new LinuxPathType("app.gedit", "gedit");
LinuxPathType LEAFPAD = new LinuxPathType("app.leafpad", "leafpad");
LinuxPathType MOUSEPAD = new LinuxPathType("app.mousepad", "mousepad");
LinuxPathType PLUMA = new LinuxPathType("app.pluma", "pluma");
ExternalEditorType TEXT_EDIT = new MacOsEditor("app.textEdit", "TextEdit");
ExternalEditorType BBEDIT = new MacOsEditor("app.bbedit", "BBEdit");
ExternalEditorType SUBLIME_MACOS = new MacOsEditor("app.sublime", "Sublime Text");
ExternalEditorType VSCODE_MACOS = new MacOsEditor("app.vscode", "Visual Studio Code");
ExternalEditorType VSCODIUM_MACOS = new MacOsEditor("app.vscodium", "VSCodium");
ExternalEditorType CUSTOM = new ExternalEditorType() {
@Override
public void launch(Path file) throws Exception {
var customCommand = AppPrefs.get().customEditorCommand().getValue();
if (customCommand == null || customCommand.isBlank()) {
throw ErrorEvent.expected(new IllegalStateException("No custom editor command specified"));
}
var format =
customCommand.toLowerCase(Locale.ROOT).contains("$file") ? customCommand : customCommand + " $FILE";
ExternalApplicationHelper.startAsync(CommandBuilder.of()
.add(ExternalApplicationHelper.replaceFileArgument(format, "FILE", file.toString())));
}
@Override
public String getId() {
return "app.custom";
}
};
ExternalEditorType FLEET = new GenericPathType("app.fleet", "fleet", false);
ExternalEditorType INTELLIJ = new GenericPathType("app.intellij", "idea", false);
ExternalEditorType PYCHARM = new GenericPathType("app.pycharm", "pycharm", false);
ExternalEditorType WEBSTORM = new GenericPathType("app.webstorm", "webstorm", false);
ExternalEditorType CLION = new GenericPathType("app.clion", "clion", false);
List<ExternalEditorType> WINDOWS_EDITORS =
List.of(VSCODIUM_WINDOWS, VSCODE_INSIDERS_WINDOWS, VSCODE_WINDOWS, NOTEPADPLUSPLUS, NOTEPAD);
List<LinuxPathType> LINUX_EDITORS =
List.of(VSCODIUM_LINUX, VSCODE_LINUX, KATE, GEDIT, PLUMA, LEAFPAD, MOUSEPAD, GNOME);
List<ExternalEditorType> MACOS_EDITORS = List.of(BBEDIT, VSCODIUM_MACOS, VSCODE_MACOS, SUBLIME_MACOS, TEXT_EDIT);
List<ExternalEditorType> CROSS_PLATFORM_EDITORS = List.of(FLEET, INTELLIJ, PYCHARM, WEBSTORM, CLION);
@SuppressWarnings("TrivialFunctionalExpressionUsage")
List<ExternalEditorType> ALL = ((Supplier<List<ExternalEditorType>>) () -> {
var all = new ArrayList<ExternalEditorType>();
if (OsType.getLocal().equals(OsType.WINDOWS)) {
all.addAll(WINDOWS_EDITORS);
}
if (OsType.getLocal().equals(OsType.LINUX)) {
all.addAll(LINUX_EDITORS);
}
if (OsType.getLocal().equals(OsType.MACOS)) {
all.addAll(MACOS_EDITORS);
}
all.addAll(CROSS_PLATFORM_EDITORS);
all.add(CUSTOM);
return all;
})
.get();
static void detectDefault() {
var typeProperty = AppPrefs.get().externalEditor;
var customProperty = AppPrefs.get().customEditorCommand;
if (OsType.getLocal().equals(OsType.WINDOWS)) {
typeProperty.set(WINDOWS_EDITORS.stream()
.filter(PrefsChoiceValue::isAvailable)
.findFirst()
.orElse(null));
}
if (OsType.getLocal().equals(OsType.LINUX)) {
var env = System.getenv("VISUAL");
if (env != null) {
var found = LINUX_EDITORS.stream()
.filter(externalEditorType -> externalEditorType.executable.equalsIgnoreCase(env))
.findFirst()
.orElse(null);
if (found == null) {
typeProperty.set(CUSTOM);
customProperty.set(env);
} else {
typeProperty.set(found);
}
} else {
typeProperty.set(LINUX_EDITORS.stream()
.filter(ExternalApplicationType.PathApplication::isAvailable)
.findFirst()
.orElse(null));
}
}
if (OsType.getLocal().equals(OsType.MACOS)) {
typeProperty.set(MACOS_EDITORS.stream()
.filter(PrefsChoiceValue::isAvailable)
.findFirst()
.orElse(null));
}
}
void launch(Path file) throws Exception;
class MacOsEditor extends ExternalApplicationType.MacApplication implements ExternalEditorType {
public MacOsEditor(String id, String applicationName) {
super(id, applicationName);
}
@Override
public void launch(Path file) throws Exception {
ExternalApplicationHelper.startAsync(CommandBuilder.of()
.add("open", "-a")
.addQuoted(applicationName)
.addFile(file.toString()));
}
}
class GenericPathType extends ExternalApplicationType.PathApplication implements ExternalEditorType {
public GenericPathType(String id, String command, boolean explicityAsync) {
super(id, command, explicityAsync);
}
@Override
public void launch(Path file) throws Exception {
var builder = CommandBuilder.of().addFile(executable).addFile(file.toString());
if (explicitlyAsync) {
ExternalApplicationHelper.startAsync(builder);
} else {
LocalShell.getShell().executeSimpleCommand(builder);
}
}
}
class LinuxPathType extends GenericPathType {
public LinuxPathType(String id, String command) {
super(id, command, true);
}
@Override
public boolean isSelectable() {
return OsType.getLocal().equals(OsType.LINUX);
}
}
abstract class WindowsType extends ExternalApplicationType.WindowsType implements ExternalEditorType {
private final boolean detach;
public WindowsType(String id, String executable, boolean detach) {
super(id, executable);
this.detach = detach;
}
@Override
public void launch(Path file) throws Exception {
var location = determineFromPath();
if (location.isEmpty()) {
location = determineInstallation();
if (location.isEmpty()) {
throw ErrorEvent.expected(new IOException("Unable to find installation of "
+ toTranslatedString().getValue()));
}
}
var builder = CommandBuilder.of().addFile(location.get().toString()).addFile(file.toString());
if (detach) {
ExternalApplicationHelper.startAsync(builder);
} else {
LocalShell.getShell().executeSimpleCommand(builder);
}
}
}
}