JetBrains-Research / snakecharm

Plugin for PyCharm / IntelliJ IDEA Platform IDEs which adds support for Snakemake language.
MIT License
61 stars 7 forks source link

Code indentation fix in rules #26

Open iromeo opened 5 years ago

iromeo commented 5 years ago

Code indentation fix in rules for new line after:

winni2k commented 5 years ago

yes please.

winni2k commented 5 years ago

:)

iromeo commented 5 years ago

Now next line is aligned relative to ':' and no line continuation is inserted. I think better to align rule parameter values relative to rule parameter name start pos + indent, but not clear how to fix this at the moment.

winni2k commented 5 years ago

Hey, I am not sure I understand everything, but this looks like it might be useful!

Just to re-iterate what I would expect to happen: On a line that ends with a colon, when I hit enter, my cursor is sent to the next line and indented by four spaces compared to the previous line.

iromeo commented 5 years ago

Let me show you an example:

rule all:
    wildcard_constraints:<caret> fooo=".*"

After my previous commit. If you press 'Enter', you will get

rule all:
    wildcard_constraints:
                         <caret>fooo=".*"

instead of

rule all:
    wildcard_constraints:\
        <caret>fooo=".*"

On a line that ends with a colon, when I hit enter, my cursor is sent to the next line and indented by four spaces compared to the previous line.

Yes, agree, I'd like to get:

rule all:
    wildcard_constraints:
        <caret>fooo=".*"

P.S: Example updated, the initial version was incorrect

winni2k commented 5 years ago

Hmmm, that may be worse than before?

iromeo commented 5 years ago

I think not worse because we don't need '\' here and moving caret back to prev line to delete '\' is more annoying.

winni2k commented 5 years ago

Hmm, currently when I start with this:

rule all:
    wildcard_constraints:<caret>

and hit enter, I get this:

rule all:
    wildcard_constraints:
    <caret>

I then have to hit space four times to get a sensible indent. Am I on an outdated version of the plugin?

iromeo commented 5 years ago

Ok, correct my minimal example was too minimal, indeed for

rule all:
    wildcard_constraints:<caret>

you will get

rule all:
    wildcard_constraints:
    <caret>

I was talking about the case:

rule all:
    wildcard_constraints:<caret> fooo=".*"

or

rule all:
    wildcard_constraints:<caret>
                        fooo=".*"

When I want to add new param to the existing single line definition, I don't want to get \ char on Enter

rule all:
    wildcard_constraints: \
        fooo=".*"

And the desired behavior should be

rule all:
    wildcard_constraints:
        fooo=".*"

At the moment I've implemented a partial fix which doesn't insert line continuation \, but unfortunately aligns text relatively to ':'

rule all:
    wildcard_constraints:
                         <caret>fooo=".*"
winni2k commented 5 years ago

Ah! Yes, that's an improvement :D

iromeo commented 5 years ago

@winni2k Updated plugin is available in JetBrains Plugins Repository and could be installed using Preferences|Plugins

winni2k commented 5 years ago

Just a bit of feedback on our conversation above: I've started using this version of the plugin, and I love that the code -> reformat code option now works!

winni2k commented 5 years ago

I am still quite happy with this plugin, but I have a request for an improvement:

When I use code -> reformat code on the this code (note the indentation of two spaces before """cats"""):

rule bla:
    shell:
      """cats"""

then the code is reformatted as:

rule bla:
    shell:
         """cats"""

with an indentation of five spaces.

I see this happening a lot. Is there any way to constrain the indents to be four spaces?

iromeo commented 5 years ago

@winni2k At the moment you cannot change that. You get 5 space indentation because """cats""" is aligned relative to shell keyword end offset. I also don't like that section arguments get different indentation depending on it's section name length (e.g. resources vs input)

This issue is likely to be connected with #16. Snakemake language support reuses parts of Python plugin parser and code formater and at some points we cannot tune python code formatter to process snakemake syntax in the way which we want.

We are going to work on these 2 issues during this summer.

lpla commented 4 years ago

Hi! Any progress on this?

iromeo commented 4 years ago

@lpla Yes, there is some progress, you can configure the formatter to keep constant indent independent on section keyword length. To do this:

image

Then instead of image

reformat code will behave like: image

The formatter is still not ideal, but to my mind, it works quite ok and this issue isn't critical at the moment.

winni2k commented 4 years ago

Thanks for the workaround! This has been driving me insane.

iromeo commented 3 years ago

Related API docs - https://www.jetbrains.org/intellij/sdk/docs/reference_guide/custom_language_support/code_formatting.html

We need own formatting model builder base extending com.jetbrains.python.formatter.PythonFormattingModelBuilder and com.jetbrains.python.formatter.PyBlock. E.g. add support for Snakemake specific PSI + change some rules for py arg lists inside snakemake sections

Also we want to get Uncheck checkbox PyCharm | Preferences | Editor | Code Style | Python | Wrapping and Braces | Method call args | Align when multiline behaviour w/o modifiying python formatting settings.

See tests examples in PyFormatterTest

iromeo commented 3 years ago

Snakemake specific codestyle settings paged added in coming 0.9.0 image