Skip to content

bug: comments are not part of the function body #73

@drowning-cat

Description

@drowning-cat

Did you check existing issues?

  • I have read all the tree-sitter docs if it relates to using the parser
  • I have searched the existing issues

Tree-Sitter CLI Version, if relevant (output of tree-sitter --version)

tree-sitter 0.25.10 (da6fe9beb4f7f67beb75914ca8e0d48ae48d6406)

Describe the bug

Comments inside a function are not treated as part of the function body.

function strange()
  -- Comment
  local test = true
end

I am not sure whether this is a bug or a deliberate design choice due to my limited knowledge. I noticed this behavior while using the @function.inner capture provided by nvim-treesitter-textobjects (see lua/queries/textobjects.scm).

In the case of lua-language-server, a function description (via a comment) is defined above it, which makes me expect comments inside the function to be considered part of the function body.

Steps To Reproduce/Bad Parse Tree

(chunk [0, 0] - [4, 0]
  (function_declaration [0, 0] - [3, 3]
    name: (identifier [0, 9] - [0, 16])
    parameters: (parameters [0, 16] - [0, 18])
    (comment [1, 2] - [1, 12]
      content: (comment_content [1, 4] - [1, 12]))
    body: (block [2, 2] - [2, 19]
      local_declaration: (variable_declaration [2, 2] - [2, 19]
        (assignment_statement [2, 8] - [2, 19]
          (variable_list [2, 8] - [2, 12]
            name: (identifier [2, 8] - [2, 12]))
          (expression_list [2, 15] - [2, 19]
            value: (true [2, 15] - [2, 19])))))))

Expected Behavior/Parse Tree

(chunk [0, 0] - [4, 0]
  (function_declaration [0, 0] - [3, 3]
    name: (identifier [0, 9] - [0, 16])
    parameters: (parameters [0, 16] - [0, 18])
    body: (block [2, 2] - [2, 19]
      (comment [1, 2] - [1, 12]
        content: (comment_content [1, 4] - [1, 12]))
      local_declaration: (variable_declaration [2, 2] - [2, 19]
        (assignment_statement [2, 8] - [2, 19]
          (variable_list [2, 8] - [2, 12]
            name: (identifier [2, 8] - [2, 12]))
          (expression_list [2, 15] - [2, 19]
            value: (true [2, 15] - [2, 19])))))))

Repro

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions