#14931 Show previous opened VSCode workspaces (#15108)

This commit is contained in:
ricardosantos9521
2021-12-27 18:17:20 +00:00
committed by GitHub
parent 7b280ebde1
commit 5f9cf69a24
9 changed files with 101 additions and 51 deletions

View File

@@ -41,20 +41,20 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces
// Search opened workspaces // Search opened workspaces
_workspacesApi.Workspaces.ForEach(a => _workspacesApi.Workspaces.ForEach(a =>
{ {
var title = $"{a.FolderName}"; var title = a.WorkspaceType == WorkspaceType.ProjectFolder ? a.FolderName : a.FolderName.Replace(".code-workspace", $" ({Resources.Workspace})");
var typeWorkspace = a.WorkspaceTypeToString(); var typeWorkspace = a.WorkspaceEnvironmentToString();
if (a.TypeWorkspace != TypeWorkspace.Local) if (a.WorkspaceEnvironment != WorkspaceEnvironment.Local)
{ {
title = $"{title}{(a.ExtraInfo != null ? $" - {a.ExtraInfo}" : string.Empty)} ({typeWorkspace})"; title = $"{title}{(a.ExtraInfo != null ? $" - {a.ExtraInfo}" : string.Empty)} ({typeWorkspace})";
} }
var tooltip = new ToolTipData(title, $"{Resources.Workspace}{(a.TypeWorkspace != TypeWorkspace.Local ? $" {Resources.In} {typeWorkspace}" : string.Empty)}: {SystemPath.RealPath(a.RelativePath)}"); var tooltip = new ToolTipData(title, $"{(a.WorkspaceType == WorkspaceType.WorkspaceFile ? Resources.Workspace : Resources.ProjectFolder)}{(a.WorkspaceEnvironment != WorkspaceEnvironment.Local ? $" {Resources.In} {typeWorkspace}" : string.Empty)}: {SystemPath.RealPath(a.RelativePath)}");
results.Add(new Result results.Add(new Result
{ {
Title = title, Title = title,
SubTitle = $"{Resources.Workspace}{(a.TypeWorkspace != TypeWorkspace.Local ? $" {Resources.In} {typeWorkspace}" : string.Empty)}: {SystemPath.RealPath(a.RelativePath)}", SubTitle = $"{(a.WorkspaceType == WorkspaceType.WorkspaceFile ? Resources.Workspace : Resources.ProjectFolder)}{(a.WorkspaceEnvironment != WorkspaceEnvironment.Local ? $" {Resources.In} {typeWorkspace}" : string.Empty)}: {SystemPath.RealPath(a.RelativePath)}",
Icon = a.VSCodeInstance.WorkspaceIcon, Icon = a.VSCodeInstance.WorkspaceIcon,
ToolTipData = tooltip, ToolTipData = tooltip,
Action = c => Action = c =>
@@ -66,7 +66,7 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces
{ {
FileName = a.VSCodeInstance.ExecutablePath, FileName = a.VSCodeInstance.ExecutablePath,
UseShellExecute = true, UseShellExecute = true,
Arguments = $"--folder-uri {a.Path}", Arguments = a.WorkspaceType == WorkspaceType.ProjectFolder ? $"--folder-uri {a.Path}" : $"--file-uri {a.Path}",
WindowStyle = ProcessWindowStyle.Hidden, WindowStyle = ProcessWindowStyle.Hidden,
}; };
Process.Start(process); Process.Start(process);
@@ -142,23 +142,23 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces
} }
results.ForEach(x => results.ForEach(x =>
{ {
if (x.Score == 0) if (x.Score == 0)
{ {
x.Score = 100; x.Score = 100;
} }
// intersect the title with the query // intersect the title with the query
var intersection = Convert.ToInt32(x.Title.ToLowerInvariant().Intersect(query.Search.ToLowerInvariant()).Count() * query.Search.Count()); var intersection = Convert.ToInt32(x.Title.ToLowerInvariant().Intersect(query.Search.ToLowerInvariant()).Count() * query.Search.Count());
var differenceWithQuery = Convert.ToInt32((x.Title.Count() - intersection) * query.Search.Count() * 0.7); var differenceWithQuery = Convert.ToInt32((x.Title.Count() - intersection) * query.Search.Count() * 0.7);
x.Score = x.Score - differenceWithQuery + intersection; x.Score = x.Score - differenceWithQuery + intersection;
// if is a remote machine give it 12 extra points // if is a remote machine give it 12 extra points
if (x.ContextData is VSCodeRemoteMachine) if (x.ContextData is VSCodeRemoteMachine)
{ {
x.Score = Convert.ToInt32(x.Score + (intersection * 2)); x.Score = Convert.ToInt32(x.Score + (intersection * 2));
} }
}); });
results = results.OrderByDescending(x => x.Score).ToList(); results = results.OrderByDescending(x => x.Score).ToList();
if (query.Search == string.Empty || query.Search.Replace(" ", string.Empty) == string.Empty) if (query.Search == string.Empty || query.Search.Replace(" ", string.Empty) == string.Empty)

View File

@@ -19,7 +19,7 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.Properties {
// class via a tool like ResGen or Visual Studio. // class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen // To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project. // with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources { internal class Resources {
@@ -87,6 +87,15 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.Properties {
} }
} }
/// <summary>
/// Looks up a localized string similar to Project Folder.
/// </summary>
internal static string ProjectFolder {
get {
return ResourceManager.GetString("ProjectFolder", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to SSH remote machine. /// Looks up a localized string similar to SSH remote machine.
/// </summary> /// </summary>

View File

@@ -140,10 +140,14 @@
</data> </data>
<data name="Workspace" xml:space="preserve"> <data name="Workspace" xml:space="preserve">
<value>Workspace</value> <value>Workspace</value>
<comment>It refers to the "Visual Studio Code workspace"</comment> <comment>It refers to the Visual Studio Code .code-workspace</comment>
</data> </data>
<data name="TypeWorkspaceDevContainer" xml:space="preserve"> <data name="TypeWorkspaceDevContainer" xml:space="preserve">
<value>Dev Container</value> <value>Dev Container</value>
<comment>As in "Visual Studio Code Dev Container workspace "</comment> <comment>As in "Visual Studio Code Dev Container workspace "</comment>
</data> </data>
<data name="ProjectFolder" xml:space="preserve">
<value>Project Folder</value>
<comment>It refers to the Visual Studio Code Project Folders</comment>
</data>
</root> </root>

View File

@@ -18,7 +18,7 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper
private static readonly Regex DevContainerWorkspace = new Regex(@"^vscode-remote://dev-container\+(.+?(?=\/))(.+)$", RegexOptions.Compiled); private static readonly Regex DevContainerWorkspace = new Regex(@"^vscode-remote://dev-container\+(.+?(?=\/))(.+)$", RegexOptions.Compiled);
public static (TypeWorkspace? TypeWorkspace, string MachineName, string Path) GetTypeWorkspace(string uri) public static (WorkspaceEnvironment? WorkspaceEnvironment, string MachineName, string Path) GetWorkspaceEnvironment(string uri)
{ {
if (LocalWorkspace.IsMatch(uri)) if (LocalWorkspace.IsMatch(uri))
{ {
@@ -26,7 +26,7 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper
if (match.Groups.Count > 1) if (match.Groups.Count > 1)
{ {
return (TypeWorkspace.Local, null, match.Groups[1].Value); return (WorkspaceEnvironment.Local, null, match.Groups[1].Value);
} }
} }
else if (RemoteSSHWorkspace.IsMatch(uri)) else if (RemoteSSHWorkspace.IsMatch(uri))
@@ -35,7 +35,7 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper
if (match.Groups.Count > 1) if (match.Groups.Count > 1)
{ {
return (TypeWorkspace.RemoteSSH, match.Groups[1].Value, match.Groups[2].Value); return (WorkspaceEnvironment.RemoteSSH, match.Groups[1].Value, match.Groups[2].Value);
} }
} }
else if (RemoteWSLWorkspace.IsMatch(uri)) else if (RemoteWSLWorkspace.IsMatch(uri))
@@ -44,7 +44,7 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper
if (match.Groups.Count > 1) if (match.Groups.Count > 1)
{ {
return (TypeWorkspace.RemoteWSL, match.Groups[1].Value, match.Groups[2].Value); return (WorkspaceEnvironment.RemoteWSL, match.Groups[1].Value, match.Groups[2].Value);
} }
} }
else if (CodespacesWorkspace.IsMatch(uri)) else if (CodespacesWorkspace.IsMatch(uri))
@@ -53,7 +53,7 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper
if (match.Groups.Count > 1) if (match.Groups.Count > 1)
{ {
return (TypeWorkspace.Codespaces, null, match.Groups[2].Value); return (WorkspaceEnvironment.Codespaces, null, match.Groups[2].Value);
} }
} }
else if (DevContainerWorkspace.IsMatch(uri)) else if (DevContainerWorkspace.IsMatch(uri))
@@ -62,7 +62,7 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper
if (match.Groups.Count > 1) if (match.Groups.Count > 1)
{ {
return (TypeWorkspace.DevContainer, null, match.Groups[2].Value); return (WorkspaceEnvironment.DevContainer, null, match.Groups[2].Value);
} }
} }

View File

@@ -17,27 +17,29 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper
public string ExtraInfo { get; set; } public string ExtraInfo { get; set; }
public TypeWorkspace TypeWorkspace { get; set; } public WorkspaceEnvironment WorkspaceEnvironment { get; set; }
public WorkspaceType WorkspaceType { get; set; }
public VSCodeInstance VSCodeInstance { get; set; } public VSCodeInstance VSCodeInstance { get; set; }
public string WorkspaceTypeToString() public string WorkspaceEnvironmentToString()
{ {
switch (TypeWorkspace) switch (WorkspaceEnvironment)
{ {
case TypeWorkspace.Local: return Resources.TypeWorkspaceLocal; case WorkspaceEnvironment.Local: return Resources.TypeWorkspaceLocal;
case TypeWorkspace.Codespaces: return "Codespaces"; case WorkspaceEnvironment.Codespaces: return "Codespaces";
case TypeWorkspace.RemoteContainers: return Resources.TypeWorkspaceContainer; case WorkspaceEnvironment.RemoteContainers: return Resources.TypeWorkspaceContainer;
case TypeWorkspace.RemoteSSH: return "SSH"; case WorkspaceEnvironment.RemoteSSH: return "SSH";
case TypeWorkspace.RemoteWSL: return "WSL"; case WorkspaceEnvironment.RemoteWSL: return "WSL";
case TypeWorkspace.DevContainer: return Resources.TypeWorkspaceDevContainer; case WorkspaceEnvironment.DevContainer: return Resources.TypeWorkspaceDevContainer;
} }
return string.Empty; return string.Empty;
} }
} }
public enum TypeWorkspace public enum WorkspaceEnvironment
{ {
Local = 1, Local = 1,
Codespaces = 2, Codespaces = 2,
@@ -46,4 +48,10 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper
RemoteContainers = 5, RemoteContainers = 5,
DevContainer = 6, DevContainer = 6,
} }
public enum WorkspaceType
{
ProjectFolder = 1,
WorkspaceFile = 2,
}
} }

