Skip to content

Commit 8c1437c

Browse files
author
Kapil Borle
committed
Remove redundant for generating correction string
1 parent c7e1ba1 commit 8c1437c

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

Rules/ProvideCommentHelp.cs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -251,35 +251,44 @@ public string GetCommentHelp(bool blockComment, bool snippet)
251251
}
252252

253253
public override string ToString()
254+
{
255+
return ToString(false);
256+
}
257+
258+
// todo remove code duplication
259+
public string ToSnippetString() {
260+
return ToString(true);
261+
}
262+
263+
private string ToString(bool snippetify)
254264
{
255265
var sb = new StringBuilder();
256-
sb.AppendLine(synopsis.ToString()).AppendLine();
257-
sb.AppendLine(description.ToString()).AppendLine();
266+
var counter = new Counter(snippetify ? (int?)1 : null);
267+
sb.AppendLine(synopsis.ToString(counter.Next())).AppendLine();
268+
sb.AppendLine(description.ToString(counter.Next())).AppendLine();
258269
foreach (var parameter in parameters)
259270
{
260-
sb.AppendLine(parameter.ToString()).AppendLine();
271+
sb.AppendLine(parameter.ToString(counter.Next())).AppendLine();
261272
}
262273

263-
sb.AppendLine(example.ToString()).AppendLine();
264-
sb.Append(notes.ToString());
274+
sb.AppendLine(example.ToString(counter.Next())).AppendLine();
275+
sb.Append(notes.ToString(counter.Next()));
265276
return sb.ToString();
266277
}
267278

268-
// todo remove code duplication
269-
public string ToSnippetString() {
270-
var sb = new StringBuilder();
271-
int tabStop = 1;
272-
sb.AppendLine(synopsis.ToString(tabStop++)).AppendLine();
273-
sb.AppendLine(description.ToString(tabStop++)).AppendLine();
274-
foreach (var parameter in parameters)
279+
private class Counter {
280+
int? count;
281+
282+
public Counter(int? start)
275283
{
276-
sb.AppendLine(parameter.ToString(tabStop++)).AppendLine();
284+
count = start;
277285
}
278286

279-
sb.AppendLine(example.ToString(tabStop++)).AppendLine();
280-
sb.Append(notes.ToString(tabStop++));
281-
return sb.ToString();
287+
public int? Next() {
288+
return count.HasValue ? count++ : null;
289+
}
282290
}
291+
283292
private class CommentHelpNode
284293
{
285294
public CommentHelpNode(string nodeName, string description)
@@ -293,27 +302,25 @@ public CommentHelpNode(string nodeName, string description)
293302

294303
public override string ToString()
295304
{
296-
var sb = new StringBuilder();
297-
sb.Append(".").AppendLine(Name.ToUpper());
298-
if (!String.IsNullOrWhiteSpace(Description))
299-
{
300-
sb.Append(Description);
301-
}
302-
303-
return sb.ToString();
305+
return ToString(null);
304306
}
305307

306-
public virtual string ToString(int tabStop)
308+
public virtual string ToString(int? tabStop)
307309
{
308310
var sb = new StringBuilder();
309311
sb.Append(".").AppendLine(Name.ToUpper());
310312
if (!String.IsNullOrWhiteSpace(Description))
311313
{
312-
sb.Append($"${{{tabStop}:{Description}}}");
314+
sb.Append(Snippetify(tabStop, Description));
313315
}
314316

315317
return sb.ToString();
316318
}
319+
320+
protected string Snippetify(int? tabStop, string defaultValue)
321+
{
322+
return tabStop == null ? defaultValue : $"${{{tabStop}:{defaultValue}}}";
323+
}
317324
}
318325

319326
private class ParameterHelpNode : CommentHelpNode
@@ -328,23 +335,16 @@ public ParameterHelpNode(string parameterName, string parameterDescription)
328335

329336
public override string ToString()
330337
{
331-
var sb = new StringBuilder();
332-
sb.Append(".").Append(Name.ToUpper()).Append(" ").AppendLine(ParameterName);
333-
if (!String.IsNullOrWhiteSpace(Description))
334-
{
335-
sb.Append(Description);
336-
}
337-
338-
return sb.ToString();
338+
return ToString(null);
339339
}
340340

341-
public override string ToString(int tabStop)
341+
public override string ToString(int? tabStop)
342342
{
343343
var sb = new StringBuilder();
344344
sb.Append(".").Append(Name.ToUpper()).Append(" ").AppendLine(ParameterName);
345345
if (!String.IsNullOrWhiteSpace(Description))
346346
{
347-
sb.Append($"${{{tabStop}:{Description}}}");
347+
sb.Append(Snippetify(tabStop, Description));
348348
}
349349

350350
return sb.ToString();

0 commit comments

Comments
 (0)