The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,860 other subscribers

Enabling XML Auto Commenting C# in Visual Studio Code is in an odd setting (via Stack Overflow)

Posted by jpluimers on 2025/03/18

In Visual Studio Code I already had [Wayback/Archive] C# – Visual Studio Marketplace: C# for Visual Studio Code (powered by OmniSharp) installed in (through code --install-extension ms-dotnettools.csharp), and wanted automatic XML documentation comments generation just like Visual Studio does:

[Wayback/Archive] XML documentation comments – document APIs using /// comments | Microsoft Learn

[Wayback/Archive] xml comments – XML Auto Commenting C# in Visual Studio Code – Stack Overflow (thanks [Wayback/Archive] s2Jakob and [Wayback/Archive] Pascal Berger) taught me how to enable XML Auto Commenting in C# (type /// in front of a method to expand to a full XML comment with placeholders generated from the method signature):

Q

In MonoDevelop, when I type “///”, it auto-generates an xml-style comment like this:

/// <summary>
/// Describes the variable x
/// </summary>
int x = 0;

Is there a way to get this behavior in Visual Studio Code?

A

Starting with [Wayback/Archive] C# for Visual Studio Code Extension 1.23.8 this is supported when Editor: Format On Type. setting is enabled.

Settings can be found in the menu under File/Preferences/Settings (Windows/Linux) / Code/Preferences/Settings (macOS).

The required setting is available under Text Editor/Formatting.

{
    "editor.formatOnType": true,
}

For me, the easiest to enable this is:

  1. Press Ctrl+, on Windows/Linux or on MacOS Command+,.
  2. If the search field does not have the focus, press Ctrl+F (Find) on Windows/Linux or on MacOS Command+F.
  3. Type Format Type with will get you the “Format On Type” setting under “Editor”: a generic setting for the full editor.
  4. Enable the checkbox you see below:

    Image

  5. Close the settings tab (press Ctrl+W on Windows/Linux or on MacOS Command+W).
  6. Profit!

[Wayback/Archive] C# XML Comments in Visual Studio Code – Simple Talk highlights my concern with this very generic setting:

I confess I don’t know for sure what other formatting features are included when we enable this option but I would love to discover this. If you find this out, drop a comment on this blog.

I guess time will tell, especially when noting which characters are involved in the expansion. Those are in the commit [Wayback/Archive] Support auto doc comment generation · 333fred/omnisharp-vscode@159de98:

- localDisposables.add(vscode.languages.registerOnTypeFormattingEditProvider(documentSelector, new FormatProvider(server, languageMiddlewareFeature), '}', ';'));
+ localDisposables.add(vscode.languages.registerOnTypeFormattingEditProvider(documentSelector, new FormatProvider(server, languageMiddlewareFeature), '}', '/', '\n', ';'));

  • } ending curly brace
  • / slash
  • \n new-line
  • ; semi-colon

The middle two are for XML documentation comments, the others already existing. Need to keep an eye on what behaviour is new when typing those.

Some more notes

There used to be another extension that exclusively did XML Comments (I think it had it’s own implementation) that is now deprecated because of the official support, see {Wayback/Archive] C# XML Documentation Comments – Visual Studio Marketplace

C# for Visual Studio Code (powered by OmniSharp) now officially supports the documentation comment from [Wayback/Archive] v1.23.8 (December 18th, 2020). You can use the official one by turning on Editor: Format On Type.

This extension will be deprecated in the future. Thanks to the users who have supported me so far.

[Wayback/Archive] Release v1.23.8 · OmniSharp/omnisharp-vscode implemented this and mentions these hard to find entries:

Only when you open the individual links, you will find just one about XML documentation:

Via

Got al this because of [Wayback/Archive] vscode xml documentation – Google Search.

It also got me [Wayback/Archive] XML – Visual Studio Marketplace (code --install-extension redhat.vscode-xml)

This VS Code extension provides support for creating and editing XML documents, based on the LemMinX XML Language Server.

Note: it uses a binary version of the [Wayback/Archive] LemMinX XML Language Server, so you can run it even if you do not have Java installed, but you have to allow it to run :

The lemminx-win32.exe file is part of the assets in that repository (see for instance [Wayback] lemminx-win32.zip in [Wayback/Archive] Release 0.21.0 · redhat-developer/vscode-xml). [Wayback/Archive] vscode-xml/CONTRIBUTING.md at main · redhat-developer/vscode-xml: binary-server-development describes more about it.

Twitter thread

On one page: [Wayback/Archive] Thread by @jpluimers on Thread Reader App

--jeroen

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.