Skip to content

Formatting and Code Style

m0rkeulv edited this page Oct 25, 2026 · 4 revisions

The plugin formats Haxe code with the IDE's own Reformat Code action. Its rules follow haxe-formatter, the formatter that vshaxe (Haxe support for VS Code) uses, and it reads a project's hxformat.json. This page explains where the formatting rules come from, how hxformat.json files are used and the Haxe code style settings.

The most common tasks:

Where the formatting rules come from

Every Haxe file is formatted with one set of rules. The IDE picks it like this:

Situation Rules used
An hxformat.json sits in the file's folder or in a parent folder, up to the project's root folder, or up to the file system root for a file outside the project That hxformat.json. The nearest one wins.
No hxformat.json above the file, but you chose one with Use as Haxe Formatting Rules The chosen hxformat.json.
The hxformat.json that applies contains "disableFormatting": true, or is not valid JSON The code style scheme.
No hxformat.json applies, or the integration is turned off The code style scheme in Settings | Editor | Code Style | Haxe.

Format code as usual with Code | Reformat Code or Code | Reformat File…. See the JetBrains help on reformatting code for the general options.

The formatter was reworked in 2.0 toward the same output as haxe-formatter. With an hxformat.json, the IDE aims to format the way haxe-formatter 1.18 does with that file. Some haxe-formatter options have no equivalent in the IDE; see Settings that cannot be imported.

Tip

Want haxe-formatter's default style without changing anything else? Put an hxformat.json that contains only {} in your project's root folder. The IDE then formats every Haxe file below it with haxe-formatter's defaults.

Using a project's hxformat.json

You don't need to set anything up. When an hxformat.json applies to a file, the IDE uses it for that file instead of the scheme's Haxe settings. It does not create or change a code style scheme.

How the IDE reads the file:

  • It starts from haxe-formatter's built-in defaults and applies the keys in the file on top. A key you leave out keeps haxe-formatter's default, not the scheme's value. Most IDE options that haxe-formatter has no key for, such as Format inactive branches or Indent documentation comment lines, keep the scheme's value.
  • It searches the file's folder and then each parent folder, and stops at the project's root folder. For a file outside the project, such as a library's source, it searches up to the file system root. A config in a subfolder replaces one higher up, as with the command-line formatter.
  • It picks up changes to any file named hxformat.json as soon as they are saved.

For example, this file switches to two-space indentation and a 120-column limit, and keeps haxe-formatter's defaults for everything else:

{
	"indentation": {
		"character": "  ",
		"tabWidth": 2
	},
	"wrapping": {
		"maxLineLength": 120
	}
}

Note

The indent is the character string: a string of spaces indents by that many spaces, a tab by one tab. tabWidth only sets how wide a tab is shown. Other character values are not supported.

While a file is formatted by an hxformat.json, the code style indicator in the status bar shows hxformat. Its tooltip names the config file (Formatting rules from followed by the path). Click it for these actions:

  • Open hxformat.json opens the config that applies to the current file.
  • Disable hxformat.json overrides turns the integration off. The IDE then uses the scheme's settings again.

The hxformat code style indicator in the status bar

The hxformat indicator in the status bar

Differences from the command-line formatter

hxformat.json feature haxe-formatter The IDE
"disableFormatting": true Does not format the folder. Still formats, with the code style scheme's settings.
excludes Skips matching files. Ignored: matching files are formatted with the config.
Search for the config Up to the file system root. Up to the project's root folder, then the fallback config. For a file outside the project, up to the file system root.
Settings without an exact IDE equivalent Applied. Approximated or ignored, without a message. Import the file to see most of them.

A fallback hxformat.json

A Haxe file that has no hxformat.json above it can still use one from elsewhere, for example a shared config in a configs folder:

  1. Right-click the hxformat.json in the Project view, or in its editor.
  2. Choose Use as Haxe Formatting Rules. The menu item now shows a check mark.

Files that have their own hxformat.json above them keep using it. Choosing the fallback also turns the integration on. Choose the item again to clear the fallback; the integration stays on.

Turning the integration off

The integration is on by default. To format with the scheme's settings even where an hxformat.json exists:

  1. Open Settings | Editor | Code Style | Haxe and select the hxformat tab.
  2. Clear Use the project's hxformat.json when present.

This option belongs to the code style scheme, so it applies wherever that scheme is used. The status bar action Disable hxformat.json overrides clears the same option.

While the option is on and the project has an hxformat.json, or you chose a fallback config, the IDE's code style pages show the note Settings may be overridden by hxformat.json. Its Disable link turns the integration off, like the hxformat tab and the status bar action.

Importing an hxformat.json into a scheme

