Skip to content

Commit ec06e57

Browse files
committed
Remove useless ToArray of BreakpointDetails
This can just be an `IEnumerable` throughout instead.
1 parent c6c902d commit ec06e57

File tree

4 files changed

+41
-46
lines changed

4 files changed

+41
-46
lines changed

src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ public DebugService(
130130
/// <param name="breakpoints">BreakpointDetails for each breakpoint that will be set.</param>
131131
/// <param name="clearExisting">If true, causes all existing breakpoints to be cleared before setting new ones.</param>
132132
/// <returns>An awaitable Task that will provide details about the breakpoints that were set.</returns>
133-
public async Task<BreakpointDetails[]> SetLineBreakpointsAsync(
133+
public async Task<IEnumerable<BreakpointDetails>> SetLineBreakpointsAsync(
134134
ScriptFile scriptFile,
135-
BreakpointDetails[] breakpoints,
135+
IEnumerable<BreakpointDetails> breakpoints,
136136
bool clearExisting = true)
137137
{
138138
DscBreakpointCapability dscBreakpoints = await _debugContext.GetDscBreakpointCapabilityAsync(CancellationToken.None).ConfigureAwait(false);
@@ -146,15 +146,15 @@ public async Task<BreakpointDetails[]> SetLineBreakpointsAsync(
146146
if (!_remoteFileManager.IsUnderRemoteTempPath(scriptPath))
147147
{
148148
_logger.LogTrace($"Could not set breakpoints for local path '{scriptPath}' in a remote session.");
149-
return Array.Empty<BreakpointDetails>();
149+
return Enumerable.Empty<BreakpointDetails>();
150150
}
151151

152152
scriptPath = _remoteFileManager.GetMappedPath(scriptPath, _psesHost.CurrentRunspace);
153153
}
154154
else if (temporaryScriptListingPath?.Equals(scriptPath, StringComparison.CurrentCultureIgnoreCase) == true)
155155
{
156156
_logger.LogTrace($"Could not set breakpoint on temporary script listing path '{scriptPath}'.");
157-
return Array.Empty<BreakpointDetails>();
157+
return Enumerable.Empty<BreakpointDetails>();
158158
}
159159

160160
// Fix for issue #123 - file paths that contain wildcard chars [ and ] need to
@@ -168,7 +168,7 @@ public async Task<BreakpointDetails[]> SetLineBreakpointsAsync(
168168
await _breakpointService.RemoveAllBreakpointsAsync(scriptFile.FilePath).ConfigureAwait(false);
169169
}
170170

171-
return (await _breakpointService.SetBreakpointsAsync(escapedScriptPath, breakpoints).ConfigureAwait(false)).ToArray();
171+
return await _breakpointService.SetBreakpointsAsync(escapedScriptPath, breakpoints).ConfigureAwait(false);
172172
}
173173

174174
return await dscBreakpoints

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.IO;
67
using System.Linq;
78
using System.Threading;
@@ -82,18 +83,17 @@ public async Task<SetBreakpointsResponse> Handle(SetBreakpointsArguments request
8283
}
8384

8485
// At this point, the source file has been verified as a PowerShell script.
85-
BreakpointDetails[] breakpointDetails = request.Breakpoints
86+
IEnumerable<BreakpointDetails> breakpointDetails = request.Breakpoints
8687
.Select((srcBreakpoint) => BreakpointDetails.Create(
8788
scriptFile.FilePath,
8889
srcBreakpoint.Line,
8990
srcBreakpoint.Column,
9091
srcBreakpoint.Condition,
9192
srcBreakpoint.HitCondition,
92-
srcBreakpoint.LogMessage))
93-
.ToArray();
93+
srcBreakpoint.LogMessage));
9494

