@@ -54,7 +54,7 @@ ILanguageServerConfiguration config
54
54
internal bool DisclaimerAcceptedForSession ; //This is exposed to allow testing non-interactively
55
55
private bool DisclaimerDeclinedForSession ;
56
56
private const string ConfigSection = "powershell.rename" ;
57
-
57
+ private RenameServiceOptions ? options ;
58
58
public async Task < RangeOrPlaceholderRange ? > PrepareRenameSymbol ( PrepareRenameParams request , CancellationToken cancellationToken )
59
59
{
60
60
RenameParams renameRequest = new ( )
@@ -78,7 +78,7 @@ ILanguageServerConfiguration config
78
78
public async Task < WorkspaceEdit ? > RenameSymbol ( RenameParams request , CancellationToken cancellationToken )
79
79
{
80
80
// We want scoped settings because a workspace setting might be relevant here.
81
- RenameServiceOptions options = await GetScopedSettings ( request . TextDocument . Uri , cancellationToken ) . ConfigureAwait ( false ) ;
81
+ options = await GetScopedSettings ( request . TextDocument . Uri , cancellationToken ) . ConfigureAwait ( false ) ;
82
82
83
83
if ( ! await AcceptRenameDisclaimer ( options . acceptDisclaimer , cancellationToken ) . ConfigureAwait ( false ) ) { return null ; }
84
84
@@ -98,7 +98,7 @@ or CommandAst
98
98
VariableExpressionAst
99
99
or CommandParameterAst
100
100
or StringConstantExpressionAst
101
- => RenameVariable ( tokenToRename , scriptFile . ScriptAst , request , options . createParameterAlias ) ,
101
+ => RenameVariable ( tokenToRename , scriptFile . ScriptAst , request ) ,
102
102
103
103
_ => throw new InvalidOperationException ( "This should not happen as PrepareRename should have already checked for viability. File an issue if you see this." )
104
104
} ;
@@ -120,10 +120,10 @@ private static TextEdit[] RenameFunction(Ast target, Ast scriptAst, RenameParams
120
120
return visitor . VisitAndGetEdits ( scriptAst ) ;
121
121
}
122
122
123
- private static TextEdit [ ] RenameVariable ( Ast symbol , Ast scriptAst , RenameParams requestParams , bool createParameterAlias )
123
+ private TextEdit [ ] RenameVariable ( Ast symbol , Ast scriptAst , RenameParams requestParams )
124
124
{
125
- NewRenameVariableVisitor visitor = new (
126
- symbol , requestParams . NewName
125
+ RenameVariableVisitor visitor = new (
126
+ symbol , requestParams . NewName , createParameterAlias : options ? . createParameterAlias ?? false
127
127
) ;
128
128
return visitor . VisitAndGetEdits ( scriptAst ) ;
129
129
}
@@ -407,7 +407,7 @@ internal static bool IsValidFunctionName(string name)
407
407
}
408
408
}
409
409
410
- internal class NewRenameVariableVisitor ( Ast target , string newName , bool skipVerify = false ) : RenameVisitorBase
410
+ internal class RenameVariableVisitor ( Ast target , string newName , bool skipVerify = false , bool createParameterAlias = false ) : RenameVisitorBase
411
411
{
412
412
// Used to store the original definition of the variable to use as a reference.
413
413
internal Ast ? VariableDefinition ;
@@ -446,6 +446,24 @@ internal AstVisitAction Visit(Ast ast)
446
446
447
447
if ( ShouldRename ( ast ) )
448
448
{
449
+ if (
450
+ createParameterAlias
451
+ && ast == VariableDefinition
452
+ && VariableDefinition is not null and VariableExpressionAst varDefAst
453
+ && varDefAst . Parent is ParameterAst paramAst
454
+ )
455
+ {
456
+ Edits . Add ( new TextEdit
457
+ {
458
+ NewText = $ "[Alias('{ varDefAst . VariablePath . UserPath } ')]",
459
+ Range = new Range ( )
460
+ {
461
+ Start = new ScriptPositionAdapter ( paramAst . Extent . StartScriptPosition ) ,
462
+ End = new ScriptPositionAdapter ( paramAst . Extent . StartScriptPosition )
463
+ }
464
+ } ) ;
465
+ }
466
+
449
467
Edits . Add ( GetRenameVariableEdit ( ast ) ) ;
450
468
}
451
469
0 commit comments