Skip to content

Commit dba5985

Browse files
committed
Fix issue with dependency injection weirdly setting Disclaimer to true and fix when dialog is closed rather than just button
1 parent aa4ecf8 commit dba5985

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

src/PowerShellEditorServices/Services/TextDocument/RenameService.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ internal interface IRenameService
4848
internal class RenameService(
4949
WorkspaceService workspaceService,
5050
ILanguageServerFacade lsp,
51-
ILanguageServerConfiguration config,
52-
bool disclaimerDeclinedForSession = false,
53-
bool disclaimerAcceptedForSession = false,
54-
string configSection = "powershell.rename"
51+
ILanguageServerConfiguration config
5552
) : IRenameService
5653
{
54+
internal bool DisclaimerAcceptedForSession; //This is exposed to allow testing non-interactively
55+
private bool DisclaimerDeclinedForSession;
56+
private const string ConfigSection = "powershell.rename";
5757

5858
public async Task<RangeOrPlaceholderRange?> PrepareRenameSymbol(PrepareRenameParams request, CancellationToken cancellationToken)
5959
{
@@ -211,8 +211,10 @@ internal static ScriptExtentAdapter GetFunctionNameExtent(FunctionDefinitionAst
211211
/// <returns>true if accepted, false if rejected</returns>
212212
private async Task<bool> AcceptRenameDisclaimer(bool acceptDisclaimerOption, CancellationToken cancellationToken)
213213
{
214-
if (disclaimerDeclinedForSession) { return false; }
215-
if (acceptDisclaimerOption || disclaimerAcceptedForSession) { return true; }
214+
const string disclaimerDeclinedMessage = "PowerShell rename has been disabled for this session as the disclaimer message was declined. Please restart the extension if you wish to use rename and accept the disclaimer.";
215+
216+
if (DisclaimerDeclinedForSession) { throw new HandlerErrorException(disclaimerDeclinedMessage); }
217+
if (acceptDisclaimerOption || DisclaimerAcceptedForSession) { return true; }
216218

217219
// TODO: Localization
218220
const string renameDisclaimer = "PowerShell rename functionality is only supported in a limited set of circumstances. [Please review the notice](https://github.com/PowerShell/PowerShellEditorServices?tab=readme-ov-file#rename-disclaimer) and accept the limitations and risks.";
@@ -235,8 +237,9 @@ private async Task<bool> AcceptRenameDisclaimer(bool acceptDisclaimerOption, Can
235237
}
236238
};
237239

238-
MessageActionItem result = await lsp.SendRequest(reqParams, cancellationToken).ConfigureAwait(false);
239-
if (result.Title == declineAnswer)
240+
MessageActionItem? result = await lsp.SendRequest(reqParams, cancellationToken).ConfigureAwait(false);
241+
// null happens if the user closes the dialog rather than making a selection.
242+
if (result is null || result.Title == declineAnswer)
240243
{
241244
const string renameDisabledNotice = "PowerShell Rename functionality will be disabled for this session and you will not be prompted again until restart.";
242245

@@ -246,8 +249,8 @@ private async Task<bool> AcceptRenameDisclaimer(bool acceptDisclaimerOption, Can
246249
Type = MessageType.Info
247250
};
248251
lsp.SendNotification(msgParams);
249-
disclaimerDeclinedForSession = true;
250-
return !disclaimerDeclinedForSession;
252+
DisclaimerDeclinedForSession = true;
253+
throw new HandlerErrorException(disclaimerDeclinedMessage);
251254
}
252255
if (result.Title == acceptAnswer)
253256
{
@@ -259,8 +262,8 @@ private async Task<bool> AcceptRenameDisclaimer(bool acceptDisclaimerOption, Can
259262
};
260263
lsp.SendNotification(msgParams);
261264

262-
disclaimerAcceptedForSession = true;
263-
return disclaimerAcceptedForSession;
265+
DisclaimerAcceptedForSession = true;
266+
return DisclaimerAcceptedForSession;
264267
}
265268
// if (result.Title == acceptWorkspaceAnswer)
266269
// {
@@ -279,7 +282,7 @@ private async Task<bool> AcceptRenameDisclaimer(bool acceptDisclaimerOption, Can
279282
private async Task<RenameServiceOptions> GetScopedSettings(DocumentUri uri, CancellationToken cancellationToken = default)
280283
{
281284
IScopedConfiguration scopedConfig = await config.GetScopedConfiguration(uri, cancellationToken).ConfigureAwait(false);
282-
return scopedConfig.GetSection(configSection).Get<RenameServiceOptions>() ?? new RenameServiceOptions();
285+
return scopedConfig.GetSection(ConfigSection).Get<RenameServiceOptions>() ?? new RenameServiceOptions();
283286
}
284287
}
285288

test/PowerShellEditorServices.Test/Refactoring/PrepareRenameHandlerTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ public PrepareRenameHandlerTests()
4343
(
4444
workspace,
4545
new fakeLspSendMessageRequestFacade("I Accept"),
46-
new EmptyConfiguration(),
47-
disclaimerAcceptedForSession: true //Suppresses prompts
46+
new EmptyConfiguration()
4847
)
48+
{
49+
DisclaimerAcceptedForSession = true //Disables UI prompts
50+
}
4951
);
5052
}
5153

test/PowerShellEditorServices.Test/Refactoring/RenameHandlerTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ public RenameHandlerTests()
3636
(
3737
workspace,
3838
new fakeLspSendMessageRequestFacade("I Accept"),
39-
new EmptyConfiguration(),
40-
disclaimerAcceptedForSession: true //Disables UI prompts
39+
new EmptyConfiguration()
4140
)
41+
{
42+
DisclaimerAcceptedForSession = true //Disables UI prompts
43+
}
4244
);
4345
}
4446

0 commit comments

Comments
 (0)