Skip to content

@single_line_no_indent adds extra blank line #1062

@Tuupertunut

Description

@Tuupertunut

Environment

  • OS: Ubuntu 22.04
  • Topiary: 0.6.1

Describe the bug

My grammar has C-like macros and I'm trying to make the macros not indented while the code around them is indented. However if I add a formatting rule like (macro) @single_line_no_indent, it will also add an extra blank line before it. Normally this isn't noticeable because extra blank lines get removed, but if I also want to allow blank lines before macros with (macro) @allow_blank_line_before, there will be a blank line before every macro. If there already was a blank line before the macro in the input, there will now be two blank lines.

To Reproduce

  1. Take any grammar and find some rule that is usually in their own line, such as (macro) in the SQF grammar.
  2. Create a Topiary formatter with the following rules (or equivalent in your grammar):
    "{" @append_indent_start
    "}" @prepend_indent_end
    
    [
        "{"
        "}"
        ";"
        (macro)
    ] @append_hardline
    
    (macro) @single_line_no_indent @allow_blank_line_before
    
  3. Try formatting a file with the content (or equivalent):
    {
        a = 1 + 1;
        #define macro b
        c = 2 + 2;
    
        #define macro d
        e = 3 + 3;
    }
    
  4. See how it formats into
    {
        a = 1 + 1;
    
    #define macro b
        c = 2 + 2;
    
    
    #define macro d
        e = 3 + 3;
    }
    

Expected behavior

I would expect it to format into

{
    a = 1 + 1;
#define macro b
    c = 2 + 2;

#define macro d
    e = 3 + 3;
}

without the added blank lines. @single_line_no_indent sounds like it should only remove indentation for that node but not add any lines.

Workarounds

One could rewrite the @append_hardline rule to ignore cases where the following node is (macro), like this:

"{" @append_indent_start
"}" @prepend_indent_end

(
    [
        "{"
        "}"
        ";"
        (macro)
    ]
    .
    (macro)? @do_nothing
) @append_hardline

(macro) @single_line_no_indent @allow_blank_line_before

This will help for the single blank line case but still not for the two blank lines case. The file now formats into

{
    a = 1 + 1;
#define macro b
    c = 2 + 2;


#define macro d
    e = 3 + 3;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2major: an upcoming releasetype: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions