grouping single line comments together with roslyn

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

internal class Program
{
	private static void Main(string[] args)
	{
		// example code to check for comments
		const string programText = @"
            using System;
        using System.Collections;
        using System.Linq;
        using System.Text;

            /* Block start 1
             *
             *
             *
             *
             * Block End 2
             */


        // single line comment 0

        // single line comment 1
        // single line comment 2
        // single line comment 3
        namespace HelloWorld
        {
            /* Block start 3
             *
             *
             *
             *
             * Block End 4
             */
            class Program
            {
                /// documentation 1
                /// documentation 2
                /// documentation 3
                /// documentation 4
                static void Main(string[] args)
                {
                    Console.WriteLine(""Hello, World!"");
                }
            }

            // another comment 1


            // another comment 2
            // another comment 3


            /* Adjacent
             *
             */

            // another comment 4
            // another comment 5

            /* Adjacent
             *
             *
             */
        }";
		SyntaxTree tree = CSharpSyntaxTree.ParseText(programText);
		var root = tree.GetRoot();
		var walker = new CommentsWalker();
		walker.Visit(root);
		// printing out all comments in groups that we found
		foreach (var(start, end)in walker.GetCommentBlocks())
		{
			Console.WriteLine($"Comment block: {start} - {end}");
		}
	}
}

public class CommentsWalker : CSharpSyntaxWalker
{
	// keeps track of all comment blocks in code
	private List<(int start, int end)> commentBlocks;
	// keeps track of current continuous comment block
	// using linked list so it is easy to access first and last
	// line in a comment block
	private LinkedList<(int start, int end)> currentBlock;
	public CommentsWalker() : base(SyntaxWalkerDepth.StructuredTrivia)
	{
		commentBlocks = new();
		currentBlock = new();
	}

	public List<(int start, int end)> GetCommentBlocks()
	{
		// before returning all comments
		// we have to ensure that all comment blocks were added
		if (currentBlock.First != null)
			commentBlocks.Add((currentBlock.First.Value.start, currentBlock.Last.Value.end));
		return commentBlocks;
	}

	public override void VisitTrivia(SyntaxTrivia trivia)
	{
		var span = trivia.GetLocation().GetLineSpan();
		// get starting and ending line numbers of a current comment 
		// it can be a multi-line and a single-line comment
		(int start, int end) = (span.StartLinePosition.Line, span.EndLinePosition.Line);
		switch (trivia.Kind())
		{
			case SyntaxKind.SingleLineCommentTrivia:
			{
				// if this is a first comment in a block
				// or if this is a continuation of a previous comment
				if (currentBlock.First == null || currentBlock.Last.Value.end + 1 == start)
					currentBlock.AddLast((start, end));
				else
				{
					// this is a new single line comment that isn't adjacent to previous one
					// add previous block and start a new one with current comment
					commentBlocks.Add((currentBlock.First.Value.start, currentBlock.Last.Value.end));
					currentBlock = new();
					currentBlock.AddFirst((start, end));
				}
			}

				break;
			case SyntaxKind.MultiLineCommentTrivia:
			{
				// treat whole comment block at once
				if (currentBlock.First != null)
				{
					commentBlocks.Add((currentBlock.First.Value.start, currentBlock.Last.Value.end));
					currentBlock = new();
				}

				currentBlock.AddFirst((start, end));
			}

				break;
		}

		base.VisitTrivia(trivia);
	}
}
;