Skip to content

Commit 2da8267

Browse files
committed
Add configuration and adjust opt-in message due to server-side LSP config limitation
1 parent 553ed7b commit 2da8267

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ internal class IterativeVariableRename
2424
internal bool isParam;
2525
internal bool AliasSet;
2626
internal FunctionDefinitionAst TargetFunction;
27-
internal RenameSymbolOptions options;
27+
internal RenameServiceOptions options;
2828

29-
public IterativeVariableRename(string NewName, int StartLineNumber, int StartColumnNumber, Ast ScriptAst, RenameSymbolOptions options = null)
29+
public IterativeVariableRename(string NewName, int StartLineNumber, int StartColumnNumber, Ast ScriptAst, RenameServiceOptions options)
3030
{
3131
this.NewName = NewName;
3232
this.StartLineNumber = StartLineNumber;
3333
this.StartColumnNumber = StartColumnNumber;
3434
this.ScriptAst = ScriptAst;
35-
this.options = options ?? new RenameSymbolOptions { CreateAlias = false };
35+
this.options = options;
3636

3737
VariableExpressionAst Node = (VariableExpressionAst)GetVariableTopAssignment(StartLineNumber, StartColumnNumber, ScriptAst);
3838
if (Node != null)
@@ -404,7 +404,7 @@ private void ProcessVariableExpressionAst(VariableExpressionAst variableExpressi
404404
};
405405
// If the variables parent is a parameterAst Add a modification
406406
if (variableExpressionAst.Parent is ParameterAst paramAst && !AliasSet &&
407-
options.CreateAlias)
407+
options.createVariableAlias)
408408
{
409409
TextEdit aliasChange = NewParameterAliasChange(variableExpressionAst, paramAst);
410410
Modifications.Add(aliasChange);

src/PowerShellEditorServices/Services/TextDocument/RenameService.cs

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
namespace Microsoft.PowerShell.EditorServices.Services;
2121

22-
internal class RenameServiceOptions
22+
public class RenameServiceOptions
2323
{
2424
internal bool createFunctionAlias { get; set; }
2525
internal bool createVariableAlias { get; set; }
@@ -50,11 +50,15 @@ internal class RenameService(
5050
) : IRenameService
5151
{
5252
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);
5458

5559
public async Task<RangeOrPlaceholderRange?> PrepareRenameSymbol(PrepareRenameParams request, CancellationToken cancellationToken)
5660
{
57-
config.GetSection(configSection).Bind(options);
61+
5862
if (!await AcceptRenameDisclaimer(cancellationToken).ConfigureAwait(false)) { return null; }
5963
ScriptFile scriptFile = workspaceService.GetFile(request.TextDocument.Uri);
6064

@@ -79,7 +83,6 @@ internal class RenameService(
7983

8084
public async Task<WorkspaceEdit?> RenameSymbol(RenameParams request, CancellationToken cancellationToken)
8185
{
82-
config.GetSection(configSection).Bind(options);
8386
if (!await AcceptRenameDisclaimer(cancellationToken).ConfigureAwait(false)) { return null; }
8487

8588
ScriptFile scriptFile = workspaceService.GetFile(request.TextDocument.Uri);
@@ -119,7 +122,7 @@ internal static TextEdit[] RenameFunction(Ast target, Ast scriptAst, RenameParam
119122
return visitor.VisitAndGetEdits(scriptAst);
120123
}
121124

122-
internal static TextEdit[] RenameVariable(Ast symbol, Ast scriptAst, RenameParams requestParams)
125+
internal TextEdit[] RenameVariable(Ast symbol, Ast scriptAst, RenameParams requestParams)
123126
{
124127
if (symbol is VariableExpressionAst or ParameterAst or CommandParameterAst or StringConstantExpressionAst)
125128
{
@@ -129,11 +132,10 @@ internal static TextEdit[] RenameVariable(Ast symbol, Ast scriptAst, RenameParam
129132
symbol.Extent.StartLineNumber,
130133
symbol.Extent.StartColumnNumber,
131134
scriptAst,
132-
null //FIXME: Pass through Alias config
135+
settings
133136
);
134137
visitor.Visit(scriptAst);
135138
return visitor.Modifications.ToArray();
136-
137139
}
138140
return [];
139141
}
@@ -200,28 +202,32 @@ internal static ScriptExtentAdapter GetFunctionNameExtent(FunctionDefinitionAst
200202
/// <returns>true if accepted, false if rejected</returns>
201203
private async Task<bool> AcceptRenameDisclaimer(CancellationToken cancellationToken)
202204
{
205+
// Fetch the latest settings from the client, in case they have changed.
206+
config.GetSection(configSection).Bind(settings);
207+
203208
// User has declined for the session so we don't want this popping up a bunch.
204209
if (disclaimerDeclined) { return false; }
205210

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; }
208212

209213
// TODO: Localization
210214
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.";
211215
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]";
214218
const string declineAnswer = "Decline";
215219

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.
216222
ShowMessageRequestParams reqParams = new()
217223
{
218224
Type = MessageType.Warning,
219225
Message = renameDisclaimer,
220226
Actions = new MessageActionItem[] {
221227
new MessageActionItem() { Title = acceptAnswer },
222-
new MessageActionItem() { Title = acceptWorkspaceAnswer },
223-
new MessageActionItem() { Title = acceptSessionAnswer },
224228
new MessageActionItem() { Title = declineAnswer }
229+
// new MessageActionItem() { Title = acceptWorkspaceAnswer },
230+
// new MessageActionItem() { Title = acceptSessionAnswer },
225231
}
226232
};
227233

@@ -241,19 +247,27 @@ private async Task<bool> AcceptRenameDisclaimer(CancellationToken cancellationTo
241247
}
242248
if (result.Title == acceptAnswer)
243249
{
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;
256260
}
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+
// }
257271

258272
throw new InvalidOperationException("Unknown Disclaimer Response received. This is a bug and you should report it.");
259273
}

0 commit comments

Comments
 (0)