Accessing all file Templates

I have 2 small issues that a dotPeek of "FileTemplatesManager" isn't showing a simple solution for. Can anyone offer some insight please ?

Q1: I'd like to access the full list of available file templates for an action as the method FileTemplatesManager.Instance.GetFileTemplatesForActions(context) only returns those assigned to the 'quicklist' ?

Q2: (which may be connected to Q1)

For a plugin property page I'd like to access the full list of available file templates for a specific language? e.g. all C# or VB.Net file templates. Do I need to construct a 'context' manually...and how do I do this?

Many thanks

4 comments
Comment actions Permalink

Looks like _storedTemplatesProvider.EnumerateTemplates( _settingsStore, TemplateApplicability.File, true).ToList();

will give me what I need...

0
Comment actions Permalink

I've ended up with following code. Is their a better way to find the templates for the language I need? Is their an better way to go from 'ProjectLanguage' to 'PsiLanguageType' rather than using string comparision?




var projectLanguage = associatedProject.ProjectProperties.DefaultLanguage.PresentableName;


var classTemplate = storedTemplatesProvider.EnumerateTemplates(boundSettingsStore, TemplateApplicability.File)

.Where(x => x.Description == desiredTemplateName && x.ScopePoints.Any(s=>s.RelatedLanguage.PresentableName==projectLanguage))
.Select(x => x)
.FirstOrDefault();

0
Comment actions Permalink

You can't rely on string comparison between ProjectLanguage and PsiLanguageType, since there are more PsiLanguageTypes than ProjectLanguages. The ProjectLanguage type is used to show the project's default/primary language type, which is usually just C# or VB, but the PsiLanguageType is for the type of the file, which can be C# or VB, but also, xml, xaml, aspx. css, js, html, web.config, etc.

Are you trying to show a list of templates per language/scope, like the "more" template chooser dialog? If so, take a look a look at TemplatesFilterPanel.ReapplyTemplates. It gets the list of known scopes, and will filter templates to the selected scope. In fact, you can host the TemplatesFilterPanel as a control if you need this.

Or, if you're just trying to find the file templates that are valid for the current project, you should be able to use FileTemplatesManager.FileTemplatesSupports.Where(s => s.Accepts(project)).

0
Comment actions Permalink

Thank you. The code reads a little better now..



var applicableFileTemplates = _fileTemplatesManager.FileTemplatesSupports.Where(s => s.Accepts(associatedProject));


var applicableFileTemplateScopes = applicableFileTemplates.SelectMany(s => s.ScopePoints).Distinct().ToList();


var classTemplate = _storedTemplatesProvider.EnumerateTemplates(boundSettingsStore, TemplateApplicability
.File)

      .Where(x => x.Description == desiredTemplateName

       &&TemplateScopeManager.TemplateIsAvailable(x, applicableFileTemplateScopes))

      .Select(x => x)
      .FirstOrDefault();




0

Please sign in to leave a comment.