Skip to content

Commit

Permalink
#14931 Show previous opened VSCode workspaces (#15108)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardosantos9521 committed Dec 27, 2021
1 parent 7b280eb commit 5f9cf69
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ public List<Result> Query(Query query)
// Search opened workspaces
_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();
if (a.TypeWorkspace != TypeWorkspace.Local)
var typeWorkspace = a.WorkspaceEnvironmentToString();
if (a.WorkspaceEnvironment != WorkspaceEnvironment.Local)
{
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
{
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,
ToolTipData = tooltip,
Action = c =>
Expand All @@ -66,7 +66,7 @@ public List<Result> Query(Query query)
{
FileName = a.VSCodeInstance.ExecutablePath,
UseShellExecute = true,
Arguments = $"--folder-uri {a.Path}",
Arguments = a.WorkspaceType == WorkspaceType.ProjectFolder ? $"--folder-uri {a.Path}" : $"--file-uri {a.Path}",
WindowStyle = ProcessWindowStyle.Hidden,
};
Process.Start(process);
Expand Down Expand Up @@ -142,23 +142,23 @@ public List<Result> Query(Query query)
}

results.ForEach(x =>
{
if (x.Score == 0)
{
x.Score = 100;
}
{
if (x.Score == 0)
{
x.Score = 100;
}
// intersect the title with the query
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);
x.Score = x.Score - differenceWithQuery + intersection;
// intersect the title with the query
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);
x.Score = x.Score - differenceWithQuery + intersection;
// if is a remote machine give it 12 extra points
if (x.ContextData is VSCodeRemoteMachine)
{
x.Score = Convert.ToInt32(x.Score + (intersection * 2));
}
});
// if is a remote machine give it 12 extra points
if (x.ContextData is VSCodeRemoteMachine)
{
x.Score = Convert.ToInt32(x.Score + (intersection * 2));
}
});

results = results.OrderByDescending(x => x.Score).ToList();
if (query.Search == string.Empty || query.Search.Replace(" ", string.Empty) == string.Empty)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,14 @@
</data>
<data name="Workspace" xml:space="preserve">
<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 name="TypeWorkspaceDevContainer" xml:space="preserve">
<value>Dev Container</value>
<comment>As in "Visual Studio Code Dev Container workspace "</comment>
</data>
<data name="ProjectFolder" xml:space="preserve">
<value>Project Folder</value>
<comment>It refers to the Visual Studio Code Project Folders</comment>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public class ParseVSCodeUri

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))
{
var match = LocalWorkspace.Match(uri);

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))
Expand All @@ -35,7 +35,7 @@ public static (TypeWorkspace? TypeWorkspace, string MachineName, string Path) Ge

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))
Expand All @@ -44,7 +44,7 @@ public static (TypeWorkspace? TypeWorkspace, string MachineName, string Path) Ge

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))
Expand All @@ -53,7 +53,7 @@ public static (TypeWorkspace? TypeWorkspace, string MachineName, string Path) Ge

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))
Expand All @@ -62,7 +62,7 @@ public static (TypeWorkspace? TypeWorkspace, string MachineName, string Path) Ge

if (match.Groups.Count > 1)
{
return (TypeWorkspace.DevContainer, null, match.Groups[2].Value);
return (WorkspaceEnvironment.DevContainer, null, match.Groups[2].Value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,29 @@ public class VSCodeWorkspace

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 string WorkspaceTypeToString()
public string WorkspaceEnvironmentToString()
{
switch (TypeWorkspace)
switch (WorkspaceEnvironment)
{
case TypeWorkspace.Local: return Resources.TypeWorkspaceLocal;
case TypeWorkspace.Codespaces: return "Codespaces";
case TypeWorkspace.RemoteContainers: return Resources.TypeWorkspaceContainer;
case TypeWorkspace.RemoteSSH: return "SSH";
case TypeWorkspace.RemoteWSL: return "WSL";
case TypeWorkspace.DevContainer: return Resources.TypeWorkspaceDevContainer;
case WorkspaceEnvironment.Local: return Resources.TypeWorkspaceLocal;
case WorkspaceEnvironment.Codespaces: return "Codespaces";
case WorkspaceEnvironment.RemoteContainers: return Resources.TypeWorkspaceContainer;
case WorkspaceEnvironment.RemoteSSH: return "SSH";
case WorkspaceEnvironment.RemoteWSL: return "WSL";
case WorkspaceEnvironment.DevContainer: return Resources.TypeWorkspaceDevContainer;
}

return string.Empty;
}
}

public enum TypeWorkspace
public enum WorkspaceEnvironment
{
Local = 1,
Codespaces = 2,
Expand All @@ -46,4 +48,10 @@ public enum TypeWorkspace
RemoteContainers = 5,
DevContainer = 6,
}

public enum WorkspaceType
{
ProjectFolder = 1,
WorkspaceFile = 2,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ public class VSCodeWorkspaceEntry

[JsonPropertyName("label")]
public string Label { get; set; }

[JsonPropertyName("workspace")]
public VSCodeWorkspaceProperty Workspace { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public VSCodeWorkspacesApi()
{
}

private VSCodeWorkspace ParseVSCodeUri(string uri, VSCodeInstance vscodeInstance)
private VSCodeWorkspace ParseVSCodeUri(string uri, VSCodeInstance vscodeInstance, bool isWorkspaceFile = false)
{
if (uri != null && uri is string)
{
string unescapeUri = Uri.UnescapeDataString(uri);
var typeWorkspace = WorkspacesHelper.ParseVSCodeUri.GetTypeWorkspace(unescapeUri);
if (typeWorkspace.TypeWorkspace.HasValue)
var typeWorkspace = WorkspacesHelper.ParseVSCodeUri.GetWorkspaceEnvironment(unescapeUri);
if (typeWorkspace.WorkspaceEnvironment.HasValue)
{
var folderName = Path.GetFileName(unescapeUri);

Expand All @@ -38,10 +38,11 @@ private VSCodeWorkspace ParseVSCodeUri(string uri, VSCodeInstance vscodeInstance
return new VSCodeWorkspace()
{
Path = uri,
WorkspaceType = isWorkspaceFile ? WorkspaceType.WorkspaceFile : WorkspaceType.ProjectFolder,
RelativePath = typeWorkspace.Path,
FolderName = folderName,
ExtraInfo = typeWorkspace.MachineName,
TypeWorkspace = typeWorkspace.TypeWorkspace.Value,
WorkspaceEnvironment = typeWorkspace.WorkspaceEnvironment.Value,
VSCodeInstance = vscodeInstance,
};
}
Expand Down Expand Up @@ -76,23 +77,31 @@ public List<VSCodeWorkspace> Workspaces
{
foreach (var workspaceUri in vscodeStorageFile.OpenedPathsList.Workspaces3)
{
var uri = ParseVSCodeUri(workspaceUri, vscodeInstance);
if (uri != null)
var workspace = ParseVSCodeUri(workspaceUri, vscodeInstance);
if (workspace != null)
{
results.Add(uri);
results.Add(workspace);
}
}
}

// vscode v1.55.0 or later
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);
if (uri != null)
bool isWorkspaceFile = false;
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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"ActionKeyword": "{",
"Name": "VS Code Workspaces",
"Author": "ricardosantos9521",
"Version": "1.0.0",
"Version": "1.1.0",
"Language": "csharp",
"Website": "https://github.com/ricardosantos9521/PowerToys/",
"ExecuteFileName": "Community.PowerToys.Run.Plugin.VSCodeWorkspaces.dll",
Expand Down

0 comments on commit 5f9cf69

Please sign in to comment.