Commenting Code in a Legacy Language Service


The new home for Visual Studio documentation is Visual Studio 2017 Documentation on

The latest version of this topic can be found at Commenting Code in a Legacy Language Service.

Programming languages typically provide a means to annotate or comment the code. A comment is a section of text that provides additional information about the code but is ignored during compilation or interpretation.

The managed package framework (MPF) classes provide support for commenting and uncommenting selected text.

There are two general styles of comment:

  1. Line comments, where the comment is on a single line.

  2. Block comments, where the comment may include multiple lines.

Line comments typically have a starting character (or characters), while block comments have both start and end characters. For example, in C#, a line comment starts with //, and a block comment starts with /* and ends with */.

When the user selects the command Comment Selection from the Edit -> Advanced menu, the command is routed to the CommentSpan method on the Source class. When the user selects the command Uncomment Selection, the command is routed to the UncommentSpan method.

You can have your language service support code comments by means of the EnableCommenting named parameter of the ProvideLanguageServiceAttribute . This sets the EnableCommenting property of the LanguagePreferences class. For more information about setting language servicce features, see Registering a Legacy Language Service).

You must also override the GetCommentFormat method to return a CommentInfo structure with the comment characters for your language. C#-style line comment characters are the default.


Here is an example implementation of the GetCommentFormat method.

using Microsoft.VisualStudio.Package;  
namespace MyLanguagePackage  
    class MySource : Source  
        public override CommentInfo GetCommentFormat() {  
            CommentInfo info = new CommentInfo();  
            info.LineStart       = "//";  
            info.BlockStart      = "/*";  
            info.BlockEnd        = "*/";  
            info.UseLineComments = true;  
            return info;  

Legacy Language Service Features
Registering a Legacy Language Service