Context:
I'm creating custom project template for visual studio 2017 which works well. Inside this project template, I emit a file named "manifest.json".
I need to create custom editor\designer for the "manifest.json" file that's when the user double clicks this file in "Solution Explorer", it opens my custom editor.
I already found few articles on Microsoft Doc (like this one Create custom editors and designers) and found some GitHub examples about creating custom editors and custom designer (like this Editor_With_Toolbox, SingleFileGenerator, WPFDesigner_XML and Snippet Designer).
Problem:
- Most of the articles, examples and documentations explain how to associate the custom editor with specific file extension (in my case I want to associate the editor with specific file "manifest.json" in a specific project).
- The articles related to single file generator, they don't fit my solution because they speak about a file is edited in the designer and another file is emitted in the solution (like windows forms designer).
Summary:
I want to implement visual studio custom editor\designer that run only for a specific file in my custom project template. How to achieve this?
Notes:
- The file to be associated in the custom editor must be named "manifest.json".
- If there's a file named "manifest.json" in other project types, the standard editor should be executed not my custom editor.
Finally after a lot of try and error approach supported with open source samples, I got solution to my problem in the following steps:
IVsEditorFactory
), inside theCreateEditorInstance
function, you can make condition like this to limit the editor to JSON files with specific name:ProjectTemplate.csproj
) as the following:To check for the metadata inside the editor factory, we will continue our work in the
CreateEditorInstance
function as the following:4.1 We already have these 2 parameters passed to the
CreateEditorInstance
function: (IVsHierarchy pvHier
anduint itemid
).4.2 Use these 2 parameters to obtain
IVsBuildPropertyStorage
of the parent project (some code examples exist online).4.3 Use this code to check for our mark metadata:
Once I finish my custom project with this custom editor, I'll post its GitHub link for more clarity.