Skip to content

Commit 3336814

Browse files
committed
Refine VariableVisitor to use ScriptExtentAdapter
1 parent bf1515a commit 3336814

File tree

3 files changed

+14
-44
lines changed

3 files changed

+14
-44
lines changed

src/PowerShellEditorServices/Services/PowerShell/Handlers/RenameHandler.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public async Task<RangeOrPlaceholderRange> Handle(PrepareRenameParams request, C
4141
if (token is null) { return null; }
4242

4343
// TODO: Really should have a class with implicit convertors handing these conversions to avoid off-by-one mistakes.
44-
return Utilities.ToRange(token.Extent); ;
44+
return new ScriptExtentAdapter(token.Extent);
4545
}
4646

4747
/// <summary>
@@ -211,7 +211,6 @@ public ScriptPositionAdapter(Position position) : this(position.Line + 1, positi
211211
public static implicit operator ScriptPositionAdapter(ScriptPosition position) => new(position);
212212

213213
public static implicit operator Position(ScriptPositionAdapter scriptPosition) => new(scriptPosition.position.LineNumber - 1, scriptPosition.position.ColumnNumber - 1);
214-
215214
public static implicit operator ScriptPosition(ScriptPositionAdapter position) => position;
216215

217216
internal ScriptPositionAdapter Delta(int LineAdjust, int ColumnAdjust) => new(
@@ -242,26 +241,20 @@ internal record ScriptExtentAdapter(IScriptExtent extent) : IScriptExtent
242241
public ScriptPositionAdapter End = new(extent.EndScriptPosition);
243242

244243
public static implicit operator ScriptExtentAdapter(ScriptExtent extent) => new(extent);
245-
public static implicit operator ScriptExtent(ScriptExtentAdapter extent) => extent;
246-
247-
public static implicit operator Range(ScriptExtentAdapter extent) => new()
248-
{
249-
Start = extent.Start,
250-
End = extent.End
251-
// End = extent.End with
252-
// {
253-
// // The end position in Script Extents is actually shifted an additional 1 column, no idea why
254-
// // https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.language.iscriptextent.endscriptposition?view=powershellsdk-7.4.0#system-management-automation-language-iscriptextent-endscriptposition
255-
256-
// position = (extent.EndScriptPosition as ScriptPositionAdapter).Delta(0, -1)
257-
// }
258-
};
259244
public static implicit operator ScriptExtentAdapter(Range range) => new(new ScriptExtent(
260245
// Will get shifted to 1-based
261246
new ScriptPositionAdapter(range.Start),
262247
new ScriptPositionAdapter(range.End)
263248
));
264249

250+
public static implicit operator ScriptExtent(ScriptExtentAdapter adapter) => adapter;
251+
public static implicit operator Range(ScriptExtentAdapter adapter) => new()
252+
{
253+
Start = adapter.Start,
254+
End = adapter.End
255+
};
256+
public static implicit operator RangeOrPlaceholderRange(ScriptExtentAdapter adapter) => new(adapter);
257+
265258
public IScriptPosition StartScriptPosition => Start;
266259
public IScriptPosition EndScriptPosition => End;
267260
public int EndColumnNumber => End.ColumnNumber;

src/PowerShellEditorServices/Services/PowerShell/Refactoring/IterativeVariableVisitor.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ private void ProcessVariableExpressionAst(VariableExpressionAst variableExpressi
400400
TextEdit Change = new()
401401
{
402402
NewText = NewName.Contains("$") ? NewName : "$" + NewName,
403-
Range = Utilities.ToRange(variableExpressionAst.Extent),
403+
Range = new ScriptExtentAdapter(variableExpressionAst.Extent),
404404
};
405405
// If the variables parent is a parameterAst Add a modification
406406
if (variableExpressionAst.Parent is ParameterAst paramAst && !AliasSet &&
@@ -432,7 +432,7 @@ private void ProcessCommandParameterAst(CommandParameterAst commandParameterAst)
432432
TextEdit Change = new()
433433
{
434434
NewText = NewName.Contains("-") ? NewName : "-" + NewName,
435-
Range = Utilities.ToRange(commandParameterAst.Extent)
435+
Range = new ScriptExtentAdapter(commandParameterAst.Extent)
436436
};
437437
Modifications.Add(Change);
438438
}
@@ -466,7 +466,7 @@ assignmentStatementAst.Right is CommandExpressionAst commExpAst &&
466466
TextEdit Change = new()
467467
{
468468
NewText = NewName,
469-
Range = Utilities.ToRange(strConstAst.Extent)
469+
Range = new ScriptExtentAdapter(strConstAst.Extent)
470470
};
471471

472472
Modifications.Add(Change);
@@ -500,7 +500,7 @@ internal TextEdit NewParameterAliasChange(VariableExpressionAst variableExpressi
500500
aliasChange = aliasChange with
501501
{
502502
NewText = $"[Alias({nentries})]",
503-
Range = Utilities.ToRange(AttrAst.Extent)
503+
Range = new ScriptExtentAdapter(AttrAst.Extent)
504504
};
505505
}
506506
}
@@ -510,7 +510,7 @@ internal TextEdit NewParameterAliasChange(VariableExpressionAst variableExpressi
510510
aliasChange = aliasChange with
511511
{
512512
NewText = $"[Alias(\"{OldName}\")]",
513-
Range = Utilities.ToRange(paramAst.Extent)
513+
Range = new ScriptExtentAdapter(paramAst.Extent)
514514
};
515515
}
516516

src/PowerShellEditorServices/Services/PowerShell/Refactoring/Utilities.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,11 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.Management.Automation.Language;
8-
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
98

109
namespace Microsoft.PowerShell.EditorServices.Refactoring
1110
{
1211
internal class Utilities
1312
{
14-
/// <summary>
15-
/// Helper function to convert 1-based script positions to zero-based LSP positions
16-
/// </summary>
17-
/// <param name="extent"></param>
18-
/// <returns></returns>
19-
public static Range ToRange(IScriptExtent extent)
20-
{
21-
return new Range
22-
{
23-
Start = new Position
24-
{
25-
Line = extent.StartLineNumber - 1,
26-
Character = extent.StartColumnNumber - 1
27-
},
28-
End = new Position
29-
{
30-
Line = extent.EndLineNumber - 1,
31-
Character = extent.EndColumnNumber - 1
32-
}
33-
};
34-
}
35-
3613
public static Ast GetAstAtPositionOfType(int StartLineNumber, int StartColumnNumber, Ast ScriptAst, params Type[] type)
3714
{
3815
Ast result = null;

0 commit comments

Comments
 (0)