Skip to content

Commit

Permalink
Merge pull request #534 from dlang-community/knr_brace_style
Browse files Browse the repository at this point in the history
  • Loading branch information
WebFreak001 authored Oct 9, 2021
2 parents e5e1160 + b3b8ff1 commit f1e5713
Show file tree
Hide file tree
Showing 226 changed files with 2,399 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/dfmt/ast_info.d
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ struct ASTInformation
/// Closing braces of function literals
size_t[] funLitEndLocations;

/// Locations of aggregate bodies (struct, class, union)
size_t[] aggregateBodyLocations;

/// Locations of function bodies
size_t[] funBodyLocations;

/// Conditional statements that have matching "else" statements
size_t[] conditionalWithElseLocations;

Expand Down Expand Up @@ -200,6 +206,18 @@ final class FormatVisitor : ASTVisitor
destructor.accept(this);
}

override void visit (const FunctionBody functionBody)
{
if (auto bd = functionBody.specifiedFunctionBody)
{
if (bd.blockStatement)
{
astInformation.funBodyLocations ~= bd.blockStatement.startLocation;
}
}
functionBody.accept(this);
}

override void visit(const ConditionalDeclaration dec)
{
if (dec.hasElse)
Expand Down Expand Up @@ -313,6 +331,7 @@ final class FormatVisitor : ASTVisitor

override void visit(const StructBody structBody)
{
astInformation.aggregateBodyLocations ~= structBody.startLocation;
astInformation.doubleNewlineLocations ~= structBody.endLocation;
structBody.accept(this);
}
Expand Down
4 changes: 3 additions & 1 deletion src/dfmt/config.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ enum BraceStyle
/// $(LINK https://en.wikipedia.org/wiki/Indent_style#Variant:_1TBS)
otbs,
/// $(LINK https://en.wikipedia.org/wiki/Indent_style#Variant:_Stroustrup)
stroustrup
stroustrup,
/// $(LINK https://en.wikipedia.org/wiki/Indentation_style#K&R_style)
knr,
}

enum TemplateConstraintStyle
Expand Down
7 changes: 6 additions & 1 deletion src/dfmt/formatter.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import dfmt.indentation;
import dfmt.tokens;
import dfmt.wrapping;
import std.array;
import std.algorithm.comparison : among;

