Skip to content

Commit 2a9c6ad

Browse files
committed
Add disclaimer scaffolding (needs config)
1 parent 5cc72ce commit 2a9c6ad

File tree

2 files changed

+71
-21
lines changed

2 files changed

+71
-21
lines changed

src/PowerShellEditorServices/Server/PsesLanguageServer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ public async Task StartAsync()
9191
.ClearProviders()
9292
.AddPsesLanguageServerLogging()
9393
.SetMinimumLevel(_minimumLogLevel))
94+
// TODO: Consider replacing all WithHandler with AddSingleton
95+
.WithConfigurationSection("powershell")
9496
.WithHandler<PsesWorkspaceSymbolsHandler>()
9597
.WithHandler<PsesTextDocumentHandler>()
9698
.WithHandler<GetVersionHandler>()

src/PowerShellEditorServices/Services/TextDocument/RenameService.cs

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Management.Automation.Language;
99
using System.Threading;
1010
using System.Threading.Tasks;
11+
using Microsoft.Extensions.Configuration;
1112
using Microsoft.PowerShell.EditorServices.Handlers;
1213
using Microsoft.PowerShell.EditorServices.Language;
1314
using Microsoft.PowerShell.EditorServices.Refactoring;
@@ -40,40 +41,26 @@ internal class RenameService(
4041
ILanguageServerConfiguration config
4142
) : IRenameService
4243
{
44+
private bool disclaimerDeclined;
45+
4346
public async Task<RangeOrPlaceholderRange?> PrepareRenameSymbol(PrepareRenameParams request, CancellationToken cancellationToken)
4447
{
45-
// FIXME: Config actually needs to be read and implemented, this is to make the referencing satisfied
46-
// config.ToString();
47-
// ShowMessageRequestParams reqParams = new()
48-
// {
49-
// Type = MessageType.Warning,
50-
// Message = "Test Send",
51-
// Actions = new MessageActionItem[] {
52-
// new MessageActionItem() { Title = "I Accept" },
53-
// new MessageActionItem() { Title = "I Accept [Workspace]" },
54-
// new MessageActionItem() { Title = "Decline" }
55-
// }
56-
// };
57-
58-
// MessageActionItem result = await lsp.SendRequest(reqParams, cancellationToken).ConfigureAwait(false);
59-
// if (result.Title == "Test Action")
60-
// {
61-
// // FIXME: Need to accept
62-
// Console.WriteLine("yay");
63-
// }
48+
if (!await AcceptRenameDisclaimer(cancellationToken).ConfigureAwait(false)) { return null; }
6449

6550
ScriptFile scriptFile = workspaceService.GetFile(request.TextDocument.Uri);
6651

67-
// 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 take rename actions inside the dotsourced file.
52+
// 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.
6853
if (Utilities.AssertContainsDotSourced(scriptFile.ScriptAst))
6954
{
7055
throw new HandlerErrorException("Dot Source detected, this is currently not supported");
7156
}
7257

58+
// TODO: FindRenamableSymbol may create false positives for renaming, so we probably should go ahead and execute a full rename and return true if edits are found.
59+
7360
ScriptPositionAdapter position = request.Position;
7461
Ast? target = FindRenamableSymbol(scriptFile, position);
7562

76-
// Since 3.16 we can simply basically return a DefaultBehavior true or null to signal to the client that the position is valid for rename and it should use its default selection criteria (which is probably the language semantic highlighting or grammar). For the current scope of the rename provider, this should be fine, but we have the option to supply the specific range in the future for special cases.
63+
// Since LSP 3.16 we can simply basically return a DefaultBehavior true or null to signal to the client that the position is valid for rename and it should use its default selection criteria (which is probably the language semantic highlighting or grammar). For the current scope of the rename provider, this should be fine, but we have the option to supply the specific range in the future for special cases.
7764
RangeOrPlaceholderRange? renamable = target is null ? null : new RangeOrPlaceholderRange
7865
(
7966
new RenameDefaultBehavior() { DefaultBehavior = true }
@@ -83,6 +70,7 @@ ILanguageServerConfiguration config
8370

8471
public async Task<WorkspaceEdit?> RenameSymbol(RenameParams request, CancellationToken cancellationToken)
8572
{
73+
if (!await AcceptRenameDisclaimer(cancellationToken).ConfigureAwait(false)) { return null; }
8674

8775
ScriptFile scriptFile = workspaceService.GetFile(request.TextDocument.Uri);
8876
ScriptPositionAdapter position = request.Position;
@@ -195,6 +183,66 @@ public static ScriptExtentAdapter GetFunctionNameExtent(FunctionDefinitionAst as
195183

196184
return funcExtent;
197185
}
186+
187+
/// <summary>
188+
/// Prompts the user to accept the rename disclaimer.
189+
/// </summary>
190+
/// <returns>true if accepted, false if rejected</returns>
191+
private async Task<bool> AcceptRenameDisclaimer(CancellationToken cancellationToken)
192+
{
193+
// User has declined for the session so we don't want this popping up a bunch.
194+
if (disclaimerDeclined) { return false; }
195+
196+
// 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; }
198+
199+
// TODO: Localization
200+
const string acceptAnswer = "I Accept";
201+
const string acceptWorkspaceAnswer = "I Accept [Workspace]";
202+
const string acceptSessionAnswer = "I Accept [Session]";
203+
const string declineAnswer = "Decline";
204+
ShowMessageRequestParams reqParams = new()
205+
{
206+
Type = MessageType.Warning,
207+
Message = "Test Send",
208+
Actions = new MessageActionItem[] {
209+
new MessageActionItem() { Title = acceptAnswer },
210+
new MessageActionItem() { Title = acceptWorkspaceAnswer },
211+
new MessageActionItem() { Title = acceptSessionAnswer },
212+
new MessageActionItem() { Title = declineAnswer }
213+
}
214+
};
215+
216+
MessageActionItem result = await lsp.SendRequest(reqParams, cancellationToken).ConfigureAwait(false);
217+
if (result.Title == declineAnswer)
218+
{
219+
ShowMessageParams msgParams = new()
220+
{
221+
Message = "PowerShell Rename functionality will be disabled for this session and you will not be prompted again until restart.",
222+
Type = MessageType.Info
223+
};
224+
lsp.SendNotification(msgParams);
225+
disclaimerDeclined = true;
226+
return !disclaimerDeclined;
227+
}
228+
if (result.Title == acceptAnswer)
229+
{
230+
// FIXME: Set the appropriate setting
231+
return true;
232+
}
233+
if (result.Title == acceptWorkspaceAnswer)
234+
{
235+
// FIXME: Set the appropriate setting
236+
return true;
237+
}
238+
if (result.Title == acceptSessionAnswer)
239+
{
240+
// FIXME: Set the appropriate setting
241+
return true;
242+
}
243+
244+
throw new InvalidOperationException("Unknown Disclaimer Response received. This is a bug and you should report it.");
245+
}
198246
}
199247

200248
/// <summary>

0 commit comments

Comments
 (0)