tree-sitter-python icon indicating copy to clipboard operation
tree-sitter-python copied to clipboard

leading comment not considered part of function body

Open orph opened this issue 3 years ago • 1 comments

I would expect the comment to be part of the body in both these cases.

Screen Shot 2021-07-23 at 5 57 15 PM Screen Shot 2021-07-23 at 5 59 58 PM

If this is intended, is there a good strategy to consider a leading comment as part of the body? If no body, check the node following the function_definition is a comment, and if there is a body check if previous siblings are comments?

orph avatar Jul 23 '21 23:07 orph

The same bug applies to the body blocks of with statements unfortunately. Consider:

with managed_resource(timeout=3600) as resource:
     # Resource is released at the end of this block,
     # even if code in the block raises an exception
     a = 42
     # hello
     b = 19

The first two comment lines will be put between the with_clause and body: block, while the third comment is part of the body. The grammar cannot be the issue, as it only allows a block to follow the with clause and the colon:

    "with_statement": {
      "type": "SEQ",
      "members": [
        {
          "type": "CHOICE",
          "members": [
            {
              "type": "STRING",
              "value": "async"
            },
            {
              "type": "BLANK"
            }
          ]
        },
        {
          "type": "STRING",
          "value": "with"
        },
        {
          "type": "SYMBOL",
          "name": "with_clause"
        },
        {
          "type": "STRING",
          "value": ":"
        },
        {
          "type": "FIELD",
          "name": "body",
          "content": {
            "type": "SYMBOL",
            "name": "_suite"
          }
        }
      ]
    },

I suppose this bug is part of tree-sitter itself?

niklaskorz avatar Feb 25 '22 13:02 niklaskorz