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).
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?
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 the CreateEditorInstance function, you can make condition like this to limit the editor to JSON files with specific name:if (System.IO.Path.GetFileName(pszMkDocument).ToLower() != "manifest.json")
{
return VSConstants.VS_E_UNSUPPORTEDFORMAT;
}
ProjectTemplate.csproj) as the following:<Content Include="manifest.json" >
<IsWebExtensionManifest>true</IsWebExtensionManifest>
</Content>
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 and uint 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:
buildPropertyStorage.GetItemAttribute(itemid, "IsWebExtensionManifest", out string propVal);
if (!Convert.ToBoolean(propVal))
{
return VSConstants.VS_E_UNSUPPORTEDFORMAT;
}
Once I finish my custom project with this custom editor, I'll post its GitHub link for more clarity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With