From e8bd41c70d5abdc44509c45479a4ec5dbde9dbf5 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Tue, 7 Sep 2021 14:29:45 +0200 Subject: [PATCH] Fix comment before contract Fixes #451. --- src/dfmt/formatter.d | 26 +++++++++++++---------- tests/allman/issue0451.d.ref | 40 ++++++++++++++++++++++++++++++++++++ tests/issue0451.d | 40 ++++++++++++++++++++++++++++++++++++ tests/otbs/issue0451.d.ref | 36 ++++++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+), 11 deletions(-) create mode 100644 tests/allman/issue0451.d.ref create mode 100644 tests/issue0451.d create mode 100644 tests/otbs/issue0451.d.ref diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index 47ae31f..48e93d9 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -1192,11 +1192,13 @@ private: writeParens(config.dfmt_space_after_cast == OptionalBoolean.t); break; case tok!"out": - if (!peekBackIs(tok!"}") - && astInformation.contractLocations.canFindIndex(current.index)) - newline(); - else if (peekBackIsKeyword) - write(" "); + if (!peekBackIsSlashSlash) { + if (!peekBackIs(tok!"}") + && astInformation.contractLocations.canFindIndex(current.index)) + newline(); + else if (peekBackIsKeyword) + write(" "); + } writeToken(); if (!currentIs(tok!"{") && !currentIs(tok!"comment")) write(" "); @@ -1220,13 +1222,15 @@ private: break; case tok!"in": immutable isContract = astInformation.contractLocations.canFindIndex(current.index); - if (isContract) - { - indents.popTempIndents(); - newline(); + if (!peekBackIsSlashSlash) { + if (isContract) + { + indents.popTempIndents(); + newline(); + } + else if (!peekBackIsOneOf(false, tok!"(", tok!",", tok!"!")) + write(" "); } - else if (!peekBackIsOneOf(false, tok!"(", tok!",", tok!"!")) - write(" "); writeToken(); immutable isFunctionLit = astInformation.funLitStartLocations.canFindIndex( current.index); diff --git a/tests/allman/issue0451.d.ref b/tests/allman/issue0451.d.ref new file mode 100644 index 0000000..c5b2f27 --- /dev/null +++ b/tests/allman/issue0451.d.ref @@ -0,0 +1,40 @@ +class C +{ + abstract void f1() // + in (true); + + abstract void f2() /* */ + in (true); + + abstract bool f3() // + out (r; r); + + abstract bool f4() /* */ + out (r; r); + + abstract void f5() // + do + { + } + + abstract void f6() /* */ + do + { + } + + abstract bool f7() // + in (true) // + out (r; r) // + do // + { + return true; + } + + abstract bool f8() /* */ + in (true) /* */ + out (r; r) /* */ + do /* */ + { + return true; + } +} diff --git a/tests/issue0451.d b/tests/issue0451.d new file mode 100644 index 0000000..c5b2f27 --- /dev/null +++ b/tests/issue0451.d @@ -0,0 +1,40 @@ +class C +{ + abstract void f1() // + in (true); + + abstract void f2() /* */ + in (true); + + abstract bool f3() // + out (r; r); + + abstract bool f4() /* */ + out (r; r); + + abstract void f5() // + do + { + } + + abstract void f6() /* */ + do + { + } + + abstract bool f7() // + in (true) // + out (r; r) // + do // + { + return true; + } + + abstract bool f8() /* */ + in (true) /* */ + out (r; r) /* */ + do /* */ + { + return true; + } +} diff --git a/tests/otbs/issue0451.d.ref b/tests/otbs/issue0451.d.ref new file mode 100644 index 0000000..fed04e5 --- /dev/null +++ b/tests/otbs/issue0451.d.ref @@ -0,0 +1,36 @@ +class C { + abstract void f1() // + in (true); + + abstract void f2() /* */ + in (true); + + abstract bool f3() // + out (r; r); + + abstract bool f4() /* */ + out (r; r); + + abstract void f5() // + do { + } + + abstract void f6() /* */ + do { + } + + abstract bool f7() // + in (true) // + out (r; r) // + do // + { + return true; + } + + abstract bool f8() /* */ + in (true) /* */ + out (r; r) /* */ + do /* */ { + return true; + } +}