Skip to content

Commit

Permalink
chore: run dfmt on all files
Browse files Browse the repository at this point in the history
_sigh_ should've done this at the beginning, but better late than never.

Signed-off-by: Prajwal S N <[email protected]>
  • Loading branch information
snprajwal committed Dec 29, 2023
1 parent 00c8872 commit a3fbaf3
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 83 deletions.
16 changes: 2 additions & 14 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,5 @@ indent_size = 4
indent_style = tab

[*.d]
dfmt_align_switch_statements = true
dfmt_brace_style = allman
dfmt_outdent_attributes = true
dfmt_outdent_labels = true
dfmt_soft_max_line_length = 80
dfmt_space_after_cast = true
dfmt_space_after_keywords = true
dfmt_split_operator_at_line_end = false
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
max_line_length = 120
tab_width = 8
dfmt_single_template_constraint_indent = true
dfmt_single_indent = true
97 changes: 44 additions & 53 deletions src/dfmt/ast.d
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
// if the current length + data length crosses the hard limit,
// insert a newline.
if (length > config.dfmt_soft_max_line_length
|| (length + data.length) > config.max_line_length)
|| (length + data.length) > config.max_line_length)
newline();
}

Expand Down Expand Up @@ -161,8 +161,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
write(rrs);
writeSpace = true;
stc &= ~(
ASTCodegen.STC.out_ | ASTCodegen.STC.scope_ | ASTCodegen.STC.ref_ | ASTCodegen
.STC.return_);
ASTCodegen.STC.out_ | ASTCodegen.STC.scope_
| ASTCodegen.STC.ref_ | ASTCodegen.STC.return_);
break;
}

Expand Down Expand Up @@ -313,8 +313,7 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
write(')');
if (target.ptrsize == 8)
goto case Tuns64;
else if (target.ptrsize == 4 ||
target.ptrsize == 2)
else if (target.ptrsize == 4 || target.ptrsize == 2)
goto case Tuns32;
else
assert(0);
Expand Down Expand Up @@ -1085,10 +1084,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
}
}

