Skip to content

Commit

Permalink
Add single_indent option
Browse files Browse the repository at this point in the history
  • Loading branch information
belka-ew committed May 18, 2021
1 parent e79ba9f commit ea36fbf
Show file tree
Hide file tree
Showing 13 changed files with 296 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ dfmt_template_constraint_style | **`conditional_newline_indent`** `conditional_n
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.

## 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 @@ -59,6 +59,8 @@ struct Config
OptionalBoolean dfmt_space_before_aa_colon;
///
OptionalBoolean dfmt_keep_line_breaks;
///
OptionalBoolean dfmt_single_indent;

mixin StandardEditorConfigFields;

Expand Down Expand Up @@ -88,6 +90,7 @@ struct Config
dfmt_single_template_constraint_indent = OptionalBoolean.f;
dfmt_space_before_aa_colon = OptionalBoolean.f;
dfmt_keep_line_breaks = OptionalBoolean.f;
dfmt_single_indent = OptionalBoolean.f;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/dfmt/formatter.d
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ struct TokenFormatter(OutputRange)
this.output = output;
this.astInformation = astInformation;
this.config = config;
this.indents = IndentStack(config);

{
auto eol = config.end_of_line;
Expand Down
26 changes: 24 additions & 2 deletions src/dfmt/indentation.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

module dfmt.indentation;

import dfmt.config;
import dfmt.editorconfig;
import dparse.lexer;

import std.bitmanip : bitfields;
Expand All @@ -31,6 +33,14 @@ bool isTempIndent(IdType type) pure nothrow @nogc @safe
*/
struct IndentStack
{
/// Configuration
private const Config* config;

this(const Config* config)
{
this.config = config;
}

static struct Details
{
mixin(bitfields!(
Expand Down Expand Up @@ -221,7 +231,7 @@ struct IndentStack
/**
* Dumps the current state of the indentation stack to `stderr`. Used for debugging.
*/
void dump(size_t pos = size_t.max, string file = __FILE__, uint line = __LINE__)
void dump(size_t pos = size_t.max, string file = __FILE__, uint line = __LINE__) const
{
import dparse.lexer : str;
import std.algorithm.iteration : map;
Expand Down Expand Up @@ -260,6 +270,12 @@ private:

if (i + 1 < index)
{
if (config.dfmt_single_indent == OptionalBoolean.t && skipDoubleIndent(i, parenCount))
{
parenCount = pc;
continue;
}

immutable currentIsNonWrapTemp = !details[i].wrap
&& details[i].temp && arr[i] != tok!")" && arr[i] != tok!"!";
if (arr[i] == tok!"static"
Expand All @@ -276,7 +292,7 @@ private:
continue;
}
}
else if (parenCount == 0 && arr[i] == tok!"(")
else if (parenCount == 0 && arr[i] == tok!"(" && config.dfmt_single_indent == OptionalBoolean.f)
size++;

if (arr[i] == tok!"!")
Expand All @@ -287,6 +303,12 @@ private:
}
return size;
}

bool skipDoubleIndent(size_t i, int parenCount) const pure nothrow @safe @nogc
{
return (details[i + 1].wrap && arr[i] == tok!")")
|| (parenCount == 0 && arr[i + 1] == tok!"," && arr[i] == tok!"(");
}
}

unittest
Expand Down
7 changes: 6 additions & 1 deletion src/dfmt/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ else
case "keep_line_breaks":
optConfig.dfmt_keep_line_breaks = optVal;
break;
case "single_indent":
optConfig.dfmt_single_indent = optVal;
break;
default:
assert(false, "Invalid command-line switch");
}
Expand Down Expand Up @@ -125,7 +128,8 @@ else
"space_before_aa_colon", &handleBooleans,
"tab_width", &optConfig.tab_width,
"template_constraint_style", &optConfig.dfmt_template_constraint_style,
"keep_line_breaks", &handleBooleans);
"keep_line_breaks", &handleBooleans,
"single_indent", &handleBooleans);
// dfmt on
}
catch (GetOptException e)
Expand Down Expand Up @@ -329,6 +333,7 @@ Formatting Options:
--compact_labeled_statements
--template_constraint_style
--space_before_aa_colon
--single_indent
`,
optionsToString!(typeof(Config.dfmt_template_constraint_style)));
}
Expand Down
49 changes: 49 additions & 0 deletions tests/allman/keep_single_indent.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
unittest
{
{
bool anotherTemplatedFunction(One, Two, Three)(One alpha, Two bravo,
Three charlie, double delta)
{
if (isNumeric!One && isNumeric!Two && isNumeric!Three && echo
&& foxtrot && golf && hotel && india && juliet)
{
}
}
}
}

