• Stars
    star
    202
  • Rank 189,245 (Top 4 %)
  • Language
    D
  • License
    Boost Software Li...
  • Created over 9 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Dfmt is a formatter for D source code

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
  • --brace_style: see dfmt_brace_style below
  • --compact_labeled_statements: see dfmt_compact_labeled_statements below
  • --end_of_line: see end_of_line below
  • --indent_size: see indent_size below
  • --indent_style | -t: see indent_style below
  • --max_line_length: see max_line_length below
  • --outdent_attributes: see dfmt_outdent_attributes below
  • --selective_import_space: see dfmt_selective_import_space below
  • --single_template_constraint_indent: see dfmt_single_template_constraint_indent below
  • --soft_max_line_length: see dfmt_soft_max_line_length below
  • --space_after_cast: see dfmt_space_after_cast below
  • --space_before_aa_colon: see dfmt_space_before_aa_colon below
  • --space_before_function_parameters: see dfmt_space_before_function_parameters below
  • --split_operator_at_line_end: see dfmt_split_operator_at_line_end below
  • --tab_width: see tab_width below
  • --template_constraint_style: see dfmt_template_constraint_style below
  • --keep_line_breaks: see dfmt_keep_line_breaks below
  • --single_indent: see dfmt_single_indent below
  • --reflow_property_chains: see dfmt_property_chains below
  • --space_after_keywords: see dfmt_space_after_keywords below

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 Name Allowed Values Description
end_of_line cr, crlf and lf See EditorConfig documentation. When not set, dfmt adopts the first line ending in the input.
insert_final_newline true Not supported. dfmt always inserts a final newline.
charset UTF-8 Not supported. dfmt only works correctly on UTF-8.
indent_style tab, space See EditorConfig documentation.
indent_size positive integers (4) See EditorConfig documentation.
tab_width positive integers (4) See EditorConfig documentation.
trim_trailing_whitespace true Not supported. dfmt does not emit trailing whitespace.
max_line_length positive integers (120) See EditorConfig documentation.

dfmt-specific properties

Property Name Allowed Values Description
dfmt_brace_style allman, otbs, stroustrup or knr See Wikipedia
dfmt_soft_max_line_length positive 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_statements true, false Align labels, cases, and defaults with their enclosing switch.
dfmt_outdent_attributes (Not yet implemented) true, false Decrease the indentation level of attributes.
dfmt_split_operator_at_line_end true, false Place operators on the end of the previous line when splitting lines.
dfmt_space_after_cast true, false Insert space after the closing paren of a cast expression.
dfmt_space_after_keywords (Not yet implemented) true, false Insert space after if, while, foreach, etc, and before the (.
dfmt_space_before_function_parameters true, false Insert space before the opening paren of a function parameter list.
dfmt_selective_import_space true, false Insert space after the module name and before the : for selective imports.
dfmt_compact_labeled_statements true, false Place labels on the same line as the labeled switch, for, foreach, or while statement.
dfmt_template_constraint_style conditional_newline_indent conditional_newline always_newline always_newline_indent Control the formatting of template constraints.
dfmt_single_template_constraint_indent true, 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.
dfmt_space_before_aa_colon true, false Adds a space after an associative array key before the : like in older dfmt versions.
dfmt_keep_line_breaks true, false Keep existing line breaks if these don't violate other formatting rules.
dfmt_single_indent true, false Set if the code in parens is indented by a single tab instead of two.
dfmt_reflow_property_chains true, false Recalculate the splitting of property chains into multiple lines.
dfmt_space_after_keywords true, false Insert space after keywords (if,while,foreach,for, etc.).

Terminology

  • Braces - { and }
  • Brackets - [ and ]
  • Parenthesis / Parens - ( and )

More Repositories

1

awesome-d

A curated list of awesome D documents, frameworks, libraries and software. Inspired by awesome-python.
D
640
star
2

DCD

The D Completion Daemon is an auto-complete program for the D programming language
D
345
star
3

D-Scanner

Swiss-army knife for D source code
D
236
star
4

SDLang-D

An SDLang (Simple Declarative Language) library for D
D
118
star
5

D-YAML

YAML parser and emitter for the D programming language
D
116
star
6

libdparse

Library for lexing and parsing D source code
D
113
star
7

containers

Containers backed by std.experimental.allocator
D
109
star
8

drepl

A REPL for D
D
76
star
9

d2sqlite3

A small wrapper around SQLite for the D programming language
C
74
star
10

mecca

Weka.IO's userland OS
D
64
star
11

setup-dlang

Github action for dlang compiler setup
TypeScript
45
star
12

dfix

Tool for automatically upgrading D source code
D
44
star
13

std_data_json

Phobos candidate JSON implementation.
D
25
star
14

dmarkdown

Markdown to HTML processor for D
D
24
star
15

d-mans

Collection of D-Man drawings from around the world
HTML
20
star
16

DGrammar

An attempt to completely and correctly document the grammar of the D programming language
HTML
20
star
17

harbored-mod

A documentation generator for D with support for both DDoc and Markdown.
D
18
star
18

dsymbol

Basic symbol lookup/resolution for libdparse (Deprecated, see link)
D
15
star
19

quic-d

Implementation of QUIC in D. See: https://github.com/dlang/projects/issues/84
Nix
14
star
20

experimental.xml

A replacement of Phobos std.xml
D
11
star
21

discussions

Get in touch with the DLang community
11
star
22

stdx-allocator

Extracted std.experimental.allocator for usage via DUB
D
9
star
23

d-money

Handling amounts of money safely and efficiently
D
9
star
24

artwork

Collection of D artwork from around the world
HTML
8
star
25

tinyendian

Lightweight endianness library for D
D
8
star
26

libddoc

D implementation of the DDoc macro system
D
7
star
27

toml

D
4
star
28

configy

An automatic YAML to struct configuration parser for dlang
D
3
star
29

d-apt

Debian APT repository for D tools
2
star
30

gitcompatibledubpackage

An example of a repositery usable both as a DUB package and a git submodule.
D
2
star
31

decimal

1
star