From bef02a3b55e5b87e916d7255643ea9f2e5712267 Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Mon, 8 Jun 2015 03:05:11 -0700 Subject: [PATCH] Implement #130 --- README.md | 2 -- src/dfmt/config.d | 2 -- src/dfmt/formatter.d | 24 +++++++++++++++++------- src/dfmt/indentation.d | 5 +++-- tests/allman/issue0130.d.ref | 11 +++++++++++ tests/issue0130.args | 1 + tests/issue0130.d | 11 +++++++++++ tests/otbs/issue0130.d.ref | 10 ++++++++++ 8 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 tests/allman/issue0130.d.ref create mode 100644 tests/issue0130.args create mode 100644 tests/issue0130.d create mode 100644 tests/otbs/issue0130.d.ref diff --git a/README.md b/README.md index ffa63ee..d2cb219 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,6 @@ 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 -* **--outdent_labels**: See **dfmt_outdent_labels** below * **--space_after_cast**: See **dfmt_space_after_cast** below * **--split_operator_at_line_end**: See **dfmt_split_operator_at_line_end** below * **--tab_width**: See **tab_width** below @@ -90,7 +89,6 @@ Property Name | Allowed Values | Default Value | Description --------------|----------------|---------------|------------ dfmt_brace_style | `allman`, `otbs`, or `stroustrup` | `allman` | [See Wikipedia](https://en.wikipedia.org/wiki/Brace_style) 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_outdent_labels (Not yet implemented) | `true`, `false` | `true` | Decrease the indentation of labels dfmt_align_switch_statements (Not yet implemented) | `true`, `false` | `true` | Align labels, cases, and defaults with their enclosing switch dfmt_outdent_attributes (Not yet implemented) | `true`, `false` | `true` | Decrease the indentation level of attributes dfmt_split_operator_at_line_end | `true`, `false` | `false` | Place operators on the end of the previous line when splitting lines diff --git a/src/dfmt/config.d b/src/dfmt/config.d index 4f8b450..6127be5 100644 --- a/src/dfmt/config.d +++ b/src/dfmt/config.d @@ -29,8 +29,6 @@ struct Config /// OptionalBoolean dfmt_outdent_attributes; /// - OptionalBoolean dfmt_outdent_labels; - /// int dfmt_soft_max_line_length = -1; /// OptionalBoolean dfmt_space_after_cast; diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index d762537..d4f228c 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -475,6 +475,8 @@ private: { if (isCase && !indents.topIs(tok!"case") && config.dfmt_align_switch_statements == OptionalBoolean.f) indents.push(tok!"case"); + else if (isAttribute && !indents.topIs(tok!"@") && config.dfmt_outdent_attributes == OptionalBoolean.f) + indents.push(tok!"@"); newline(); } @@ -1120,7 +1122,7 @@ private: else if (currentIs(tok!"}")) { indents.popTempIndents(); - if (indents.topIs(tok!"case")) + while (indents.topIs(tok!"case") || indents.topIs(tok!"@")) indents.pop(); if (indents.topIs(tok!"{")) { @@ -1144,16 +1146,24 @@ private: } else if (astInformation.attributeDeclarationLines.canFindIndex(current.line)) { - immutable l = indents.indentToMostRecent(tok!"{"); - if (l != -1) - indentLevel = l; + if (config.dfmt_outdent_attributes == OptionalBoolean.t) + { + immutable l = indents.indentToMostRecent(tok!"{"); + if (l != -1) + indentLevel = l; + } + else + { + if (indents.topIs(tok!"@")) + indents.pop(); + indentLevel = indents.indentLevel; + } } else { - while (indents.topIsTemp() && (peekBackIsOneOf(true, tok!"}", tok!";") && indents.top != tok!";")) - { + while (indents.topIsTemp() && (peekBackIsOneOf(true, tok!"}", tok!";") + && indents.top != tok!";")) indents.pop(); - } indentLevel = indents.indentLevel; } indent(); diff --git a/src/dfmt/indentation.d b/src/dfmt/indentation.d index 8369cac..861c595 100644 --- a/src/dfmt/indentation.d +++ b/src/dfmt/indentation.d @@ -12,7 +12,8 @@ import std.d.lexer; */ bool isWrapIndent(IdType type) pure nothrow @nogc @safe { - return type != tok!"{" && type != tok!"case" && type != tok!"]" && isOperator(type); + return type != tok!"{" && type != tok!"case" && type != tok!"@" + && type != tok!"]" && isOperator(type); } /** @@ -20,7 +21,7 @@ bool isWrapIndent(IdType type) pure nothrow @nogc @safe */ bool isTempIndent(IdType type) pure nothrow @nogc @safe { - return type != tok!"{" && type != tok!"case"; + return type != tok!"{" && type != tok!"case" && type != tok!"@"; } /** diff --git a/tests/allman/issue0130.d.ref b/tests/allman/issue0130.d.ref new file mode 100644 index 0000000..dcaa691 --- /dev/null +++ b/tests/allman/issue0130.d.ref @@ -0,0 +1,11 @@ +class SomeClass +{ + public: + int x; + int y; + private: + int z; +} + +public: + void doStuff(); diff --git a/tests/issue0130.args b/tests/issue0130.args new file mode 100644 index 0000000..c73b709 --- /dev/null +++ b/tests/issue0130.args @@ -0,0 +1 @@ +--outdent_attributes=false diff --git a/tests/issue0130.d b/tests/issue0130.d new file mode 100644 index 0000000..09b70bd --- /dev/null +++ b/tests/issue0130.d @@ -0,0 +1,11 @@ +class SomeClass +{ + public: + int x; + int y; + private: + int z; +} + +public: + void doStuff(); diff --git a/tests/otbs/issue0130.d.ref b/tests/otbs/issue0130.d.ref new file mode 100644 index 0000000..616293a --- /dev/null +++ b/tests/otbs/issue0130.d.ref @@ -0,0 +1,10 @@ +class SomeClass { + public: + int x; + int y; + private: + int z; +} + +public: + void doStuff();