Importing copies the rules of an hxformat.json into a new code style scheme. Do this when you want to fine-tune the rules in the settings dialog, or use them for files that no hxformat.json covers.

  1. Open Settings | Editor | Code Style | Haxe.
  2. Click Show Scheme Actions (the gear icon next to the scheme) and choose Import Scheme | hxformat.json (haxe-formatter).
  3. Select the hxformat.json file.
  4. The IDE creates a new scheme named after the file (hxformat) and selects it. Rename it with Show Scheme Actions | Rename… if you like, then click OK or Apply to use it.

The import starts from haxe-formatter's defaults and then applies the file's keys, exactly like the automatic integration. When it finishes, a message ends with All hxformat.json settings were imported., or says how many settings could not be imported and lists them (setting(s) could not be imported:). The message shows the first ten; the IDE log (idea.log) lists all of them.

Note

An imported scheme does not replace the automatic integration. If the integration is on and a file has an hxformat.json above it, that file still follows the hxformat.json, not the scheme.

Settings that cannot be imported

The IDE reproduces most haxe-formatter options, but not all. The following are reported when you import a file. The automatic integration treats them the same way, but without a message.

  • Wrap settings for constructs that have no wrap setting in the IDE: wrapping.typeParameter, wrapping.metadataCallParameter, wrapping.casePattern and wrapping.anonType.
  • The rules of wrapping.methodChain, implementsExtends, functionSignature, anonFunctionSignature and callParameter. The IDE has one wrap setting per construct, which only approximates a list of rules.
  • wrapping.multiVar: only the split at a line length is reproduced.
  • Rules of wrapping.arrayWrap, mapWrap and objectLiteral that do not match one of the limits on the Wrap Rules tab. The limits that match are taken over.
  • Separate blank-line settings per kind of type, such as emptyLines.interfaceEmptyLines. All kinds of types share one set of blank-line settings in the IDE.
  • Spacing inside #if (...) conditions (whitespace.parenConfig.sharpConditionParens).
  • whitespace.formatStringInterpolation: false. The IDE always formats the code inside ${...}.
  • Values the IDE cannot reproduce, for example a setting the IDE supports with only one value when your file sets another one.
  • Keys the IDE does not know.

The rules of the operator chains (wrapping.opBoolChain and opAddSubChain) are handled like the literal rules: limits that match the Wrap Rules tab are taken over. Other chain rules are ignored, and they are not reported.

Editing hxformat.json

The plugin brings a JSON schema for hxformat.json, named hxformat.json (haxe-formatter). In any file named hxformat.json, the IDE completes keys and values and flags unknown keys and wrong values. The schema was written for haxe-formatter 1.18.0.

Running haxe-formatter itself

The IDE formatter is part of the plugin and needs nothing installed. When an hxformat.json sits in a Haxe file's folder or above it, the editor's context menu also offers Haxe Tools | Format File, Check Formatting and Show Formatting Diff, which run the formatter haxelib itself on the file. See Tools.

Code style settings

Open Settings | Editor | Code Style | Haxe. The page has the IDE's four standard tabs, with extra Haxe options, plus four tabs of its own (Wrap Rules, Imports, Conditional Compilation and hxformat):

Tab What it holds
Tabs and Indents Tabs or spaces, indent and continuation indent.
Spaces Standard spacing options plus the Haxe options below.
Wrapping and Braces Standard wrapping, brace and "keep when reformatting" options plus the Haxe options below.
Blank Lines Standard blank-line options plus the Haxe options below.
Wrap Rules The numeric limits that decide when operator chains, multi-variable declarations and literals wrap.
Imports Blank lines in the import section and import grouping.
Conditional Compilation How reformatting treats inactive #if branches.
hxformat Whether a project's hxformat.json overrides this scheme.

All tabs except hxformat show a preview of their options. For a full list of the IDE's settings pages, see Settings Reference.

Spaces

Group Haxe option
Around operators Arrow functions (x -> x)
Function types ((Int) -> Int)
Legacy function types (Int->Int)
Within Angle brackets (type parameters)
String interpolation '${' braces
Metadata parentheses
Other Before colon, after declaration name and After colon, before declaration type (type hints such as x:Int)
Around type check colon '(value : Type)'
Before object field colon and After object field colon ({x: 1})
After '//' in line comments: turns //text into // text. Divider lines and /// comments keep their shape.

Wrapping and Braces

The standard Extends/implements list option controls extends and implements clauses. The Haxe groups are:

