Skip to content

Commit 8b13155

Browse files
authored
Merge pull request #20 from taooceros/workspace
Support workspace
2 parents c2bc0d0 + fa7bead commit 8b13155

8 files changed

+141
-86
lines changed

Flow.Plugin.VSCodeWorkspaces.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<AssemblyName>Flow.Plugin.VSCodeWorkspaces</AssemblyName>
88
<OutputType>Library</OutputType>
99
<Version>1.0</Version>
10-
<useWPF>true</useWPF>
10+
<UseWpf>true</UseWpf>
1111
<Platforms>x64</Platforms>
1212
<AppendRuntimeIdentifierToOutputPath>False</AppendRuntimeIdentifierToOutputPath>
1313
<LangVersion>latest</LangVersion>

Main.cs

+26-22
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ namespace Flow.Plugin.VSCodeWorkspaces
1919

2020
public class Main : IPlugin, IPluginI18n, ISettingProvider, IContextMenu
2121
{
22-
internal static PluginInitContext _context { get; private set; }
22+
internal static PluginInitContext Context { get; private set; }
2323

2424
private static Settings _settings;
2525

2626
public string Name => GetTranslatedPluginTitle();
2727

2828
public string Description => GetTranslatedPluginDescription();
2929

30-
private VSCodeInstance defaultInstalce;
30+
private VSCodeInstance _defaultInstance;
3131

3232
private readonly VSCodeWorkspacesApi _workspacesApi = new();
3333

@@ -36,13 +36,13 @@ public class Main : IPlugin, IPluginI18n, ISettingProvider, IContextMenu
3636
public List<Result> Query(Query query)
3737
{
3838
var results = new List<Result>();
39-
var workspaces = new List<VSCodeWorkspace>();
39+
var workspaces = new List<VsCodeWorkspace>();
4040

4141
// User defined extra workspaces
42-
if (defaultInstalce != null)
42+
if (_defaultInstance != null)
4343
{
4444
workspaces.AddRange(_settings.CustomWorkspaces.Select(uri =>
45-
VSCodeWorkspacesApi.ParseVSCodeUri(uri, defaultInstalce)));
45+
VSCodeWorkspacesApi.ParseVSCodeUri(uri, _defaultInstance)));
4646
}
4747

4848
// Search opened workspaces
@@ -95,9 +95,9 @@ public List<Result> Query(Query query)
9595
}
9696
catch (Win32Exception)
9797
{
98-
var name = $"{_context.CurrentPluginMetadata.Name}";
98+
var name = $"{Context.CurrentPluginMetadata.Name}";
9999
string msg = Resources.OpenFail;
100-
_context.API.ShowMsg(name, msg, string.Empty);
100+
Context.API.ShowMsg(name, msg, string.Empty);
101101
hide = false;
102102
}
103103

@@ -113,7 +113,7 @@ public List<Result> Query(Query query)
113113
{
114114
results = results.Where(r =>
115115
{
116-
r.Score = _context.API.FuzzySearch(query.Search, r.Title).Score;
116+
r.Score = Context.API.FuzzySearch(query.Search, r.Title).Score;
117117
return r.Score > 0;
118118
}).ToList();
119119
}
@@ -122,20 +122,20 @@ public List<Result> Query(Query query)
122122
return results;
123123
}
124124

125-
private Result CreateWorkspaceResult(VSCodeWorkspace ws)
125+
private static Result CreateWorkspaceResult(VsCodeWorkspace ws)
126126
{
127127
var title = $"{ws.FolderName}";
128128
var typeWorkspace = ws.WorkspaceTypeToString();
129129

130-
if (ws.TypeWorkspace != TypeWorkspace.Local)
130+
if (ws.WorkspaceLocation != WorkspaceLocation.Local)
131131
{
132-
title = ws.Lable != null
133-
? $"{ws.Lable}"
132+
title = ws.Label != null
133+
? $"{ws.Label}"
134134
: $"{title}{(ws.ExtraInfo != null ? $" - {ws.ExtraInfo}" : string.Empty)} ({typeWorkspace})";
135135
}
136136

137137
var tooltip =
138-
$"{Resources.Workspace}{(ws.TypeWorkspace != TypeWorkspace.Local ? $" {Resources.In} {typeWorkspace}" : string.Empty)}: {SystemPath.RealPath(ws.RelativePath)}";
138+
$"{Resources.Workspace}{(ws.WorkspaceLocation != WorkspaceLocation.Local ? $" {Resources.In} {typeWorkspace}" : string.Empty)}: {SystemPath.RealPath(ws.RelativePath)}";
139139

140140
return new Result
141141
{
@@ -150,7 +150,7 @@ private Result CreateWorkspaceResult(VSCodeWorkspace ws)
150150
var modifierKeys = c.SpecialKeyState.ToModifierKeys();
151151
if (modifierKeys == System.Windows.Input.ModifierKeys.Control)
152152
{
153-
_context.API.OpenDirectory(SystemPath.RealPath(ws.RelativePath));
153+
Context.API.OpenDirectory(SystemPath.RealPath(ws.RelativePath));
154154
return true;
155155
}
156156

@@ -160,17 +160,21 @@ private Result CreateWorkspaceResult(VSCodeWorkspace ws)
160160
UseShellExecute = true,
161161
WindowStyle = ProcessWindowStyle.Hidden,
162162
};
163-
process.ArgumentList.Add("--folder-uri");
163+
164+
process.ArgumentList.Add(ws.WorkspaceType == WorkspaceType.Workspace
165+
? "--file-uri"
166+
: "--folder-uri");
167+
164168
process.ArgumentList.Add(ws.Path);
165169

166170
Process.Start(process);
167171
return true;
168172
}
169173
catch (Win32Exception)
170174
{
171-
var name = $"{_context.CurrentPluginMetadata.Name}";
175+
var name = $"{Context.CurrentPluginMetadata.Name}";
172176
string msg = Resources.OpenFail;
173-
_context.API.ShowMsg(name, msg, string.Empty);
177+
Context.API.ShowMsg(name, msg, string.Empty);
174178
}
175179

