How to get end of line comment above class declaration?

I want my plugin to inspect class declarations for a comment containing a GUID.

For example;

    // ReTester id:123-123-123
    public class TestMissingProcess : GenericDaemonProcessor<IClassDeclaration, CSharpLanguage, ICSharpFile>
        /// <summary>
        /// Constructor
        /// </summary>
        public TestMissingProcess(IDaemonProcess pProcess)
            : base(pProcess)

I'd like to get the "// ReTester id:123-123-123" comment.

When using the PSI Viewer it shows the comment as whitespace above the class declaration. Browsing the IClassDeclaration interface I can only find IClassDeclaration.GetDocCommentBlockNode() but that appears to be for the XML comments.

Not sure where to look now.

Comment actions Permalink

There don't seem to be any helper methods to find a comment, so I think the best thing to do is to find previous siblings that are comment nodes. Walk along IClassDeclaration.PrevSibling, checking each one with the IsCommentToken() extension method. Skip whitespace and stop walking PrevSibling when you hit null or non-whitespace and non-comment. You can check for whitespace with the IsWhitespaceToken extension method.

Something like this (I haven't tested it):

var node = classDeclaration.PrevSibling;
while(node != null && (node.IsWhitespaceToken() || node.IsCommentToken()))
  if (node.IsCommentToken())
    // Process comment. This might be a single line comment, so you might
    // want to keep processing

  node = node.PrevSibling;

Comment actions Permalink

Thanks, I think that'll do the trick.

I was orginially using Attributes to assign feature to elements in the source code, but this was creating a dependency on a project for anyone who used my plugin. So I thought I would do something like "// ReSharper disable...." comments and assume there was a way to easily find those.

Might be something to add as a feature to the next release of the SDK.


Please sign in to leave a comment.