Last active
November 19, 2015 15:11
-
-
Save VincentDondain/0c44a40b51e09750e1c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit bcbca0c28c4aad594e3e43abb8d476d0a3ac6861 | |
Author: VincentDondain <vincent.dondain@xamarin.com> | |
Date: Thu Nov 19 14:16:59 2015 +0000 | |
[FileTemplate] Massive code cleanup | |
diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Templates/FileTemplate.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Templates/FileTemplate.cs | |
index 6f4b9fe..93c00dc 100644 | |
--- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Templates/FileTemplate.cs | |
+++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Templates/FileTemplate.cs | |
@@ -6,6 +6,7 @@ | |
// Michael Hutchinson (mhutchinson@novell.com) | |
// Marek Sieradzki (marek.sieradzki@gmail.com) | |
// Viktoria Dudka (viktoriad@remobjects.com) | |
+// Vincent Dondain (vincent@xamarin.com) | |
// | |
// Copyright (c) 2009 RemObjects Software | |
// | |
@@ -29,229 +30,161 @@ | |
// | |
// | |
- | |
using System; | |
-using MonoDevelop.Core; | |
-using Mono.Addins; | |
-using MonoDevelop.Ide.Gui; | |
-using MonoDevelop.Projects; | |
+using System.Collections.Generic; | |
using System.IO; | |
+using System.Linq; | |
+using System.Xml; | |
using Gtk; | |
-using System.Collections.Generic; | |
-using System.Collections; | |
+using Mono.Addins; | |
+using MonoDevelop.Core; | |
using MonoDevelop.Ide.Codons; | |
-using System.Xml; | |
-using System.Linq; | |
+using MonoDevelop.Ide.Gui; | |
+using MonoDevelop.Projects; | |
namespace MonoDevelop.Ide.Templates | |
{ | |
- class FileTemplate | |
- { | |
- | |
- | |
- public static List<FileTemplate> fileTemplates = new List<FileTemplate> (); | |
- | |
- | |
- private List<FileDescriptionTemplate> files = new List<FileDescriptionTemplate> (); | |
- public List<FileDescriptionTemplate> Files | |
- { | |
- get { return files; } | |
- } | |
- | |
- private List<FileTemplateCondition> conditions = new List<FileTemplateCondition> (); | |
- public List<FileTemplateCondition> Conditions | |
- { | |
- get { return conditions; } | |
- } | |
- | |
- private string id = String.Empty; | |
- public string Id | |
- { | |
- get { return id; } | |
- } | |
- | |
- private string icon = String.Empty; | |
- public IconId Icon | |
- { | |
- get { return icon; } | |
- } | |
- | |
- private string category = String.Empty; | |
- public string Category | |
- { | |
- get { return category; } | |
- } | |
- | |
- private string wizardPath = String.Empty; | |
- public string WizardPath | |
- { | |
- get { return wizardPath; } | |
- } | |
- | |
- private string description = String.Empty; | |
- public string Description | |
- { | |
- get { return description; } | |
- } | |
- | |
- private bool isFixedFilename = false; | |
- public bool IsFixedFilename | |
- { | |
- get { return isFixedFilename; } | |
- } | |
- | |
- private string defaultFilename = String.Empty; | |
- public string DefaultFilename | |
- { | |
- get { return defaultFilename; } | |
- } | |
- | |
- public string name = String.Empty; | |
- public string Name | |
- { | |
- get { return name; } | |
- } | |
- | |
- private string languageName = String.Empty; | |
- public string LanguageName | |
- { | |
- get { return languageName; } | |
- } | |
- | |
- private string originator = String.Empty; | |
- public string Originator | |
- { | |
- get { return originator; } | |
- } | |
- | |
- private string created = String.Empty; | |
- public string Created | |
- { | |
- get { return created; } | |
- } | |
- | |
- private string lastModified = String.Empty; | |
- public string LastModified | |
- { | |
- get { return lastModified; } | |
- } | |
- | |
- private string projecttype = String.Empty; | |
- public string ProjectType | |
- { | |
- get { return projecttype; } | |
- } | |
- | |
- | |
- private static FileTemplate LoadFileTemplate (RuntimeAddin addin, ProjectTemplateCodon codon) | |
- { | |
+ class FileTemplate | |
+ { | |
+ public string Category { get; private set; } | |
+ | |
+ public List<FileTemplateCondition> Conditions { get; private set; } | |
+ | |
+ public string Created { get; private set; } | |
+ | |
+ public string DefaultFilename { get; private set; } | |
+ | |
+ public string Description { get; private set; } | |
+ | |
+ public List<FileDescriptionTemplate> Files { get; private set; } | |
+ | |
+ public static List<FileTemplate> fileTemplates = new List<FileTemplate> (); | |
+ | |
+ public IconId Icon { get; private set; } | |
+ | |
+ public string Id { get; private set; } | |
+ | |
+ public bool IsFixedFilename { get; private set; } | |
+ | |
+ public string LanguageName { get; private set; } | |
+ | |
+ public string LastModified { get; private set; } | |
+ | |
+ public string Name { get; private set; } | |
+ | |
+ public string Originator { get; private set; } | |
+ | |
+ public string ProjectType { get; private set; } | |
+ | |
+ public string WizardPath { get; private set; } | |
+ | |
+ static FileTemplate LoadFileTemplate (RuntimeAddin addin, ProjectTemplateCodon codon) | |
+ { | |
XmlDocument xmlDocument = codon.GetTemplate (); | |
FilePath baseDirectory = codon.BaseDirectory; | |
- //Configuration | |
- XmlElement xmlNodeConfig = xmlDocument.DocumentElement["TemplateConfiguration"]; | |
- | |
- FileTemplate fileTemplate = null; | |
- if (xmlNodeConfig["Type"] != null) { | |
- Type configType = addin.GetType (xmlNodeConfig["Type"].InnerText); | |
- | |
- if (typeof (FileTemplate).IsAssignableFrom (configType)) { | |
- fileTemplate = (FileTemplate)Activator.CreateInstance (configType); | |
- } | |
- else | |
- throw new InvalidOperationException (string.Format ("The file template class '{0}' must be a subclass of MonoDevelop.Ide.Templates.FileTemplate", xmlNodeConfig["Type"].InnerText)); | |
- } | |
- else | |
- fileTemplate = new FileTemplate (); | |
- | |
- fileTemplate.originator = xmlDocument.DocumentElement.GetAttribute ("Originator"); | |
- fileTemplate.created = xmlDocument.DocumentElement.GetAttribute ("Created"); | |
- fileTemplate.lastModified = xmlDocument.DocumentElement.GetAttribute ("LastModified"); | |
- | |
- if (xmlNodeConfig["_Name"] != null) { | |
- fileTemplate.name = xmlNodeConfig["_Name"].InnerText; | |
- } | |
- else { | |
- throw new InvalidOperationException (string.Format ("Missing element '_Name' in file template: {0}", codon.Id)); | |
- } | |
- | |
- if (xmlNodeConfig["_Category"] != null) { | |
- fileTemplate.category = xmlNodeConfig["_Category"].InnerText; | |
- } | |
- else { | |
- throw new InvalidOperationException (string.Format ("Missing element '_Category' in file template: {0}", codon.Id)); | |
- } | |
- | |
- if (xmlNodeConfig["LanguageName"] != null) { | |
- fileTemplate.languageName = xmlNodeConfig["LanguageName"].InnerText; | |
- } | |
- | |
- if (xmlNodeConfig["ProjectType"] != null) { | |
- fileTemplate.projecttype = xmlNodeConfig["ProjectType"].InnerText; | |
- } | |
- | |
- if (xmlNodeConfig["_Description"] != null) { | |
- fileTemplate.description = xmlNodeConfig["_Description"].InnerText; | |
- } | |
- | |
- if (xmlNodeConfig["Icon"] != null) { | |
- fileTemplate.icon = ImageService.GetStockId (addin, xmlNodeConfig["Icon"].InnerText, IconSize.Dnd); //xmlNodeConfig["_Description"].InnerText; | |
- } | |
- | |
- if (xmlNodeConfig["Wizard"] != null) { | |
- fileTemplate.icon = xmlNodeConfig["Wizard"].Attributes["path"].InnerText; | |
- } | |
- | |
- if (xmlNodeConfig["DefaultFilename"] != null) { | |
- fileTemplate.defaultFilename = xmlNodeConfig["DefaultFilename"].InnerText; | |
- string isFixed = xmlNodeConfig["DefaultFilename"].GetAttribute ("IsFixed"); | |
+ //Configuration | |
+ XmlElement xmlNodeConfig = xmlDocument.DocumentElement ["TemplateConfiguration"]; | |
+ | |
+ FileTemplate fileTemplate; | |
+ if (xmlNodeConfig ["Type"] != null) { | |
+ Type configType = addin.GetType (xmlNodeConfig ["Type"].InnerText); | |
+ | |
+ if (typeof(FileTemplate).IsAssignableFrom (configType)) { | |
+ fileTemplate = (FileTemplate)Activator.CreateInstance (configType); | |
+ } else | |
+ throw new InvalidOperationException (string.Format ("The file template class '{0}' must be a subclass of MonoDevelop.Ide.Templates.FileTemplate", xmlNodeConfig ["Type"].InnerText)); | |
+ } else | |
+ fileTemplate = new FileTemplate (); | |
+ | |
+ fileTemplate.Originator = xmlDocument.DocumentElement.GetAttribute ("Originator"); | |
+ fileTemplate.Created = xmlDocument.DocumentElement.GetAttribute ("Created"); | |
+ fileTemplate.LastModified = xmlDocument.DocumentElement.GetAttribute ("LastModified"); | |
+ | |
+ if (xmlNodeConfig ["_Name"] != null) { | |
+ fileTemplate.Name = xmlNodeConfig ["_Name"].InnerText; | |
+ } else { | |
+ throw new InvalidOperationException (string.Format ("Missing element '_Name' in file template: {0}", codon.Id)); | |
+ } | |
+ | |
+ if (xmlNodeConfig ["_Category"] != null) { | |
+ fileTemplate.Category = xmlNodeConfig ["_Category"].InnerText; | |
+ } else { | |
+ throw new InvalidOperationException (string.Format ("Missing element '_Category' in file template: {0}", codon.Id)); | |
+ } | |
+ | |
+ if (xmlNodeConfig ["LanguageName"] != null) { | |
+ fileTemplate.LanguageName = xmlNodeConfig ["LanguageName"].InnerText; | |
+ } | |
+ | |
+ if (xmlNodeConfig ["ProjectType"] != null) { | |
+ fileTemplate.ProjectType = xmlNodeConfig ["ProjectType"].InnerText; | |
+ } | |
+ | |
+ if (xmlNodeConfig ["_Description"] != null) { | |
+ fileTemplate.Description = xmlNodeConfig ["_Description"].InnerText; | |
+ } | |
+ | |
+ if (xmlNodeConfig ["Icon"] != null) { | |
+ fileTemplate.Icon = ImageService.GetStockId (addin, xmlNodeConfig ["Icon"].InnerText, IconSize.Dnd); //xmlNodeConfig["_Description"].InnerText; | |
+ } | |
+ | |
+ if (xmlNodeConfig ["Wizard"] != null) { | |
+ fileTemplate.Icon = xmlNodeConfig ["Wizard"].Attributes ["path"].InnerText; | |
+ } | |
+ | |
+ if (xmlNodeConfig ["DefaultFilename"] != null) { | |
+ fileTemplate.DefaultFilename = xmlNodeConfig ["DefaultFilename"].InnerText; | |
+ string isFixed = xmlNodeConfig ["DefaultFilename"].GetAttribute ("IsFixed"); | |
if (isFixed.Length > 0) { | |
bool bFixed; | |
if (bool.TryParse (isFixed, out bFixed)) | |
- fileTemplate.isFixedFilename = bFixed; | |
+ fileTemplate.IsFixedFilename = bFixed; | |
else | |
throw new InvalidOperationException ("Invalid value for IsFixed in template."); | |
} | |
- } | |
+ } | |
- //Template files | |
- XmlNode xmlNodeTemplates = xmlDocument.DocumentElement["TemplateFiles"]; | |
+ //Template files | |
+ XmlNode xmlNodeTemplates = xmlDocument.DocumentElement ["TemplateFiles"]; | |
- if(xmlNodeTemplates != null) { | |
- foreach(XmlNode xmlNode in xmlNodeTemplates.ChildNodes) { | |
- if(xmlNode is XmlElement) { | |
- fileTemplate.files.Add ( | |
- FileDescriptionTemplate.CreateTemplate ((XmlElement)xmlNode, baseDirectory)); | |
+ if (xmlNodeTemplates != null) { | |
+ foreach (XmlNode xmlNode in xmlNodeTemplates.ChildNodes) { | |
+ var xmlElement = xmlNode as XmlElement; | |
+ if (xmlElement != null) { | |
+ fileTemplate.Files.Add ( | |
+ FileDescriptionTemplate.CreateTemplate (xmlElement, baseDirectory)); | |
} | |
} | |
} | |
- //Conditions | |
- XmlNode xmlNodeConditions = xmlDocument.DocumentElement["Conditions"]; | |
- if(xmlNodeConditions != null) { | |
- foreach(XmlNode xmlNode in xmlNodeConditions.ChildNodes) { | |
- if(xmlNode is XmlElement) { | |
- fileTemplate.conditions.Add (FileTemplateCondition.CreateCondition ((XmlElement)xmlNode)); | |
+ //Conditions | |
+ XmlNode xmlNodeConditions = xmlDocument.DocumentElement ["Conditions"]; | |
+ if (xmlNodeConditions != null) { | |
+ foreach (XmlNode xmlNode in xmlNodeConditions.ChildNodes) { | |
+ var xmlElement = xmlNode as XmlElement; | |
+ if (xmlElement != null) { | |
+ fileTemplate.Conditions.Add (FileTemplateCondition.CreateCondition (xmlElement)); | |
} | |
} | |
} | |
- return fileTemplate; | |
- } | |
+ return fileTemplate; | |
+ } | |
- static FileTemplate() | |
- { | |
- AddinManager.AddExtensionNodeHandler ("/MonoDevelop/Ide/FileTemplates", OnExtensionChanged); | |
- } | |
+ static FileTemplate () | |
+ { | |
+ AddinManager.AddExtensionNodeHandler ("/MonoDevelop/Ide/FileTemplates", OnExtensionChanged); | |
+ } | |
static void OnExtensionChanged (object s, ExtensionNodeEventArgs args) | |
- { | |
- if (args.Change == ExtensionChange.Add) { | |
- ProjectTemplateCodon codon = (ProjectTemplateCodon) args.ExtensionNode; | |
- try { | |
- FileTemplate t = LoadFileTemplate (codon.Addin, codon); | |
- t.id = codon.Id; | |
- fileTemplates.Add (t); | |
+ { | |
+ if (args.Change == ExtensionChange.Add) { | |
+ var codon = (ProjectTemplateCodon)args.ExtensionNode; | |
+ try { | |
+ FileTemplate t = LoadFileTemplate (codon.Addin, codon); | |
+ t.Id = codon.Id; | |
+ fileTemplates.Add (t); | |
} catch (Exception e) { | |
string extId = null, addinId = null; | |
if (codon != null) { | |
@@ -261,19 +194,18 @@ namespace MonoDevelop.Ide.Templates | |
addinId = codon.Addin.Id; | |
} | |
LoggingService.LogError ("Error loading template id {0} in addin {1}:\n{2}", | |
- extId ?? "(null)", addinId ?? "(null)", e.ToString ()); | |
+ extId ?? "(null)", addinId ?? "(null)", e.ToString ()); | |
+ } | |
+ } else { | |
+ var codon = (ProjectTemplateCodon)args.ExtensionNode; | |
+ foreach (FileTemplate t in fileTemplates) { | |
+ if (t.Id == codon.Id) { | |
+ fileTemplates.Remove (t); | |
+ break; | |
+ } | |
} | |
- } | |
- else { | |
- ProjectTemplateCodon codon = (ProjectTemplateCodon) args.ExtensionNode; | |
- foreach (FileTemplate t in fileTemplates) { | |
- if (t.Id == codon.Id) { | |
- fileTemplates.Remove (t); | |
- break; | |
- } | |
- } | |
- } | |
- } | |
+ } | |
+ } | |
internal static List<FileTemplate> GetFileTemplates (Project project, string projectPath) | |
{ | |
@@ -286,45 +218,37 @@ namespace MonoDevelop.Ide.Templates | |
} | |
internal static FileTemplate GetFileTemplateByID (string templateID) | |
- { | |
- foreach (FileTemplate t in fileTemplates) | |
- if (t.Id == templateID) | |
- return t; | |
+ { | |
+ foreach (FileTemplate t in fileTemplates) | |
+ if (t.Id == templateID) | |
+ return t; | |
- return null; | |
- } | |
+ return null; | |
+ } | |
public virtual bool Create (SolutionItem policyParent, Project project, string directory, string language, string name) | |
- { | |
- if (!String.IsNullOrEmpty(WizardPath)) { | |
- //Properties customizer = new Properties(); | |
- //customizer.Set("Template", item); | |
- //customizer.Set("Creator", this); | |
- //WizardDialog wizard = new WizardDialog("File Wizard", customizer, item.WizardPath); | |
- //if (wizard.ShowDialog() == DialogResult.OK) { | |
- //DialogResult = DialogResult.OK; | |
- //} | |
- return false; | |
+ { | |
+ if (!String.IsNullOrEmpty (WizardPath)) { | |
+ return false; | |
} else { | |
- foreach (FileDescriptionTemplate newfile in Files) | |
- if (!CreateFile (newfile, policyParent, project, directory, language, name)) | |
- return false; | |
- return true; | |
- } | |
- } | |
+ foreach (FileDescriptionTemplate newfile in Files) | |
+ if (!CreateFile (newfile, policyParent, project, directory, language, name)) | |
+ return false; | |
+ return true; | |
+ } | |
+ } | |
public virtual bool IsValidName (string name, string language) | |
- { | |
- if (isFixedFilename) | |
- return (name == defaultFilename); | |
+ { | |
+ if (IsFixedFilename) | |
+ return (name == DefaultFilename); | |
- bool valid = true; | |
- foreach (FileDescriptionTemplate templ in Files) | |
- if (!templ.IsValidName (name, language)) | |
- valid = false; | |
+ bool valid = true; | |
+ foreach (FileDescriptionTemplate templ in Files) | |
+ valid &= templ.IsValidName (name, language); | |
- return valid; | |
- } | |
+ return valid; | |
+ } | |
public static string GuessMimeType (string fileName) | |
{ | |
@@ -337,17 +261,17 @@ namespace MonoDevelop.Ide.Templates | |
FileService.MoveFile (fn, fn + n + ext); | |
string mimeType = DesktopService.GetMimeTypeForUri (fn + n + ext); | |
FileService.DeleteFile (fn + n + ext); | |
- if (mimeType == null || mimeType == "") | |
+ if (string.IsNullOrEmpty (mimeType)) | |
mimeType = "text"; | |
return mimeType; | |
} | |
- | |
+ | |
public virtual bool CanCreateUnsavedFiles (FileDescriptionTemplate newfile, SolutionItem policyParent, Project project, string directory, string language, string name) | |
{ | |
if (project != null) { | |
return true; | |
} else { | |
- SingleFileDescriptionTemplate singleFile = newfile as SingleFileDescriptionTemplate; | |
+ var singleFile = newfile as SingleFileDescriptionTemplate; | |
if (singleFile == null) | |
return false; | |
@@ -362,145 +286,145 @@ namespace MonoDevelop.Ide.Templates | |
} | |
protected virtual bool CreateFile (FileDescriptionTemplate newfile, SolutionItem policyParent, Project project, string directory, string language, string name) | |
- { | |
- if (project != null) { | |
+ { | |
+ if (project != null) { | |
var model = project.GetStringTagModel (new DefaultConfigurationSelector ()); | |
newfile.SetProjectTagModel (model); | |
try { | |
- if (newfile.AddToProject (policyParent, project, language, directory, name)) { | |
- newfile.Show (); | |
- return true; | |
+ if (newfile.AddToProject (policyParent, project, language, directory, name)) { | |
+ newfile.Show (); | |
+ return true; | |
} | |
} finally { | |
newfile.SetProjectTagModel (null); | |
} | |
} else { | |
- SingleFileDescriptionTemplate singleFile = newfile as SingleFileDescriptionTemplate; | |
- if (singleFile == null) | |
- throw new InvalidOperationException ("Single file template expected"); | |
+ var singleFile = newfile as SingleFileDescriptionTemplate; | |
+ if (singleFile == null) | |
+ throw new InvalidOperationException ("Single file template expected"); | |
- if (directory != null) { | |
- string fileName = singleFile.SaveFile (policyParent, project, language, directory, name); | |
- if (fileName != null) { | |
+ if (directory != null) { | |
+ string fileName = singleFile.SaveFile (policyParent, project, language, directory, name); | |
+ if (fileName != null) { | |
IdeApp.Workbench.OpenDocument (fileName, project); | |
- return true; | |
- } | |
+ return true; | |
+ } | |
} else { | |
- string fileName = singleFile.GetFileName (policyParent, project, language, directory, name); | |
- Stream stream = singleFile.CreateFileContent (policyParent, project, language, fileName, name); | |
+ string fileName = singleFile.GetFileName (policyParent, project, language, directory, name); | |
+ Stream stream = singleFile.CreateFileContent (policyParent, project, language, fileName, name); | |
string mimeType = GuessMimeType (fileName); | |
IdeApp.Workbench.NewDocument (fileName, mimeType, stream); | |
return true; | |
- } | |
- } | |
- return false; | |
- } | |
+ } | |
+ } | |
+ return false; | |
+ } | |
protected virtual bool IsValidForProject (Project project, string projectPath) | |
- { | |
- // When there is no project, only single template files can be created. | |
- if (project == null) { | |
- foreach (FileDescriptionTemplate f in files) | |
- if (!(f is SingleFileDescriptionTemplate)) | |
- return false; | |
- } | |
- | |
- // Filter on templates | |
- foreach (FileDescriptionTemplate f in files) | |
- if (!f.SupportsProject (project, projectPath)) | |
- return false; | |
- | |
- //filter on conditions | |
- if (project != null) { | |
- if (!string.IsNullOrEmpty (projecttype) && project.GetProjectTypes ().All (p => p != projecttype)) | |
- return false; | |
- | |
- foreach (FileTemplateCondition condition in conditions) | |
- if (!condition.ShouldEnableFor (project, projectPath)) | |
- return false; | |
- } | |
- | |
- return true; | |
- } | |
+ { | |
+ // When there is no project, only single template files can be created. | |
+ if (project == null) { | |
+ foreach (FileDescriptionTemplate f in Files) | |
+ if (!(f is SingleFileDescriptionTemplate)) | |
+ return false; | |
+ } | |
+ | |
+ // Filter on templates | |
+ foreach (FileDescriptionTemplate f in Files) | |
+ if (!f.SupportsProject (project, projectPath)) | |
+ return false; | |
+ | |
+ //filter on conditions | |
+ if (project != null) { | |
+ if (!string.IsNullOrEmpty (ProjectType) && project.GetProjectTypes ().All (p => p != ProjectType)) | |
+ return false; | |
+ | |
+ foreach (FileTemplateCondition condition in Conditions) | |
+ if (!condition.ShouldEnableFor (project, projectPath)) | |
+ return false; | |
+ } | |
+ | |
+ return true; | |
+ } | |
public virtual List<string> GetCompatibleLanguages (Project project, string projectPath) | |
- { | |
- if (project == null) | |
- return SupportedLanguages; | |
- | |
- //find the languages that both the template and the project support | |
- List<string> langMatches = MatchLanguagesWithProject (project); | |
- | |
- //filter on conditions | |
- List<string> filtered = new List<string> (); | |
- foreach (string lang in langMatches) { | |
- bool shouldEnable = true; | |
- foreach (FileTemplateCondition condition in conditions) { | |
- if (!condition.ShouldEnableFor (project, projectPath, lang)) { | |
- shouldEnable = false; | |
- break; | |
- } | |
- } | |
- if (shouldEnable) | |
- filtered.Add (lang); | |
- } | |
- | |
- return filtered; | |
- } | |
- | |
- //The languages that the template supports | |
- //FIXME: would it be memory-effective to cache this? | |
+ { | |
+ if (project == null) | |
+ return SupportedLanguages; | |
+ | |
+ //find the languages that both the template and the project support | |
+ List<string> langMatches = MatchLanguagesWithProject (project); | |
+ | |
+ //filter on conditions | |
+ var filtered = new List<string> (); | |
+ foreach (string lang in langMatches) { | |
+ bool shouldEnable = true; | |
+ foreach (FileTemplateCondition condition in Conditions) { | |
+ if (!condition.ShouldEnableFor (project, projectPath, lang)) { | |
+ shouldEnable = false; | |
+ break; | |
+ } | |
+ } | |
+ if (shouldEnable) | |
+ filtered.Add (lang); | |
+ } | |
+ | |
+ return filtered; | |
+ } | |
+ | |
+ //The languages that the template supports | |
+ //FIXME: would it be memory-effective to cache this? | |
List<string> SupportedLanguages { | |
get { | |
- List<string> templateLangs = new List<string> (); | |
- foreach (string s in this.LanguageName.Split (',')) | |
- templateLangs.Add (s.Trim ()); | |
- ExpandLanguageWildcards (templateLangs); | |
- return templateLangs; | |
- } | |
- } | |
+ var templateLangs = new List<string> (); | |
+ foreach (string s in LanguageName.Split (',')) | |
+ templateLangs.Add (s.Trim ()); | |
+ ExpandLanguageWildcards (templateLangs); | |
+ return templateLangs; | |
+ } | |
+ } | |
List<string> MatchLanguagesWithProject (Project project) | |
- { | |
- //The languages that the project supports | |
- List<string> projectLangs = new List<string> (project.SupportedLanguages); | |
- ExpandLanguageWildcards (projectLangs); | |
- | |
- List<string> templateLangs = SupportedLanguages; | |
- | |
- //Find all matches between the language strings of project and template | |
- List<string> langMatches = new List<string> (); | |
- | |
- foreach (string templLang in templateLangs) | |
- foreach (string projLang in projectLangs) | |
- if (templLang == projLang) | |
- langMatches.Add (projLang); | |
- | |
- //Eliminate duplicates | |
- int pos = 0; | |
- while (pos < langMatches.Count) { | |
- int next = langMatches.IndexOf (langMatches [pos], pos +1); | |
- if (next != -1) | |
- langMatches.RemoveAt (next); | |
- else | |
- pos++; | |
- } | |
- | |
- return langMatches; | |
- } | |
- | |
- void ExpandLanguageWildcards (List<string> list) | |
- { | |
- //Template can match all CodeDom .NET languages with a "*" | |
- if (list.Contains ("*")) { | |
- foreach (var lb in LanguageBindingService.LanguageBindings) { | |
- IDotNetLanguageBinding dnlang = lb as IDotNetLanguageBinding; | |
- if (dnlang != null && dnlang.GetCodeDomProvider () != null) | |
- list.Add (dnlang.Language); | |
- list.Remove ("*"); | |
- } | |
- } | |
- } | |
- } | |
+ { | |
+ //The languages that the project supports | |
+ var projectLangs = new List<string> (project.SupportedLanguages); | |
+ ExpandLanguageWildcards (projectLangs); | |
+ | |
+ List<string> templateLangs = SupportedLanguages; | |
+ | |
+ //Find all matches between the language strings of project and template | |
+ var langMatches = new List<string> (); | |
+ | |
+ foreach (string templLang in templateLangs) | |
+ foreach (string projLang in projectLangs) | |
+ if (templLang == projLang) | |
+ langMatches.Add (projLang); | |
+ | |
+ //Eliminate duplicates | |
+ int pos = 0; | |
+ while (pos < langMatches.Count) { | |
+ int next = langMatches.IndexOf (langMatches [pos], pos + 1); | |
+ if (next != -1) | |
+ langMatches.RemoveAt (next); | |
+ else | |
+ pos++; | |
+ } | |
+ | |
+ return langMatches; | |
+ } | |
+ | |
+ static void ExpandLanguageWildcards (ICollection<string> list) | |
+ { | |
+ //Template can match all CodeDom .NET languages with a "*" | |
+ if (list.Contains ("*")) { | |
+ foreach (var lb in LanguageBindingService.LanguageBindings) { | |
+ var dnlang = lb as IDotNetLanguageBinding; | |
+ if (dnlang != null && dnlang.GetCodeDomProvider () != null) | |
+ list.Add (dnlang.Language); | |
+ list.Remove ("*"); | |
+ } | |
+ } | |
+ } | |
+ } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment