Compartir vía


Comment code in a legacy language service

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

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.

Support 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 service features, see Register 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.

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