Skip to content

Commit 43c4829

Browse files
committed
FallbackWorkspaceItem retrieves items from the VisualStudioCodePage's ListItem cache
1 parent cb2dcfd commit 43c4829

3 files changed

Lines changed: 36 additions & 54 deletions

File tree

WorkspaceLauncherForVSCode/Pages/FallbackWorkspaceItem.cs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,17 @@ namespace WorkspaceLauncherForVSCode.Pages;
1212
internal sealed partial class FallbackWorkspaceItem : FallbackCommandItem
1313
{
1414
private readonly VisualStudioCodePage _page;
15-
private readonly SettingsManager _settingsManager;
16-
private readonly WorkspaceStorage _workspaceStorage;
17-
private readonly CommandContextItem _refreshWorkspacesCommandContextItem;
18-
private readonly IPinService _pinService;
1915
private readonly int _index;
2016

2117
public FallbackWorkspaceItem(
2218
VisualStudioCodePage page,
23-
SettingsManager settingsManager,
24-
WorkspaceStorage workspaceStorage,
25-
CommandContextItem refreshWorkspacesCommandContextItem,
26-
IPinService pinService,
2719
int index)
2820
: base(new NoOpCommand(),
2921
$"{Constant.VisualStudioCodeDisplayName} fallback result no. {index + 1}",
3022
$"{Package.Current.Id.Name}.{nameof(FallbackWorkspaceItem)}.{index}")
3123
{
3224
Icon = Classes.Icon.VisualStudioAndVisualStudioCode;
3325
_page = page;
34-
_settingsManager = settingsManager;
35-
_workspaceStorage = workspaceStorage;
36-
_refreshWorkspacesCommandContextItem = refreshWorkspacesCommandContextItem;
37-
_pinService = pinService;
3826
_index = index;
3927
}
4028

@@ -58,17 +46,12 @@ public override void UpdateQuery(string query)
5846
}
5947

6048
var best = filtered[_index];
61-
var isVSSolution = best.WorkspaceType == WorkspaceType.Solution || best.WorkspaceType == WorkspaceType.Solution2026;
62-
var (command, icon, _, _, moreCommands) = isVSSolution
63-
? WorkspaceItemFactory.CreateSolutionComponents(best, _page, _settingsManager, _page.VSCodeService.GetVisualStudioInstances())
64-
: WorkspaceItemFactory.CreateVSCodeComponents(best, _page, _settingsManager);
49+
var listItem = _page.GetOrCreateListItem(best);
6550

66-
WorkspaceItemFactory.AddCommonCommands(moreCommands, best, _settingsManager, _refreshWorkspacesCommandContextItem, _pinService);
67-
68-
Title = best.Name ?? string.Empty;
69-
Subtitle = best.WindowsPath ?? string.Empty;
70-
Icon = icon;
71-
Command = command;
72-
MoreCommands = moreCommands.ToArray();
51+
Title = listItem.Title;
52+
Subtitle = listItem.Subtitle;
53+
Icon = listItem.Icon;
54+
Command = listItem.Command;
55+
MoreCommands = listItem.MoreCommands;
7356
}
7457
}

WorkspaceLauncherForVSCode/Pages/VisualStudioCodePage.cs

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -268,42 +268,45 @@ public override void LoadMore()
268268
RaiseItemsChanged(_visibleItems.Count);
269269
}
270270

271-
private ListItem GetOrCreateListItem(VisualStudioCodeWorkspace workspace, bool isTopLevelPinCommand = false)
271+
public ListItem GetOrCreateListItem(VisualStudioCodeWorkspace workspace, bool isTopLevelPinCommand = false)
272272
{
273-
if (!_listItemCache.TryGetValue(workspace.Id, out var listItem))
274-
{
275-
listItem = WorkspaceItemFactory.Create(workspace, this, _workspaceStorage, _settingsManager, _refreshWorkspacesCommandContextItem, _pinService, _visualStudioInstanceList);
276-
_listItemCache[workspace.Id] = listItem;
277-
}
278-
if (isTopLevelPinCommand)
273+
lock (_itemsLock)
279274
{
280-
var newMoreCommands = new List<ICommandContextItem>();
281-
if (listItem.MoreCommands != null)
275+
if (!_listItemCache.TryGetValue(workspace.Id, out var listItem))
276+
{
277+
listItem = WorkspaceItemFactory.Create(workspace, this, _workspaceStorage, _settingsManager, _refreshWorkspacesCommandContextItem, _pinService, _visualStudioInstanceList);
278+
_listItemCache[workspace.Id] = listItem;
279+
}
280+
if (isTopLevelPinCommand)
282281
{
283-
foreach (var mc in listItem.MoreCommands)
282+
var newMoreCommands = new List<ICommandContextItem>();
283+
if (listItem.MoreCommands != null)
284284
{
285-
if (mc is CommandContextItem cci)
285+
foreach (var mc in listItem.MoreCommands)
286286
{
287-
if (cci.Command is HelpPage || cci.Command is RefreshWorkspacesCommand || cci.Command is PinWorkspaceCommand)
287+
if (mc is CommandContextItem cci)
288288
{
289-
continue;
289+
if (cci.Command is HelpPage || cci.Command is RefreshWorkspacesCommand || cci.Command is PinWorkspaceCommand)
290+
{
291+
continue;
292+
}
293+
newMoreCommands.Add(cci);
290294
}
291-
newMoreCommands.Add(cci);
292295
}
293296
}
294-
}
295297

296-
return new ListItem(listItem.Command)
297-
{
298-
Title = listItem.Title,
299-
Subtitle = listItem.Subtitle,
300-
Details = listItem.Details,
301-
Icon = listItem.Icon,
302-
Tags = listItem.Tags,
303-
MoreCommands = newMoreCommands.ToArray()
304-
};
298+
return new ListItem(listItem.Command)
299+
{
300+
Title = listItem.Title,
301+
Subtitle = listItem.Subtitle,
302+
Details = listItem.Details,
303+
Icon = listItem.Icon,
304+
Tags = listItem.Tags,
305+
MoreCommands = newMoreCommands.ToArray()
306+
};
307+
}
308+
return listItem;
305309
}
306-
return listItem;
307310
}
308311

309312
private async Task RefreshWorkspacesAsync(bool isUserInitiated, bool isBackground = false)

WorkspaceLauncherForVSCode/WorkspaceLauncherForVSCodeCommandsProvider.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,11 @@ public WorkspaceLauncherForVSCodeCommandsProvider(
4848
var refreshCommandContextItem = new CommandContextItem(refreshWorkspacesCommand);
4949
_fallbacks = new IFallbackCommandItem[_settingsManager.FallbackCount + Constant.FallbackIndex.StartOfDynamicFallback];
5050
_fallbacks[Constant.FallbackIndex.OpenInVisualStudioCode] = new FallbackOpenRecentVisualStudioCodeItem(_page);
51-
for (int i = Constant.FallbackIndex.StartOfDynamicFallback, j = 0; i < _fallbacks.Length; i++, j++)
51+
for (int i = Constant.FallbackIndex.StartOfDynamicFallback, fallbackResultIndex = 0; i < _fallbacks.Length; i++, fallbackResultIndex++)
5252
{
5353
_fallbacks[i] = new FallbackWorkspaceItem(
5454
_page,
55-
_settingsManager,
56-
workspaceStorage,
57-
refreshCommandContextItem,
58-
pinService,
59-
j); // index start with 0
55+
fallbackResultIndex); // index start with 0
6056
}
6157
}
6258
catch (Exception ex)

0 commit comments

Comments
 (0)