Skip to content

Commit 7e85ccc

Browse files
author
Kapil Borle
committed
Add corrections for uncuddled branch statements
1 parent 32f15e9 commit 7e85ccc

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

Rules/PlaceCloseBrace.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,15 @@ private DiagnosticRecord GetViolationsForUncuddledBranches(
346346
{
347347
var expectedNewLinePos = closeBracePos + 1;
348348

349+
// this will not work if there is a comment in between any tokens.
350+
// find violation only if the open brace is on the same line as the branching statement.
351+
// todo handle types in catch statement
349352
if (tokens.Length > 1 && tokens.Length > expectedNewLinePos)
350353
{
351354
var closeBraceToken = tokens[closeBracePos];
352355
if (tokens[closeBracePos + 1].Kind == TokenKind.NewLine &&
353-
IsBranchingStatementToken(tokens[closeBracePos + 2]))
356+
IsBranchingStatementToken(tokens[closeBracePos + 2]) &&
357+
tokens[closeBracePos + 3].Kind == TokenKind.LCurly)
354358
{
355359
return new DiagnosticRecord(
356360
GetError(Strings.PlaceCloseBraceErrorShouldCuddleBranchStatement),
@@ -359,13 +363,32 @@ private DiagnosticRecord GetViolationsForUncuddledBranches(
359363
GetDiagnosticSeverity(),
360364
fileName,
361365
null,
362-
GetCorrectionsForBraceShouldHaveNewLineAfter(tokens, closeBracePos, openBracePos, fileName));
366+
GetCorrectionsForUncuddledBranches(tokens, closeBracePos, closeBracePos + 2, fileName));
363367
}
364368
}
365369

366370
return null;
367371
}
368372

373+
private List<CorrectionExtent> GetCorrectionsForUncuddledBranches(
374+
Token[] tokens,
375+
int closeBracePos,
376+
int branchStatementPos,
377+
string fileName)
378+
{
379+
var corrections = new List<CorrectionExtent>();
380+
var closeBraceToken = tokens[closeBracePos];
381+
var branchStatementToken = tokens[branchStatementPos];
382+
corrections.Add(new CorrectionExtent(
383+
closeBraceToken.Extent.StartLineNumber,
384+
branchStatementToken.Extent.EndLineNumber,
385+
closeBraceToken.Extent.StartColumnNumber,
386+
branchStatementToken.Extent.EndColumnNumber,
387+
closeBraceToken.Extent.Text + ' ' + branchStatementToken.Extent.Text,
388+
fileName));
389+
return corrections;
390+
391+
}
369392

370393
private DiagnosticRecord GetViolationForBraceShouldBeOnNewLine(
371394
Token[] tokens,

0 commit comments

Comments
 (0)