Group Option What it does
Statement bodies (non-block) One drop-down per construct: 'if' body, 'else' body, 'for' body, 'while' body, 'do' body, 'try' body, 'catch' body, 'case' body, 'if' used as a value, 'try' used as a value, 'case' in a switch used as a value Where a body without braces goes: Next line, Same line or Keep as written. Default follows Keep when reformatting | Control statement in one line: checked keeps the body as written, cleared moves it to the next line.
Operator chains Indent wrapped chain one step (no alignment) Wrapped &&/|| and +/- chains continue one indent step deeper, as haxe-formatter does, instead of aligning.
Call arguments Fill as if written on one line (hxformat style) Joins the arguments and parameters, then breaks only where a line reaches the right margin. Line breaks you wrote inside the list are removed.
Expression body functions Place body on next line Moves the body of a named function written without braces (function f() return x;) to its own line.
Anonymous structures Structure extension '> Base' on own line Puts a > Base, extension in a multi-line structure type on its own line.
'return' statement Value on same line as 'return' Joins a return that you broke before its value.
Comments Indent documentation comment lines Indents the text lines of a doc comment one step inside the comment. Lines indented deeper than the others, such as nested Markdown lists, keep their extra indentation.
Reindent multi-line comment content (hxformat style) Reindents the inner lines of a /* ... */ comment as haxe-formatter does. Cleared, the inside of the comment stays as written.

Blank Lines

Group Haxe option
Keep maximum blank lines Between type declarations:, Between single-line types:, After block '{':, After case ':':, Between stacked block comments:
Minimum blank lines After using:, After file header comment:, Between static/visibility field groups:, Before a field's doc comment:, After a documented field:

Between static/visibility field groups: inserts blank lines where a run of variables changes from static to instance or from one visibility to another.

Wrap Rules

These limits mirror haxe-formatter's wrap rules, one section per rule (see the table below). Each field is a length in columns or a count of items, for example One item per line from this many items:.

A value of 0 turns that rule off. In the scheme, all of them are 0 by default, so the standard wrapping options decide alone. Under an hxformat.json, and after an import, they start at haxe-formatter's values:

Section haxe-formatter rule haxe-formatter's values
Boolean Operator Chains wrapping.opBoolChain Split when the line reaches 140; one operand per line when an operand reaches 40; one operand per line from 4 operands unless they total at most 120.
Additive Operator Chains wrapping.opAddSubChain The same with 160, 60, 4 and 120.
Multiple Variables in One 'var' wrapping.multiVar One per line when the line reaches 80, unless a declarator is at most 15.
Array Literals and Map Literals wrapping.arrayWrap, wrapping.mapWrap One line when the items total at most 80; fill after a leading break with at least 10 items of one length up to 30, or of at most 10 each; one item per line when an item reaches 30 or from 4 items.
Object Literals wrapping.objectLiteral One line up to 3 fields if it fits; one field per line when a field reaches 30, the fields total 60 or from 4 fields.

Imports

Option What it does
Blank lines between import groups (0 = no grouping): Above 0, imports are grouped by package and the groups are separated by this many blank lines.
Group by first package segments: How many leading package segments two imports must share to be in the same group.
Between imports: The most blank lines kept inside the import section when grouping is off. 0 makes it one block.

Code | Optimize Imports works on files in source folders. It removes unused imports, then sorts the imports alphabetically if nothing else (a comment, a using statement or code) stands between them. using statements are neither removed nor sorted.

Conditional Compilation

Option Default What it does
Format inactive branches On Formats the code in inactive #if branches with the same rules as active code, as long as the branch parses cleanly.
Align unformatted branches to their directives Off Shifts each inactive branch that is not formatted (because it does not parse, or because Format inactive branches is cleared) as a whole, so that it lines up with its #if/#else line. The lines keep their indentation relative to each other.

With both options cleared, inactive branches stay exactly as written. Under an hxformat.json, Align unformatted branches to their directives is on, matching haxe-formatter. Which branches are inactive is explained in Conditional Compilation.

hxformat

This tab holds the Use the project's hxformat.json when present option. See Turning the integration off.

haxe-formatter's defaults

Without an hxformat.json, the scheme's own settings apply, and they are not haxe-formatter's defaults. Under an hxformat.json (or after an import), the settings that the file does not set follow haxe-formatter's defaults, including:

  • Indentation with tabs, 4 columns wide. Wrapped declaration headers continue two indent steps deeper.
  • A right margin of 160 columns.
  • Opening braces at the end of the line; empty braces {} stay on one line.
  • else, catch and the while of do ... while on the same line as the closing brace.
  • Bodies without braces of if, else, for, while, do, try, catch and case on their own line. An if or try used as a value stays on one line.
  • At most one blank line anywhere; one blank line between functions and between types. No blank line is added between variables, and blank lines right after an opening brace are removed.
  • Spaces around binary operators and around ->, except in old-style function types (Int->Int). No spaces around the colon of a type hint (x:Int).
  • //comment becomes // comment.

Reformatting twice

By default, reformatting keeps line breaks you wrote (Keep when reformatting | Line breaks on the Wrapping and Braces tab). Haxe supports the IDE's second-pass behavior: reformat again and the IDE offers to remove the custom line breaks it kept. This is controlled by the IDE option Reformat again to remove custom line breaks in Settings | Editor | General | Smart Keys.

Clone this wiki locally