/**
* Formats the code contained in `buffer` into `output`.
Expand Down Expand Up @@ -967,6 +968,10 @@ private:
if (config.dfmt_brace_style == BraceStyle.allman
|| peekBackIsOneOf(true, tok!"{", tok!"}"))
newline();
else if (config.dfmt_brace_style == BraceStyle.knr
&& astInformation.funBodyLocations.canFindIndex(tIndex)
&& (peekBackIs(tok!")") || (!peekBackIs(tok!"do") && peekBack().text != "body")))
newline();
else if (!peekBackIsOneOf(true, tok!"{", tok!"}", tok!";"))
write(" ");
writeToken();
Expand Down Expand Up @@ -1035,7 +1040,7 @@ private:
currentLineLength = 0;
justAddedExtraNewline = true;
}
if (config.dfmt_brace_style == BraceStyle.otbs
if (config.dfmt_brace_style.among(BraceStyle.otbs, BraceStyle.knr)
&& ((peekIs(tok!"else")
&& !indents.topAre(tok!"static", tok!"if")
&& !indents.topIs(tok!"foreach") && !indents.topIs(tok!"for")
Expand Down
Empty file added tests/knr/.d.ref
Empty file.
37 changes: 37 additions & 0 deletions tests/knr/2d_arrays.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
unittest {
targets = [
[
RectangleShape.create(tex, vec2(-8 * scale, -32 * scale),
vec2(16 * scale, 48 * scale), vec4(14 / 16.0, 0, 16 / 16.0, 3 / 16.0)),
RectangleShape.create(tex, vec2(-8 * scale, -32 * scale),
vec2(16 * scale, 32 * scale), vec4(14 / 16.0, 3 / 16.0, 16 / 16.0, 5 / 16.0))
],
[
RectangleShape.create(tex, vec2(-8 * scale, -8 * scale),
vec2(16 * scale, 16 * scale), vec4(14 / 16.0, 5 / 16.0, 15 / 16.0, 6 / 16.0)),
RectangleShape.create(tex, vec2(-8 * scale, -8 * scale),
vec2(16 * scale, 16 * scale), vec4(15 / 16.0, 5 / 16.0, 16 / 16.0, 6 / 16.0))
]
];

int[][] foo = [
[
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32
],
[
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32
]
];

float[3][3] mat = [
[234.3456, 42435.8653, 23.5], [3.245, 235.3, 234.664],
[14324.6453, 23434.645, 9678.345]
];
}

string[][] globalArray = [
["123456789012345678901234567890", "123456789012345678901234567890"],
["123456789012345678901234567890", "123456789012345678901234567890"]
];
15 changes: 15 additions & 0 deletions tests/knr/DeclSpacing.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import std.stdio;

class Foo {
}

import std.conv;

void main()
{
return;
}

const baz = 11;
class Foo2 : Foo {
}
6 changes: 6 additions & 0 deletions tests/knr/array_access.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
unittest {
foo([
target.value.region[1], target.value.region[1],
target.value.region[1], target.value.region[1], target.value.region[1]
]);
}
8 changes: 8 additions & 0 deletions tests/knr/assoc_key_indent.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
void main()
{
string key;

int[string] var = [
key: 5
];
}
25 changes: 25 additions & 0 deletions tests/knr/associative_array.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
unittest {
Bson base = Bson([
"maps": Bson([
Bson(["id": Bson(4), "comment": Bson("hello")]),
Bson(["id": Bson(49), "comment": Bson(null)])
]),
"short": Bson(["a": "b", "c": "d"]),
"numbers": Bson([
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0
]),
"shuffleOnReset": serializeToBson([
"all": false,
"selected": true,
"maybe": false
]),
"resetOnEmpty": Bson(false),
"applyMods": Bson(true),
"sendComments": Bson(true)
]);
int[] x = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0
];
}
25 changes: 25 additions & 0 deletions tests/knr/associative_array_complex.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
auto find()
{
return Map.findRange([
"$and": [
["deleted": Bson(false)],
[
"$or": Bson([
serializeToBson(["forceUpdate": Bson(true)]),
serializeToBson([
"info.approved": ["$eq": Bson(1)],
"fetchDate": [
"$lte": Bson(BsonDate(currentTime - 60.days))
]
]),
serializeToBson([
"info.approved": ["$ne": Bson(1)],
"fetchDate": [
"$lte": Bson(BsonDate(currentTime - 14.days))
]
])
])
]
]
]);
}
13 changes: 13 additions & 0 deletions tests/knr/attribute_constraint.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
struct SomeStructName {
static struct InnerStruct {
version (linux) {
static if (condition) {
void longFunctionName(AAAAAAAA)(AAAAAAAA a) @property
if (someThingsAreTrue!AAAAAAAA && long_condition
&& is(elaborate == expression))
{
}
}
}
}
}
13 changes: 13 additions & 0 deletions tests/knr/breakOnDots.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
unittest {
{
{
foreach (abcde, def; abcdef.map!(battlecruiser => battlecruiser[123 .. 1231231])
.filter!(bravo => charlie[10] > 90000)
.sum()) {

}
abcdeabcdeabcde(12341234).abcdeabcdeabcde(12341234).abcdeabcdeabcde(12341234)
.abcdeabcdeabcde(12341234).abcdeabcdeabcde(12341234);
}
}
}
41 changes: 41 additions & 0 deletions tests/knr/catchExceptionNested.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class U0 : Exception {
this() @safe pure nothrow
{
super("U0 error message");
}
}

class U1 : Exception {
this() @safe pure nothrow
{
super("U1 error message");
}
}

void foo()
{
import std.stdio;

foreach (immutable i; 0 .. 2) {
try {
i.bar;
} catch (U0) {
"Function foo caught exception U0".writeln;
}
}
}

void bar(in int i) @safe pure
{
i.baz;
}

void baz(in int i) @safe pure
{
throw i ? new U1 : new U0;
}

void main()
{
foo;
}
7 changes: 7 additions & 0 deletions tests/knr/comments.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
int /*sneaky*/ foo( /*comments*/ ) /*everywhere*/
{
// comment on its own line
foo() // comment on same line
.bar(); // also on same line
/* again */ // same line
}
9 changes: 9 additions & 0 deletions tests/knr/constraint_singe_tab.d.ref
Original file line number Diff line number Diff line change
@@ -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)
{
}
10 changes: 10 additions & 0 deletions tests/knr/constraint_singe_tab2.d.ref
Original file line number Diff line number Diff line change
@@ -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)
{
}
17 changes: 17 additions & 0 deletions tests/knr/contracts.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
void main(string[] args)
{
struct SomeStruct {
private:
int a;
int b;
void doStuff(int q)
in {
assert(q);
}
out (result) {
}
body {
writeln(q);
}
}
}
7 changes: 7 additions & 0 deletions tests/knr/debug-inside-if.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
void main()
{
if (true)
debug {
foo();
}
}
7 changes: 7 additions & 0 deletions tests/knr/debug_with_param.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
void main()
{
debug (0)
foo();
else
bar();
}
18 changes: 18 additions & 0 deletions tests/knr/dip1009.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
int foo(int arg)
in {
assert(arg > 0);
}
out (result) {
assert(result == 0);
}
do {
return 0;
}

int bar(int arg)
in (arg > 0)
out (; true)
out /*Major*/ ( /*Tom*/ result /*To ground control*/ ; result == 0)
{
return 0;
}
15 changes: 15 additions & 0 deletions tests/knr/do_body.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import character.body;

void body() @nogc
in {
}
body {
body = null;
}

void body()
in {
}
do {
body = null;
}
Empty file added tests/knr/empty.d.ref
Empty file.
5 changes: 5 additions & 0 deletions tests/knr/enum_attribs.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum Foo {

deprecated member0,
@UDA(0) member1
}
13 changes: 13 additions & 0 deletions tests/knr/frontpage.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Computes average line length for standard input.
import std.stdio;

void main()
{
ulong lines = 0;
double sumLength = 0;
foreach (line; stdin.byLine()) {
++lines;
sumLength += line.length;
}
writeln("Average line length: ", lines ? sumLength / lines : 0);
}
1 change: 1 addition & 0 deletions tests/knr/func_param_attrib.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void foo(@UDA(0) @UDA(1) Bar bar);
Loading

0 comments on commit f1e5713

Please sign in to comment.