Closed ghost closed 2 years ago
Renames the repo on GitHub to fix the spelling issue in the name.
IScanner and IVsColorizer are old concepts that predates MEF. There is nothing MEF related about them, and they shouldn't be used.
Also adding @gundermanc as FYI
@madskristensen, @gundermanc, and Leslie Richardson
I agree that IScanner and IVsColorizer are old concepts, but current documentation makes it hard to understand that, See:
Editor and language service extensions Syntax Coloring in Custom Editors
Since these documents reference VS 2022 SDK and it references: IVsColorizer and IVsColorizer2, and MEF:
You can extend most features of the Visual Studio code editor. The editor is based on the Windows Presentation Foundation (WPF) and is written in managed code. Although this design differs from the designs in earlier versions of Visual Studio, it provides most of the same features. To extend the editor, use the Managed Extensibility Framework (MEF).
The Visual Studio SDK provides adapters known as shims to support VSPackages that were written for earlier versions. Nevertheless, if you have an existing VSPackage, we recommend that you update it to the new technology to obtain better performance and reliability.
When a language service with a colorizer is obtained by an instance of the core editor, the parsing and rendering of text by a language service's colorizer occurs automatically without requiring any further intervention on your part.
The IDE transparently:
Calls the colorizer as needed to parse and analyze text as it is added or modified in the implementation of IVsTextBuffer.
Ensures that the display supplied by the document view provided by the IVsTextView implementation is updated and repainted using the information returned by the colorizer.
Non-core editor instances can also use a language service's syntax colorization service, but they must explicitly retrieve and apply the service's colorizer, and repaint their document views themselves.
To do this, a non-core editor must:
Obtain a language service's colorizer object (which implements IVsColorizer and IVsColorizer2). The VSPackage does this by calling the GetColorizer method on the language service's interface.
Call the ColorizeLine method to request that a particular span of text be colorized.
The ColorizeLine method returns an array of values, one for each letter in the text span being colorized. It also identifies the text span as a particular type of colorable item, such as a comment, keyword, or data type.
Use the colorization information returned by ColorizeLine to repaint and display its text.
Hey all, I may be missing some context here, but my recommendations for how to best extend Visual Studio are:
For colorization specifically, the right MEF API to use is ITagger
@gundermanc , Thanks I will look at ITagger tomorrow. I was using ITaggerProvider but not ITagger.
Determined a workaround using the C# Compiler option for the .linq File. Example can be found in my project LinqLanguageEditor2022.
Researching the Custom Editor Walkthrough Solution
I am including Leslie Richardson and Mads Kristensen in this question since they are the best resource for Extending Visual Studio 2022.
Hi Leslie, my name is Danny McNaught I have been a developer for over 25 years, but this is the first time I am creating an Editor Extension for Visual Studio. So, when I offered to contribute to Mads Kristensen’s VSIX Cookbook; a complete ‘Walkthrough’ on how to include a Language Editor in a Visual Studio 2022 Extension. I found documentation that sends you everywhere but the right place. I can create the Editor Extension using the LanguageFactory using Mads examples and it works great. The issue that I am finding is that the current available MEF documentation dated 11/06/2021 does not provide any examples/walkthroughs on how to add syntax coloring (CSharp file like code coloring) using MEF, there are examples for legacy language services using IScanner, Colorizer… But they do not provide information on using the IScanner or Colorizer in MEF. The Legacy Language Services documentation also states:
Then on top of that when you follow the Managed Extensibility Framework (MEF) and to the Next steps section the included link for: For more information and code examples, see Managed Extensibility Framework
This link takes you to GitHub where the Readme.md states:
This CoreFx link then takes you to another broken GitHub page that states:
Now that we are at the currently supported GitHub site, but remember I wanted information on MEF and Visual Studio 2022. I was dropped at the root of the dotnet/runtime site.
I am still using these documents no closer than I was to getting the CSharp syntax highlighting I am wanting for the walkthrough in the VSIX Cookbook.
The walkthrough: VSIXLinqLanguageEditor2022 (Sample code is located here)
Once I get the code to work as expected then I will create the documentation on how to do it for the VSIX Cookbook). I have tried many things, so the sample project has a lot of extra stuff that would be removed once I get it working correctly. (i.e. LinqEditorLightBulb.cs, etc.)
The Walkthrough I am creating for the VSIX Cookbook is as follows:
Visual Studio 2022 Extension
Language Editor using the LanguageFactory from the Community.VisualStudio.Toolkit.LanguageBase.
ToolWindow allows you to select a line of LINQ code from your open CSharp file, select a method that contains a LINQ query, or open a .linq file.
The ToolWindow command then opens your selection in a new Editor window where you can modify/test it and get and display query results from the LINQ query inside of Visual Studio. (Note: Currently works using LinqPad’s “LPRun7-x64.exe, and LINQPad.Runtime.dll”)
The new Editor window with the file extension .linq instead of .cs contains you code. This still works as expected and allows you to get the results of the query in the ToolWindow or a Dump Window based on Options settings for the linq Text Editor.
What does not work is the code syntax colorization of the code.
Test.cs file looks like this:
So, if I select the private static void method above and click the button the new editor will display the private static void method and the ToolWindow will display the results.
)
But the new editor window does not have any syntax colorization.
My questions are:
How to you get CSharp style colorization to work in MEF and Visual Studio 2022 Editor extension?
Can you use IScanner and IVsColorizer in a MEF Extension? If so, how do you, [Import] [Export] IScanner and IVsColorizer in your MEF package?
Do you currently have documentation on using MEF package to colorize your code?