Skip to content

Commit 2ef44e8

Browse files
committed
Roslynator cleanups across handlers
1 parent 5e2681d commit 2ef44e8

28 files changed

+247
-392
lines changed

src/PowerShellEditorServices/Server/PsesLanguageServer.cs

+10-12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.PowerShell.EditorServices.Services.Extension;
1313
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
1414
using Microsoft.PowerShell.EditorServices.Services.Template;
15+
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
1516
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
1617
using OmniSharp.Extensions.LanguageServer.Server;
1718
using Serilog;
@@ -23,16 +24,13 @@ namespace Microsoft.PowerShell.EditorServices.Server
2324
/// </summary>
2425
internal class PsesLanguageServer
2526
{
26-
internal ILoggerFactory LoggerFactory { get; private set; }
27-
27+
internal ILoggerFactory LoggerFactory { get; }
2828
internal ILanguageServer LanguageServer { get; private set; }
29-
3029
private readonly LogLevel _minimumLogLevel;
3130
private readonly Stream _inputStream;
3231
private readonly Stream _outputStream;
3332
private readonly HostStartupInfo _hostDetails;
3433
private readonly TaskCompletionSource<bool> _serverStart;
35-
3634
private PsesInternalHost _psesHost;
3735

3836
/// <summary>
@@ -75,13 +73,13 @@ public async Task StartAsync()
7573
{
7674
LanguageServer = await OmniSharp.Extensions.LanguageServer.Server.LanguageServer.From(options =>
7775
{
78-
options
76+
_ = options
7977
.WithInput(_inputStream)
8078
.WithOutput(_outputStream)
8179
.WithServices(serviceCollection =>
8280
{
8381
// NOTE: This adds a lot of services!
84-
serviceCollection.AddPsesLanguageServices(_hostDetails);
82+
_ = serviceCollection.AddPsesLanguageServices(_hostDetails);
8583
})
8684
.ConfigureLogging(builder => builder
8785
.AddSerilog(Log.Logger) // TODO: Set dispose to true?
@@ -117,26 +115,26 @@ public async Task StartAsync()
117115
// _Initialize_ request:
118116
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#initialize
119117
.OnInitialize(
120-
(languageServer, request, cancellationToken) =>
118+
(languageServer, request, _) =>
121119
{
122120
Log.Logger.Debug("Initializing OmniSharp Language Server");
123121

124122
IServiceProvider serviceProvider = languageServer.Services;
125123

126124
_psesHost = serviceProvider.GetService<PsesInternalHost>();
127125

128-
var workspaceService = serviceProvider.GetService<WorkspaceService>();
126+
WorkspaceService workspaceService = serviceProvider.GetService<WorkspaceService>();
129127

130128
// Grab the workspace path from the parameters
131-
if (request.RootUri != null)
129+
if (request.RootUri is not null)
132130
{
133131
workspaceService.WorkspacePath = request.RootUri.GetFileSystemPath();
134132
}
135-
else if (request.WorkspaceFolders != null)
133+
else if (request.WorkspaceFolders is not null)
136134
{
137135
// If RootUri isn't set, try to use the first WorkspaceFolder.
138136
// TODO: Support multi-workspace.
139-
foreach (var workspaceFolder in request.WorkspaceFolders)
137+
foreach (WorkspaceFolder workspaceFolder in request.WorkspaceFolders)
140138
{
141139
workspaceService.WorkspacePath = workspaceFolder.Uri.GetFileSystemPath();
142140
break;
@@ -157,7 +155,7 @@ public async Task StartAsync()
157155
public async Task WaitForShutdown()
158156
{
159157
Log.Logger.Debug("Shutting down OmniSharp Language Server");
160-
await _serverStart.Task.ConfigureAwait(false);
158+
_ = await _serverStart.Task.ConfigureAwait(false);
161159
await LanguageServer.WaitForExit.ConfigureAwait(false);
162160

163161
// Doing this means we're able to route through any exceptions experienced on the pipeline thread

src/PowerShellEditorServices/Services/DebugAdapter/Handlers/BreakpointHandlers.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ await _debugService.SetCommandBreakpointsAsync(
147147
catch (Exception e)
148148
{
149149
// Log whatever the error is
150-
_logger.LogException($"Caught error while setting command breakpoints", e);
150+
_logger.LogException("Caught error while setting command breakpoints", e);
151151
}
152152
finally
153153
{

src/PowerShellEditorServices/Services/DebugAdapter/Handlers/LaunchAndAttachHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ internal record PsesAttachRequestArguments : AttachRequestArguments
8484

8585
internal class LaunchAndAttachHandler : ILaunchHandler<PsesLaunchRequestArguments>, IAttachHandler<PsesAttachRequestArguments>, IOnDebugAdapterServerStarted
8686
{
87-
private static readonly Version s_minVersionForCustomPipeName = new Version(6, 2);
87+
private static readonly Version s_minVersionForCustomPipeName = new(6, 2);
8888
private readonly ILogger<LaunchAndAttachHandler> _logger;
8989
private readonly BreakpointService _breakpointService;
9090
private readonly DebugService _debugService;

src/PowerShellEditorServices/Services/DebugAdapter/Handlers/ScopesHandler.cs

+3-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Linq;
55
using System.Threading;
66
using System.Threading.Tasks;
7-
using Microsoft.Extensions.Logging;
87
using Microsoft.PowerShell.EditorServices.Services;
98
using Microsoft.PowerShell.EditorServices.Services.DebugAdapter;
109
using Microsoft.PowerShell.EditorServices.Utility;
@@ -15,27 +14,20 @@ namespace Microsoft.PowerShell.EditorServices.Handlers
1514
{
1615
internal class ScopesHandler : IScopesHandler
1716
{
18-
private readonly ILogger _logger;
1917
private readonly DebugService _debugService;
2018

21-
public ScopesHandler(
22-
ILoggerFactory loggerFactory,
23-
DebugService debugService)
19+
public ScopesHandler(DebugService debugService)
2420
{
25-
_logger = loggerFactory.CreateLogger<ScopesHandler>();
2621
_debugService = debugService;
2722
}
2823

2924
public Task<ScopesResponse> Handle(ScopesArguments request, CancellationToken cancellationToken)
3025
{
31-
VariableScope[] variableScopes =
32-
_debugService.GetVariableScopes(
33-
(int) request.FrameId);
26+
VariableScope[] variableScopes = _debugService.GetVariableScopes((int)request.FrameId);
3427

3528
return Task.FromResult(new ScopesResponse
3629
{
37-
Scopes = new Container<Scope>(variableScopes
38-
.Select(LspDebugUtils.CreateScope))
30+
Scopes = new Container<Scope>(variableScopes.Select(LspDebugUtils.CreateScope))
3931
});
4032
}
4133
}

src/PowerShellEditorServices/Services/DebugAdapter/Handlers/StackTraceHandler.cs

+6-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Collections.Generic;
66
using System.Threading;
77
using System.Threading.Tasks;
8-
using Microsoft.Extensions.Logging;
98
using Microsoft.PowerShell.EditorServices.Services;
109
using Microsoft.PowerShell.EditorServices.Services.DebugAdapter;
1110
using Microsoft.PowerShell.EditorServices.Utility;
@@ -16,25 +15,20 @@ namespace Microsoft.PowerShell.EditorServices.Handlers
1615
{
1716
internal class StackTraceHandler : IStackTraceHandler
1817
{
19-
private readonly ILogger _logger;
2018
private readonly DebugService _debugService;
2119

22-
public StackTraceHandler(
23-
ILoggerFactory loggerFactory,
24-
DebugService debugService)
20+
public StackTraceHandler(DebugService debugService)
2521
{
26-
_logger = loggerFactory.CreateLogger<StackTraceHandler>();
2722
_debugService = debugService;
2823
}
2924

3025
public Task<StackTraceResponse> Handle(StackTraceArguments request, CancellationToken cancellationToken)
3126
{
32-
StackFrameDetails[] stackFrameDetails =
33-
_debugService.GetStackFrames(cancellationToken);
27+
StackFrameDetails[] stackFrameDetails = _debugService.GetStackFrames(cancellationToken);
3428

3529
// Handle a rare race condition where the adapter requests stack frames before they've
3630
// begun building.
37-
if (stackFrameDetails == null)
31+
if (stackFrameDetails is null)
3832
{
3933
return Task.FromResult(new StackTraceResponse
4034
{
@@ -43,14 +37,14 @@ public Task<StackTraceResponse> Handle(StackTraceArguments request, Cancellation
4337
});
4438
}
4539

46-
List<StackFrame> newStackFrames = new List<StackFrame>();
40+
List<StackFrame> newStackFrames = new();
4741

4842
long startFrameIndex = request.StartFrame ?? 0;
4943
long maxFrameCount = stackFrameDetails.Length;
5044

5145
// If the number of requested levels == 0 (or null), that means get all stack frames
5246
// after the specified startFrame index. Otherwise get all the stack frames.
53-
long requestedFrameCount = (request.Levels ?? 0);
47+
long requestedFrameCount = request.Levels ?? 0;
5448
if (requestedFrameCount > 0)
5549
{
5650
maxFrameCount = Math.Min(maxFrameCount, startFrameIndex + requestedFrameCount);
@@ -64,8 +58,7 @@ public Task<StackTraceResponse> Handle(StackTraceArguments request, Cancellation
6458
// StackFrame.Create(
6559
// stackFrameDetails[i],
6660
// i));
67-
newStackFrames.Add(
68-
LspDebugUtils.CreateStackFrame(stackFrameDetails[i], id: i));
61+
newStackFrames.Add(LspDebugUtils.CreateStackFrame(stackFrameDetails[i], id: i));
6962
}
7063

7164
return Task.FromResult(new StackTraceResponse

src/PowerShellEditorServices/Services/DebugAdapter/Handlers/VariablesHandler.cs

+3-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Linq;
66
using System.Threading;
77
using System.Threading.Tasks;
8-
using Microsoft.Extensions.Logging;
98
using Microsoft.PowerShell.EditorServices.Services;
109
using Microsoft.PowerShell.EditorServices.Services.DebugAdapter;
1110
using Microsoft.PowerShell.EditorServices.Utility;
@@ -15,33 +14,24 @@ namespace Microsoft.PowerShell.EditorServices.Handlers
1514
{
1615
internal class VariablesHandler : IVariablesHandler
1716
{
18-
private readonly ILogger _logger;
1917
private readonly DebugService _debugService;
2018

21-
public VariablesHandler(
22-
ILoggerFactory loggerFactory,
23-
DebugService debugService)
19+
public VariablesHandler(DebugService debugService)
2420
{
25-
_logger = loggerFactory.CreateLogger<VariablesHandler>();
2621
_debugService = debugService;
2722
}
2823

2924
public Task<VariablesResponse> Handle(VariablesArguments request, CancellationToken cancellationToken)
3025
{
31-
VariableDetailsBase[] variables =
32-
_debugService.GetVariables(
33-
(int)request.VariablesReference);
26+
VariableDetailsBase[] variables = _debugService.GetVariables((int)request.VariablesReference);
3427

3528
VariablesResponse variablesResponse = null;
3629

3730
try
3831
{
3932
variablesResponse = new VariablesResponse
4033
{
41-
Variables =
42-
variables
43-
.Select(LspDebugUtils.CreateVariable)
44-
.ToArray()
34+
Variables = variables.Select(LspDebugUtils.CreateVariable).ToArray()
4535
};
4636
}
4737
catch (Exception)

src/PowerShellEditorServices/Services/Extension/Handlers/IInvokeExtensionCommandHandler.cs

-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,5 @@ internal class ClientEditorContext
2828
public Position CursorPosition { get; set; }
2929

3030
public Range SelectionRange { get; set; }
31-
3231
}
3332
}

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

+3-13
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
using System.Management.Automation;
55
using System.Threading;
66
using System.Threading.Tasks;
7-
using Microsoft.Extensions.Logging;
87
using Microsoft.PowerShell.EditorServices.Services.PowerShell;
98
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution;
10-
using Microsoft.PowerShell.EditorServices.Utility;
119

1210
namespace Microsoft.PowerShell.EditorServices.Handlers
1311
{
@@ -17,14 +15,10 @@ namespace Microsoft.PowerShell.EditorServices.Handlers
1715
/// </summary>
1816
internal class EvaluateHandler : IEvaluateHandler
1917
{
20-
private readonly ILogger _logger;
2118
private readonly IInternalPowerShellExecutionService _executionService;
2219

23-
public EvaluateHandler(
24-
ILoggerFactory factory,
25-
IInternalPowerShellExecutionService executionService)
20+
public EvaluateHandler(IInternalPowerShellExecutionService executionService)
2621
{
27-
_logger = factory.CreateLogger<EvaluateHandler>();
2822
_executionService = executionService;
2923
}
3024

@@ -34,7 +28,7 @@ public async Task<EvaluateResponseBody> Handle(EvaluateRequestArguments request,
3428
// (or other foreground task).
3529
await _executionService.ExecutePSCommandAsync(
3630
new PSCommand().AddScript(request.Expression),
37-
CancellationToken.None,
31+
cancellationToken,
3832
new PowerShellExecutionOptions
3933
{
4034
WriteInputToHost = true,
@@ -45,11 +39,7 @@ await _executionService.ExecutePSCommandAsync(
4539
}).ConfigureAwait(false);
4640

4741
// TODO: Should we return a more informative result?
48-
return new EvaluateResponseBody
49-
{
50-
Result = "",
51-
VariablesReference = 0
52-
};
42+
return new EvaluateResponseBody { Result = "", VariablesReference = 0 };
5343
}
5444
}
5545
}

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using System.Collections.Generic;
45
using System.Linq;
56
using System.Management.Automation;
67
using System.Threading;
78
using System.Threading.Tasks;
8-
using Microsoft.Extensions.Logging;
99
using MediatR;
10-
using OmniSharp.Extensions.JsonRpc;
1110
using Microsoft.PowerShell.EditorServices.Services.PowerShell;
11+
using OmniSharp.Extensions.JsonRpc;
1212

1313
namespace Microsoft.PowerShell.EditorServices.Handlers
1414
{
@@ -27,12 +27,10 @@ internal class ExpandAliasResult
2727

2828
internal class ExpandAliasHandler : IExpandAliasHandler
2929
{
30-
private readonly ILogger _logger;
3130
private readonly IInternalPowerShellExecutionService _executionService;
3231

33-
public ExpandAliasHandler(ILoggerFactory factory, IInternalPowerShellExecutionService executionService)
32+
public ExpandAliasHandler(IInternalPowerShellExecutionService executionService)
3433
{
35-
_logger = factory.CreateLogger<ExpandAliasHandler>();
3634
_executionService = executionService;
3735
}
3836

@@ -63,13 +61,15 @@ function __Expand-Alias {
6361
}";
6462

6563
// TODO: Refactor to not rerun the function definition every time.
66-
var psCommand = new PSCommand();
67-
psCommand
64+
PSCommand psCommand = new PSCommand()
6865
.AddScript(script)
6966
.AddStatement()
7067
.AddCommand("__Expand-Alias")
7168
.AddArgument(request.Text);
72-
var result = await _executionService.ExecutePSCommandAsync<string>(psCommand, cancellationToken).ConfigureAwait(false);
69+
70+
IEnumerable<string> result = await _executionService.ExecutePSCommandAsync<string>(
71+
psCommand,
72+
cancellationToken).ConfigureAwait(false);
7373

7474
return new ExpandAliasResult
7575
{

0 commit comments

Comments
 (0)