tree-sitter-python
tree-sitter-python copied to clipboard
leading comment not considered part of function body
I would expect the comment to be part of the body in both these cases.


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?
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?