Improve DX for command-line formatter

Answered

We've come to learn about the possibility that the intelliJ formatter can be used from the CLI, according to https://www.jetbrains.com/help/idea/command-line-formatter.html. We've tried this out and it works really nicely. I'm reaching out to you on this forum because an improved version of it could become enormously useful for a project I'm starting on. 

The project is heavily based on code-generation and automations of boilerplate code. Some automations also edit specific places of the concerned files with surgical precision using third-party tools which work very reliably. But these tools break formatting used on the file, causing them to sometimes be entirely regenerated in kind of a minified look. 

Unfortunately, most of the automation solutions do not offer the possbiility to preserve formattings reliably. The easiest and probably most productive way to preserve file formattings (and even apply the formatting automatically after code gen, e.g. to new files / new AST nodes added programmatically by code gens) would thus be to use the CLI formatter consistently for all developers of the project. This would also align the applied formattings with the DX out-of-the-box, which would really be neat.

The main benefit I see from this is that it would enormously facilitate code review in git UIs. If any file is created / modified programmatically, sometimes the entire code is highlighted as modified (e.g. if minified) in any git UI you may use, in case the formatting is not properly equal. Being able to attach the formatter with our own code styles to all code-gen processes would be an incredible benefit for this, as it would drastically accelerate according Code reviews and make them much less confusing.

So my suggestions regarding your CLI formatting runner are:

  1. As you document, it cannot be run while intellij is open. That's sad, bc imagine the power of running a specific code gen / automation process while you're developing in intellij, and then automatically format all the generated code with the configured settings. Then, when you push the generated code into the repo, you can make sure that all developers can easily and perfectly review  the committed code; you would not have to worry about this anymore.
  2. A detail, but a pure shell-script implementation of this feature could enable it for CI pipelines, but this is not something totally needed I would say (though defo nice to have).

PS: In the Code Styles config, can you somehow add a formatting rule that adds a blank line in between statements for a given language, or even generally speaking? I've inspected the "Blank Lines" for a few languages, and couldn't find such an option, yet I think it would greatly benefit readibility. Wondering if this is somewhere globally defined?

0
5 comments
Hi,

Really thanks for the feedback.

> As you document, it cannot be run while intellij is open.
> A detail, but a pure shell-script implementation of this feature could enable it for CI pipelines, but this is not something totally needed I would say (though defo nice to have).

Please vote for the related issue here:
https://youtrack.jetbrains.com/issue/IJPL-29940/ (Command Line Formatter does not run when IDE is running)
https://youtrack.jetbrains.com/issue/IJPL-190377/ (Standalone CLI formatter that uses IntelliJ configuration)

As a workaround, you could have a script that runs theneeded code gen/automation process, and then set `IDEA_PROPERTIES` environment variable to set a path to a `idea.properties` file with thebelow properties:

idea.config.path=/some-path/ide/config
idea.system.path=/some-path/ide/system
idea.plugins.path=/some-path/ide/plugins

If the IDEA is started with a different IDE configuration folder, it can be started from the command line even if there is an IDE running with the default run configuration. See https://intellij-support.jetbrains.com/hc/en-us/articles/207240985-Changing-IDE-default-directories-used-for-config-plugins-and-caches-storage and https://stackoverflow.com/a/5889637 for details.
Then you could run the IDE command line to format this file in that script even if there is another IDE running.

> PS: In the Code Styles config, can you somehow add a formatting rule that adds a blank line in between statements for a given language, or even generally speaking? I've inspected the "Blank Lines" for a few languages, and couldn't find such an option, yet I think it would greatly benefit readibility. Wondering if this is somewhere globally defined?

Can you please add more details about what you mean by `statements`?
Adding a blank line between statements would almost result in a blank line between every line in methods/functions. I’ve never seen a code style that adds a blank line between every statement.

Also, could you share the programming languages you’re working with?
We can’t add a general formatting option like this for all languages — we’d need to implement the feature separately for each language.
0

Thanks for your answer, apologies for my late reply. 

Regarding your statement that it's nice to have but not a must, I agree with you, but would also say that that depends on the project. For projects with high amounts of code generations which normally do not focus on human readability of changed code, the benefit of having a formatter applied consistently before each commit, while the code style settings could be shared through a repositories' `.idea` folder, would be incredibly useful for code review. It would e.g. also prevent that some files appear in the diff views in version control UI while the only changes committed to the files was formatting because the files were re-generated as a part of a batch gen process or similar. All of this would finally yield much faster code reviews, the more  the project uses code gen. Without enforced formatting before comitting, increased code gen leads to less human-readable and thus slower code reviews, effectively acting against the speed gained thanks to the automations. So there is a potentially really big and direct impact on overall productivity, if this feature was available. At least for dev teams working with your IDE, Code-Gen + git CLI, which is probably not that rare.

What I came across in that sense is this:  https://stackoverflow.com/questions/52722252/is-there-a-way-to-enforce-intelij-code-style-formatting-before-committing-to-git.

This made me think that the solution is already implemented for commits committed out of your IDE. Our team heavily relies on the `git cli` to control everything related to git directly in the terminal, because we're much faster with the CLI than the IDE (interactive git CLI commands, etc.). Maybe extending the feature you already implemented as mentioned in the link above, e.g. through a dedicated git terminal in the IDE, allowing to bind the feature to git CLI commands? If that is even possible.

Regarding the addition of a blank line in between statements is as you say yes, it would lead to line breaks in between all statements, but that's definitely in the nice-to-have range, not a must. It's all for increased readability on code reviews, to e.g. visually separate sequential method calls with a line break in between them. But the first mentioned thing would already be tremendously useful.

Regarding the languages, it depends on the project. Mostly dart, php, javascript (typescript), python, swift(UI) and kotlin. In php for example, this could potentially be implented in a rather easy way by checking for the last character of a line being a semicolon? Just a thought too.

0

Hi there,

Did you have a chance to review IJPL-29940 and IJPL-190377? If so, do they match what you are looking for?
Regarding code styles, some languages already include options to add blank lines around methods. If certain languages are missing from Settings | Editor | Code Style, you can manually install the corresponding plugins (for example, PHP, Python, JavaScript and TypeScript). Kotlin and Dart do not have this functionality yet, but I can open new feature requests if you would like. 

0

Hi Petar, 

Thanks but the linked issues you provide are tickets that provide the same complaints without a solution..? 

Regarding your second comment, I have all of these plugins installed, they do not provide solutions to this.

0

Hi! Yes, they are open feature requests, but they have not been implemented yet. We use YouTrack to track requests like these, so I recommend following them to get notified about any progress.
As for the option to add blank lines between methods, I do have it available, as shown in the screenshot below. Does it look different for you?

0

Please sign in to leave a comment.