Skip to content

Commit 8593797

Browse files
author
Kapil Borle
committed
Fix close brace correction for named blocks
1 parent 5ac32d6 commit 8593797

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

Rules/PlaceCloseBrace.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,32 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
4141
throw new ArgumentNullException("ast");
4242
}
4343

44+
// TODO Given that we need to make exceptions for
45+
// ScriptBlockExpressionAst and NamedBlockAst, a
46+
// simpler approach using only tokens seems more
47+
// robust - for every close brace, check the position
48+
// of its corresponding open brace. If the open brace
49+
// is on a line by itself, use its identation to decide
50+
// close brace's indentation. If the open brace is
51+
// preceded by any non new line token then find the
52+
// fist keyword on the line and use its indentation for
53+
// the close brace
54+
4455
var tokens = Helper.Instance.Tokens.ToList();
4556
var astTokenMap = new Dictionary<Ast, List<Token>>();
4657
var violationTokens = new HashSet<Token>();
4758
var diagnosticRecords = new List<DiagnosticRecord>();
48-
var astItems = ast.FindAll(x => x is ScriptBlockAst || x is StatementBlockAst, true);
59+
var astItems = ast.FindAll(x => x is ScriptBlockAst
60+
|| x is StatementBlockAst
61+
|| x is NamedBlockAst,
62+
true);
4963
foreach (var astItem in astItems)
5064
{
5165
var astTokens = GetTokens(astItem, tokens, ref astTokenMap);
5266
AddToDiagnosticRecords(
5367
GetViolationForBraceOnSameLine(astItem, astTokens, fileName, ref violationTokens),
5468
ref diagnosticRecords);
5569

56-
// FIXME cannot detect the end brace of begin block
5770
AddToDiagnosticRecords(
5871
GetViolationForEmptyLineBeforeBrace(astItem, astTokens, fileName, ref violationTokens),
5972
ref diagnosticRecords);
@@ -179,7 +192,8 @@ private List<CorrectionExtent> GetSuggestedCorrectionsForBraceOnSameLine(
179192
private string GetIndentation(Ast ast)
180193
{
181194
var targetAst = ast;
182-
if (targetAst.Parent != null)
195+
if (!(targetAst is NamedBlockAst)
196+
&& targetAst.Parent != null)
183197
{
184198
targetAst = targetAst.Parent;
185199
if (targetAst is ScriptBlockExpressionAst)

0 commit comments

Comments
 (0)