From 5baeaa1d3138d30c5f4d4f3e2f629115c51498dd Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Fri, 28 Jul 2017 17:56:31 +0100 Subject: [Ide] Fix duplicate recent project template for grouped templates Creating a CocoaApp project that targeted Mavericks and then creating another CocoaApp project that targeted a later Mac OS would then result in two CocoaApp projects for C# shown in the recent project templates list in the New Project dialog when they are logically the same template just part of a group of templates. --- .../MonoDevelop.Ide.Templates/SolutionTemplate.cs | 13 +++++++++++ .../MonoDevelop.Ide.Templates/TemplatingService.cs | 25 ++++++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) (limited to 'main/src/core/MonoDevelop.Ide') diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Templates/SolutionTemplate.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Templates/SolutionTemplate.cs index f5f888f330..4514c77390 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Templates/SolutionTemplate.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Templates/SolutionTemplate.cs @@ -255,6 +255,19 @@ namespace MonoDevelop.Ide.Templates ^ (Name != null ? Name.GetHashCode () : 0) ^ (Category != null ? Category.GetHashCode () : 0); } + + /// + /// Returns all other templates in the group. Does not include this template. + /// + internal IEnumerable GetGroupedTemplates () + { + if (Parent != null) + return Parent.groupedTemplates + .Where (template => template != this) + .Concat (Parent); + + return groupedTemplates; + } } } diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Templates/TemplatingService.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Templates/TemplatingService.cs index 181735c339..5edc53bf20 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Templates/TemplatingService.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Templates/TemplatingService.cs @@ -109,7 +109,7 @@ namespace MonoDevelop.Ide.Templates return templateCategorizer.GetCategorizedTemplates (); } - internal SolutionTemplate GetTemplate (IEnumerable categories, string templateId) + internal static SolutionTemplate GetTemplate (IEnumerable categories, string templateId) { return GetTemplate ( categories, @@ -118,7 +118,7 @@ namespace MonoDevelop.Ide.Templates category => true); } - internal SolutionTemplate GetTemplate ( + internal static SolutionTemplate GetTemplate ( IEnumerable categories, Func isTemplateMatch, Func isTopLevelCategoryMatch, @@ -230,6 +230,8 @@ namespace MonoDevelop.Ide.Templates public void AddTemplate (SolutionTemplate template) { try { + if (template.HasGroupId) + RemoveTemplateFromSameGroup (template); var recentItem = CreateRecentItem (template); recentTemplates.AddWithLimit (recentItem, templateGroup, ItemLimit); } catch (Exception e) { @@ -237,6 +239,21 @@ namespace MonoDevelop.Ide.Templates } } + /// + /// Removes any recent templates from the same group if it has the same language. + /// Different languages for the same group can exist separately in the recent project + /// templates list. + /// + void RemoveTemplateFromSameGroup (SolutionTemplate template) + { + foreach (var groupTemplate in template.GetGroupedTemplates ()) { + if (groupTemplate.Language == template.Language) { + var recentItem = CreateRecentItem (groupTemplate); + recentTemplates.RemoveItem (recentItem); + } + } + } + RecentItem CreateRecentItem (SolutionTemplate template) { var mime = "application/vnd.monodevelop.template"; @@ -256,7 +273,7 @@ namespace MonoDevelop.Ide.Templates SolutionTemplate recentTemplate = null; if (parts.Length > 1) - recentTemplate = IdeApp.Services.TemplatingService.GetTemplate ( + recentTemplate = TemplatingService.GetTemplate ( categories, (template) => template.Id == templateId, (category) => parts.Length > 1 ? category.Id == parts[0] : true, @@ -266,7 +283,7 @@ namespace MonoDevelop.Ide.Templates // fallback to global template lookup if no category matched // in this case the category is not guaranteed if a template is listed in more than one category if (recentTemplate == null) - recentTemplate = IdeApp.Services.TemplatingService.GetTemplate (categories, templateId); + recentTemplate = TemplatingService.GetTemplate (categories, templateId); return recentTemplate; } -- cgit v1.2.3