void f()
{
string a = "foo"
~ "bar" /* bar */
~ "baz";
}

unittest
{
if (a)
{
while (sBraceDepth == 0 && indents.topIsTemp()
&& ((indents.top != tok!"if" && indents.top != tok!"version")
|| !peekIs(tok!"else")))
a();
}
}

unittest
{
callFunc({ int i = 10; return i; });
callFunc({
int i = 10;
foo(alpha, bravo, charlie, delta, echo, foxtrot, golf, echo);
doStuff(withThings, andOtherStuff);
return i;
});
callFunc({
int i = 10;
foo(alpha_longVarName, bravo_longVarName, charlie_longVarName, delta_longVarName,
echo_longVarName, foxtrot_longVarName, golf_longVarName, echo_longVarName);
doStuff(withThings, andOtherStuff);
return i;
}, more_stuff);
}
41 changes: 41 additions & 0 deletions tests/allman/single_indent.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
unittest
{
{
bool anotherTemplatedFunction(One, Two, Three)(One alpha, Two bravo,
Three charlie, double delta)
{
if (isNumeric!One && isNumeric!Two && isNumeric!Three && echo
&& foxtrot && golf && hotel && india && juliet)
{
}
}
}
}

unittest
{
if (a)
{
while (sBraceDepth == 0 && indents.topIsTemp()
&& ((indents.top != tok!"if" && indents.top != tok!"version") || !peekIs(tok!"else")))
a();
}
}

unittest
{
callFunc({ int i = 10; return i; });
callFunc({
int i = 10;
foo(alpha, bravo, charlie, delta, echo, foxtrot, golf, echo);
doStuff(withThings, andOtherStuff);
return i;
});
callFunc({
int i = 10;
foo(alpha_longVarName, bravo_longVarName, charlie_longVarName, delta_longVarName,
echo_longVarName, foxtrot_longVarName, golf_longVarName, echo_longVarName);
doStuff(withThings, andOtherStuff);
return i;
}, more_stuff);
}
2 changes: 2 additions & 0 deletions tests/keep_single_indent.args
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--single_indent true
--keep_line_breaks true
49 changes: 49 additions & 0 deletions tests/keep_single_indent.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
unittest
{
{
bool anotherTemplatedFunction(One, Two, Three)(One alpha, Two bravo,
Three charlie, double delta)
{
if (isNumeric!One && isNumeric!Two && isNumeric!Three && echo
&& foxtrot && golf && hotel && india && juliet)
{
}
}
}
}

void f()
{
string a = "foo"
~ "bar" /* bar */
~ "baz";
}

unittest
{
if (a)
{
while (sBraceDepth == 0 && indents.topIsTemp()
&& ((indents.top != tok!"if" && indents.top != tok!"version")
|| !peekIs(tok!"else")))
a();
}
}

