Skip to content

Commit fd47614

Browse files
author
Kapil Borle
committed
Use delegate in PlaceOpenBrace rule
1 parent 735c860 commit fd47614

1 file changed

Lines changed: 39 additions & 23 deletions

File tree

Rules/PlaceOpenBrace.cs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,19 @@ public static RuleArguments Create(Dictionary<string, object> arguments)
6060
}
6161

6262
private RuleArguments ruleArgs;
63+
private Func<Token[], string, IEnumerable<DiagnosticRecord>> findViolations;
6364

6465
public PlaceOpenBrace()
6566
{
6667
ruleArgs = RuleArguments.Create(Helper.Instance.GetRuleArguments(this.GetName()));
68+
if (ruleArgs.OnSameLine)
69+
{
70+
findViolations = this.FindViolationsForBraceShouldBeOnSameLine;
71+
}
72+
else
73+
{
74+
findViolations = this.FindViolationsForBraceShouldNotBeOnSameLine;
75+
}
6776
}
6877

6978
/// <summary>
@@ -80,14 +89,16 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
8089
}
8190

8291
// TODO Should have the following options
83-
// * on-same-line
84-
// * on-new-line
8592
// * new-line-after
8693
// * no-empty-line-after
8794

88-
var tokens = Helper.Instance.Tokens;
89-
if (ruleArgs.OnSameLine)
90-
{
95+
return findViolations(Helper.Instance.Tokens, fileName);
96+
}
97+
98+
private IEnumerable<DiagnosticRecord> FindViolationsForBraceShouldBeOnSameLine(
99+
Token[] tokens,
100+
string fileName)
101+
{
91102
for (int k = 2; k < tokens.Length; k++)
92103
{
93104
if (tokens[k].Kind == TokenKind.LCurly
@@ -100,31 +111,33 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
100111
GetDiagnosticSeverity(),
101112
fileName,
102113
null,
103-
GetSuggestedCorrections(tokens[k - 2], tokens[k], fileName));
114+
GetCorrectionsForBraceShouldBeOnSameLine(tokens[k - 2], tokens[k], fileName));
104115
}
105116
}
106-
}
107-
else
117+
}
118+
119+
private IEnumerable<DiagnosticRecord> FindViolationsForBraceShouldNotBeOnSameLine(
120+
Token[] tokens,
121+
string fileName)
122+
{
123+
for (int k = 1; k < tokens.Length; k++)
108124
{
109-
for (int k = 1; k < tokens.Length; k++)
125+
if (tokens[k].Kind == TokenKind.LCurly
126+
&& tokens[k - 1].Kind != TokenKind.NewLine)
110127
{
111-
if (tokens[k].Kind == TokenKind.LCurly
112-
&& tokens[k - 1].Kind != TokenKind.NewLine)
113-
{
114-
yield return new DiagnosticRecord(
115-
GetError(),
116-
tokens[k].Extent,
117-
GetName(),
118-
GetDiagnosticSeverity(),
119-
fileName,
120-
null,
121-
GetSuggestedCorrectionsForNotOneSameLine(tokens, k - 1, k, fileName));
122-
}
128+
yield return new DiagnosticRecord(
129+
GetError(),
130+
tokens[k].Extent,
131+
GetName(),
132+
GetDiagnosticSeverity(),
133+
fileName,
134+
null,
135+
GetCorrectionsForBraceShouldNotBeOnSameLine(tokens, k - 1, k, fileName));
123136
}
124137
}
125138
}
126139

127-
private List<CorrectionExtent> GetSuggestedCorrectionsForNotOneSameLine(
140+
private List<CorrectionExtent> GetCorrectionsForBraceShouldNotBeOnSameLine(
128141
Token[] tokens,
129142
int prevTokenPos,
130143
int closeBraceTokenPos,
@@ -168,7 +181,10 @@ private int GetStartColumnNumberOfTokenLine(Token[] tokens, int refTokenPos)
168181
return refToken.Extent.StartColumnNumber;
169182
}
170183

171-
private List<CorrectionExtent> GetSuggestedCorrections(Token precedingExpression, Token lCurly, string fileName)
184+
private List<CorrectionExtent> GetCorrectionsForBraceShouldBeOnSameLine(
185+
Token precedingExpression,
186+
Token lCurly,
187+
string fileName)
172188
{
173189
var corrections = new List<CorrectionExtent>();
174190
corrections.Add(

0 commit comments

Comments
 (0)