Skip to content

Commit

Permalink
feat: add 4 transformations
Browse files Browse the repository at this point in the history
- dfmt_space_before_function_parameters
- dfmt_space_after_cast
- dfmt_align_switch_statements
- dfmt_space_before_aa_colon

Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
  • Loading branch information
snprajwal committed Nov 10, 2023
1 parent ded6e8d commit 90c9040
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ dfmt_align_switch_statements | **`true`**, `false` | Align labels, cases, and de
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_after_keywords | **`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.
Expand All @@ -124,7 +124,6 @@ dfmt_space_before_named_arg_colon | `true`, **`false`** | Adds a space after a n
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 `}`
Expand Down
20 changes: 19 additions & 1 deletion src/dfmt/ast.d
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
if (i)
write(", ");
writeExprWithPrecedence(key, PREC.assign);
if (config.dfmt_space_before_aa_colon == OptionalBoolean.t)
write(' ');
write(": ");
auto value = (*e.values)[i];
writeExprWithPrecedence(value, PREC.assign);
Expand Down Expand Up @@ -792,6 +794,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
write(MODtoString(e.mod));
}
write(')');
if (config.dfmt_space_after_cast)
{
write(' ');
}
writeExprWithPrecedence(e.e1, precedence[e.op]);
}

Expand Down Expand Up @@ -1589,13 +1595,17 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor

void visitCase(ASTCodegen.CaseStatement s)
{
if (config.dfmt_align_switch_statements)
depth--;
write("case ");
writeExpr(s.exp);
write(':');
newline();
insideCase = true;
writeStatement(s.statement);
insideCase = false;
if (config.dfmt_align_switch_statements)
depth++;
}

void visitCaseRange(ASTCodegen.CaseRangeStatement s)
Expand All @@ -1611,9 +1621,13 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor

void visitDefault(ASTCodegen.DefaultStatement s)
{
if (config.dfmt_align_switch_statements)
depth--;
write("default:");
newline();
writeStatement(s.statement);
if (config.dfmt_align_switch_statements)
depth++;
}

void visitGotoDefault(ASTCodegen.GotoDefaultStatement _)
Expand Down Expand Up @@ -1978,7 +1992,9 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
if (ex)
{
writeExpr(ex);
write(':');
if (config.dfmt_space_before_aa_colon == OptionalBoolean.t)
write(' ');
write(": ");
}
if (auto iz = ai.value[i])
writeInitializer(iz);
Expand Down Expand Up @@ -2602,6 +2618,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor

void writeParamList(ParameterList pl)
{
if (config.dfmt_space_before_function_parameters)
write(' ');
write('(');
foreach (i; 0 .. pl.length)
{
Expand Down

0 comments on commit 90c9040

Please sign in to comment.