Skip to content

Commit 553ed7b

Browse files
committed
Add more config setup and lock down some classes
1 parent 1ada59e commit 553ed7b

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

src/PowerShellEditorServices/Services/TextDocument/RenameService.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919

2020
namespace Microsoft.PowerShell.EditorServices.Services;
2121

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+
2229
public interface IRenameService
2330
{
2431
/// <summary>
@@ -38,15 +45,17 @@ public interface IRenameService
3845
internal class RenameService(
3946
WorkspaceService workspaceService,
4047
ILanguageServerFacade lsp,
41-
ILanguageServerConfiguration config
48+
ILanguageServerConfiguration config,
49+
string configSection = "powershell.rename"
4250
) : IRenameService
4351
{
4452
private bool disclaimerDeclined;
53+
private readonly RenameServiceOptions options = new();
4554

4655
public async Task<RangeOrPlaceholderRange?> PrepareRenameSymbol(PrepareRenameParams request, CancellationToken cancellationToken)
4756
{
57+
config.GetSection(configSection).Bind(options);
4858
if (!await AcceptRenameDisclaimer(cancellationToken).ConfigureAwait(false)) { return null; }
49-
5059
ScriptFile scriptFile = workspaceService.GetFile(request.TextDocument.Uri);
5160

5261
// 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
7079

7180
public async Task<WorkspaceEdit?> RenameSymbol(RenameParams request, CancellationToken cancellationToken)
7281
{
82+
config.GetSection(configSection).Bind(options);
7383
if (!await AcceptRenameDisclaimer(cancellationToken).ConfigureAwait(false)) { return null; }
7484

7585
ScriptFile scriptFile = workspaceService.GetFile(request.TextDocument.Uri);
@@ -172,7 +182,7 @@ internal static TextEdit[] RenameVariable(Ast symbol, Ast scriptAst, RenameParam
172182
/// <summary>
173183
/// Return an extent that only contains the position of the name of the function, for Client highlighting purposes.
174184
/// </summary>
175-
public static ScriptExtentAdapter GetFunctionNameExtent(FunctionDefinitionAst ast)
185+
internal static ScriptExtentAdapter GetFunctionNameExtent(FunctionDefinitionAst ast)
176186
{
177187
string name = ast.Name;
178188
// 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
194204
if (disclaimerDeclined) { return false; }
195205

196206
// 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; }
198208

199209
// 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.";
200211
const string acceptAnswer = "I Accept";
201212
const string acceptWorkspaceAnswer = "I Accept [Workspace]";
202213
const string acceptSessionAnswer = "I Accept [Session]";
203214
const string declineAnswer = "Decline";
215+
204216
ShowMessageRequestParams reqParams = new()
205217
{
206218
Type = MessageType.Warning,
207-
Message = "Test Send",
219+
Message = renameDisclaimer,
208220
Actions = new MessageActionItem[] {
209221
new MessageActionItem() { Title = acceptAnswer },
210222
new MessageActionItem() { Title = acceptWorkspaceAnswer },
@@ -216,9 +228,11 @@ private async Task<bool> AcceptRenameDisclaimer(CancellationToken cancellationTo
216228
MessageActionItem result = await lsp.SendRequest(reqParams, cancellationToken).ConfigureAwait(false);
217229
if (result.Title == declineAnswer)
218230
{
231+
const string renameDisabledNotice = "PowerShell Rename functionality will be disabled for this session and you will not be prompted again until restart.";
232+
219233
ShowMessageParams msgParams = new()
220234
{
221-
Message = "PowerShell Rename functionality will be disabled for this session and you will not be prompted again until restart.",
235+
Message = renameDisabledNotice,
222236
Type = MessageType.Info
223237
};
224238
lsp.SendNotification(msgParams);
@@ -250,9 +264,9 @@ private async Task<bool> AcceptRenameDisclaimer(CancellationToken cancellationTo
250264
/// You should use a new instance for each rename operation.
251265
/// Skipverify can be used as a performance optimization when you are sure you are in scope.
252266
/// </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
254268
{
255-
public List<TextEdit> Edits { get; } = new();
269+
internal List<TextEdit> Edits { get; } = new();
256270
private Ast? CurrentDocument;
257271
private FunctionDefinitionAst? FunctionToRename;
258272

@@ -353,18 +367,13 @@ private TextEdit GetRenameFunctionEdit(Ast candidate)
353367
};
354368
}
355369

356-
public TextEdit[] VisitAndGetEdits(Ast ast)
370+
internal TextEdit[] VisitAndGetEdits(Ast ast)
357371
{
358372
ast.Visit(this);
359373
return Edits.ToArray();
360374
}
361375
}
362376

363-
public class RenameSymbolOptions
364-
{
365-
public bool CreateAlias { get; set; }
366-
}
367-
368377
internal class Utilities
369378
{
370379
public static Ast? GetAstAtPositionOfType(int StartLineNumber, int StartColumnNumber, Ast ScriptAst, params Type[] type)
@@ -519,8 +528,8 @@ public int CompareTo(ScriptPositionAdapter other)
519528
/// <param name="extent"></param>
520529
internal record ScriptExtentAdapter(IScriptExtent extent) : IScriptExtent
521530
{
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);
524533

525534
public static implicit operator ScriptExtentAdapter(ScriptExtent extent) => new(extent);
526535

0 commit comments

Comments
 (0)