lukas-reineke / indent-blankline.nvim

Indent guides for Neovim
MIT License
4.07k stars 102 forks source link

[Question] Python scopes are detected in the first line (header) for top-level scopes but not nested scopes. #799

Open anthony-S93 opened 8 months ago

anthony-S93 commented 8 months ago

This isn't an issue per se; I'm asking out of curiosity more than anything else.

The behavior can be demonstrated using the following sample code (refer to the attached video):

def top_level_function(): # For top-level blocks, the scope is highlighted when the cursor is at the header.

    print("foo")

class TopLevelClass:  # For top-level blocks, the scope is highlighted when the cursor is at the header.

    def __init__(self) -> None: # Which isn't the case for nested blocks.

        # For nested blocks, the scope is only highlighted when the cursor is one line past the header
        self.foo = "bar"

Note that both class_definition and function_definition are listed in scope.include.python

https://github.com/lukas-reineke/indent-blankline.nvim/assets/69449791/d18c2175-7a77-4a64-bd42-7e38ba1a405b

Which seems a bit strange to me because upon inspecting the highlight groups using :InspectTree, I didn't see any difference in the way the two function_definitions are parsed other than the fact that one is nested in a block while the other is at the top level.

(function_definition) ; [3:1 - 5:16]      <<<< This is the top-level function
 name: (identifier) ; [3:5 - 22]
 parameters: (parameters) ; [3:23 - 24]
 (comment) ; [3:27 - 108]
 body: (block) ; [5:5 - 16]
  (expression_statement) ; [5:5 - 16]
   (call) ; [5:5 - 16]
    function: (identifier) ; [5:5 - 9]
    arguments: (argument_list) ; [5:10 - 16]
     (string) ; [5:11 - 15]
      (string_start) ; [5:11 - 11]
      (string_content) ; [5:12 - 14]
      (string_end) ; [5:15 - 15]
(class_definition) ; [8:1 - 13:24]
 name: (identifier) ; [8:7 - 19]
 (comment) ; [8:23 - 104]
 body: (block) ; [10:5 - 13:24]
  (function_definition) ; [10:5 - 13:24]     <<<< This is the nested function
   name: (identifier) ; [10:9 - 16]
   parameters: (parameters) ; [10:17 - 22]
    (identifier) ; [10:18 - 21]
   return_type: (type) ; [10:27 - 30]
    (none) ; [10:27 - 30]
   (comment) ; [10:33 - 73]
   (comment) ; [12:9 - 102]
   body: (block) ; [13:9 - 24]
    (expression_statement) ; [13:9 - 24]
     (assignment) ; [13:9 - 24]
      left: (attribute) ; [13:9 - 16]
       object: (identifier) ; [13:9 - 12]
       attribute: (identifier) ; [13:14 - 16]
      right: (string) ; [13:20 - 24]
       (string_start) ; [13:20 - 20]
       (string_content) ; [13:21 - 23]
       (string_end) ; [13:24 - 24]

As you can see, in both function_definitions, the start and end positions listed by the parser are accurate. Given their similarities, I was wondering why the plugin treats both cases differently. According to the parser, the nested function begins at position [10:5]; but since the indent line is shown starting from one line below the declaration, the highlighting of the indent line will not take place until the cursor has gone past the first newline in the scope. This behavior is sensible albeit inconsistent with the case where the declaration is at the top-level of the source file. For top_level_function(), the highlighting takes place as soon as the cursor enters the line containing the declaration.

lukas-reineke commented 8 months ago

This is a side effect of a trick I'm using when getting the scope. I convert the cursor position to a range from the beginning of the line, to the cursor.

https://github.com/lukas-reineke/indent-blankline.nvim/blob/7206c77cb931f79885fc47f88ae18f99148392eb/lua/ibl/scope.lua#L7-L11

I do this to avoid weird single line scopes. In most languages this works, but in Python indentation matters, so it's not quite right.

This would be pretty tricky to fix without breaking other cases worse. So I think I will not look into this.

But PRs are always welcome.