Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong indentation for multi-line if statement at file level #40

Closed
enixmail opened this issue May 6, 2020 · 5 comments
Closed

Wrong indentation for multi-line if statement at file level #40

enixmail opened this issue May 6, 2020 · 5 comments
Assignees

Comments

@enixmail
Copy link

enixmail commented May 6, 2020

When having a multi-lines if statement at file level, the second and following lines are not indented.

Note: this was ran from commit 9919e4b

Given this Snakefile:

if (config['a'] is None and
    config['b'] is None and
    config['c'] is None):


    rule rule1:
        output:
            "my_input_1.txt",
        shell:
            "touch {output[0]}"


elif (config['d'] is None and
      config['e'] is None):


    rule rule2:
        output:
            "my_input_2.txt",
        shell:
            "touch {output[0]}"


else:


    rule rule3:
        output:
            "my_input_3.txt",
        shell:
            "touch {output[0]}"

snakefmt formats it this way:

if (config['a'] is None and
config['b'] is None and
config['c'] is None):


    rule rule1:
        output:
            "my_input_1.txt",
        shell:
            "touch {output[0]}"
elif (config['d'] is None and
config['e'] is None):


    rule rule2:
        output:
            "my_input_2.txt",
        shell:
            "touch {output[0]}"
else:


    rule rule3:
        output:
            "my_input_3.txt",
        shell:
            "touch {output[0]}"

Also, to be discussed in #38 I guess, blank lines are removed between the end of the rule and the elif or else keywords (should it be 2 blank lines as a top element or simply because of the rules inside?).

@bricoletc
Copy link
Collaborator

Gah. I'm struggling to think what is readable here. Should we have 2 lines between the python code and the nested rule, and two lines before rule and python code @johanneskoester ?

@bricoletc
Copy link
Collaborator

Also, thanks @enixmail for spotting ;-)

@bricoletc bricoletc self-assigned this May 10, 2020
@bricoletc
Copy link
Collaborator

black does this:

if p:

    def p():
        pass


elif p2:

    def p2():
        pass


else:

    def p3():
        pass

One line before the function, two lines after

bricoletc added a commit that referenced this issue May 10, 2020
When snakemake keywords are inside python code, they get 1 newline
between keywords, and two newlines when going from keyword back to code.

This mirrors black when formatting functions not at indent level 0.
bricoletc added a commit that referenced this issue May 10, 2020
When snakemake keywords are inside python code, they get 1 newline
between keywords, and two newlines when going from keyword back to code.

This mirrors black when formatting functions not at indent level 0.
@mbhall88
Copy link
Member

I think using black as a guide is a safe bet.

bricoletc added a commit that referenced this issue May 17, 2020
* Requires dealing with lone 'if a:' or 'else:' which break python
  syntax
* Solves #40
* Will break on 'for:' or 'while:'
@bricoletc
Copy link
Collaborator

Alrighty with the latest commits (on dev, will PR to master) here is the diff with your file @enixmail

+ if config["a"] is None and config["b"] is None and config["c"] is None:
- if (config['a'] is None and
-     config['b'] is None and
-     config['c'] is None):
- 
  
      rule rule1:
          output:
              "my_input_1.txt",
          shell:
              "touch {output[0]}"
  
  
+ elif config["d"] is None and config["e"] is None:
- elif (config['d'] is None and
-       config['e'] is None):
- 
  
      rule rule2:
          output:
              "my_input_2.txt",
          shell:
              "touch {output[0]}"
  
  
  else:
  
- 
      rule rule3:
          output:
              "my_input_3.txt",
          shell:
              "touch {output[0]}"

So implemented the black-like one line spacing in indented context, two line spacing at top leve.

Also the first if condition gets formatted, this is now generic to all python statements preceding snakemake code in nested contexts.

Note for your specific example the line is short enough to get collapsed, but make it longer and you get

if (
    config["a"] is None
    and config["b"] is None
    and config["d"] is None
    and config["c"] is None
):

So looking dandy!

I will close if you're happy with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants