VsixCommunity / docs

MIT License
9 stars 7 forks source link

Researching the Custom Editor Walkthrough Solution #30

Closed ghost closed 2 years ago

ghost commented 2 years ago

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:

image

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:

MEF

This project has been migrated from CodePlex and isn't actively maintained. It's provided for archival purposes only.

You can find the original wiki home page here.

Replaced by CoreFx

If you're looking for the current MEF code base, you should go to CoreFx.

This CoreFx link then takes you to another broken GitHub page that states:

Going forward, the .NET team is using https://github.com/dotnet/runtime to develop the code and issues formerly in this repository.

Please see the following for more context:

dotnet/announcements#119 "Consolidating .NET GitHub repos"

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:

  1. Visual Studio 2022 Extension

  2. Language Editor using the LanguageFactory from the Community.VisualStudio.Toolkit.LanguageBase.

  3. 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.

  4. 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”)

  5. 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.

  6. What does not work is the code syntax colorization of the code.
    Test.cs file looks like this:
    image So, if I select the private static void method above and click the image button the new editor will display the private static void method and the ToolWindow will display the results.
    image)
    But the new editor window does not have any syntax colorization.

    NOTE: I have noticed that if I take a CSharp (.cs) file in the solution copy it back in the solution and rename the extension to (.linq) all the colorization remains, but only if my sample extension is installed. This does not work if I open a .linq file or create the .linq file from the selection with or without the extension installed.

    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?

ghost commented 2 years ago

Renames the repo on GitHub to fix the spelling issue in the name.

https://github.com/SFC-Sarge/VSIXLinqLanguageEditor2022

madskristensen commented 2 years ago

IScanner and IVsColorizer are old concepts that predates MEF. There is nothing MEF related about them, and they shouldn't be used.

madskristensen commented 2 years ago

Also adding @gundermanc as FYI

ghost commented 2 years ago

@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:

Editor and language service extensions

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.

Core Editor Usage of a Language Service's Colorizer

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 Usage of a Language Service's 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.

gundermanc commented 2 years ago

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, as in this sample: https://github.com/microsoft/VSSDK-Extensibility-Samples/blob/346f5b0289e5fb8de639ba96fb10703df06cd22d/Ook_Language_Integration/C%23/Classification/OokClassifier.cs

ghost commented 2 years ago

@gundermanc , Thanks I will look at ITagger tomorrow. I was using ITaggerProvider but not ITagger.

ghost commented 2 years ago

Determined a workaround using the C# Compiler option for the .linq File. Example can be found in my project LinqLanguageEditor2022.