-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cs
More file actions
362 lines (330 loc) · 14.6 KB
/
Main.cs
File metadata and controls
362 lines (330 loc) · 14.6 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Wox.Plugin;
using ProjectLauncher.Core.IPC;
namespace Community.PowerToys.Run.Plugin.ProjectLauncher
{
public class Main : IPlugin, IContextMenu
{
public static string PluginID => "EAC611DB-B5BB-4467-BD5C-F870F95BEDBC";
private PluginInitContext? _context;
private ProjectLauncherClient? _client;
private Dictionary<string, string>? _ideKeywords;
public string Name => "Project Launcher";
public string Description => "Search projects from Project Launcher";
public void Init(PluginInitContext context)
{
try
{
System.IO.File.WriteAllText(System.IO.Path.Combine(System.IO.Path.GetTempPath(), "project_launcher_plugin_debug.txt"), "Starting Init...\n");
_context = context;
_client = new ProjectLauncherClient();
// Fetch IDE keywords from server
Task.Run(async () => {
try {
_ideKeywords = await _client.GetSettingsAsync();
System.IO.File.AppendAllText(System.IO.Path.Combine(System.IO.Path.GetTempPath(), "project_launcher_plugin_debug.txt"), $"Loaded {_ideKeywords?.Count ?? 0} IDE keywords.\n");
} catch (Exception ex) {
System.IO.File.AppendAllText(System.IO.Path.Combine(System.IO.Path.GetTempPath(), "project_launcher_plugin_debug.txt"), $"Failed to load IDE keywords: {ex.Message}\n");
}
});
System.IO.File.AppendAllText(System.IO.Path.Combine(System.IO.Path.GetTempPath(), "project_launcher_plugin_debug.txt"), "Init success.\n");
}
catch (Exception ex)
{
System.IO.File.AppendAllText(System.IO.Path.Combine(System.IO.Path.GetTempPath(), "project_launcher_plugin_debug.txt"), $"Init Failed: {ex}\n");
throw;
}
}
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
{
if (selectedResult.ContextData is not IpcProjectResult p)
return new List<ContextMenuResult>();
var menus = new List<ContextMenuResult>();
// Open in Explorer
menus.Add(new ContextMenuResult
{
PluginName = Name,
Title = "Open in Explorer",
Glyph = "\uE838", // FolderOpen
FontFamily = "Segoe MDL2 Assets",
Action = _ =>
{
try
{
System.Diagnostics.Process.Start("explorer.exe", $"/select,\"{p.Path}\"");
return true;
}
catch { return false; }
}
});
// Open Terminal
menus.Add(new ContextMenuResult
{
PluginName = Name,
Title = "Open Terminal",
Glyph = "\uE756", // CommandPrompt
FontFamily = "Segoe MDL2 Assets",
Action = _ =>
{
try
{
// Try Windows Terminal first, fall back to cmd
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = "wt",
Arguments = $"-d \"{p.Path}\"",
UseShellExecute = true
});
return true;
}
catch
{
try
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = "cmd",
Arguments = $"/k cd /d \"{p.Path}\"",
UseShellExecute = true
});
return true;
} catch { return false; }
}
}
});
// Open Admin Terminal
menus.Add(new ContextMenuResult
{
PluginName = Name,
Title = "Open Terminal (Admin)",
Glyph = "\uE7EF", // Admin
FontFamily = "Segoe MDL2 Assets",
Action = _ =>
{
try
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = "wt",
Arguments = $"-d \"{p.Path}\"",
Verb = "runas",
UseShellExecute = true
});
return true;
}
catch
{
try
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = "cmd",
Arguments = $"/k cd /d \"{p.Path}\"",
Verb = "runas",
UseShellExecute = true
});
return true;
} catch { return false; }
}
}
});
// Open Repository
if (!string.IsNullOrEmpty(p.GitUrl))
{
menus.Add(new ContextMenuResult
{
PluginName = Name,
Title = "Open Repository",
Glyph = "\uE774", // Globe
FontFamily = "Segoe MDL2 Assets",
Action = _ =>
{
try
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = p.GitUrl,
UseShellExecute = true
});
return true;
}
catch { return false; }
}
});
}
return menus;
}
private static readonly string[] KnownIdeKeywords = new[] { "vscode", "code", "cursor", "windsurf", "visualstudio", "vs", "rider", "intellij", "idea", "webstorm", "pycharm", "clion", "goland", "phpstorm", "sublime", "atom", "nebula" };
public List<Result> Query(Query query)
{
if (string.IsNullOrEmpty(query.Search))
{
return new List<Result>
{
new Result
{
Title = "Type to search projects...",
SubTitle = "Example: pl my-project | pl new vscode my-project",
IcoPath = "Images\\icon.png",
Action = _ => true
}
};
}
// Handle "new" command
if (query.Search.StartsWith("new ", StringComparison.OrdinalIgnoreCase))
{
var remainder = query.Search.Substring(4).Trim();
// Parsing: <ide> <project>
// Try to find if first word is an IDE
var parts = remainder.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
string targetIde = "";
string targetProject = "";
if (parts.Length == 2)
{
var potentialIde = parts[0].ToLowerInvariant();
// Check hardcoded keywords first
if (KnownIdeKeywords.Contains(potentialIde))
{
targetIde = potentialIde;
targetProject = parts[1];
}
// Then check user-configured IDE keywords from server
else if (_ideKeywords != null && _ideKeywords.TryGetValue(potentialIde, out var mappedIde))
{
targetIde = mappedIde; // Use the mapped IDE id (e.g., "gv" -> "antigravity")
targetProject = parts[1];
}
else
{
targetProject = remainder; // No specific IDE found, treat all as project name
}
}
else
{
targetProject = remainder;
}
if (string.IsNullOrEmpty(targetProject))
{
return new List<Result>
{
new Result
{
Title = "New Project: <ide> <name>",
SubTitle = "e.g. 'pl new macchu-picchu' or 'pl new vscode macchu-picchu'",
IcoPath = "Images\\icon.png",
Action = _ => true
}
};
}
return new List<Result>
{
new Result
{
Title = $"Create project '{targetProject}' {(string.IsNullOrEmpty(targetIde) ? "" : $"in {targetIde}")}",
SubTitle = "Click to create and open",
IcoPath = "Images\\icon.png",
Action = _ =>
{
try
{
if (_client == null) return false;
var req = new IpcSearchRequest
{
Type = IpcRequestType.Create,
TargetIde = targetIde,
TargetProject = targetProject
};
// Fire and forget mostly, or wait for response?
// Wox Query is sync, Action is sync bool.
// We can run async task.
Task.Run(async () => {
try {
var response = await _client.SendRequestAsync(req);
if (!response.Success) _context?.API.ShowMsg($"Creation Failed: {response.Message}");
} catch(Exception ex) {
_context?.API.ShowMsg($"Creation Error: {ex.Message}");
}
});
return true;
}
catch { return false; }
}
}
};
}
// Handle "IDE Launch" command (pl <ide> <project>)
// Check if start with IDE keyword
string? forcedIde = null;
// We do NOT strip the query anymore, to allow Server-side SearchEngine to apply filtering logic.
// But we detect the forcedIde here just for the Action override if needed,
// though ideally Server returns the correct specific IDE result now.
var queryParts = query.Search.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
if (queryParts.Length >= 1)
{
var potentialIde = queryParts[0].ToLowerInvariant();
if (KnownIdeKeywords.Contains(potentialIde))
{
forcedIde = potentialIde;
}
// Also check user-configured IDE keywords
else if (_ideKeywords != null && _ideKeywords.TryGetValue(potentialIde, out var mappedIde))
{
forcedIde = mappedIde;
}
}
string effectiveQuery = query.Search;
try
{
if (_client == null) return new List<Result>();
// Normal search
var searchResults = Task.Run(() => _client.QueryAsync(effectiveQuery)).Result;
return searchResults.Select(r => new Result
{
Title = r.Name,
SubTitle = $"{r.IdeName} • {string.Join(", ", r.Languages)} • {r.Path}",
IcoPath = r.IdeIconPath,
Score = r.Score,
Action = _ =>
{
try
{
// Send Launch command to the main app via IPC
// The app handles all the IDE launching properly without terminal windows
Task.Run(async () => {
try {
var response = await _client.LaunchAsync(r.Path, r.IdeId);
if (!response.Success)
{
_context?.API.ShowMsg($"Launch failed: {response.Message}");
}
} catch (Exception ex) {
_context?.API.ShowMsg($"Launch error: {ex.Message}");
}
});
return true;
}
catch (Exception ex)
{
_context?.API.ShowMsg($"Error opening {r.Name}: {ex.Message}");
return false;
}
},
ContextData = r
}).ToList();
}
catch (Exception ex)
{
return new List<Result> {
new Result {
Title = "Error querying Project Launcher",
SubTitle = ex.Message,
IcoPath = "Images\\app.png"
}
};
}
}
}
}