Skip to content

Commit 56ba0dc

Browse files
author
Kapil Borle
committed
Add indentation to correction
1 parent 684a8c1 commit 56ba0dc

File tree

2 files changed

+65
-12
lines changed

2 files changed

+65
-12
lines changed

Rules/ProvideCommentHelp.cs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
9393
GetDiagnosticSeverity(),
9494
fileName,
9595
null,
96-
GetCorrection(functionDefinitionAst).ToList());
96+
GetCorrection(ast, functionDefinitionAst).ToList());
9797
}
9898
}
9999

@@ -154,7 +154,7 @@ private DiagnosticSeverity GetDiagnosticSeverity()
154154
return DiagnosticSeverity.Information;
155155
}
156156

157-
private IEnumerable<CorrectionExtent> GetCorrection(FunctionDefinitionAst funcDefnAst)
157+
private IEnumerable<CorrectionExtent> GetCorrection(Ast ast, FunctionDefinitionAst funcDefnAst)
158158
{
159159
var helpBuilder = new CommentHelpBuilder();
160160

@@ -173,26 +173,45 @@ private IEnumerable<CorrectionExtent> GetCorrection(FunctionDefinitionAst funcDe
173173
endLine,
174174
startColumn,
175175
endColumn,
176-
ProcessCorrectionForPlacement(
177-
helpBuilder.GetCommentHelp(
178-
BlockComment,
179-
VSCodeSnippetCorrection)),
176+
GetCorrectionText(
177+
helpBuilder.GetCommentHelp(BlockComment, VSCodeSnippetCorrection),
178+
ast,
179+
funcDefnAst),
180180
funcDefnAst.Extent.File);
181181
}
182182

183-
private string ProcessCorrectionForPlacement(string correction)
183+
private string GetCorrectionText(string correction, Ast ast, FunctionDefinitionAst funcDefnAst)
184184
{
185+
var indentationString = String.Empty;
186+
if (funcDefnAst.Extent.StartColumnNumber > 1)
187+
{
188+
indentationString = GetLines(ast.Extent.Text)
189+
.ElementAt(funcDefnAst.Extent.StartLineNumber - 1)
190+
.Substring(0, funcDefnAst.Extent.StartColumnNumber - 1);
191+
correction = String.Join(
192+
Environment.NewLine,
193+
GetLines(correction).Select(l => indentationString + l));
194+
}
195+
185196
switch (placement)
186197
{
187198
case CommentHelpPlacement.Begin:
188-
return "{" + Environment.NewLine + correction + Environment.NewLine;
199+
return $"{{{Environment.NewLine}{correction}{Environment.NewLine}";
200+
189201
case CommentHelpPlacement.End:
190-
return Environment.NewLine + correction + Environment.NewLine;
202+
return $"{Environment.NewLine}{correction}{Environment.NewLine}{indentationString}";
203+
191204
default: // CommentHelpPlacement.Before
192-
return correction + Environment.NewLine;
205+
return $"{correction}{Environment.NewLine}";
193206
}
194207
}
195208

209+
// TODO replace with extension version
210+
private static IEnumerable<string> GetLines(string text)
211+
{
212+
return text.Split('\n').Select(l => l.Trim('\r'));
213+
}
214+
196215
private void GetCorrectionPosition(
197216
FunctionDefinitionAst funcDefnAst,
198217
out int startLine,
@@ -220,7 +239,7 @@ private void GetCorrectionPosition(
220239
default: // CommentHelpPlacement.Before
221240
startLine = funcDefnAst.Extent.StartLineNumber;
222241
endLine = startLine;
223-
startColumn = funcDefnAst.Extent.StartColumnNumber;
242+
startColumn = 1;
224243
endColumn = startColumn;
225244
break;
226245
}
@@ -314,7 +333,7 @@ public string GetCommentHelp(bool blockComment, bool snippet)
314333
{
315334
var boundaryString = new String('#', 30);
316335
sb.AppendLine(boundaryString);
317-
var lines = helpContent.Split('\n').Select(l => l.Trim('\r'));
336+
var lines = GetLines(helpContent);
318337
foreach (var line in lines)
319338
{
320339
sb.Append("#");

Tests/Rules/ProvideCommentHelp.tests.ps1

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,40 @@ General notes
287287
Test-Correction $def $expectedCorrection $settings
288288
}
289289

290+
It "should return a help snippet correction with correct indentation" {
291+
$def = @'
292+
function foo {
293+
param($param1)
294+
}
295+
'@
296+
$s = ' '
297+
$expectedCorrection = @"
298+
<#
299+
.SYNOPSIS
300+
Short description
301+
$s$s$s$s
302+
.DESCRIPTION
303+
Long description
304+
$s$s$s$s
305+
.PARAMETER param1
306+
Parameter description
307+
$s$s$s$s
308+
.EXAMPLE
309+
An example
310+
$s$s$s$s
311+
.NOTES
312+
General notes
313+
#>
314+
315+
"@
316+
$ruleSettings.'ExportedOnly' = $false
317+
$ruleSettings.'BlockComment' = $true
318+
$ruleSettings.'VSCodeSnippetCorrection' = $false
319+
$ruleSettings.'Placement' = 'before'
320+
Test-Correction $def $expectedCorrection $settings
321+
}
322+
323+
290324
if ($PSVersionTable.PSVersion -ge [Version]'5.0.0') {
291325
It "Does not count violation in DSC class" {
292326
$dscViolations.Count | Should Be 0

0 commit comments

Comments
 (0)