19
19
20
20
namespace Microsoft . PowerShell . EditorServices . Services ;
21
21
22
- internal class RenameServiceOptions
22
+ public class RenameServiceOptions
23
23
{
24
24
internal bool createFunctionAlias { get ; set ; }
25
25
internal bool createVariableAlias { get ; set ; }
@@ -50,11 +50,15 @@ internal class RenameService(
50
50
) : IRenameService
51
51
{
52
52
private bool disclaimerDeclined ;
53
- private readonly RenameServiceOptions options = new ( ) ;
53
+ private bool disclaimerAccepted ;
54
+
55
+ private readonly RenameServiceOptions settings = new ( ) ;
56
+
57
+ internal void RefreshSettings ( ) => config . GetSection ( configSection ) . Bind ( settings ) ;
54
58
55
59
public async Task < RangeOrPlaceholderRange ? > PrepareRenameSymbol ( PrepareRenameParams request , CancellationToken cancellationToken )
56
60
{
57
- config . GetSection ( configSection ) . Bind ( options ) ;
61
+
58
62
if ( ! await AcceptRenameDisclaimer ( cancellationToken ) . ConfigureAwait ( false ) ) { return null ; }
59
63
ScriptFile scriptFile = workspaceService . GetFile ( request . TextDocument . Uri ) ;
60
64
@@ -79,7 +83,6 @@ internal class RenameService(
79
83
80
84
public async Task < WorkspaceEdit ? > RenameSymbol ( RenameParams request , CancellationToken cancellationToken )
81
85
{
82
- config . GetSection ( configSection ) . Bind ( options ) ;
83
86
if ( ! await AcceptRenameDisclaimer ( cancellationToken ) . ConfigureAwait ( false ) ) { return null ; }
84
87
85
88
ScriptFile scriptFile = workspaceService . GetFile ( request . TextDocument . Uri ) ;
@@ -119,7 +122,7 @@ internal static TextEdit[] RenameFunction(Ast target, Ast scriptAst, RenameParam
119
122
return visitor . VisitAndGetEdits ( scriptAst ) ;
120
123
}
121
124
122
- internal static TextEdit [ ] RenameVariable ( Ast symbol , Ast scriptAst , RenameParams requestParams )
125
+ internal TextEdit [ ] RenameVariable ( Ast symbol , Ast scriptAst , RenameParams requestParams )
123
126
{
124
127
if ( symbol is VariableExpressionAst or ParameterAst or CommandParameterAst or StringConstantExpressionAst )
125
128
{
@@ -129,11 +132,10 @@ internal static TextEdit[] RenameVariable(Ast symbol, Ast scriptAst, RenameParam
129
132
symbol . Extent . StartLineNumber ,
130
133
symbol . Extent . StartColumnNumber ,
131
134
scriptAst ,
132
- null //FIXME: Pass through Alias config
135
+ settings
133
136
) ;
134
137
visitor . Visit ( scriptAst ) ;
135
138
return visitor . Modifications . ToArray ( ) ;
136
-
137
139
}
138
140
return [ ] ;
139
141
}
@@ -200,28 +202,32 @@ internal static ScriptExtentAdapter GetFunctionNameExtent(FunctionDefinitionAst
200
202
/// <returns>true if accepted, false if rejected</returns>
201
203
private async Task < bool > AcceptRenameDisclaimer ( CancellationToken cancellationToken )
202
204
{
205
+ // Fetch the latest settings from the client, in case they have changed.
206
+ config . GetSection ( configSection ) . Bind ( settings ) ;
207
+
203
208
// User has declined for the session so we don't want this popping up a bunch.
204
209
if ( disclaimerDeclined ) { return false ; }
205
210
206
- // FIXME: This should be referencing an options type that is initialized with the Service or is a getter.
207
- if ( options . acceptDisclaimer ) { return true ; }
211
+ if ( settings . acceptDisclaimer || disclaimerAccepted ) { return true ; }
208
212
209
213
// TODO: Localization
210
214
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." ;
211
215
const string acceptAnswer = "I Accept" ;
212
- const string acceptWorkspaceAnswer = "I Accept [Workspace]" ;
213
- const string acceptSessionAnswer = "I Accept [Session]" ;
216
+ // const string acceptWorkspaceAnswer = "I Accept [Workspace]";
217
+ // const string acceptSessionAnswer = "I Accept [Session]";
214
218
const string declineAnswer = "Decline" ;
215
219
220
+ // TODO: Unfortunately the LSP spec has no spec for the server to change a client setting, so
221
+ // We have a suboptimal experience until we implement a custom feature for this.
216
222
ShowMessageRequestParams reqParams = new ( )
217
223
{
218
224
Type = MessageType . Warning ,
219
225
Message = renameDisclaimer ,
220
226
Actions = new MessageActionItem [ ] {
221
227
new MessageActionItem ( ) { Title = acceptAnswer } ,
222
- new MessageActionItem ( ) { Title = acceptWorkspaceAnswer } ,
223
- new MessageActionItem ( ) { Title = acceptSessionAnswer } ,
224
228
new MessageActionItem ( ) { Title = declineAnswer }
229
+ // new MessageActionItem() { Title = acceptWorkspaceAnswer },
230
+ // new MessageActionItem() { Title = acceptSessionAnswer },
225
231
}
226
232
} ;
227
233
@@ -241,19 +247,27 @@ private async Task<bool> AcceptRenameDisclaimer(CancellationToken cancellationTo
241
247
}
242
248
if ( result . Title == acceptAnswer )
243
249
{
244
- // FIXME: Set the appropriate setting
245
- return true ;
246
- }
247
- if ( result . Title == acceptWorkspaceAnswer )
248
- {
249
- // FIXME: Set the appropriate setting
250
- return true ;
251
- }
252
- if ( result . Title == acceptSessionAnswer )
253
- {
254
- // FIXME: Set the appropriate setting
255
- return true ;
250
+ const string acceptDisclaimerNotice = "PowerShell rename functionality has been enabled for this session. To avoid this prompt in the future, set the powershell.rename.acceptDisclaimer to true in your settings." ;
251
+ ShowMessageParams msgParams = new ( )
252
+ {
253
+ Message = acceptDisclaimerNotice ,
254
+ Type = MessageType . Info
255
+ } ;
256
+ lsp . SendNotification ( msgParams ) ;
257
+
258
+ disclaimerAccepted = true ;
259
+ return disclaimerAccepted ;
256
260
}
261
+ // if (result.Title == acceptWorkspaceAnswer)
262
+ // {
263
+ // // FIXME: Set the appropriate setting
264
+ // return true;
265
+ // }
266
+ // if (result.Title == acceptSessionAnswer)
267
+ // {
268
+ // // FIXME: Set the appropriate setting
269
+ // return true;
270
+ // }
257
271
258
272
throw new InvalidOperationException ( "Unknown Disclaimer Response received. This is a bug and you should report it." ) ;
259
273
}
0 commit comments