Skip to content

Commit 0029580

Browse files
committed
feat: improve performance and add support for custom vs code install locations
1 parent 46c1b10 commit 0029580

File tree

5 files changed

+180
-53
lines changed

5 files changed

+180
-53
lines changed

VsCode/Classes/VSCodeHandler.cs

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Data.Sqlite;
22
using System;
33
using System.Collections.Generic;
4+
using System.Globalization;
45
using System.IO;
56
using System.Text.Json;
67

@@ -30,11 +31,79 @@ public static void LoadInstances(string preferredEdition)
3031
AddInstance("VS Code - Insiders", Path.Combine(appdataProgramFilesPath, "Programs", "Microsoft VS Code Insiders", "Code - Insiders.exe"), insiderStoragePath, VSCodeInstallationType.User, VSCodeType.Insider);
3132
AddInstance("VS Code - Insiders [System]", Path.Combine(programsFolderPathBase, "Microsoft VS Code Insiders", "Code - Insiders.exe"), insiderStoragePath, VSCodeInstallationType.System, VSCodeType.Insider);
3233

34+
// search for custom installations in PATH environment variable
35+
try
36+
{
37+
var pathEnv = Environment.GetEnvironmentVariable("PATH");
38+
if (!string.IsNullOrEmpty(pathEnv))
39+
{
40+
var paths = pathEnv.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries);
41+
foreach (var dir in paths)
42+
{
43+
44+
// get parent directory of the current directory
45+
if (string.IsNullOrEmpty(dir) || !Directory.Exists(dir))
46+
{
47+
continue;
48+
}
49+
var parentDir = Path.GetDirectoryName(dir) ?? dir;
50+
try
51+
{
52+
var codeExe = Path.Combine(parentDir, "code.exe");
53+
var codeInsidersExe = Path.Combine(parentDir, "Code - Insiders.exe");
54+
55+
if (File.Exists(codeExe))
56+
{
57+
AddInstance("VS Code [Custom]", codeExe, defaultStoragePath, VSCodeInstallationType.User, VSCodeType.Default);
58+
}
59+
if (File.Exists(codeInsidersExe))
60+
{
61+
AddInstance("VS Code - Insiders [Custom]", codeInsidersExe, insiderStoragePath, VSCodeInstallationType.User, VSCodeType.Insider);
62+
}
63+
}
64+
catch
65+
{
66+
// ignore any errors while checking for custom installations
67+
}
68+
}
69+
}
70+
}
71+
catch
72+
{
73+
// ignore invalid PATH entries
74+
}
3375

3476
if (preferredEdition == "Insider")
3577
{
36-
// Reverse the order of the instances
37-
Instances.Reverse();
78+
// sort instances to have insiders first
79+
Instances.Sort((x, y) =>
80+
{
81+
if (x.VSCodeType == VSCodeType.Insider && y.VSCodeType != VSCodeType.Insider)
82+
{
83+
return -1;
84+
}
85+
else if (x.VSCodeType != VSCodeType.Insider && y.VSCodeType == VSCodeType.Insider)
86+
{
87+
return 1;
88+
}
89+
return 0;
90+
});
91+
}
92+
else
93+
{
94+
// sort instances to have default first
95+
Instances.Sort((x, y) =>
96+
{
97+
if (x.VSCodeType == VSCodeType.Default && y.VSCodeType != VSCodeType.Default)
98+
{
99+
return -1;
100+
}
101+
else if (x.VSCodeType != VSCodeType.Default && y.VSCodeType == VSCodeType.Default)
102+
{
103+
return 1;
104+
}
105+
return 0;
106+
});
38107
}
39108
}
40109

