-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMain.cs
234 lines (197 loc) · 8.31 KB
/
Main.cs
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
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Flow.Plugin.VSCodeWorkspaces
{
using Flow.Launcher.Plugin;
using Properties;
using RemoteMachinesHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Windows.Controls;
using VSCodeHelper;
using WorkspacesHelper;
public class Main : IPlugin, IPluginI18n, ISettingProvider, IContextMenu
{
internal static PluginInitContext _context { get; private set; }
private static Settings _settings;
public string Name => GetTranslatedPluginTitle();
public string Description => GetTranslatedPluginDescription();
private VSCodeInstance defaultInstalce;
private readonly VSCodeWorkspacesApi _workspacesApi = new();
private readonly VSCodeRemoteMachinesApi _machinesApi = new();
public List<Result> Query(Query query)
{
var results = new List<Result>();
var workspaces = new List<VSCodeWorkspace>();
// User defined extra workspaces
if (defaultInstalce != null)
{
workspaces.AddRange(_settings.CustomWorkspaces.Select(uri =>
VSCodeWorkspacesApi.ParseVSCodeUri(uri, defaultInstalce)));
}
// Search opened workspaces
if (_settings.DiscoverWorkspaces)
{
workspaces.AddRange(_workspacesApi.Workspaces);
}
// Simple de-duplication
results.AddRange(workspaces.Distinct()
.Select(CreateWorkspaceResult)
);
// Search opened remote machines
if (_settings.DiscoverMachines)
{
_machinesApi.Machines.ForEach(a =>
{
var title = $"{a.Host}";
if (!string.IsNullOrEmpty(a.User) && !string.IsNullOrEmpty(a.HostName))
{
title += $" [{a.User}@{a.HostName}]";
}
var tooltip = Resources.SSHRemoteMachine;
results.Add(new Result
{
Title = title,
SubTitle = Resources.SSHRemoteMachine,
Icon = a.VSCodeInstance.RemoteIcon,
TitleToolTip = tooltip,
Action = c =>
{
bool hide;
try
{
var process = new ProcessStartInfo
{
FileName = a.VSCodeInstance.ExecutablePath,
UseShellExecute = true,
Arguments =
$"--new-window --enable-proposed-api ms-vscode-remote.remote-ssh --remote ssh-remote+{((char)34) + a.Host + ((char)34)}",
WindowStyle = ProcessWindowStyle.Hidden,
};
Process.Start(process);
hide = true;
}
catch (Win32Exception)
{
var name = $"{_context.CurrentPluginMetadata.Name}";
string msg = Resources.OpenFail;
_context.API.ShowMsg(name, msg, string.Empty);
hide = false;
}
return hide;
},
ContextData = a,
});
});
}
if (query.ActionKeyword == string.Empty ||
(query.ActionKeyword != string.Empty && query.Search != string.Empty))
{
results = results.Where(r =>
{
r.Score = _context.API.FuzzySearch(query.Search, r.Title).Score;
return r.Score > 0;
}).ToList();
}
return results;
}
private Result CreateWorkspaceResult(VSCodeWorkspace ws)
{
var title = $"{ws.FolderName}";
var typeWorkspace = ws.WorkspaceTypeToString();
if (ws.TypeWorkspace != TypeWorkspace.Local)
{
title = ws.Lable != null
? $"{ws.Lable}"
: $"{title}{(ws.ExtraInfo != null ? $" - {ws.ExtraInfo}" : string.Empty)} ({typeWorkspace})";
}
var tooltip =
$"{Resources.Workspace}{(ws.TypeWorkspace != TypeWorkspace.Local ? $" {Resources.In} {typeWorkspace}" : string.Empty)}: {SystemPath.RealPath(ws.RelativePath)}";
return new Result
{
Title = title,
SubTitle = tooltip,
Icon = ws.VSCodeInstance.WorkspaceIcon,
TitleToolTip = tooltip,
Action = c =>
{
try
{
var modifierKeys = c.SpecialKeyState.ToModifierKeys();
if (modifierKeys == System.Windows.Input.ModifierKeys.Control)
{
_context.API.OpenDirectory(SystemPath.RealPath(ws.RelativePath));
return true;
}
var process = new ProcessStartInfo
{
FileName = ws.VSCodeInstance.ExecutablePath,
UseShellExecute = true,
WindowStyle = ProcessWindowStyle.Hidden,
};
process.ArgumentList.Add("--folder-uri");
process.ArgumentList.Add(ws.Path);
Process.Start(process);
return true;
}
catch (Win32Exception)
{
var name = $"{_context.CurrentPluginMetadata.Name}";
string msg = Resources.OpenFail;
_context.API.ShowMsg(name, msg, string.Empty);
}
return false;
},
ContextData = ws,
};
}
public void Init(PluginInitContext context)
{
_context = context;
_settings = context.API.LoadSettingJsonStorage<Settings>();
VSCodeInstances.LoadVSCodeInstances();
// Prefer stable version, or the first one we got
defaultInstalce = VSCodeInstances.Instances.Find(e => e.VSCodeVersion == VSCodeVersion.Stable) ??
VSCodeInstances.Instances.FirstOrDefault();
}
public Control CreateSettingPanel() => new SettingsView(_context, _settings);
public void OnCultureInfoChanged(CultureInfo newCulture)
{
Resources.Culture = newCulture;
}
public string GetTranslatedPluginTitle()
{
return Resources.PluginTitle;
}
public string GetTranslatedPluginDescription()
{
return Resources.PluginDescription;
}
public List<Result> LoadContextMenus(Result selectedResult)
{
List<Result> results = new();
if (selectedResult.ContextData is VSCodeWorkspace ws && ws.TypeWorkspace == TypeWorkspace.Local)
{
results.Add(new Result
{
Title = Resources.OpenFolder,
SubTitle = Resources.OpenFolderSubTitle,
Icon = ws.VSCodeInstance.WorkspaceIcon,
TitleToolTip = Resources.OpenFolderSubTitle,
Action = c =>
{
_context.API.OpenDirectory(SystemPath.RealPath(ws.RelativePath));
return true;
},
ContextData = ws,
});
}
return results;
}
}
}