From 24ba1ff69bfce002c4cb1328159d4b2321d78f31 Mon Sep 17 00:00:00 2001 From: Rodrigo Moya Date: Thu, 14 Nov 2019 20:15:07 +0100 Subject: [PATCH] [Core] Avoid showing duplicated files in project tree There are some cases where, it seems, we don't evaluate conditions as we should, resulting in duplicated items being added to the project and, thus, also shown in the project tree (see https://github.com/aspnet/AspNetCore/issues/17088). Also, by just adding a , duplicated files show also. So, due to this limitation on the project system, work around it on the project tree, by checking for duplicates based on the files' paths. Fixes https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1005277 --- .../FolderNodeBuilder.cs | 4 +++- .../FileNestingService.cs | 17 +++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui.Pads.ProjectPad/FolderNodeBuilder.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui.Pads.ProjectPad/FolderNodeBuilder.cs index d2328e2434c..54323dae44c 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui.Pads.ProjectPad/FolderNodeBuilder.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui.Pads.ProjectPad/FolderNodeBuilder.cs @@ -81,6 +81,7 @@ void GetFolderContent (Project project, string folder, out List fil { string folderPrefix = folder + Path.DirectorySeparatorChar; + var visitedFiles = new HashSet (); files = new List (); folders = new List (); @@ -100,7 +101,8 @@ void GetFolderContent (Project project, string folder, out List fil ? project.BaseDirectory.Combine (file.ProjectVirtualPath).ParentDirectory : file.FilePath.ParentDirectory; - if (dir == folder) { + if (dir == folder && !visitedFiles.Contains (file.FilePath)) { + visitedFiles.Add (file.FilePath); files.Add (file); continue; } diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Projects.FileNesting/FileNestingService.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Projects.FileNesting/FileNestingService.cs index b10336aea4f..63a9eff8959 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Projects.FileNesting/FileNestingService.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Projects.FileNesting/FileNestingService.cs @@ -31,6 +31,7 @@ using System.Linq; using System.Runtime.CompilerServices; using Mono.Addins; +using MonoDevelop.Core; using MonoDevelop.Projects; namespace MonoDevelop.Ide.Projects.FileNesting @@ -166,7 +167,7 @@ public ProjectFileNestingInfo (ProjectFile projectFile) } } - readonly ConcurrentDictionary projectFiles = new ConcurrentDictionary (); + readonly ConcurrentDictionary projectFiles = new ConcurrentDictionary (); bool fileNestingEnabled; public Project Project { get; } @@ -195,10 +196,10 @@ void EnsureInitialized () ProjectFileNestingInfo AddFile (ProjectFile projectFile) { - var tmp = projectFiles.GetOrAdd (projectFile, new ProjectFileNestingInfo (projectFile)); + var tmp = projectFiles.GetOrAdd (projectFile.FilePath, new ProjectFileNestingInfo (projectFile)); tmp.Parent = FileNestingService.InternalGetParentFile (projectFile); if (tmp.Parent != null) { - var parent = projectFiles.GetOrAdd (tmp.Parent, new ProjectFileNestingInfo (tmp.Parent)); + var parent = projectFiles.GetOrAdd (tmp.Parent.FilePath, new ProjectFileNestingInfo (tmp.Parent)); if (parent.Children == null) { parent.Children = new ProjectFileCollection (); } @@ -207,7 +208,7 @@ ProjectFileNestingInfo AddFile (ProjectFile projectFile) } } - projectFiles [projectFile] = tmp; + projectFiles [projectFile.FilePath] = tmp; return tmp; } @@ -215,11 +216,11 @@ ProjectFileNestingInfo RemoveFile (ProjectFile projectFile, List or { bool actuallyRemoveFiles = originalFilesToRemove == null; - projectFiles.TryRemove (projectFile, out var nestingInfo); + projectFiles.TryRemove (projectFile.FilePath, out var nestingInfo); // Update parent if (nestingInfo?.Parent != null) { - if (projectFiles.TryGetValue (nestingInfo.Parent, out var tmp)) { + if (projectFiles.TryGetValue (nestingInfo.Parent.FilePath, out var tmp)) { tmp.Children.Remove (projectFile); } } @@ -296,7 +297,7 @@ public ProjectFile GetParentForFile (ProjectFile inputFile) if (!fileNestingEnabled) return null; - return projectFiles.TryGetValue (inputFile, out var nestingInfo) ? nestingInfo.Parent : null; + return projectFiles.TryGetValue (inputFile.FilePath, out var nestingInfo) ? nestingInfo.Parent : null; } public ProjectFileCollection GetChildrenForFile (ProjectFile inputFile) @@ -305,7 +306,7 @@ public ProjectFileCollection GetChildrenForFile (ProjectFile inputFile) if (!fileNestingEnabled) return null; - return projectFiles.TryGetValue (inputFile, out var nestingInfo) ? nestingInfo.Children : null; + return projectFiles.TryGetValue (inputFile.FilePath, out var nestingInfo) ? nestingInfo.Children : null; } public void Dispose ()