Skip to content

Commit dccde57

Browse files
author
Kapil Borle
committed
Return lines instead of entire text in correction extent
1 parent 254b601 commit dccde57

1 file changed

Lines changed: 37 additions & 48 deletions

File tree

rules/UseSupportsShouldProcess.cs

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ private List<CorrectionExtent> GetCorrections(
194194
correctionExtents.Add(GetCorrectionExtentAddParamBlock(funcDefnAst, parameterAsts));
195195
}
196196

197+
// This is how we handle multiple edits.
198+
// create separate edits
199+
// apply those edits to the original script extent1
200+
// and then give the corrected extent as suggested correction.
201+
197202
// sort in descending order of start position
198203
correctionExtents.Sort((x, y) =>
199204
{
@@ -206,7 +211,7 @@ private List<CorrectionExtent> GetCorrections(
206211
foreach (var correctionExtent in correctionExtents)
207212
{
208213
var shiftedCorrectionExtent = Normalize(funcDefnAst.Extent, correctionExtent);
209-
editableText = editableText.ApplyEdit(shiftedCorrectionExtent);
214+
editableText = editableText.ApplyEdit1(shiftedCorrectionExtent);
210215
}
211216

212217
var result = new List<CorrectionExtent>();
@@ -219,10 +224,6 @@ private List<CorrectionExtent> GetCorrections(
219224
editableText.ToString(),
220225
funcDefnAst.Extent.File));
221226
return result;
222-
// This is how we handle multiple edits.
223-
// create separate edits
224-
// apply those edits to the original script extent1
225-
// and then give the corrected extent as suggested correction.
226227
}
227228

228229
private CorrectionExtent GetCorrectionExtentAddParamBlock(
@@ -233,53 +234,47 @@ private CorrectionExtent GetCorrectionExtentAddParamBlock(
233234
var paramBlockText = WriteParamBlock(parameterAsts);
234235
// TODO replace this hard coding
235236
var indentation = new string(' ', funcStartScriptPos.ColumnNumber + 3);
236-
var sb = new StringBuilder();
237-
sb.Append("{");
238-
sb.AppendLine();
239-
sb.Append(String.Join(
240-
Environment.NewLine,
241-
paramBlockText.GetLines().Select(line => indentation + line)));
242-
237+
var lines = new List<String>();
238+
lines.Add("{");
239+
lines.AddRange(paramBlockText.Select(line => indentation + line));
243240
return new CorrectionExtent(
244241
funcDefnAst.Body.Extent.StartLineNumber,
245242
funcDefnAst.Body.Extent.StartLineNumber,
246243
funcDefnAst.Body.Extent.StartColumnNumber,
247244
funcDefnAst.Body.Extent.StartColumnNumber + 1,
248-
sb.ToString(),
249-
funcDefnAst.Extent.File);
245+
lines,
246+
funcDefnAst.Extent.File,
247+
null);
250248
}
251249

252-
private string WriteParamBlock(ParameterAst[] parameterAsts)
250+
private IList<String> WriteParamBlock(ParameterAst[] parameterAsts)
253251
{
254-
var sb = new StringBuilder();
255-
sb.AppendLine("[CmdletBinding(SupportsShouldProcess)]");
256-
sb.AppendLine("param(");
252+
var lines = new List<String>();
253+
lines.Add("[CmdletBinding(SupportsShouldProcess)]");
254+
lines.Add("param(");
255+
257256
// TODO replace this hard coding
258257
string indentation = new string(' ', 4);
259258
int count = 0;
260-
foreach (var paramAst in parameterAsts)
259+
var usableParamAsts = parameterAsts.Where(p => !IsWhatIf(p) && !IsConfirm(p));
260+
var usableCount = usableParamAsts.Count();
261+
foreach (var paramAst in usableParamAsts)
261262
{
262263
count++;
263-
if (IsWhatIf(paramAst) || IsConfirm(paramAst))
264+
var suffix = ",";
265+
if (count == usableCount)
264266
{
265-
continue;
267+
suffix = "";
266268
}
267269

268270
foreach (var line in paramAst.Extent.Text.GetLines())
269271
{
270-
sb.Append(indentation);
271-
sb.Append(line);
272-
}
273-
274-
if (count != parameterAsts.Length)
275-
{
276-
sb.AppendLine(",");
272+
lines.Add(indentation + line + suffix);
277273
}
278274
}
279275

280-
sb.AppendLine();
281-
sb.AppendLine(")");
282-
return sb.ToString();
276+
lines.Add(")");
277+
return lines;
283278
}
284279

285280

@@ -293,10 +288,10 @@ private static CorrectionExtent GetCorrectionExtentRemoveParams(
293288
var rParenTokenIdx = Array.FindIndex(funcDefnTokens, tok => tok.Kind == TokenKind.RParen);
294289

295290
return new CorrectionExtent(
296-
tokens[lParenTokenIdx - 1].Extent.EndLineNumber,
297-
tokens[rParenTokenIdx].Extent.EndLineNumber,
298-
tokens[lParenTokenIdx - 1].Extent.EndColumnNumber,
299-
tokens[rParenTokenIdx].Extent.EndColumnNumber,
291+
funcDefnTokens[lParenTokenIdx - 1].Extent.EndLineNumber,
292+
funcDefnTokens[rParenTokenIdx].Extent.EndLineNumber,
293+
funcDefnTokens[lParenTokenIdx - 1].Extent.EndColumnNumber,
294+
funcDefnTokens[rParenTokenIdx].Extent.EndColumnNumber,
300295
"",
301296
ast.Extent.File);
302297
}
@@ -311,13 +306,7 @@ private CorrectionExtent Normalize(IScriptExtent referenceExtent, CorrectionExte
311306
referenceExtent.EndLineNumber,
312307
referenceExtent.EndColumnNumber);
313308

314-
var range = new Range(
315-
cextent.StartLineNumber,
316-
cextent.StartColumnNumber,
317-
cextent.EndLineNumber,
318-
cextent.EndColumnNumber);
319-
320-
var shiftedRange = Range.Normalize(refRange, range);
309+
var shiftedRange = Range.Normalize(refRange, cextent);
321310

322311
// TODO Add a method to TextEdit class that takes in range and text
323312
// TODO Add a method to CorrectionExtent that takes in range and all other stuff
@@ -344,17 +333,17 @@ private static bool TryGetCmdletBindingAttribute(
344333

345334
private static CorrectionExtent GetCorrectionExtent(ParamBlockAst paramBlockAst)
346335
{
347-
348-
string correctionText = new String(' ', paramBlockAst.Extent.StartColumnNumber - 1)
349-
+ "[CmdletBinding(SupportsShouldProcess)]"
350-
+ Environment.NewLine;
351-
352336
return new CorrectionExtent(
353337
paramBlockAst.Extent.StartLineNumber,
354338
paramBlockAst.Extent.StartLineNumber,
355339
1,
356340
1,
357-
correctionText,
341+
new String[] {
342+
new String(' ', paramBlockAst.Extent.StartColumnNumber - 1)
343+
+ "[CmdletBinding(SupportsShouldProcess)]",
344+
String.Empty
345+
},
346+
null,
358347
null);
359348
}
360349
private static CorrectionExtent GetCorrectionExtent(AttributeAst cmdletBindingAttributeAst)

0 commit comments

Comments
 (0)