9595
// If this is a "run without debugging (Ctrl+F5)" session ignore requests to set breakpoints.
96-
BreakpointDetails[] updatedBreakpointDetails = breakpointDetails;
96+
IEnumerable<BreakpointDetails> updatedBreakpointDetails = breakpointDetails;
9797
if (!_debugStateService.NoDebug)
9898
{
9999
await _debugStateService.WaitForSetBreakpointHandleAsync().ConfigureAwait(false);

src/PowerShellEditorServices/Services/PowerShell/Debugging/DscBreakpointCapability.cs

+6-11
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,19 @@ internal class DscBreakpointCapability
2222
{
2323
private string[] dscResourceRootPaths = Array.Empty<string>();
2424

25-
private readonly Dictionary<string, int[]> breakpointsPerFile =
26-
new();
25+
private readonly Dictionary<string, int[]> breakpointsPerFile = new();
2726

28-
public async Task<BreakpointDetails[]> SetLineBreakpointsAsync(
27+
public async Task<IEnumerable<BreakpointDetails>> SetLineBreakpointsAsync(
2928
IInternalPowerShellExecutionService executionService,
3029
string scriptPath,
31-
BreakpointDetails[] breakpoints)
30+
IEnumerable<BreakpointDetails> breakpoints)
3231
{
33-
List<BreakpointDetails> resultBreakpointDetails =
34-
new();
35-
3632
// We always get the latest array of breakpoint line numbers
3733
// so store that for future use
38-
if (breakpoints.Length > 0)
34+
if (breakpoints.Any())
3935
{
4036
// Set the breakpoints for this scriptPath
41-
breakpointsPerFile[scriptPath] =
42-
breakpoints.Select(b => b.LineNumber).ToArray();
37+
breakpointsPerFile[scriptPath] = breakpoints.Select(b => b.LineNumber).ToArray();
4338
}
4439
else
4540
{
@@ -72,7 +67,7 @@ await executionService.ExecutePSCommandAsync(
7267
breakpoint.Verified = true;
7368
}
7469

75-
return breakpoints.ToArray();
70+
return breakpoints;
7671
}
7772

7873
public bool IsDscResourcePath(string scriptPath)

test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs

+26-26
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ await debugService.SetCommandBreakpointsAsync(
203203
[MemberData(nameof(DebuggerAcceptsScriptArgsTestData))]
204204
public async Task DebuggerAcceptsScriptArgs(string[] args)
205205
{
206-
BreakpointDetails[] breakpoints = await debugService.SetLineBreakpointsAsync(
206+
IEnumerable<BreakpointDetails> breakpoints = await debugService.SetLineBreakpointsAsync(
207207
oddPathScriptFile,
208208
new[] { BreakpointDetails.Create(oddPathScriptFile.FilePath, 3) }).ConfigureAwait(true);
209209

@@ -264,8 +264,8 @@ public async Task DebuggerSetsAndClearsFunctionBreakpoints()
264264
}).ConfigureAwait(true);
265265

266266
Assert.Equal(2, breakpoints.Length);
267-
Assert.Equal("Write-Host", breakpoints[0].Name);
268-
Assert.Equal("Get-Date", breakpoints[1].Name);
267+
Assert.Equal("Write-Host", breakpoints.ElementAt(0).Name);
268+
Assert.Equal("Get-Date", breakpoints.ElementAt(1).Name);
269269

270270
breakpoints = await debugService.SetCommandBreakpointsAsync(
271271
new[] { CommandBreakpointDetails.Create("Get-Host") }).ConfigureAwait(true);
@@ -311,7 +311,7 @@ public async Task DebuggerStopsOnFunctionBreakpoints()
311311
[Fact]
312312
public async Task DebuggerSetsAndClearsLineBreakpoints()
313313
{
314-
BreakpointDetails[] breakpoints =
314+
IEnumerable<BreakpointDetails> breakpoints =
315315
await debugService.SetLineBreakpointsAsync(
316316
debugScriptFile,
317317
new[] {
@@ -322,16 +322,16 @@ await debugService.SetLineBreakpointsAsync(
322322
IReadOnlyList<LineBreakpoint> confirmedBreakpoints = await GetConfirmedBreakpoints(debugScriptFile).ConfigureAwait(true);
323323

324324
Assert.Equal(2, confirmedBreakpoints.Count);
325-
Assert.Equal(5, breakpoints[0].LineNumber);
326-
Assert.Equal(10, breakpoints[1].LineNumber);
325+
Assert.Equal(5, breakpoints.ElementAt(0).LineNumber);
326+
Assert.Equal(10, breakpoints.ElementAt(1).LineNumber);
327327

328328
breakpoints = await debugService.SetLineBreakpointsAsync(
329329
debugScriptFile,
330330
new[] { BreakpointDetails.Create(debugScriptFile.FilePath, 2) }).ConfigureAwait(true);
331331
confirmedBreakpoints = await GetConfirmedBreakpoints(debugScriptFile).ConfigureAwait(true);
332332

333333
Assert.Single(confirmedBreakpoints);
334-
Assert.Equal(2, breakpoints[0].LineNumber);
334+
Assert.Equal(2, breakpoints.ElementAt(0).LineNumber);
335335

336336
await debugService.SetLineBreakpointsAsync(
337337
debugScriptFile,
@@ -442,7 +442,7 @@ await debugService.SetLineBreakpointsAsync(
442442
[Fact]
443443
public async Task DebuggerProvidesMessageForInvalidConditionalBreakpoint()
444444
{
445-
BreakpointDetails[] breakpoints =
445+
IEnumerable<BreakpointDetails> breakpoints =
446446
await debugService.SetLineBreakpointsAsync(
447447
debugScriptFile,
448448
new[] {
@@ -457,36 +457,36 @@ await debugService.SetLineBreakpointsAsync(
457457
}).ConfigureAwait(true);
458458

459459
Assert.Single(breakpoints);
460-
// Assert.Equal(5, breakpoints[0].LineNumber);
461-
// Assert.True(breakpoints[0].Verified);
462-
// Assert.Null(breakpoints[0].Message);
463-
464-
Assert.Equal(10, breakpoints[0].LineNumber);
465-
Assert.False(breakpoints[0].Verified);
466-
Assert.NotNull(breakpoints[0].Message);
467-
Assert.Contains("Unexpected token '-ez'", breakpoints[0].Message);
460+
// Assert.Equal(5, breakpoints.ElementAt(0).LineNumber);
461+
// Assert.True(breakpoints.ElementAt(0).Verified);
462+
// Assert.Null(breakpoints.ElementAt(0).Message);
463+
464+
Assert.Equal(10, breakpoints.ElementAt(0).LineNumber);
465+
Assert.False(breakpoints.ElementAt(0).Verified);
466+
Assert.NotNull(breakpoints.ElementAt(0).Message);
467+
Assert.Contains("Unexpected token '-ez'", breakpoints.ElementAt(0).Message);
468468
}
469469

470470
[Fact]
471471
public async Task DebuggerFindsParsableButInvalidSimpleBreakpointConditions()
472472
{
473-
BreakpointDetails[] breakpoints =
473+
IEnumerable<BreakpointDetails> breakpoints =
474474
await debugService.SetLineBreakpointsAsync(
475475
debugScriptFile,
476476
new[] {
477477
BreakpointDetails.Create(debugScriptFile.FilePath, 5, column: null, condition: "$i == 100"),
478478
BreakpointDetails.Create(debugScriptFile.FilePath, 7, column: null, condition: "$i > 100")
479479
}).ConfigureAwait(true);
480480

481-
Assert.Equal(2, breakpoints.Length);
482-
Assert.Equal(5, breakpoints[0].LineNumber);
483-
Assert.False(breakpoints[0].Verified);
484-
Assert.Contains("Use '-eq' instead of '=='", breakpoints[0].Message);
485-
486-
Assert.Equal(7, breakpoints[1].LineNumber);
487-
Assert.False(breakpoints[1].Verified);
488-
Assert.NotNull(breakpoints[1].Message);
489-
Assert.Contains("Use '-gt' instead of '>'", breakpoints[1].Message);
481+
Assert.Equal(2, breakpoints.Count());
482+
Assert.Equal(5, breakpoints.ElementAt(0).LineNumber);
483+
Assert.False(breakpoints.ElementAt(0).Verified);
484+
Assert.Contains("Use '-eq' instead of '=='", breakpoints.ElementAt(0).Message);
485+
486+
Assert.Equal(7, breakpoints.ElementAt(1).LineNumber);
487+
Assert.False(breakpoints.ElementAt(1).Verified);
488+
Assert.NotNull(breakpoints.ElementAt(1).Message);
489+
Assert.Contains("Use '-gt' instead of '>'", breakpoints.ElementAt(1).Message);
490490
}
491491

492492
[Fact]

0 commit comments

Comments
 (0)