View File

@@ -13,5 +13,8 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper
[JsonPropertyName("label")] [JsonPropertyName("label")]
public string Label { get; set; } public string Label { get; set; }
[JsonPropertyName("workspace")]
public VSCodeWorkspaceProperty Workspace { get; set; }
} }
} }

View File

@@ -0,0 +1,17 @@
// 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.
using System.Text.Json.Serialization;
namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper
{
public class VSCodeWorkspaceProperty
{
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("configPath")]
public string ConfigPath { get; set; }
}
}

View File

@@ -18,13 +18,13 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper
{ {
} }
private VSCodeWorkspace ParseVSCodeUri(string uri, VSCodeInstance vscodeInstance) private VSCodeWorkspace ParseVSCodeUri(string uri, VSCodeInstance vscodeInstance, bool isWorkspaceFile = false)
{ {
if (uri != null && uri is string) if (uri != null && uri is string)
{ {
string unescapeUri = Uri.UnescapeDataString(uri); string unescapeUri = Uri.UnescapeDataString(uri);
var typeWorkspace = WorkspacesHelper.ParseVSCodeUri.GetTypeWorkspace(unescapeUri); var typeWorkspace = WorkspacesHelper.ParseVSCodeUri.GetWorkspaceEnvironment(unescapeUri);
if (typeWorkspace.TypeWorkspace.HasValue) if (typeWorkspace.WorkspaceEnvironment.HasValue)
{ {
var folderName = Path.GetFileName(unescapeUri); var folderName = Path.GetFileName(unescapeUri);
@@ -38,10 +38,11 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper
return new VSCodeWorkspace() return new VSCodeWorkspace()
{ {
Path = uri, Path = uri,
WorkspaceType = isWorkspaceFile ? WorkspaceType.WorkspaceFile : WorkspaceType.ProjectFolder,
RelativePath = typeWorkspace.Path, RelativePath = typeWorkspace.Path,
FolderName = folderName, FolderName = folderName,
ExtraInfo = typeWorkspace.MachineName, ExtraInfo = typeWorkspace.MachineName,
TypeWorkspace = typeWorkspace.TypeWorkspace.Value, WorkspaceEnvironment = typeWorkspace.WorkspaceEnvironment.Value,
VSCodeInstance = vscodeInstance, VSCodeInstance = vscodeInstance,
}; };
} }
@@ -76,10 +77,10 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper
{ {
foreach (var workspaceUri in vscodeStorageFile.OpenedPathsList.Workspaces3) foreach (var workspaceUri in vscodeStorageFile.OpenedPathsList.Workspaces3)
{ {
var uri = ParseVSCodeUri(workspaceUri, vscodeInstance); var workspace = ParseVSCodeUri(workspaceUri, vscodeInstance);
if (uri != null) if (workspace != null)
{ {
results.Add(uri); results.Add(workspace);
} }
} }
} }
@@ -87,12 +88,20 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper
// vscode v1.55.0 or later // vscode v1.55.0 or later
if (vscodeStorageFile.OpenedPathsList.Entries != null) if (vscodeStorageFile.OpenedPathsList.Entries != null)
{ {
foreach (var workspaceUri in vscodeStorageFile.OpenedPathsList.Entries.Select(x => x.FolderUri)) foreach (var entry in vscodeStorageFile.OpenedPathsList.Entries)
{ {
var uri = ParseVSCodeUri(workspaceUri, vscodeInstance); bool isWorkspaceFile = false;
if (uri != null) var uri = entry.FolderUri;
if (entry.Workspace != null && entry.Workspace.ConfigPath != null)
{ {
results.Add(uri); isWorkspaceFile = true;
uri = entry.Workspace.ConfigPath;
}
var workspace = ParseVSCodeUri(uri, vscodeInstance, isWorkspaceFile);
if (workspace != null)
{
results.Add(workspace);
} }
} }
} }

View File

@@ -4,7 +4,7 @@
"ActionKeyword": "{", "ActionKeyword": "{",
"Name": "VS Code Workspaces", "Name": "VS Code Workspaces",
"Author": "ricardosantos9521", "Author": "ricardosantos9521",
"Version": "1.0.0", "Version": "1.1.0",
"Language": "csharp", "Language": "csharp",
"Website": "https://github.com/ricardosantos9521/PowerToys/", "Website": "https://github.com/ricardosantos9521/PowerToys/",
"ExecuteFileName": "Community.PowerToys.Run.Plugin.VSCodeWorkspaces.dll", "ExecuteFileName": "Community.PowerToys.Run.Plugin.VSCodeWorkspaces.dll",