Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reflow_property_chains option #577

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ found there.
* `--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)*

### Example
```
Expand Down Expand Up @@ -117,6 +119,7 @@ dfmt_single_template_constraint_indent | `true`, **`false`** | Set if the constr
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.

## Terminology
* Braces - `{` and `}`
Expand Down
3 changes: 3 additions & 0 deletions src/dfmt/config.d
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ struct Config
OptionalBoolean dfmt_keep_line_breaks;
///
OptionalBoolean dfmt_single_indent;
///
OptionalBoolean dfmt_reflow_property_chains;

mixin StandardEditorConfigFields;

Expand Down Expand Up @@ -93,6 +95,7 @@ struct Config
dfmt_space_before_aa_colon = OptionalBoolean.f;
dfmt_keep_line_breaks = OptionalBoolean.f;
dfmt_single_indent = OptionalBoolean.f;
dfmt_reflow_property_chains = OptionalBoolean.t;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/dfmt/formatter.d
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,8 @@ private:
break;
case tok!".":
regenLineBreakHintsIfNecessary(index);
immutable bool ufcsWrap = astInformation.ufcsHintLocations.canFindIndex(current.index);
immutable bool ufcsWrap = config.dfmt_reflow_property_chains == OptionalBoolean.t
&& astInformation.ufcsHintLocations.canFindIndex(current.index);
if (ufcsWrap || linebreakHints.canFind(index) || onNextLine
|| (linebreakHints.length == 0 && currentLineLength + nextTokenLength() > config.max_line_length))
{
Expand Down
7 changes: 6 additions & 1 deletion src/dfmt/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ else
case "single_indent":
optConfig.dfmt_single_indent = optVal;
break;
case "reflow_property_chains":
optConfig.dfmt_reflow_property_chains = optVal;
break;
default:
assert(false, "Invalid command-line switch");
}
Expand Down Expand Up @@ -129,7 +132,8 @@ else
"tab_width", &optConfig.tab_width,
"template_constraint_style", &optConfig.dfmt_template_constraint_style,
"keep_line_breaks", &handleBooleans,
"single_indent", &handleBooleans);
"single_indent", &handleBooleans,
"reflow_property_chains", &handleBooleans);
// dfmt on
}
catch (GetOptException e)
Expand Down Expand Up @@ -341,6 +345,7 @@ Formatting Options:
--template_constraint_style
--space_before_aa_colon
--single_indent
--reflow_property_chains
`,
optionsToString!(typeof(Config.dfmt_template_constraint_style)));
}
Expand Down
15 changes: 15 additions & 0 deletions tests/allman/issue0503.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
string f()
{
return duration.total!"seconds".to!string;
}

string g()
{
return duration.total!"seconds"().to!string;
}

string h()
{
return duration.total!"seconds"().to!string.to!string.to!string.to!string.to!string.to!string
.to!string.to!string.to!string;
}
1 change: 1 addition & 0 deletions tests/issue0503.args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--reflow_property_chains=false
14 changes: 14 additions & 0 deletions tests/issue0503.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
string f()
{
return duration.total!"seconds".to!string;
}

string g()
{
return duration.total!"seconds"().to!string;
}

string h()
{
return duration.total!"seconds"().to!string.to!string.to!string.to!string.to!string.to!string.to!string.to!string.to!string;
}
15 changes: 15 additions & 0 deletions tests/knr/issue0503.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
string f()
{
return duration.total!"seconds".to!string;
}

string g()
{
return duration.total!"seconds"().to!string;
}

string h()
{
return duration.total!"seconds"().to!string.to!string.to!string.to!string.to!string.to!string
.to!string.to!string.to!string;
}
12 changes: 12 additions & 0 deletions tests/otbs/issue0503.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
string f() {
return duration.total!"seconds".to!string;
}

string g() {
return duration.total!"seconds"().to!string;
}

string h() {
return duration.total!"seconds"().to!string.to!string.to!string.to!string.to!string.to!string
.to!string.to!string.to!string;
}