Skip to content

Commit c7e1ba1

Browse files
author
Kapil Borle
committed
Add option to return correction as vscode snippet
1 parent c7269d5 commit c7e1ba1

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

Rules/ProvideCommentHelp.cs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public class ProvideCommentHelp : ConfigurableRule
4141
[ConfigurableRuleProperty(defaultValue: true)]
4242
public bool BlockComment { get; protected set; }
4343

44+
[ConfigurableRuleProperty(defaultValue: false)]
45+
public bool VSCodeSnippetCorrection { get; protected set; }
46+
4447
public ProvideCommentHelp() : base()
4548
{
4649
// Enable the rule by default
@@ -148,7 +151,7 @@ private IEnumerable<CorrectionExtent> GetCorrection(FunctionDefinitionAst funcDe
148151
funcDefnAst.Extent.StartLineNumber,
149152
funcDefnAst.Extent.StartColumnNumber,
150153
funcDefnAst.Extent.StartColumnNumber,
151-
helpBuilder.GetCommentHelp(BlockComment) + Environment.NewLine,
154+
helpBuilder.GetCommentHelp(BlockComment, VSCodeSnippetCorrection) + Environment.NewLine,
152155
funcDefnAst.Extent.File);
153156
}
154157

@@ -220,20 +223,21 @@ public void AddParameter(string paramName)
220223
parameters.Add(new ParameterHelpNode(paramName, "Parameter description"));
221224
}
222225

223-
public string GetCommentHelp(bool blockComment)
226+
public string GetCommentHelp(bool blockComment, bool snippet)
224227
{
225228
var sb = new StringBuilder();
229+
var helpContent = snippet ? this.ToSnippetString() : this.ToString();
226230
if (blockComment)
227231
{
228232
sb.AppendLine("<#");
229-
sb.AppendLine(this.ToString());
233+
sb.AppendLine(helpContent);
230234
sb.Append("#>");
231235
}
232236
else
233237
{
234238
var boundaryString = new String('#', 30);
235239
sb.AppendLine(boundaryString);
236-
var lines = this.ToString().Split('\n').Select(l => l.Trim('\r'));
240+
var lines = helpContent.Split('\n').Select(l => l.Trim('\r'));
237241
foreach (var line in lines)
238242
{
239243
sb.Append("#");
@@ -261,6 +265,21 @@ public override string ToString()
261265
return sb.ToString();
262266
}
263267

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)
275+
{
276+
sb.AppendLine(parameter.ToString(tabStop++)).AppendLine();
277+
}
278+
279+
sb.AppendLine(example.ToString(tabStop++)).AppendLine();
280+
sb.Append(notes.ToString(tabStop++));
281+
return sb.ToString();
282+
}
264283
private class CommentHelpNode
265284
{
266285
public CommentHelpNode(string nodeName, string description)
@@ -283,6 +302,18 @@ public override string ToString()
283302

284303
return sb.ToString();
285304
}
305+
306+
public virtual string ToString(int tabStop)
307+
{
308+
var sb = new StringBuilder();
309+
sb.Append(".").AppendLine(Name.ToUpper());
310+
if (!String.IsNullOrWhiteSpace(Description))
311+
{
312+
sb.Append($"${{{tabStop}:{Description}}}");
313+
}
314+
315+
return sb.ToString();
316+
}
286317
}
287318

288319
private class ParameterHelpNode : CommentHelpNode
@@ -306,6 +337,18 @@ public override string ToString()
306337

307338
return sb.ToString();
308339
}
340+
341+
public override string ToString(int tabStop)
342+
{
343+
var sb = new StringBuilder();
344+
sb.Append(".").Append(Name.ToUpper()).Append(" ").AppendLine(ParameterName);
345+
if (!String.IsNullOrWhiteSpace(Description))
346+
{
347+
sb.Append($"${{{tabStop}:{Description}}}");
348+
}
349+
350+
return sb.ToString();
351+
}
309352
}
310353
}
311354
}

Tests/Rules/ProvideCommentHelp.tests.ps1

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ $ruleSettings = @{
1010
Enable = $true
1111
ExportedOnly = $false
1212
BlockComment = $true
13+
VSCodeSnippetCorrection = $false
1314
}
1415

1516
$settings = @{
@@ -178,6 +179,41 @@ Export-ModuleMember -Function foo
178179
Test-Correction $def $expectedCorrection $settings
179180
}
180181

182+
It "should return a vscode snippet with line comment style" {
183+
$def = @'
184+
function foo {
185+
param($param1, $param2)
186+
}
187+
188+
Export-ModuleMember -Function foo
189+
'@
190+
$expectedCorrection = @'
191+
##############################
192+
#.SYNOPSIS
193+
#${1:Short description}
194+
#
195+
#.DESCRIPTION
196+
#${2:Long description}
197+
#
198+
#.PARAMETER param1
199+
#${3:Parameter description}
200+
#
201+
#.PARAMETER param2
202+
#${4:Parameter description}
203+
#
204+
#.EXAMPLE
205+
#${5:An example}
206+
#
207+
#.NOTES
208+
#${6:General notes}
209+
##############################
210+
211+
'@
212+
$ruleSettings.'BlockComment' = $false
213+
$ruleSettings.'VSCodeSnippetCorrection' = $true
214+
Test-Correction $def $expectedCorrection $settings
215+
}
216+
181217
if ($PSVersionTable.PSVersion -ge [Version]'5.0.0') {
182218
It "Does not count violation in DSC class" {
183219
$dscViolations.Count | Should Be 0
@@ -190,4 +226,4 @@ Export-ModuleMember -Function foo
190226
$noViolations.Count | Should Be 0
191227
}
192228
}
193-
}
229+
}

0 commit comments

Comments
 (0)