Compartir a través de


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

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

Comment Styles

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.

Supporting Code Comments

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 Language Service (Managed Package Framework)).

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.

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

Registering a Language Service (Managed Package Framework)

Other Resources

Language Service Features (Managed Package Framework)