void writeArgs(
ASTCodegen.Expressions* expressions,
ASTCodegen.Expression basis = null,
ASTCodegen.Identifiers* names = null)
void writeArgs(ASTCodegen.Expressions* expressions,
ASTCodegen.Expression basis = null, ASTCodegen.Identifiers* names = null)
{
if (!expressions || !expressions.length)
return;
Expand Down Expand Up @@ -1229,8 +1226,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor

void visitExp(ASTCodegen.ExpStatement s)
{
if (s.exp && s.exp.op == EXP.declaration &&
(cast(ASTCodegen.DeclarationExp) s.exp).declaration)
if (s.exp && s.exp.op == EXP.declaration
&& (cast(ASTCodegen.DeclarationExp) s.exp).declaration)
{
(cast(ASTCodegen.DeclarationExp) s.exp).declaration.accept(this);
return;
Expand Down Expand Up @@ -1309,7 +1306,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
{
if (!insideCase)
{
if (config.dfmt_brace_style != BraceStyle.knr || s.statement.isCompoundStatement.statements.length != 1)
if (config.dfmt_brace_style != BraceStyle.knr
|| s.statement.isCompoundStatement.statements.length != 1)
write('{');
newline();
}
Expand All @@ -1319,7 +1317,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
depth--;
if (!insideCase)
{
if (config.dfmt_brace_style != BraceStyle.knr || s.statement.isCompoundStatement.statements.length != 1)
if (config.dfmt_brace_style != BraceStyle.knr
|| s.statement.isCompoundStatement.statements.length != 1)
{
write('}');
if (!insideIfOrDo)
Expand Down Expand Up @@ -1375,8 +1374,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
}
if (config.dfmt_brace_style == BraceStyle.otbs)
write(' ');
else if (config.dfmt_brace_style != BraceStyle.knr || s._body
.isScopeStatement.statement.isCompoundStatement.statements.length > 1)
else if (config.dfmt_brace_style != BraceStyle.knr
|| s._body.isScopeStatement.statement.isCompoundStatement.statements.length > 1)
newline();
write("while");
if (config.dfmt_space_after_keywords)
Expand Down Expand Up @@ -1557,8 +1556,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
{
if (config.dfmt_brace_style == BraceStyle.otbs)
write(' ');
else if (config.dfmt_brace_style != BraceStyle.knr ||
s.ifbody.isScopeStatement.statement.isCompoundStatement.statements.length > 1)
else if (config.dfmt_brace_style != BraceStyle.knr
|| s.ifbody.isScopeStatement.statement.isCompoundStatement.statements.length > 1)
newline();
write("else");
if (!s.elsebody.isIfStatement() && config.dfmt_brace_style == BraceStyle.allman)
Expand Down Expand Up @@ -1939,14 +1938,12 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
while (t)
{
write(Token.toString(t.value));
if (t.next &&
t.value != TOK.min &&
t.value != TOK.comma && t.next.value != TOK.comma &&
t.value != TOK.leftBracket && t.next.value != TOK.leftBracket &&
t.next.value != TOK.rightBracket &&
t.value != TOK.leftParenthesis && t.next.value != TOK.leftParenthesis &&
t.next.value != TOK.rightParenthesis &&
t.value != TOK.dot && t.next.value != TOK.dot)
if (t.next && t.value != TOK.min && t.value != TOK.comma
&& t.next.value != TOK.comma && t.value != TOK.leftBracket
&& t.next.value != TOK.leftBracket && t.next.value != TOK.rightBracket
&& t.value != TOK.leftParenthesis && t.next.value != TOK.leftParenthesis
&& t.next.value != TOK.rightParenthesis
&& t.value != TOK.dot && t.next.value != TOK.dot)
{
write(' ');
}
Expand Down Expand Up @@ -1983,8 +1980,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor

void writeFuncBody(ASTCodegen.FuncDeclaration f)
{
if (config.dfmt_brace_style == BraceStyle.allman || config.dfmt_brace_style == BraceStyle
.knr)
if (config.dfmt_brace_style == BraceStyle.allman || config.dfmt_brace_style
== BraceStyle.knr)
newline();
else
write(' ');
Expand Down Expand Up @@ -2076,8 +2073,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
if (requireDo)
{
write("do");
if (config.dfmt_brace_style == BraceStyle.allman || config.dfmt_brace_style == BraceStyle
.knr)
if (config.dfmt_brace_style == BraceStyle.allman
|| config.dfmt_brace_style == BraceStyle.knr)
newline();
else
write(' ');
Expand Down Expand Up @@ -2289,8 +2286,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
writeExprWithPrecedence(e, PREC.assign);
}

void writeFuncIdentWithPrefix(TypeFunction t, const Identifier ident, ASTCodegen
.TemplateDeclaration td)
void writeFuncIdentWithPrefix(TypeFunction t, const Identifier ident,
ASTCodegen.TemplateDeclaration td)
{
if (t.inuse)
{
Expand Down Expand Up @@ -2625,8 +2622,7 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
switch (t.ty)
{
default:
return t.isTypeBasic() ?
visitBasic(cast(TypeBasic) t) : visitType(t);
return t.isTypeBasic() ? visitBasic(cast(TypeBasic) t) : visitType(t);

case Terror:
return visitError(cast(TypeError) t);
Expand Down Expand Up @@ -2696,7 +2692,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
{
write('@');

bool isAnonymous = p.userAttribDecl.atts.length > 0 && !(*p.userAttribDecl.atts)[0].isCallExp();
bool isAnonymous = p.userAttribDecl.atts.length > 0
&& !(*p.userAttribDecl.atts)[0].isCallExp();
if (isAnonymous)
write('(');

Expand All @@ -2723,9 +2720,9 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
stc &= ~STC.shared_;

writeStc(stc & (
STC.const_ | STC.immutable_ | STC.wild | STC.shared_ |
STC.return_ | STC.returninferred | STC.scope_ | STC.scopeinferred | STC.out_ | STC.ref_ | STC
.returnScope));
STC.const_ | STC.immutable_ | STC.wild | STC.shared_ | STC.return_
| STC.returninferred | STC.scope_ | STC.scopeinferred | STC.out_
| STC.ref_ | STC.returnScope));

import core.stdc.string : strncmp;

Expand All @@ -2734,10 +2731,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
if (p.ident)
write(p.ident.toString());
}
else if (p.type.ty == Tident &&
(cast(TypeIdentifier) p.type)
.ident.toString().length > 3 &&
strncmp((cast(TypeIdentifier) p.type)
else if (p.type.ty == Tident && (cast(TypeIdentifier) p.type)
.ident.toString().length > 3 && strncmp((cast(TypeIdentifier) p.type)
.ident.toChars(), "__T", 3) == 0)
{
// print parameter name, instead of undetermined type parameter
Expand Down Expand Up @@ -2804,12 +2799,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
with (ASTCodegen.Visibility.Kind)
{
immutable string[7] a = [
none: "none",
private_: "private",
package_: "package",
protected_: "protected",
public_: "public",
export_: "export"
none: "none", private_: "private", package_: "package",
protected_: "protected", public_: "public", export_: "export"
];
return a[kind];
}
Expand All @@ -2835,17 +2826,18 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
RootObject oarg = (*ti.tiargs)[0];
if (Type t = isType(oarg))
{
if ((t.equals(Type.tstring) || t.equals(Type.twstring) || t.equals(Type.tdstring) || t.mod == 0) && (
(t.isTypeBasic() || t.ty == Tident) && (cast(TypeIdentifier) t).idents.length == 0))
if ((t.equals(Type.tstring) || t.equals(Type.twstring)
|| t.equals(Type.tdstring) || t.mod == 0) && ((t.isTypeBasic()
|| t.ty == Tident) && (cast(TypeIdentifier) t).idents.length == 0))
{
write(t.toString());
return;
}
}
else if (ASTCodegen.Expression e = isExpression(oarg))
{
if (e.op == EXP.int64 || e.op == EXP.float64 || e.op == EXP.null_ || e.op == EXP.string_ || e.op == EXP
.this_)
if (e.op == EXP.int64 || e.op == EXP.float64 || e.op == EXP.null_
|| e.op == EXP.string_ || e.op == EXP.this_)
{
write(e.toString());
return;
Expand Down Expand Up @@ -3551,8 +3543,7 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
{
import dmd.id : Id;

declString = (d.ident == Id.string || d.ident == Id.wstring || d.ident == Id
.dstring);
declString = (d.ident == Id.string || d.ident == Id.wstring || d.ident == Id.dstring);
write(d.ident.toString());
write(" = ");
writeStc(d.storage_class);
Expand Down
9 changes: 5 additions & 4 deletions src/dfmt/editorconfig.d
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ EC getConfigFor(EC)(string path)
{
import std.stdio : File;
import std.regex : regex, match;
import std.path : globMatch, dirName, baseName, pathSplitter, buildPath,
absolutePath;
import std.path : globMatch, dirName, baseName, pathSplitter, buildPath, absolutePath;
import std.algorithm : reverse, map, filter;
import std.array : array;
import std.file : isDir;
Expand All @@ -141,7 +140,9 @@ EC getConfigFor(EC)(string path)
EC[] sections = parseConfig!EC(buildPath(pathParts[0 .. i]));
if (sections.length)
configs ~= sections;
if (!sections.map!(a => a.root).filter!(a => a == OptionalBoolean.t).empty)
if (!sections.map!(a => a.root)
.filter!(a => a == OptionalBoolean.t)
.empty)
break;
}
reverse(configs);
Expand Down Expand Up @@ -208,7 +209,7 @@ private EC[] parseConfig(EC)(string dir)
mixin(configDot) = propertyValue == "true" ? OptionalBoolean.t
: OptionalBoolean.f;
else
mixin(configDot) = to!(FieldType)(propertyValue);
mixin(configDot) = to!(FieldType)(propertyValue);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/dfmt/globmatch_editorconfig.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module dfmt.globmatch_editorconfig;
import std.path : CaseSensitive;
import std.range : isForwardRange, ElementEncodingType;
import std.traits : isSomeChar, isSomeString;
import std.range.primitives : empty, save, front, popFront;
import std.range.primitives : empty, save, front, popFront;
import std.traits : Unqual;
import std.conv : to;
import std.path : filenameCharCmp, isDirSeparator;
Expand All @@ -12,9 +12,9 @@ import std.path : filenameCharCmp, isDirSeparator;
// * changes meaning to match all characters except '/'
// ** added to take over the old meaning of *
bool globMatchEditorConfig(CaseSensitive cs = CaseSensitive.osDefault, C, Range)(
Range path, const(C)[] pattern) @safe pure
if (isForwardRange!Range && isSomeChar!(ElementEncodingType!Range)
&& isSomeChar!C && is(Unqual!C == Unqual!(ElementEncodingType!Range)))
Range path, const(C)[] pattern) @safe pure
if (isForwardRange!Range && isSomeChar!(ElementEncodingType!Range)
&& isSomeChar!C && is(Unqual!C == Unqual!(ElementEncodingType!Range)))
in
{
// Verify that pattern[] is valid
Expand Down
12 changes: 4 additions & 8 deletions src/dfmt/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ int main(string[] args)
import std.exception : enforce;

enforce!GetOptException(value == "true" || value == "false", "Invalid argument");
immutable OptionalBoolean optVal = value == "true" ? OptionalBoolean.t
: OptionalBoolean.f;
immutable OptionalBoolean optVal = value == "true" ? OptionalBoolean.t : OptionalBoolean.f;
switch (option)
{
case "align_switch_statements":
Expand Down Expand Up @@ -327,12 +326,10 @@ Options:
Formatting Options:
--align_switch_statements
--brace_style `, optionsToString!(typeof(Config.dfmt_brace_style)),
`
--brace_style `, optionsToString!(typeof(Config.dfmt_brace_style)), `
--end_of_line `, optionsToString!(typeof(Config.end_of_line)), `
--indent_size
--indent_style, -t `,
optionsToString!(typeof(Config.indent_style)), `
--indent_style, -t `, optionsToString!(typeof(Config.indent_style)), `
--keep_line_breaks
--soft_max_line_length
--max_line_length
Expand All @@ -349,8 +346,7 @@ Formatting Options:
--space_before_named_arg_colon
--single_indent
--reflow_property_chains
`,
optionsToString!(typeof(Config.dfmt_template_constraint_style)));
`, optionsToString!(typeof(Config.dfmt_template_constraint_style)));
}

private string createFilePath(bool readFromStdin, string fileName)
Expand Down

0 comments on commit a3fbaf3

Please sign in to comment.