@@ -50,6 +119,11 @@ private static void AddInstance(string name, string path, string storagePath, VS
50119
{
51120
if (File.Exists(path))
52121
{
122+
// check if there is already an instance with the same executable path
123+
if (Instances.Exists(instance => instance.ExecutablePath.Equals(path, StringComparison.OrdinalIgnoreCase)))
124+
{
125+
return; // Instance already exists
126+
}
53127
Instances.Add(new VSCodeInstance(name, path, storagePath, type, codeType));
54128
}
55129
}

VsCode/Classes/VSCodeInstance.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ internal class VSCodeInstance
3131
public string StoragePath;
3232
public VSCodeInstallationType InstallationType;
3333
public VSCodeType VSCodeType;
34+
public IconInfo Icon;
3435

3536
/// <summary>
3637
/// Initializes a new instance of the <see cref="VSCodeInstance"/> class.
@@ -47,13 +48,15 @@ public VSCodeInstance(string name, string executablePath, string storagePath, VS
4748
this.StoragePath = storagePath;
4849
this.InstallationType = installationType;
4950
this.VSCodeType = type;
51+
this.Icon = GetIcon();
52+
5053
}
5154

5255
/// <summary>
5356
/// Gets the icon associated with the VS Code instance.
5457
/// </summary>
5558
/// <returns>An icon representing the VS Code instance.</returns>
56-
public IIconInfo GetIcon()
59+
private IconInfo GetIcon()
5760
{
5861
switch (VSCodeType)
5962
{

VsCode/Classes/VSCodeWorkspace.cs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ internal class VSCodeWorkspace
2323
public VSCodeInstance Instance;
2424
public string Path;
2525
public VSCodeWorkspaceType VSCodeWorkspaceType;
26+
public string WorkspaceName = "";
27+
public string VSTypeString = "";
28+
public string WorkspaceTypeString = "";
29+
public DetailsElement[] Details = [];
2630

2731
/// <summary>
2832
/// Initializes a new instance of the <see cref="VSCodeWorkspace"/> class.
@@ -34,72 +38,76 @@ public VSCodeWorkspace(VSCodeInstance instance, string path, VSCodeWorkspaceType
3438
this.Path = path;
3539
this.Instance = instance;
3640
this.VSCodeWorkspaceType = vsCodeWorkspaceType;
41+
42+
SetName();
43+
SetVSType();
44+
SetWorkspaceType();
45+
SetMetadata();
3746
}
3847

3948
/// <summary>
40-
/// Gets the name of the workspace.
49+
/// Sets the name of the workspace.
4150
/// </summary>
4251
/// <returns>The name of the workspace.</returns>
43-
public string GetName()
52+
public void SetName()
4453
{
45-
string workspaceName = "";
54+
WorkspaceName = "";
4655

4756
// split name by / and get last part
4857
var nameParts = Uri.UnescapeDataString(Path).Split('/');
4958
if (nameParts.Length == 0)
5059
{
51-
return workspaceName;
60+
return;
5261
}
5362

54-
workspaceName = nameParts[nameParts.Length - 1];
63+
WorkspaceName = nameParts[nameParts.Length - 1];
5564

5665
if (VSCodeWorkspaceType == VSCodeWorkspaceType.Workspace)
5766
{
5867
// remove .code-workspace
59-
workspaceName = workspaceName.Replace(".code-workspace", "");
68+
WorkspaceName = WorkspaceName.Replace(".code-workspace", "");
6069

6170
// if the workspace name is "workspace", use the folder name instead
62-
if (workspaceName == "workspace" && nameParts.Length >= 2)
71+
if (WorkspaceName == "workspace" && nameParts.Length >= 2)
6372
{
64-
workspaceName = nameParts[nameParts.Length - 2];
73+
WorkspaceName = nameParts[nameParts.Length - 2];
6574
}
6675
}
67-
68-
return workspaceName;
6976
}
7077

7178
/// <summary>
7279
/// Determines the type of the workspace (e.g., Local, WSL, Remote).
7380
/// </summary>
7481
/// <returns>The type of the workspace as a string.</returns>
75-
public string GetVSType()
82+
public void SetVSType()
7683
{
7784
if (Path.StartsWith("vscode-remote://wsl", System.StringComparison.OrdinalIgnoreCase))
7885
{
79-
return "WSL";
86+
VSTypeString = "WSL";
8087
}
8188
else if (Path.StartsWith("vscode-remote://", System.StringComparison.OrdinalIgnoreCase))
8289
{
83-
return "Remote";
90+
VSTypeString = "Remote";
8491
}
85-
86-
return "";
8792
}
8893

8994
/// <summary>
90-
/// Gets the workspace type (e.g., Workspace, Folder).
95+
/// Sets the workspace type (e.g., Workspace, Folder).
9196
/// </summary>
9297
/// <returns>The type of the workspace as a string.</returns>
93-
public string GetWorkspaceType()
98+
public void SetWorkspaceType()
9499
{
95100
switch (VSCodeWorkspaceType)
96101
{
97102
case VSCodeWorkspaceType.Workspace:
98-
return "Workspace";
103+
WorkspaceTypeString = "Workspace";
104+
break;
99105
case VSCodeWorkspaceType.Folder:
100-
return "Folder";
106+
WorkspaceTypeString = "Folder";
107+
break;
101108
default:
102-
return "Unknown Type";
109+
WorkspaceTypeString = "Unknown Type";
110+
break;
103111
}
104112
}
105113

@@ -108,15 +116,15 @@ public string GetWorkspaceType()
108116
/// Gets the details of the workspace.
109117
/// </summary>
110118
/// <returns>An array of details elements containing information about the workspace.</returns>
111-
public DetailsElement[] GetMetadata()
119+
public void SetMetadata()
112120
{
113-
var typeTags = new List<Tag>() { new Tag(GetWorkspaceType()) };
114-
if (GetVSType() != "")
121+
var typeTags = new List<Tag>() { new Tag(WorkspaceTypeString) };
122+
if (VSTypeString != "")
115123
{
116-
typeTags.Add(new Tag(GetVSType()));
124+
typeTags.Add(new Tag(VSTypeString));
117125
}
118126

119-
return new List<DetailsElement>(){
127+
Details = new List<DetailsElement>(){
120128
new DetailsElement()
121129
{
122130
Key = Resource.item_details_target,

VsCode/Commands/OpenVsCodeCommand.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public override CommandResult Invoke()
4747
page.UpdateSearchText(page.SearchText, "");
4848
page.SearchText = "";
4949

50-
return CommandResult.Hide();
50+
VSCodePage.LoadItems = true;
51+
52+
return CommandResult.GoHome();
5153
}
5254
}

0 commit comments

Comments
 (0)