Skip to content

Commit 015bca3

Browse files
author
Jonathan Coe
committed
[clang-format] C# property formatting can be controlled by config options
Summary: Allow brace wrapping in C# property accessors to be controlled by configuration options. Add new tests and revert old test results for MS style to their old state (as intended). `FormatStyle.BraceWrapping.AfterFunction = true;` will change automatic property formatting from ``` Type MyType { get; set } ``` to ``` Type MyType { get; set } ``` Reviewers: krasimir, MyDeveloperDay Subscribers: cfe-commits Tags: #clang-format, #clang Differential Revision: https://reviews.llvm.org/D79000
1 parent 7443f86 commit 015bca3

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,8 @@ bool UnwrappedLineParser::tryToParsePropertyAccessor() {
15311531
// Try to parse the property accessor:
15321532
// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
15331533
Tokens->setPosition(StoredPosition);
1534+
if (Style.BraceWrapping.AfterFunction == true)
1535+
addUnwrappedLine();
15341536
nextToken();
15351537
do {
15361538
switch (FormatTok->Tok.getKind()) {

clang/unittests/Format/FormatTestCSharp.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,13 @@ TEST_F(FormatTestCSharp, Attributes) {
245245
"}");
246246

247247
verifyFormat("[TestMethod]\n"
248-
"public string Host { set; get; }");
248+
"public string Host\n"
249+
"{ set; get; }");
249250

250251
verifyFormat("[TestMethod(\"start\", HelpText = \"Starts the server "
251252
"listening on provided host\")]\n"
252-
"public string Host { set; get; }");
253+
"public string Host\n"
254+
"{ set; get; }");
253255

254256
verifyFormat(
255257
"[DllImport(\"Hello\", EntryPoint = \"hello_world\")]\n"
@@ -669,6 +671,32 @@ class MyClass {
669671
set => veryLongNamedField = value;
670672
} = VeryLongNamedTypeIndeed.Create(DefaultFirstArgument, DefaultSecondArgument,
671673
DefaultThirdArgument);
674+
})",
675+
Style);
676+
677+
// Brace wrapping and single-lining of accessor can be controlled by config.
678+
Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
679+
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
680+
Style.BraceWrapping.AfterFunction = true;
681+
682+
verifyFormat(R"(//
683+
public class SaleItem {
684+
public decimal Price
685+
{ get; set; }
686+
})",
687+
Style);
688+
689+
verifyFormat(R"(//
690+
class TimePeriod {
691+
public double Hours
692+
{
693+
get {
694+
return _seconds / 3600;
695+
}
696+
set {
697+
_seconds = value * 3600;
698+
}
699+
}
672700
})",
673701
Style);
674702
}

0 commit comments

Comments
 (0)