dfmt ~master

Dfmt is a formatter for D source code


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

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

  • Clone the repository
  • Run `git submodule update --init --recursive` in the dfmt directory
  • To compile with DMD, run `make in the dfmt directory. To compile with LDC, run make ldc instead. The generated binary will be placed in dfmt/bin/`.

Building from source using dub

  • Clone the repository
  • run dub build --build=release, optionally with --compiler=ldc2

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

  • --help | -h: Display command line options.
  • --inplace | -i: A file name is required and the file will be edited in-place.
  • --align_switch_statements: see dfmt_align_switch_statements [below](#dfmt-specific-properties)
  • --brace_style: see dfmt_brace_style [below](#dfmt-specific-properties)
  • --compact_labeled_statements: see dfmt_compact_labeled_statements [below](#dfmt-specific-properties)
  • --end_of_line: see end_of_line [below](#standard-editorconfig-properties)
  • --indent_size: see indent_size [below](#standard-editorconfig-properties)
  • --indent_style | -t: see indent_style [below](#standard-editorconfig-properties)
  • --max_line_length: see max_line_length [below](#standard-editorconfig-properties)
  • --outdent_attributes: see dfmt_outdent_attributes [below](#dfmt-specific-properties)
  • --selective_import_space: see dfmt_selective_import_space [below](#dfmt-specific-properties)
  • --single_template_constraint_indent: see dfmt_single_template_constraint_indent [below](#dfmt-specific-properties)
  • --soft_max_line_length: see dfmt_soft_max_line_length [below](#dfmt-specific-properties)
  • --space_after_cast: see dfmt_space_after_cast [below](#dfmt-specific-properties)
  • --space_before_aa_colon: see dfmt_space_before_aa_colon [below](#dfmt-specific-properties)
  • --space_before_named_arg_colon: see dfmt_space_before_named_arg_colon [below](#dfmt-specific-properties)
  • --space_before_function_parameters: see dfmt_space_before_function_parameters [below](#dfmt-specific-properties)
  • --split_operator_at_line_end: see dfmt_split_operator_at_line_end [below](#dfmt-specific-properties)
  • --tab_width: see tab_width [below](#standard-editorconfig-properties)
  • --template_constraint_style: see dfmt_template_constraint_style [below](#dfmt-specific-properties)
  • --keep_line_breaks: see dfmt_keep_line_breaks [below](#dfmt-specific-properties)
  • --single_indent: see dfmt_single_indent [below](#dfmt-specific-properties)
  • --reflow_property_chains: see dfmt_property_chains [below](#dfmt-specific-properties)
  • --space_after_keywords: see dfmt_space_after_keywords [below](#dfmt-specific-properties)

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
endoflinecr, crlf and lfSee EditorConfig documentation. When not set, dfmt adopts the first line ending in the input.
insertfinalnewline`true`Not supported. dfmt always inserts a final newline.
charset`UTF-8`Not supported. dfmt only works correctly on UTF-8.
indent_styletab, `space`See EditorConfig documentation.
indent_sizepositive integers (`4`)See EditorConfig documentation.
tab_widthpositive integers (`4`)See EditorConfig documentation.
trimtrailingwhitespace`true`Not supported. dfmt does not emit trailing whitespace.
maxlinelengthpositive integers (`120`)See EditorConfig documentation.

dfmt-specific properties

Property NameAllowed ValuesDescription
dfmtbracestyle`allman`, otbs, stroustrup or knrSee Wikipedia
dfmtsoftmaxlinelengthpositive integers (`80`)The formatting process will usually keep lines below this length, but they may be up to max_line_length columns long.
dfmtalignswitch_statements`true`, falseAlign labels, cases, and defaults with their enclosing switch.
dfmtoutdentattributes (Not yet implemented)`true`, falseDecrease the indentation level of attributes.
dfmtsplitoperatoratline_endtrue, `false`Place operators on the end of the previous line when splitting lines.
dfmtspaceafter_cast`true`, falseInsert space after the closing paren of a cast expression.
dfmtspaceafter_keywords (Not yet implemented)`true`, falseInsert space after if, while, foreach, etc, and before the (.
dfmtspacebeforefunctionparameterstrue, `false`Insert space before the opening paren of a function parameter list.
dfmtselectiveimport_space`true`, falseInsert space after the module name and before the : for selective imports.
dfmtcompactlabeled_statements`true`, falsePlace labels on the same line as the labeled switch, for, foreach, or while statement.
dfmttemplateconstraint_style`conditional_newline_indent` conditional_newline always_newline always_newline_indentControl the formatting of template constraints.
dfmtsingletemplateconstraintindenttrue, `false`Set 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.
dfmtspacebeforeaacolontrue, `false`Adds a space after an associative array key before the : like in older dfmt versions.
dfmtspacebeforenamedarg_colontrue, `false`Adds a space after a named function argument or named struct constructor argument before the :.
dfmtkeepline_breakstrue, `false`Keep existing line breaks if these don't violate other formatting rules.
dfmtsingleindenttrue, `false`Set if the code in parens is indented by a single tab instead of two.
dfmtreflowproperty_chains`true`, falseRecalculate the splitting of property chains into multiple lines.
dfmtspaceafter_keywords`true`, falseInsert space after keywords (if,while,foreach,for, etc.).

Terminology

  • Braces - { and }
  • Brackets - [ and ]
  • Parenthesis / Parens - ( and )
Dependencies:
libdparse
Versions:
0.15.2 2024-Aug-21
0.15.1 2023-Jul-19
0.15.0 2023-Apr-07
0.15.0-beta.2 2023-Feb-06
0.15.0-beta.1 2022-Oct-15
Show all 61 versions
Download Stats:
  • 21 downloads today

  • 111 downloads this week

  • 643 downloads this month

  • 109844 downloads total

Score:
4.7
Short URL:
dfmt.dub.pm