COMMUNITY

New formatter haxelib library


(Alexander Blum) #1

Does your code look old?
Has your code lost its shininess?
Is your whitespace all over the place?
Do you use tabs and/or spaces for indentation?
Have you messed up your curlys?
Do you use empty lines?
Do you like it when format happens in your code?


Then why not try haxelib run formatter -s folderWithDustyOldCode? (*)

Out now: formatter - available at your local haxelib store.

Check out our low low prices!

For a limited(**) time only: free VSCode integration!

(*) use backups; diff tool recommended
(**) for very very broad values of limited


(Alexander Blum) #2

For anyone interested in seeing what the default built-in hxformat.json configuration file looks like, here it is: https://github.com/HaxeCheckstyle/haxe-formatter/blob/master/resources/default-hxformat.json (note: dev version differs from 1.0.0 release)


(Valentin Lemière) #3

Seems there is already a good amount of configuration option :+1:

Something that would be really awesome is an online tool (or a vscode extension) similar to https://zed0.co.uk/clang-format-configurator/ to create your own config easily.


(Alexander Blum) #4

Formatter has lots of options, but obviously we haven’t tested every combination of options, most unittests use built-in defaults, so there might be some side effects when configuring your personal code style.

A configuration tool would be really cool, e.g. it could be a HaxeUI based VSCode extension (maybe with multiple tabs for different types of configuration options), that has a split view showing a live preview of formatting results. All we need is a volunteer… :slight_smile:

For now we have auto-completion in VSCode through JSON schema.


(Alexander Blum) #5

For anyone not running VSCode, I have heard from someone using the command line version from within HaxeDevelop to format files on demand. I’m not using HaxeDevelop, so I don’t know how to set it up or how well you can integrate it with your workflow.
That might also work for other editors.

It helps to have NodeJS installed, since haxelib run formatter or neko <formatter>\run.n will try to run node run.js - the NodeJS version is a lot faster than the Neko version (a C++ version is only slightly faster compared to NodeJS).


(Jason O'neil) #6

Finally got a chance to use this - it’s amazing! Thanks so much, this was something I’ve learned to love in other languages that I really missed in Haxe.


(Alexander Blum) #7

The latest vshaxe extension (2.4.1) brings haxe-formatter 1.1.0 to VSCode which offers additional emptyLines settings and the ability to wrap method chains (and a few other changes).

If you have a hxformat.json containing custom wrapping rules, you will have to adjust them from e.g.:

{
  "wrapping": {
    "anonType": [{"conditions": [{"cond": "itemCount <= n", "value": 3}], "type": "noWrap"}]
  }
}

to

 
{
  "wrapping": {
    "anonType": {
      "defaultWrap": "noWrap",
      "rules": [{"conditions": [{"cond": "itemCount <= n", "value": 3}], "type": "noWrap"}]
    }
  }
}

(Alexander Blum) #8

I have released haxe-formatter 1.1.2 yesterday (for VSCode users: it’s already included with vshaxe 2.4.4).
It brings a few bugfixes, e.g. wrapping with “keep” should now work as intended, and indentation logic should give nicer results for cases where left curly is on “next” line instead of “same”.

I also added a small section to README.md explaining how to start using formatter in your projects.


(Alexander Blum) #9

Skipping a post for haxe-formatter 1.2.0, because I released formatter 1.3.0 earlier (for those using VSCode, there is vshaxe 2.4.5 out now as well).

There are some breaking changes in that new version, if you were using custom values for

  • whitespace.openingParenPolicy
  • whitespace.closingParenPolicy
  • whitespace.openingBracePolicy
  • whitespace.closingBracePolicy
  • whitespace.objectOpeningBracePolicy
  • whitespace.objectClosingBracePolicy

you need to update your configuration and use the new whitespace.parenConfig and whitespace.bracesConfig options.
There is a full dump of all built-in default options under resources/default-hxformat.json inside your haxelib folder (you can also generate your own version by running haxelib run formatter --default-config <filename>).
Again VSCode users should see JSON schema support for hxformat.json files, giving them suggestions and autocomplete when editing formatter settings.

Also if people were having issues where formatter would “eat” or destroy their code, version 1.3.0 has a new safeguard that should detect and prevent such behaviour. Formatter should skip formatting a file before destroying your code (better no formatting than destructive formatting).
If you have samples where formatter fails to format or destroys code, please open an issue on github (https://github.com/HaxeCheckstyle/haxe-formatter).


(Alexander Blum) #10

haxe-formatter 1.4.0 is out now either standalone as haxelib or integrated into VSCode through vshaxe 2.5.1 (also released earlier).

Among many bugfixes formatter now has a new wrapping option for extends/implements (wrapping.implementsExtends) and two new empty lines settings for file header and between multiline comments (emptylines.afterFileHeaderComment and emptylines.betweenMultilineComments).

Defaults for wrapping.opBoolChain have changed slightly, wrapping should occur less frequent than before and it will now wrap before || and &&, e.g.:

dirty = dirty
        || (Tilemap.x != _prevTilemapX)
        || ((Tilemap.y != _prevTilemapY)
                || (Tilemap.scale.x != _prevTilemapScaleX)
                || (Tilemap.scale.x != _prevTilemapScaleX)
                || (Tilemap.scale.x != _prevTilemapScaleX))
        && (Tilemap.scale.x != _prevTilemapScaleX);

(Alexander Blum) #11

Version 1.5.1 has a ton of bugfixes and comes with a few additional settings, such as:

  • wrapping.opAddSubChain provides wrapping rules for +/- chains, e.g. string concatenation or large math equations
  • wrapping.metadataCallParameter wraps metadata parameter, e.g. when you have a large list of @:forward parameters
  • emptyLines.macroClassEmptyLines empty line options for macro class {}
  • emptyLines.lineCommentsBetweenTypes empty line option for line comments (//) before, after or between type definitions
  • emptyLines.lineCommentsBetweenFunctions empty line option for line comments (//) before, after or between function definitions
  • whitespace.addLineCommentSpace controls whether to add a space after // or not, (keep //TODO or convert it to // TODO), no change if there is whitespace after //

Formatter 1.5.1 is included with vshaxe 2.7.0 which was released to marketplace a few hours ago.