unittest
{
callFunc({ int i = 10; return i; });
callFunc({
int i = 10;
foo(alpha, bravo, charlie, delta, echo, foxtrot, golf, echo);
doStuff(withThings, andOtherStuff);
return i;
});
callFunc({
int i = 10;
foo(alpha_longVarName, bravo_longVarName, charlie_longVarName, delta_longVarName,
echo_longVarName, foxtrot_longVarName, golf_longVarName, echo_longVarName);
doStuff(withThings, andOtherStuff);
return i;
}, more_stuff);
}
42 changes: 42 additions & 0 deletions tests/otbs/keep_single_indent.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
unittest {
{
bool anotherTemplatedFunction(One, Two, Three)(One alpha, Two bravo,
Three charlie, double delta) {
if (isNumeric!One && isNumeric!Two && isNumeric!Three && echo
&& foxtrot && golf && hotel && india && juliet) {
}
}
}
}

void f() {
string a = "foo"
~ "bar" /* bar */
~ "baz";
}

unittest {
if (a) {
while (sBraceDepth == 0 && indents.topIsTemp()
&& ((indents.top != tok!"if" && indents.top != tok!"version")
|| !peekIs(tok!"else")))
a();
}
}

unittest {
callFunc({ int i = 10; return i; });
callFunc({
int i = 10;
foo(alpha, bravo, charlie, delta, echo, foxtrot, golf, echo);
doStuff(withThings, andOtherStuff);
return i;
});
callFunc({
int i = 10;
foo(alpha_longVarName, bravo_longVarName, charlie_longVarName, delta_longVarName,
echo_longVarName, foxtrot_longVarName, golf_longVarName, echo_longVarName);
doStuff(withThings, andOtherStuff);
return i;
}, more_stuff);
}
35 changes: 35 additions & 0 deletions tests/otbs/single_indent.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
unittest {
{
bool anotherTemplatedFunction(One, Two, Three)(One alpha, Two bravo,
Three charlie, double delta) {
if (isNumeric!One && isNumeric!Two && isNumeric!Three && echo
&& foxtrot && golf && hotel && india && juliet) {
}
}
}
}

unittest {
if (a) {
while (sBraceDepth == 0 && indents.topIsTemp()
&& ((indents.top != tok!"if" && indents.top != tok!"version") || !peekIs(tok!"else")))
a();
}
}

unittest {
callFunc({ int i = 10; return i; });
callFunc({
int i = 10;
foo(alpha, bravo, charlie, delta, echo, foxtrot, golf, echo);
doStuff(withThings, andOtherStuff);
return i;
});
callFunc({
int i = 10;
foo(alpha_longVarName, bravo_longVarName, charlie_longVarName, delta_longVarName,
echo_longVarName, foxtrot_longVarName, golf_longVarName, echo_longVarName);
doStuff(withThings, andOtherStuff);
return i;
}, more_stuff);
}
1 change: 1 addition & 0 deletions tests/single_indent.args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--single_indent true
42 changes: 42 additions & 0 deletions tests/single_indent.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
unittest
{
{
bool anotherTemplatedFunction(One, Two, Three)(One alpha, Two bravo,
Three charlie, double delta)
{
if (isNumeric!One && isNumeric!Two && isNumeric!Three && echo
&& foxtrot && golf && hotel && india && juliet)
{
}
}
}
}

unittest
{
if (a)
{
while (sBraceDepth == 0 && indents.topIsTemp()
&& ((indents.top != tok!"if" && indents.top != tok!"version")
|| !peekIs(tok!"else")))
a();
}
}

unittest
{
callFunc({ int i = 10; return i; });
callFunc({
int i = 10;
foo(alpha, bravo, charlie, delta, echo, foxtrot, golf, echo);
doStuff(withThings, andOtherStuff);
return i;
});
callFunc({
int i = 10;
foo(alpha_longVarName, bravo_longVarName, charlie_longVarName, delta_longVarName,
echo_longVarName, foxtrot_longVarName, golf_longVarName, echo_longVarName);
doStuff(withThings, andOtherStuff);
return i;
}, more_stuff);
}

0 comments on commit ea36fbf

Please sign in to comment.