From b01c624ef0abed12a3779fb21c4a3484e4c5204e Mon Sep 17 00:00:00 2001 From: BBasile Date: Fri, 2 Mar 2018 16:26:54 +0100 Subject: [PATCH] fix #236 - Allow constraints to be indented by a single tab (#337) fix #236 - Allow constraints to be indented by a single tab merged-on-behalf-of: BBasile --- .travis.sh | 3 --- README.md | 2 ++ dub.json | 4 +++- libdparse | 2 +- src/dfmt/config.d | 3 +++ src/dfmt/formatter.d | 11 ++++++++--- src/dfmt/main.d | 5 +++++ tests/allman/constraint_singe_tab.d.ref | 9 +++++++++ tests/allman/constraint_singe_tab2.d.ref | 10 ++++++++++ tests/constraint_singe_tab.args | 2 ++ tests/constraint_singe_tab.d | 4 ++++ tests/constraint_singe_tab2.args | 2 ++ tests/constraint_singe_tab2.d | 4 ++++ tests/otbs/constraint_singe_tab.d.ref | 7 +++++++ tests/otbs/constraint_singe_tab2.d.ref | 8 ++++++++ 15 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 tests/allman/constraint_singe_tab.d.ref create mode 100644 tests/allman/constraint_singe_tab2.d.ref create mode 100644 tests/constraint_singe_tab.args create mode 100644 tests/constraint_singe_tab.d create mode 100644 tests/constraint_singe_tab2.args create mode 100644 tests/constraint_singe_tab2.d create mode 100644 tests/otbs/constraint_singe_tab.d.ref create mode 100644 tests/otbs/constraint_singe_tab2.d.ref diff --git a/.travis.sh b/.travis.sh index 3812c45..fa34497 100755 --- a/.travis.sh +++ b/.travis.sh @@ -4,9 +4,6 @@ set -e if [[ $BUILD == dub ]]; then dub build --build=release - - mkdir bin - mv dfmt ./bin elif [[ $DC == ldc2 ]]; then git submodule update --init --recursive make ldc -j2 diff --git a/README.md b/README.md index 022a98b..b97e46f 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ found in .editorconfig files. * **--max_line_length**: See **max_line_length** below * **--soft_max_line_length**: See **dfmt_soft_max_line_length** below * **--outdent_attributes**: See **dfmt_outdent_attributes** below +* **--single_template_constraint_indent**: See **dfmt_template_constraint_style** below * **--space_after_cast**: See **dfmt_space_after_cast** 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 @@ -105,6 +106,7 @@ dfmt_space_before_function_parameters | `true`, `false` | `false` | Insert space dfmt_selective_import_space | `true`, `false` | `true` | Insert space after the module name and before the `:` for selective imports. dfmt_compact_labeled_statements | `true`, `false` | `true` | 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` | `conditional_newline_indent` | Control the formatting of template constraints. +dfmt_single_template_constraint_indent | `true`, `false` | `false` | Set if the constraints are indented by a single tab instead of two. Has only an effect for if indentation style if set to `always_newline_indent` or `conditional_newline_indent`. ## Terminology * Braces - `{` and `}` diff --git a/dub.json b/dub.json index 40015f9..9d949a6 100644 --- a/dub.json +++ b/dub.json @@ -5,5 +5,7 @@ "license": "BSL-1.0", "dependencies": { "libdparse": "~>0.8.0-alpha.5" - } + }, + "targetPath" : "bin/", + "targetName" : "dfmt", } diff --git a/libdparse b/libdparse index ee0fa01..687c0ca 160000 --- a/libdparse +++ b/libdparse @@ -1 +1 @@ -Subproject commit ee0fa01ab74b6bf27bed3c7bdb9d6fb789963342 +Subproject commit 687c0ca751747ebe498c183da1a3ee3119d57932 diff --git a/src/dfmt/config.d b/src/dfmt/config.d index 9dcc8fe..a9e59a3 100644 --- a/src/dfmt/config.d +++ b/src/dfmt/config.d @@ -53,6 +53,8 @@ struct Config OptionalBoolean dfmt_compact_labeled_statements; /// TemplateConstraintStyle dfmt_template_constraint_style; + /// + OptionalBoolean dfmt_single_template_constraint_indent; mixin StandardEditorConfigFields; @@ -79,6 +81,7 @@ struct Config dfmt_selective_import_space = OptionalBoolean.t; dfmt_compact_labeled_statements = OptionalBoolean.t; dfmt_template_constraint_style = TemplateConstraintStyle.conditional_newline_indent; + dfmt_single_template_constraint_indent = OptionalBoolean.f; } /** diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index 9112aa1..d2579e9 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -324,6 +324,7 @@ private: void formatConstraint() { + import dfmt.editorconfig : OB = OptionalBoolean; with (TemplateConstraintStyle) final switch (config.dfmt_template_constraint_style) { case unspecified: @@ -342,15 +343,19 @@ private: immutable l = currentLineLength + betweenParenLength(tokens[index + 1 .. $]); if (l > config.dfmt_soft_max_line_length) { - pushWrapIndent(tok!"!"); + config.dfmt_single_template_constraint_indent == OB.t ? + pushWrapIndent() : pushWrapIndent(tok!"!"); newline(); } else if (peekBackIs(tok!")")) write(" "); break; case always_newline_indent: - pushWrapIndent(tok!"!"); - newline(); + { + config.dfmt_single_template_constraint_indent == OB.t ? + pushWrapIndent() : pushWrapIndent(tok!"!"); + newline(); + } break; } // if diff --git a/src/dfmt/main.d b/src/dfmt/main.d index 30548d8..97088c9 100644 --- a/src/dfmt/main.d +++ b/src/dfmt/main.d @@ -113,6 +113,9 @@ else case "compact_labeled_statements": optConfig.dfmt_compact_labeled_statements = optVal; break; + case "single_template_constraint_indent": + optConfig.dfmt_single_template_constraint_indent = optVal; + break; default: assert(false, "Invalid command-line switch"); } @@ -139,6 +142,7 @@ else "space_before_function_parameters", &handleBooleans, "split_operator_at_line_end", &handleBooleans, "compact_labeled_statements", &handleBooleans, + "single_template_constraint_indent", &handleBooleans, "tab_width", &optConfig.tab_width, "template_constraint_style", &optConfig.dfmt_template_constraint_style); // dfmt on @@ -329,6 +333,7 @@ Formatting Options: --space_after_cast --space_before_function_parameters --selective_import_space + --single_template_constraint_indent --split_operator_at_line_end --compact_labeled_statements --template_constraint_style diff --git a/tests/allman/constraint_singe_tab.d.ref b/tests/allman/constraint_singe_tab.d.ref new file mode 100644 index 0000000..176e0f4 --- /dev/null +++ b/tests/allman/constraint_singe_tab.d.ref @@ -0,0 +1,9 @@ +void foo()() + if (dogs && pigs && birds && ants && foxes && flies && cats && bugs && bees + && cows && sheeps && monkeys && whales) +{ +} + +void foo()() if (dogs && pigs && birds) +{ +} diff --git a/tests/allman/constraint_singe_tab2.d.ref b/tests/allman/constraint_singe_tab2.d.ref new file mode 100644 index 0000000..59c0cb1 --- /dev/null +++ b/tests/allman/constraint_singe_tab2.d.ref @@ -0,0 +1,10 @@ +void foo()() + if (dogs && pigs && birds && ants && foxes && flies && cats && bugs && bees + && cows && sheeps && monkeys && whales) +{ +} + +void foo()() + if (dogs && pigs && birds) +{ +} diff --git a/tests/constraint_singe_tab.args b/tests/constraint_singe_tab.args new file mode 100644 index 0000000..2797662 --- /dev/null +++ b/tests/constraint_singe_tab.args @@ -0,0 +1,2 @@ +--template_constraint_style=conditional_newline_indent +--single_template_constraint_indent=true diff --git a/tests/constraint_singe_tab.d b/tests/constraint_singe_tab.d new file mode 100644 index 0000000..097b267 --- /dev/null +++ b/tests/constraint_singe_tab.d @@ -0,0 +1,4 @@ +void foo()() if (dogs && pigs && birds && ants && foxes && flies && cats && bugs && bees && cows && sheeps && monkeys && whales) +{} + +void foo()() if (dogs && pigs && birds) {} diff --git a/tests/constraint_singe_tab2.args b/tests/constraint_singe_tab2.args new file mode 100644 index 0000000..608415d --- /dev/null +++ b/tests/constraint_singe_tab2.args @@ -0,0 +1,2 @@ +--template_constraint_style=always_newline_indent +--single_template_constraint_indent=true diff --git a/tests/constraint_singe_tab2.d b/tests/constraint_singe_tab2.d new file mode 100644 index 0000000..097b267 --- /dev/null +++ b/tests/constraint_singe_tab2.d @@ -0,0 +1,4 @@ +void foo()() if (dogs && pigs && birds && ants && foxes && flies && cats && bugs && bees && cows && sheeps && monkeys && whales) +{} + +void foo()() if (dogs && pigs && birds) {} diff --git a/tests/otbs/constraint_singe_tab.d.ref b/tests/otbs/constraint_singe_tab.d.ref new file mode 100644 index 0000000..39fb8eb --- /dev/null +++ b/tests/otbs/constraint_singe_tab.d.ref @@ -0,0 +1,7 @@ +void foo()() + if (dogs && pigs && birds && ants && foxes && flies && cats && bugs && bees + && cows && sheeps && monkeys && whales) { +} + +void foo()() if (dogs && pigs && birds) { +} diff --git a/tests/otbs/constraint_singe_tab2.d.ref b/tests/otbs/constraint_singe_tab2.d.ref new file mode 100644 index 0000000..975bf28 --- /dev/null +++ b/tests/otbs/constraint_singe_tab2.d.ref @@ -0,0 +1,8 @@ +void foo()() + if (dogs && pigs && birds && ants && foxes && flies && cats && bugs && bees + && cows && sheeps && monkeys && whales) { +} + +void foo()() + if (dogs && pigs && birds) { +}