Skip to content

Commit b5838e8

Browse files
committed
Explicitly Show Unsaved Files as currently unsupported. It's probably doable but will take work
Also add convenience HandlerErrorException as RPCErrorException is super obtuse.
1 parent 4148425 commit b5838e8

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

src/PowerShellEditorServices/Services/PowerShell/Handlers/PrepareRenameSymbol.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Management.Automation.Language;
88
using OmniSharp.Extensions.JsonRpc;
99
using Microsoft.PowerShell.EditorServices.Services;
10-
using Microsoft.Extensions.Logging;
1110
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
1211
using Microsoft.PowerShell.EditorServices.Refactoring;
1312
using Microsoft.PowerShell.EditorServices.Services.Symbols;
@@ -31,21 +30,16 @@ internal class PrepareRenameSymbolResult
3130

3231
internal class PrepareRenameSymbolHandler : IPrepareRenameSymbolHandler
3332
{
34-
private readonly ILogger _logger;
3533
private readonly WorkspaceService _workspaceService;
3634

37-
public PrepareRenameSymbolHandler(ILoggerFactory loggerFactory, WorkspaceService workspaceService)
38-
{
39-
_logger = loggerFactory.CreateLogger<RenameSymbolHandler>();
40-
_workspaceService = workspaceService;
41-
}
35+
public PrepareRenameSymbolHandler(WorkspaceService workspaceService) => _workspaceService = workspaceService;
4236

4337
public async Task<PrepareRenameSymbolResult> Handle(PrepareRenameSymbolParams request, CancellationToken cancellationToken)
4438
{
4539
if (!_workspaceService.TryGetFile(request.FileName, out ScriptFile scriptFile))
4640
{
47-
_logger.LogDebug("Failed to open file!");
48-
return await Task.FromResult<PrepareRenameSymbolResult>(null).ConfigureAwait(false);
41+
// TODO: Unsaved file support. We need to find the unsaved file in the text documents synced to the LSP and use that as our Ast Base.
42+
throw new HandlerErrorException($"File {request.FileName} not found in workspace. Unsaved files currently do not support the rename symbol feature.");
4943
}
5044
return await Task.Run(() =>
5145
{

src/PowerShellEditorServices/Services/PowerShell/Handlers/RenameSymbol.cs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
using System.Management.Automation.Language;
99
using OmniSharp.Extensions.JsonRpc;
1010
using Microsoft.PowerShell.EditorServices.Services;
11-
using Microsoft.Extensions.Logging;
1211
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
1312
using Microsoft.PowerShell.EditorServices.Refactoring;
13+
using System;
1414
namespace Microsoft.PowerShell.EditorServices.Handlers
1515
{
1616
[Serial, Method("powerShell/renameSymbol")]
1717
internal interface IRenameSymbolHandler : IJsonRpcRequestHandler<RenameSymbolParams, RenameSymbolResult> { }
1818

19-
public class RenameSymbolOptions {
19+
public class RenameSymbolOptions
20+
{
2021
public bool CreateAlias { get; set; }
2122
}
2223

@@ -68,14 +69,10 @@ public class RenameSymbolResult
6869

6970
internal class RenameSymbolHandler : IRenameSymbolHandler
7071
{
71-
private readonly ILogger _logger;
7272
private readonly WorkspaceService _workspaceService;
7373

74-
public RenameSymbolHandler(ILoggerFactory loggerFactory, WorkspaceService workspaceService)
75-
{
76-
_logger = loggerFactory.CreateLogger<RenameSymbolHandler>();
77-
_workspaceService = workspaceService;
78-
}
74+
public RenameSymbolHandler(WorkspaceService workspaceService) => _workspaceService = workspaceService;
75+
7976
internal static ModifiedFileResponse RenameFunction(Ast token, Ast scriptAst, RenameSymbolParams request)
8077
{
8178
string tokenName = "";
@@ -98,20 +95,20 @@ internal static ModifiedFileResponse RenameFunction(Ast token, Ast scriptAst, Re
9895
Changes = visitor.Modifications
9996
};
10097
return FileModifications;
101-
102-
103-
10498
}
99+
105100
internal static ModifiedFileResponse RenameVariable(Ast symbol, Ast scriptAst, RenameSymbolParams request)
106101
{
107102
if (symbol is VariableExpressionAst or ParameterAst or CommandParameterAst or StringConstantExpressionAst)
108103
{
109104

110-
IterativeVariableRename visitor = new(request.RenameTo,
111-
symbol.Extent.StartLineNumber,
112-
symbol.Extent.StartColumnNumber,
113-
scriptAst,
114-
request.Options ?? null);
105+
IterativeVariableRename visitor = new(
106+
request.RenameTo,
107+
symbol.Extent.StartLineNumber,
108+
symbol.Extent.StartColumnNumber,
109+
scriptAst,
110+
request.Options ?? null
111+
);
115112
visitor.Visit(scriptAst);
116113
ModifiedFileResponse FileModifications = new(request.FileName)
117114
{
@@ -121,21 +118,18 @@ internal static ModifiedFileResponse RenameVariable(Ast symbol, Ast scriptAst, R
121118

122119
}
123120
return null;
124-
125121
}
122+
126123
public async Task<RenameSymbolResult> Handle(RenameSymbolParams request, CancellationToken cancellationToken)
127124
{
128125
if (!_workspaceService.TryGetFile(request.FileName, out ScriptFile scriptFile))
129126
{
130-
_logger.LogDebug("Failed to open file!");
131-
return await Task.FromResult<RenameSymbolResult>(null).ConfigureAwait(false);
127+
throw new InvalidOperationException("This should not happen as PrepareRename should have already checked for viability. File an issue if you see this.");
132128
}
133129

134130
return await Task.Run(() =>
135131
{
136-
137132
Ast token = Utilities.GetAst(request.Line + 1, request.Column + 1, scriptFile.ScriptAst);
138-
139133
if (token == null) { return null; }
140134

141135
ModifiedFileResponse FileModifications = (token is FunctionDefinitionAst || token.Parent is CommandAst)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using OmniSharp.Extensions.JsonRpc;
5+
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
6+
7+
namespace Microsoft.PowerShell.EditorServices.Handlers;
8+
9+
/// <summary>
10+
/// A convenience exception for handlers to throw when a request fails for a normal reason,
11+
/// and to communicate that reason to the user without a full internal stacktrace.
12+
/// </summary>
13+
/// <param name="message">The message describing the reason for the request failure.</param>
14+
/// <param name="logDetails">Additional details to be logged regarding the failure. It should be serializable to JSON.</param>
15+
/// <param name="severity">The severity level of the message. This is only shown in internal logging.</param>
16+
public class HandlerErrorException
17+
(
18+
string message,
19+
object logDetails = null,
20+
MessageType severity = MessageType.Error
21+
) : RpcErrorException((int)severity, logDetails!, message)
22+
{ }

0 commit comments

Comments
 (0)