Skip to content

Commit 040d7e1

Browse files
author
Paul van Brenk
committed
More PR feedback
1 parent 10ceb66 commit 040d7e1

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

Nodejs/Product/Nodejs/SharedProject/HierarchyIdMap.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ namespace Microsoft.VisualStudioTools.Project
88
{
99
internal sealed class HierarchyIdMap
1010
{
11-
private readonly ConcurrentDictionary<int, WeakReference<HierarchyNode>> nodes = new ConcurrentDictionary<int, WeakReference<HierarchyNode>>();
12-
private readonly ConcurrentStack<int> freedIds = new ConcurrentStack<int>();
11+
private readonly ConcurrentDictionary<uint, WeakReference<HierarchyNode>> nodes = new ConcurrentDictionary<uint, WeakReference<HierarchyNode>>();
12+
private readonly ConcurrentStack<uint> freedIds = new ConcurrentStack<uint>();
13+
14+
public readonly static HierarchyIdMap Instance = new HierarchyIdMap();
15+
16+
private HierarchyIdMap() { }
1317

1418
/// <summary>
1519
/// Must be called from the UI thread
@@ -33,15 +37,21 @@ public uint Add(HierarchyNode node)
3337
#endif
3438
if (!this.freedIds.TryPop(out var idx))
3539
{
36-
idx = this.nodes.Count;
40+
idx = this.NextIndex();
3741
}
3842

3943
var addSuccess = this.nodes.TryAdd(idx, new WeakReference<HierarchyNode>(node));
4044
Debug.Assert(addSuccess, "Failed to add a new item");
4145

42-
return (uint)idx + 1;
46+
return idx;
47+
}
48+
49+
private uint NextIndex()
50+
{
51+
return (uint)this.nodes.Count + 1;
4352
}
4453

54+
4555
/// <summary>
4656
/// Must be called from the UI thread
4757
/// </summary>
@@ -51,7 +61,7 @@ public void Remove(HierarchyNode node)
5161

5262
Debug.Assert(node != null, "Called with null node");
5363

54-
var idx = (int)node.ID - 1;
64+
var idx = node.ID;
5565

5666
var removeCheck = this.nodes.TryRemove(idx, out var weakRef);
5767

@@ -71,14 +81,11 @@ public HierarchyNode this[uint itemId]
7181
{
7282
Debug.Assert(VisualStudio.Shell.ThreadHelper.CheckAccess());
7383

74-
var idx = (int)itemId - 1;
75-
if (0 <= idx && idx < this.nodes.Count)
84+
var idx = itemId;
85+
if (this.nodes.TryGetValue(idx, out var reference) && reference != null && reference.TryGetTarget(out var node))
7686
{
77-
if (this.nodes.TryGetValue(idx, out var reference) && reference != null && reference.TryGetTarget(out var node))
78-
{
79-
Debug.Assert(node != null);
80-
return node;
81-
}
87+
Debug.Assert(node != null);
88+
return node;
8289
}
8390

8491
// This is a valid return value, this gets called by VS after we deleted the item

Nodejs/Product/Nodejs/SharedProject/ProjectNode.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,6 @@ internal enum EventTriggering
158158
/// <summary>A project will only try to build if it can obtain a lock on this object</summary>
159159
private volatile static object BuildLock = new object();
160160

161-
/// <summary>Maps integer ids to project item instances</summary>
162-
private HierarchyIdMap itemIdMap = new HierarchyIdMap();
163-
164161
/// <summary>A service provider call back object provided by the IDE hosting the project manager</summary>
165162
private IServiceProvider site;
166163

@@ -590,7 +587,7 @@ public virtual string OutputBaseRelativePath
590587
/// <summary>
591588
/// Gets a collection of integer ids that maps to project item instances
592589
/// </summary>
593-
internal HierarchyIdMap ItemIdMap => this.itemIdMap;
590+
internal HierarchyIdMap ItemIdMap => HierarchyIdMap.Instance;
594591

595592
/// <summary>
596593
/// Get the helper object that track document changes.

0 commit comments

Comments
 (0)