Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio XML summary comment on single line

In Visual Studio, how do I change the default XML summary comment snippet from three lines to one line?

Currently it provides this snippet when I type ///:

/// <summary>
/// 
/// </summary>

I would like this shorter snippet:

///<summary></summary>

My summaries are often brief and the extra 2 line are unnecessary.

Is there a configuration setting for this or some customizable code/custom addon to fix this.

like image 564
Eugene Avatar asked Nov 26 '11 13:11

Eugene


People also ask

How do I add comments to XML in Visual Studio?

To insert XML comments for a code element Type /// in C#, or ''' in Visual Basic. From the Edit menu, choose IntelliSense > Insert Comment. From the right-click or context menu on or just above the code element, choose Snippet > Insert Comment.

How do you write a summary comment in C#?

The first rule for commenting is it should have /// three slash for comments as C# supports C++ style commenting so commenting can be done by // -- two slashes -- but for Documentation /// is necessary. We will go through each one by one. You can add a paragraph to the description by using <para> tag.

What is the comment syntax for C #s XML based documentation?

XML documentation comments - document APIs using /// comments | Microsoft Docs.

What is param tag in C#?

<param> Tag The <param> tag is used to describe parameters for a method or constructor. Let's say we have a method that adds two numbers together and returns the result as follows. 1 /// <param name="operand1">the left side operand.</


2 Answers

This is an older question, but I liked Jason Williams's suggestion of creating a snippet for this, so I did. Not very complicated, but copy-and-paste is even easier :)

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Single line summary</Title>
      <Shortcut>summary</Shortcut>
    </Header>
    <Snippet>
      <Code Language="csharp">
        <![CDATA[/// <summary>$end$</summary>]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

You can change the shortcut by (probably obviously) changing the <Shortcut> value.

Paste that into a new file named SingleLineSummary.snippet and save it in the folder %USERPROFILE%\Documents\Visual Studio 2012\Code Snippets\Visual C#\My Code Snippets (modify to fit your version of Windows and Visual Studio).

If you're not familiar with snippets, to use this just put the cursor above a method/property/etc, start typing summary, and then hit TAB a couple of times.

like image 70
Nick Avatar answered Nov 15 '22 16:11

Nick


Personally, I think this should be part of the VS editor itself. I know it's been requested in the past. In the meantime, the snippet idea is a good one, but the behavior is slightly different. If you want to keep the same behavior and if you are willing to purchase a 3rd party add-on, then SubMain has a product called "GhostDoc Pro" that, with a little bit of effort, will do this for you. (Note that they have a free, non-"pro" version, "GhostDoc", but I don't think it will work.)

If you want to go this route, here's how it works.

  1. After installing GhostDoc Pro, go to your Tools menu. At the top will be a new fly-out submenu, "GhostDoc Pro".

  2. Go to Tools -> GhostDoc Pro -> Options -> Rules

  3. You will need to edit the T4 template for EACH type that you want this to take effect on.

  4. Click on the rule and then hit "Edit"

  5. At the top, modify

       /// <summary>
       ///<# GenerateSummaryText(); #>
       /// </summary>
    

    to be just

       /// <summary><# GenerateSummaryText(); #></summary>
    
  6. In the method GenerateSummaryText, modify each this.WriteLine to be just this.Write

  7. Hit OK to save, move on to the next template.

  8. Before closing the options page, head up to "General" (from "Rules") and check the "Highlight auto-generated summary when Document This". This will cause the newly inserted auto-text to be selected off the bat so if you don't like it, you can just start typing. Of course, if you prefer to have the text just not generated at all, then you can do that, too, but you will have to modify the T4 templates a bit more. Specifically, you'll need to have GenerateSummaryText just use a single line,

     this.Write(Context.ExecMacro("$(End)"));
    

This will have it not generate any text, but will put the cursor between the 2 <summary> tags.


Side Note:

If anyone knows of a way to get ReSharper or other add-on tools to do this, I'd be interested in seeing that solution as well--if for no other reason than just curiosity.

like image 41
David Avatar answered Nov 15 '22 16:11

David