Getting comments associated with a node

SyntaxEditor .NET Languages Add-on for Windows Forms Forum

Posted 16 years ago by Jamie Penney - Developer, Slyce Software Ltd
Version: 4.0.0274
Avatar
Hi all,
I need to get the comments associated with a particular node in the AST, but I can't find an easy way of doing it. For example, for the following file

namespace Namespace1
{
    /// <summary> Some documentation </summary>
    public class Class1 // Trailing Comment
    {
        // Preceeding Comment
        public int i = 0; // Trailing Comment

        // Preceeding Comment
        public int j - 1; // Trailing Comment
    }
}
I need to be able to get the preceeding and trailing comments for each of these nodes. Any idea how I would go about this? It is important that we keep the comments associated with the correct code element, as we are re-ordering and reformatting the code and need to put the comments back where they should be. We need to be able to differentiate between preceeding and trailing comments.

Thanks in advance,
Jamie

Comments (6)

Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
We do collect comments and assign them to nodes however that code probably does need a lot of further tweaking and testing since it probably isn't as accurate as you would need it as this time.


Actipro Software Support

Posted 16 years ago by Jamie Penney - Developer, Slyce Software Ltd
Avatar
Yeah they seem to be just comments within a block, so in my previous example the comments on the field declarations would be assigned to the class. This isn't what I want it to do at all. Can you think of a good way of doing it?
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Unfortunately, the only way you can get to alter those is to get the source code for the add-on and make changes to how we determine where to store the comments.


Actipro Software Support

Posted 16 years ago by Jon Vollmar - Software Engineer, Teradyne, Inc.
Avatar
I have a similar need; my application must dump the AST in a language different from the one used in the editor while preserving the locations of comments as fully as possible. But in the AST from SyntaxEditor, comments tend to be bunched at certain nodes.

I'm attempting a workaround based on the text StartOffset/EndOffset found with each comment and node. An algorithm first recurses through the tree and finds all the comments; for each one, it removes it from the tree (Comments.Clear()) and stores it in a separate List<> instead. Then for each comment, it recurses through all the nodes again and identifies the one which is closest based on the offsets (and considering only node types with a Comments property). The comment is added to the closest node's comment list.

So far, this works well for comments which are between lines. Attaching comments to the end of lines ("int x=1; // blah") is more difficult, but I think I can use Document.OffsetToPosition() to examine line/column numbers.

Any comments on the viability of this approach are welcome before I spend too much time on it... sorry it's probably too late to help Jamie.

-- Jon V.
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
That probably would work for you. If you see specific cases where comments aren't being placed even close to where they should be, let us know and we can try and tweak those a bit. Right now I do know they aren't tracking in the middle of expressions, etc.

As a side note, we're working on our parsing design for future versions. One thing we're considering is moving AST nodes to be tracked by line/col (DocumentPosition) instead of offset. Would you like to see that?


Actipro Software Support

Posted 15 years ago by Jon Vollmar - Software Engineer, Teradyne, Inc.
Avatar
Yes, line/column instead of offset would have been helpful. I got this to work as I wanted, but I needed access to both the AST and the original text and needed algorithms that examined the original text based on offset. If line/column had been provided, I think I would have not needed to look at the original text.

By the way, for preserving comments, it's important to preserve blank lines as well so that as much formatting as possible can be preserved in the output. The AST comments don't include blank lines; I worked around this by preprocessing the text to change blank lines to "//<blank>" to get them into the AST and then changed this string back into a blank line in the output.

Sorry for the slow re-response.
-- Jon V.
The latest build of this product (v24.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.