Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting doc comments from C# source file [closed]

Does anyone know of any tools that allow you to extract comments directly from a .cs file in to some simple text or even the clipboard? Even better if such a tool could work at the method level too.

I am not looking for something like Sandcastle that works at build time, I am looking for something that works on single source code files. Basically I need to copy a chunk of method signatures and the associated doc comments in to a text file, but I am getting bored of removing the "///" and reformating the lines. I'd love to be able to right click on a method and have a context menu item along the lines of "Copy documentation to Clipboard".

Do any such tools exist?

like image 424
Steve Avatar asked Apr 17 '09 09:04

Steve


3 Answers

I have just written a tool that does that. It's just a few lines of code, and it is not finished yet, but very simple to extend (I might do that tomorrow somewhere).

The result : just click extract from a menu, and the result will be on the clipboard.

alt text

Two very easy steps :

  1. Write a program that takes a file as argument, takes some text from that file and paste them to the clipboard.
  2. Integrate that program into your IDE.

STEP 1, part 1 Main program, take a file, send it to an 'extractor' and write results to clipboard.

class Program
{
    [STAThread] 
    static void Main(string[] args)
    {           
        if (args.Length == 0) return;
        FileInfo f = new FileInfo(args[0]);
        Extracter e = new Extracter(f.OpenText().ReadToEnd());
        Clipboard.SetText(e.GetExtractedText());
    }
}

STEP 1, part 2 The extracter : get all the textpatterns you need out of it using regex of course, and return the conforming string. I have ommitted comments for density of post, and since the principle is simple and explained already.

public class Extracter
    {
        private Regex re;
        // extend/adapt regex patterns for better result.
        const String RE_COMMENT_AND_NEXT_LINE= @"(?<=([\/]{3})).+";

        public string FileText { get; set; }

        public Extracter(String FileText)
        {
            this.FileText = FileText;
        }

        public String GetExtractedText() 
        {
            StringBuilder sb = new StringBuilder(String.Empty);
            re = new Regex(RE_COMMENT_AND_NEXT_LINE);
            foreach (Match match in  re.Matches(FileText))
            {
                sb.Append(match.ToString());
            }
            return sb.ToString();

        }
    }

STEP 2 : Add to IDE

That IDE dependent of course, but always easy. See my screenshot for VS2008 :

alt text

like image 102
Peter Avatar answered Nov 14 '22 22:11

Peter


No need to roll your own, have a look at cr-documentor.

CR_Documentor is a plugin for DXCore that allows you to preview what the documentation will look like when it's rendered - in a tool window inside Visual Studio.

It works quite well, but I suspect it running along with Resharper is a bit shaky, so I disable it until I use it.

like image 45
kay.herzam Avatar answered Nov 14 '22 22:11

kay.herzam


comments form .cs files automatically goes to xml file. Below is the instructions:

  • Open the project properties -> Build
  • go to bottom, there is 'Output' section
  • Enable the checkbox for 'XML documentation file' and set the output path for this file
  • this file will have all the documentation of your code copied to it when you build the code.

Once you get all the comments from your project you can use it whatever way you want.

http://msdn.microsoft.com/en-us/magazine/cc302121.aspx

recommendation:

you can disable it during your day to day work, as if the project size is increased it will take time during the build to generate this file as it needs to fatch all the comments. Just run when you are releasing the code so that you get all the documentation.

like image 25
Mutant Avatar answered Nov 14 '22 22:11

Mutant