[SOLVED] Looking for automated code formatting

Hi there,

In order to get my code files nicer and cleaner I’m looking for a tool which can format the code (almost) automatically.

To get code like the one below nice, it takes some time when doing it manually :frowning:

Before:

After:

Maybe someone can point me to such a tool (especially for VSCode).

Thanks,
Selter

2 Likes

There is a addin from @mhilbush at github https://github.com/mhilbush/openhab-formatter

1 Like

Yes, but only if he’s ok with a multiline format…

items

Thats true, but to be honest for me the multiline format is the better format

1 Like

According to the readme

The ReformatItem command only reformats one line at a time. I haven’t gotten around to making it work on a selection and/or the entire file.

Does that mean that one need to reformat the file line by line?

And how to install the plugin for VSCode on Windows 10?

Afraid so. Sorry. But it does go pretty quickly if you use the keyboard shortcut to invoke the reformatter. :wink:

Download the latest extension (version 0.0.6). In VS Code, go to the Extensions panel, then select Install from VSIX… from the More Actions… menu.

So I only need this file “openhab-formatter-0.0.6.vsix” from the downloaded ZIP-archive?

Yes.

Thanks for your support - works fine.

It would be great if there will be the ability to format multiple lines someday :wink:

I have a version that reformats all lines in the file, but it hasn’t been tested extensively. It doesn’t properly handle items that are already reformatted. But, if you have a file that contains no reformatted items, you’re welcome to give it a try.

Do you mean with “already preformatted” multiple lines per item?

Yes, it doesn’t like it if the file already contains item definitions that span multiple lines. I haven’t yet figured out how to skip those.

Could you please provide this version?
It would help me a lot.

https://github.com/mhilbush/openhab-formatter/blob/master/openhab-formatter-0.0.7.vsix

2 Likes

This may be a total non sequitur, but is there any way to port this to Atom?

Not easily given that the extension uses the VS Code API.

Hi @mhilbush,
thank you very much for your add-on.
One little bug:
When a group definition contains a space, e.g. (note the space after OPEN,):

Group:Contact:OR(OPEN, CLOSED) gTest

it will be formatted to:

Group:Contact:OR(OPEN,      CLOSED
                            (OPEN, CLOSED)

also the group name is omitted.

Thanks.

Version 0.0.8 hopefully will fix this.

The Code Alignment extension was my favorite in Visual Studio that would handle this formatting easily. It has been ported to vscode, but not all functionality is there yet. In the current vscode version it takes a few keystrokes, but it does work well.

Here’s the VS Marketplace link:

https://marketplace.visualstudio.com/items?itemName=cpmcgrath.codealignment-vscode&ssr=false#overview