Home

Awesome

dfmt Build Status

dfmt is a formatter for D source code

Status

dfmt is beta quality. Make backups of your files or use source control when using the --inplace option.

Installation

Installing with DUB

> dub run dfmt -- -h

Building from source using Make

Building from source using dub

Using

By default, dfmt reads its input from stdin and writes to stdout. If a file name is specified on the command line, input will be read from the file instead, and output will be written to stdout.

dfmt uses EditorConfig files for configuration. If you run dfmt on a source file it will look for .editorconfig files that apply to that source file. If no file is specified on the command line, dfmt will look for .editorconfig files that would apply to a D file in the current working directory. Command line options can be used instead of .editorconfig files, or to override options found there.

Options

Example

dfmt --inplace --space_after_cast=false --max_line_length=80 \
    --soft_max_line_length=70 --brace_style=otbs file.d

Disabling formatting

Formatting can be temporarily disabled by placing the comments // dfmt off and // dfmt on around code that you do not want formatted.

void main(string[] args)
{
    bool optionOne, optionTwo, optionThree;

    // dfmt has no way of knowing that "getopt" is special, so it wraps the
    // argument list normally
    getopt(args, "optionOne", &optionOne, "optionTwo", &optionTwo, "optionThree", &optionThree);

    // dfmt off
    getopt(args,
        "optionOne", &optionOne,
        "optionTwo", &optionTwo,
        "optionThree", &optionThree);
    // dfmt on
}

Configuration

dfmt uses EditorConfig configuration files. dfmt-specific properties are prefixed with dfmt_.

Standard EditorConfig properties

Property NameAllowed ValuesDescription
end_of_linecr, crlf and lfSee EditorConfig documentation. When not set, dfmt adopts the first line ending in the input.
insert_final_newlinetrueNot supported. dfmt always inserts a final newline.
charsetUTF-8Not supported. dfmt only works correctly on UTF-8.
indent_styletab, spaceSee EditorConfig documentation.
indent_sizepositive integers (4)See EditorConfig documentation.
tab_widthpositive integers (4)See EditorConfig documentation.
trim_trailing_whitespacetrueNot supported. dfmt does not emit trailing whitespace.
max_line_lengthpositive integers (120)See EditorConfig documentation.

dfmt-specific properties

Property NameAllowed ValuesDescription
dfmt_brace_styleallman, otbs, stroustrup or knrSee Wikipedia
dfmt_soft_max_line_lengthpositive integers (80)The formatting process will usually keep lines below this length, but they may be up to max_line_length columns long.
dfmt_align_switch_statementstrue, falseAlign labels, cases, and defaults with their enclosing switch.
dfmt_outdent_attributes (Not yet implemented)true, falseDecrease the indentation level of attributes.
dfmt_split_operator_at_line_endtrue, falsePlace operators on the end of the previous line when splitting lines.
dfmt_space_after_casttrue, falseInsert space after the closing paren of a cast expression.
dfmt_space_after_keywords (Not yet implemented)true, falseInsert space after if, while, foreach, etc, and before the (.
dfmt_space_before_function_parameterstrue, falseInsert space before the opening paren of a function parameter list.
dfmt_selective_import_spacetrue, falseInsert space after the module name and before the : for selective imports.
dfmt_compact_labeled_statementstrue, falsePlace labels on the same line as the labeled switch, for, foreach, or while statement.
dfmt_template_constraint_styleconditional_newline_indent conditional_newline always_newline always_newline_indentControl the formatting of template constraints.
dfmt_single_template_constraint_indenttrue, falseSet if the constraints are indented by a single tab instead of two. Has only an effect if the style set to always_newline_indent or conditional_newline_indent.
dfmt_space_before_aa_colontrue, falseAdds a space after an associative array key before the : like in older dfmt versions.
dfmt_space_before_named_arg_colontrue, falseAdds a space after a named function argument or named struct constructor argument before the :.
dfmt_keep_line_breakstrue, falseKeep existing line breaks if these don't violate other formatting rules.
dfmt_single_indenttrue, falseSet if the code in parens is indented by a single tab instead of two.
dfmt_reflow_property_chainstrue, falseRecalculate the splitting of property chains into multiple lines.
dfmt_space_after_keywordstrue, falseInsert space after keywords (if,while,foreach,for, etc.).

Terminology