176180
return false;
@@ -181,17 +185,17 @@ private Result CreateWorkspaceResult(VSCodeWorkspace ws)
181185

182186
public void Init(PluginInitContext context)
183187
{
184-
_context = context;
188+
Context = context;
185189
_settings = context.API.LoadSettingJsonStorage<Settings>();
186190

187191
VSCodeInstances.LoadVSCodeInstances();
188192

189193
// Prefer stable version, or the first one we got
190-
defaultInstalce = VSCodeInstances.Instances.Find(e => e.VSCodeVersion == VSCodeVersion.Stable) ??
194+
_defaultInstance = VSCodeInstances.Instances.Find(e => e.VSCodeVersion == VSCodeVersion.Stable) ??
191195
VSCodeInstances.Instances.FirstOrDefault();
192196
}
193197

194-
public Control CreateSettingPanel() => new SettingsView(_context, _settings);
198+
public Control CreateSettingPanel() => new SettingsView(Context, _settings);
195199

196200
public void OnCultureInfoChanged(CultureInfo newCulture)
197201
{
@@ -211,7 +215,7 @@ public string GetTranslatedPluginDescription()
211215
public List<Result> LoadContextMenus(Result selectedResult)
212216
{
213217
List<Result> results = new();
214-
if (selectedResult.ContextData is VSCodeWorkspace ws && ws.TypeWorkspace == TypeWorkspace.Local)
218+
if (selectedResult.ContextData is VsCodeWorkspace ws && ws.WorkspaceLocation == WorkspaceLocation.Local)
215219
{
216220
results.Add(new Result
217221
{
@@ -221,7 +225,7 @@ public List<Result> LoadContextMenus(Result selectedResult)
221225
TitleToolTip = Resources.OpenFolderSubTitle,
222226
Action = c =>
223227
{
224-
_context.API.OpenDirectory(SystemPath.RealPath(ws.RelativePath));
228+
Context.API.OpenDirectory(SystemPath.RealPath(ws.RelativePath));
225229
return true;
226230
},
227231
ContextData = ws,

RemoteMachinesHelper/VSCodeRemoteMachinesApi.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public List<VSCodeRemoteMachine> Machines
6161
catch (Exception ex)
6262
{
6363
var message = $"Failed to deserialize ${vscode_settings}";
64-
Main._context.API.LogException("VSCodeWorkSpaces", message, ex);
64+
Main.Context.API.LogException("VSCodeWorkSpaces", message, ex);
6565
}
6666
}
6767
}

SettingsView.xaml.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ private void ButtonAdd_Click(object sender, RoutedEventArgs e)
4141
var uri = addUri.Text;
4242

4343
// System.Uri fails to parse vscode-remote://XXX+YYY URIs, skip them
44-
var type = ParseVSCodeUri.GetTypeWorkspace(uri).TypeWorkspace;
45-
if (!type.HasValue || type.Value == TypeWorkspace.Local)
44+
var type = ParseVSCodeUri.GetTypeWorkspace(uri).workspaceLocation;
45+
if (!type.HasValue || type.Value == WorkspaceLocation.Local)
4646
{
4747
// Converts file paths to proper URI
4848
uri = new Uri(uri).AbsoluteUri;

WorkspacesHelper/ParseVSCodeUri.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ public class ParseVSCodeUri
1818

1919
private static readonly Regex DevContainerWorkspace = new Regex(@"^vscode-remote://dev-container\+(.+?(?=\/))(.+)$", RegexOptions.Compiled);
2020

21-
public static (TypeWorkspace? TypeWorkspace, string MachineName, string Path) GetTypeWorkspace(string uri)
21+
public static (WorkspaceLocation? workspaceLocation, string MachineName, string Path) GetTypeWorkspace(string uri)
2222
{
2323
if (LocalWorkspace.IsMatch(uri))
2424
{
2525
var match = LocalWorkspace.Match(uri);
2626

2727
if (match.Groups.Count > 1)
2828
{
29-
return (TypeWorkspace.Local, null, match.Groups[1].Value);
29+
return (WorkspaceLocation.Local, null, match.Groups[1].Value);
3030
}
3131
}
3232
else if (RemoteSSHWorkspace.IsMatch(uri))
@@ -35,7 +35,7 @@ public static (TypeWorkspace? TypeWorkspace, string MachineName, string Path) Ge
3535

3636
if (match.Groups.Count > 1)
3737
{
38-
return (TypeWorkspace.RemoteSSH, match.Groups[1].Value, match.Groups[2].Value);
38+
return (WorkspaceLocation.RemoteSSH, match.Groups[1].Value, match.Groups[2].Value);
3939
}
4040
}
4141
else if (RemoteWSLWorkspace.IsMatch(uri))
@@ -44,7 +44,7 @@ public static (TypeWorkspace? TypeWorkspace, string MachineName, string Path) Ge
4444

4545
if (match.Groups.Count > 1)
4646
{
47-
return (TypeWorkspace.RemoteWSL, match.Groups[1].Value, match.Groups[2].Value);
47+
return (WorkspaceLocation.RemoteWSL, match.Groups[1].Value, match.Groups[2].Value);
4848
}
4949
}
5050
else if (CodespacesWorkspace.IsMatch(uri))
@@ -53,7 +53,7 @@ public static (TypeWorkspace? TypeWorkspace, string MachineName, string Path) Ge
5353

5454
if (match.Groups.Count > 1)
5555
{
56-
return (TypeWorkspace.Codespaces, null, match.Groups[2].Value);
56+
return (WorkspaceLocation.Codespaces, null, match.Groups[2].Value);
5757
}
5858
}
5959
else if (DevContainerWorkspace.IsMatch(uri))
@@ -62,7 +62,7 @@ public static (TypeWorkspace? TypeWorkspace, string MachineName, string Path) Ge
6262

6363
if (match.Groups.Count > 1)
6464
{
65-
return (TypeWorkspace.DevContainer, null, match.Groups[2].Value);
65+
return (WorkspaceLocation.DevContainer, null, match.Groups[2].Value);
6666
}
6767
}
6868

WorkspacesHelper/VSCodeWorkspace.cs

+19-11
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,40 @@
88

99
namespace Flow.Plugin.VSCodeWorkspaces.WorkspacesHelper
1010
{
11-
public record VSCodeWorkspace
11+
public record VsCodeWorkspace
1212
{
1313
public PathString Path { get; init; }
1414

1515
public PathString RelativePath { get; init; }
1616

1717
public PathString FolderName { get; init; }
1818

19-
public string Lable { get; init; }
19+
public string Label { get; init; }
2020

2121
public string ExtraInfo { get; init; }
2222

23-
public TypeWorkspace TypeWorkspace { get; init; }
23+
public WorkspaceLocation WorkspaceLocation { get; init; }
24+
25+
public WorkspaceType WorkspaceType { get; init; }
2426

2527
public VSCodeInstance VSCodeInstance { get; init; }
2628

2729
public string WorkspaceTypeToString()
2830
{
29-
return TypeWorkspace switch
31+
return WorkspaceLocation switch
3032
{
31-
TypeWorkspace.Local => Resources.TypeWorkspaceLocal,
32-
TypeWorkspace.Codespaces => "Codespaces",
33-
TypeWorkspace.RemoteContainers => Resources.TypeWorkspaceContainer,
34-
TypeWorkspace.RemoteSSH => "SSH",
35-
TypeWorkspace.RemoteWSL => "WSL",
36-
TypeWorkspace.DevContainer => Resources.TypeWorkspaceDevContainer,
33+
WorkspaceLocation.Local => Resources.TypeWorkspaceLocal,
34+
WorkspaceLocation.Codespaces => "Codespaces",
35+
WorkspaceLocation.RemoteContainers => Resources.TypeWorkspaceContainer,
36+
WorkspaceLocation.RemoteSSH => "SSH",
37+
WorkspaceLocation.RemoteWSL => "WSL",
38+
WorkspaceLocation.DevContainer => Resources.TypeWorkspaceDevContainer,
3739
_ => string.Empty
3840
};
3941
}
4042
}
4143

42-
public enum TypeWorkspace
44+
public enum WorkspaceLocation
4345
{
4446
Local = 1,
4547
Codespaces = 2,
@@ -48,4 +50,10 @@ public enum TypeWorkspace
4850
RemoteContainers = 5,
4951
DevContainer = 6,
5052
}
53+
54+
public enum WorkspaceType
55+
{
56+
Folder = 1,
57+
Workspace = 2,
58+
}
5159
}

0 commit comments

Comments
 (0)