Commenting Blocks of Code (Managed Package Framework)

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 the compilation or interpretation phase.

The managed package framework (MPF) classes provide support for commenting and uncommenting selected blocks of code using a single command.

Comment Styles

There are two general styles of comments:

  1. Line comments, where the comment starts at some point on a line and goes to the end of the line.

  2. Block comments, where the comment starts at some point and ends at another point, possibly many lines further along.

Line comments typically have a starting character or characters while block comments have both a starting character or characters and a different set of ending character or characters. For example, in C#, a line comment starts with // and block comments start with /* and end with */. In Visual Basic, a line comment starts with ' and there are no block comments. In XML, there are no line comments 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.

Supporting Commenting of Code

The MPF classes already support commenting and uncommenting of code; all that is needed are the characters defining a line comment and a block comment. In addition, the EnableCommenting registry entry must be set to 1 by the language service to indicate support for commenting code. This entry is accessed through the EnableCommenting property of the LanguagePreferences class. The EnableCommenting registry entry can be initialized with a named parameter in the ProvideLanguageServiceAttribute attribute (also see Registering a Language Service (Managed Package Framework)).

In addition to setting the registry entry, EnableCommenting, you must derive a class from the Source class and override the GetCommentFormat method to return a CommentInfo structure specifying the comment characters for your language. By default, the MPF assumes C#-style comment characters and favors line comments over block comments when commenting out multiple lines.

Example

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;
        }
    }
}

See Also

Concepts

Language Service Features (Managed Package Framework)

Registering a Language Service (Managed Package Framework)