19
19
20
20
namespace Microsoft . PowerShell . EditorServices . Services ;
21
21
22
+ internal class RenameServiceOptions
23
+ {
24
+ internal bool createFunctionAlias { get ; set ; }
25
+ internal bool createVariableAlias { get ; set ; }
26
+ internal bool acceptDisclaimer { get ; set ; }
27
+ }
28
+
22
29
public interface IRenameService
23
30
{
24
31
/// <summary>
@@ -38,15 +45,17 @@ public interface IRenameService
38
45
internal class RenameService (
39
46
WorkspaceService workspaceService ,
40
47
ILanguageServerFacade lsp ,
41
- ILanguageServerConfiguration config
48
+ ILanguageServerConfiguration config ,
49
+ string configSection = "powershell.rename"
42
50
) : IRenameService
43
51
{
44
52
private bool disclaimerDeclined ;
53
+ private readonly RenameServiceOptions options = new ( ) ;
45
54
46
55
public async Task < RangeOrPlaceholderRange ? > PrepareRenameSymbol ( PrepareRenameParams request , CancellationToken cancellationToken )
47
56
{
57
+ config . GetSection ( configSection ) . Bind ( options ) ;
48
58
if ( ! await AcceptRenameDisclaimer ( cancellationToken ) . ConfigureAwait ( false ) ) { return null ; }
49
-
50
59
ScriptFile scriptFile = workspaceService . GetFile ( request . TextDocument . Uri ) ;
51
60
52
61
// TODO: Is this too aggressive? We can still rename inside a var/function even if dotsourcing is in use in a file, we just need to be clear it's not supported to expect rename actions to propogate.
@@ -70,6 +79,7 @@ ILanguageServerConfiguration config
70
79
71
80
public async Task < WorkspaceEdit ? > RenameSymbol ( RenameParams request , CancellationToken cancellationToken )
72
81
{
82
+ config . GetSection ( configSection ) . Bind ( options ) ;
73
83
if ( ! await AcceptRenameDisclaimer ( cancellationToken ) . ConfigureAwait ( false ) ) { return null ; }
74
84
75
85
ScriptFile scriptFile = workspaceService . GetFile ( request . TextDocument . Uri ) ;
@@ -172,7 +182,7 @@ internal static TextEdit[] RenameVariable(Ast symbol, Ast scriptAst, RenameParam
172
182
/// <summary>
173
183
/// Return an extent that only contains the position of the name of the function, for Client highlighting purposes.
174
184
/// </summary>
175
- public static ScriptExtentAdapter GetFunctionNameExtent ( FunctionDefinitionAst ast )
185
+ internal static ScriptExtentAdapter GetFunctionNameExtent ( FunctionDefinitionAst ast )
176
186
{
177
187
string name = ast . Name ;
178
188
// FIXME: Gather dynamically from the AST and include backticks and whatnot that might be present
@@ -194,17 +204,19 @@ private async Task<bool> AcceptRenameDisclaimer(CancellationToken cancellationTo
194
204
if ( disclaimerDeclined ) { return false ; }
195
205
196
206
// FIXME: This should be referencing an options type that is initialized with the Service or is a getter.
197
- if ( config . GetSection ( "powershell" ) . GetValue < bool > ( "acceptRenameDisclaimer" ) ) { return true ; }
207
+ if ( options . acceptDisclaimer ) { return true ; }
198
208
199
209
// TODO: Localization
210
+ const string renameDisclaimer = "PowerShell rename functionality is only supported in a limited set of circumstances. Please review the notice and understand the limitations and risks." ;
200
211
const string acceptAnswer = "I Accept" ;
201
212
const string acceptWorkspaceAnswer = "I Accept [Workspace]" ;
202
213
const string acceptSessionAnswer = "I Accept [Session]" ;
203
214
const string declineAnswer = "Decline" ;
215
+
204
216
ShowMessageRequestParams reqParams = new ( )
205
217
{
206
218
Type = MessageType . Warning ,
207
- Message = "Test Send" ,
219
+ Message = renameDisclaimer ,
208
220
Actions = new MessageActionItem [ ] {
209
221
new MessageActionItem ( ) { Title = acceptAnswer } ,
210
222
new MessageActionItem ( ) { Title = acceptWorkspaceAnswer } ,
@@ -216,9 +228,11 @@ private async Task<bool> AcceptRenameDisclaimer(CancellationToken cancellationTo
216
228
MessageActionItem result = await lsp . SendRequest ( reqParams , cancellationToken ) . ConfigureAwait ( false ) ;
217
229
if ( result . Title == declineAnswer )
218
230
{
231
+ const string renameDisabledNotice = "PowerShell Rename functionality will be disabled for this session and you will not be prompted again until restart." ;
232
+
219
233
ShowMessageParams msgParams = new ( )
220
234
{
221
- Message = "PowerShell Rename functionality will be disabled for this session and you will not be prompted again until restart." ,
235
+ Message = renameDisabledNotice ,
222
236
Type = MessageType . Info
223
237
} ;
224
238
lsp . SendNotification ( msgParams ) ;
@@ -250,9 +264,9 @@ private async Task<bool> AcceptRenameDisclaimer(CancellationToken cancellationTo
250
264
/// You should use a new instance for each rename operation.
251
265
/// Skipverify can be used as a performance optimization when you are sure you are in scope.
252
266
/// </summary>
253
- public class RenameFunctionVisitor ( Ast target , string newName , bool skipVerify = false ) : AstVisitor
267
+ internal class RenameFunctionVisitor ( Ast target , string newName , bool skipVerify = false ) : AstVisitor
254
268
{
255
- public List < TextEdit > Edits { get ; } = new ( ) ;
269
+ internal List < TextEdit > Edits { get ; } = new ( ) ;
256
270
private Ast ? CurrentDocument ;
257
271
private FunctionDefinitionAst ? FunctionToRename ;
258
272
@@ -353,18 +367,13 @@ private TextEdit GetRenameFunctionEdit(Ast candidate)
353
367
} ;
354
368
}
355
369
356
- public TextEdit [ ] VisitAndGetEdits ( Ast ast )
370
+ internal TextEdit [ ] VisitAndGetEdits ( Ast ast )
357
371
{
358
372
ast . Visit ( this ) ;
359
373
return Edits . ToArray ( ) ;
360
374
}
361
375
}
362
376
363
- public class RenameSymbolOptions
364
- {
365
- public bool CreateAlias { get ; set ; }
366
- }
367
-
368
377
internal class Utilities
369
378
{
370
379
public static Ast ? GetAstAtPositionOfType ( int StartLineNumber , int StartColumnNumber , Ast ScriptAst , params Type [ ] type )
@@ -519,8 +528,8 @@ public int CompareTo(ScriptPositionAdapter other)
519
528
/// <param name="extent"></param>
520
529
internal record ScriptExtentAdapter ( IScriptExtent extent ) : IScriptExtent
521
530
{
522
- public ScriptPositionAdapter Start = new ( extent . StartScriptPosition ) ;
523
- public ScriptPositionAdapter End = new ( extent . EndScriptPosition ) ;
531
+ internal ScriptPositionAdapter Start = new ( extent . StartScriptPosition ) ;
532
+ internal ScriptPositionAdapter End = new ( extent . EndScriptPosition ) ;
524
533
525
534
public static implicit operator ScriptExtentAdapter ( ScriptExtent extent ) => new ( extent ) ;
526
535
0 commit comments