Skip to content

Fold-range: comment parsing is very brittle #2998

@MichaReiser

Description

@MichaReiser

The region parsing of folding_ranges iterates over every line in the source and assumes that any line starting with a # is a comment. This is extremely brittle because multiline strings can contain lines starting with # region but those aren't region markers.

We should change folding_ranges to iterate over ParsedModule.tokens() to extract all the comment tokens instead of iterating over lines.

Test case demonstrating the bug

    #[test]
    fn test_folding_range_regions_inside_multiline_fstring() {
        let test = CursorTest::builder()
            .source(
                "main.py",
                r#"
message = f"""
    # region Not a real region
    text inside an f-string
    # endregion
"""
<CURSOR>
"#,
            )
            .build();

        assert_snapshot!(test.folding_ranges(), @r#"
        info[folding-range]: Folding Range
         --> main.py:2:11
          |
        2 |   message = f"""
          |  ___________^
        3 | |     # region Not a real region
        4 | |     text inside an f-string
        5 | |     # endregion
        6 | | """
          | |___^
          |

        info[folding-range]: Folding Range (region)
         --> main.py:3:1
          |
        2 |   message = f"""
        3 | /     # region Not a real region
        4 | |     text inside an f-string
        5 | |     # endregion
          | |_______________^
        6 |   """
          |
        "#);
    }

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingserverRelated to the LSP server

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions