@@ -23,11 +23,9 @@ internal class BreakpointService
23
23
private readonly DebugStateService _debugStateService ;
24
24
25
25
// TODO: This needs to be managed per nested session
26
- internal readonly Dictionary < string , HashSet < Breakpoint > > BreakpointsPerFile =
27
- new Dictionary < string , HashSet < Breakpoint > > ( ) ;
26
+ internal readonly Dictionary < string , HashSet < Breakpoint > > BreakpointsPerFile = new ( ) ;
28
27
29
- internal readonly HashSet < Breakpoint > CommandBreakpoints =
30
- new HashSet < Breakpoint > ( ) ;
28
+ internal readonly HashSet < Breakpoint > CommandBreakpoints = new ( ) ;
31
29
32
30
public BreakpointService (
33
31
ILoggerFactory factory ,
@@ -51,9 +49,11 @@ public async Task<List<Breakpoint>> GetBreakpointsAsync()
51
49
}
52
50
53
51
// Legacy behavior
54
- PSCommand psCommand = new PSCommand ( )
55
- . AddCommand ( @"Microsoft.PowerShell.Utility\Get-PSBreakpoint" ) ;
56
- IEnumerable < Breakpoint > breakpoints = await _executionService . ExecutePSCommandAsync < Breakpoint > ( psCommand , CancellationToken . None ) . ConfigureAwait ( false ) ;
52
+ PSCommand psCommand = new PSCommand ( ) . AddCommand ( @"Microsoft.PowerShell.Utility\Get-PSBreakpoint" ) ;
53
+ IEnumerable < Breakpoint > breakpoints = await _executionService
54
+ . ExecutePSCommandAsync < Breakpoint > ( psCommand , CancellationToken . None )
55
+ . ConfigureAwait ( false ) ;
56
+
57
57
return breakpoints . ToList ( ) ;
58
58
}
59
59
@@ -73,13 +73,12 @@ public async Task<IEnumerable<BreakpointDetails>> SetBreakpointsAsync(string esc
73
73
breakpointDetails . Verified = false ;
74
74
}
75
75
}
76
-
77
76
return breakpoints ;
78
77
}
79
78
80
79
// Legacy behavior
81
80
PSCommand psCommand = null ;
82
- List < BreakpointDetails > configuredBreakpoints = new List < BreakpointDetails > ( ) ;
81
+ List < BreakpointDetails > configuredBreakpoints = new ( ) ;
83
82
foreach ( BreakpointDetails breakpoint in breakpoints )
84
83
{
85
84
ScriptBlock actionScriptBlock = null ;
@@ -106,7 +105,7 @@ public async Task<IEnumerable<BreakpointDetails>> SetBreakpointsAsync(string esc
106
105
107
106
// On first iteration psCommand will be null, every subsequent
108
107
// iteration will need to start a new statement.
109
- if ( psCommand == null )
108
+ if ( psCommand is null )
110
109
{
111
110
psCommand = new PSCommand ( ) ;
112
111
}
@@ -121,61 +120,61 @@ public async Task<IEnumerable<BreakpointDetails>> SetBreakpointsAsync(string esc
121
120
. AddParameter ( "Line" , breakpoint . LineNumber ) ;
122
121
123
122
// Check if the user has specified the column number for the breakpoint.
124
- if ( breakpoint . ColumnNumber . HasValue && breakpoint . ColumnNumber . Value > 0 )
123
+ if ( breakpoint . ColumnNumber > 0 )
125
124
{
126
125
// It bums me out that PowerShell will silently ignore a breakpoint
127
126
// where either the line or the column is invalid. I'd rather have an
128
127
// error or warning message I could relay back to the client.
129
128
psCommand . AddParameter ( "Column" , breakpoint . ColumnNumber . Value ) ;
130
129
}
131
130
132
- if ( actionScriptBlock != null )
131
+ if ( actionScriptBlock is not null )
133
132
{
134
133
psCommand . AddParameter ( "Action" , actionScriptBlock ) ;
135
134
}
136
135
}
137
136
138
137
// If no PSCommand was created then there are no breakpoints to set.
139
- if ( psCommand != null )
138
+ if ( psCommand is not null )
140
139
{
141
- IEnumerable < Breakpoint > setBreakpoints =
142
- await _executionService . ExecutePSCommandAsync < Breakpoint > ( psCommand , CancellationToken . None ) . ConfigureAwait ( false ) ;
143
- configuredBreakpoints . AddRange (
144
- setBreakpoints . Select ( ( breakpoint ) => BreakpointDetails . Create ( breakpoint ) )
145
- ) ;
140
+ IEnumerable < Breakpoint > setBreakpoints = await _executionService
141
+ . ExecutePSCommandAsync < Breakpoint > ( psCommand , CancellationToken . None )
142
+ . ConfigureAwait ( false ) ;
143
+ configuredBreakpoints . AddRange ( setBreakpoints . Select ( ( breakpoint ) => BreakpointDetails . Create ( breakpoint ) ) ) ;
146
144
}
147
-
148
145
return configuredBreakpoints ;
149
146
}
150
147
151
- public async Task < IEnumerable < CommandBreakpointDetails > > SetCommandBreakpoints ( IEnumerable < CommandBreakpointDetails > breakpoints )
148
+ public async Task < IEnumerable < CommandBreakpointDetails > > SetCommandBreakpointsAsync ( IEnumerable < CommandBreakpointDetails > breakpoints )
152
149
{
153
150
if ( BreakpointApiUtils . SupportsBreakpointApis ( _editorServicesHost . CurrentRunspace ) )
154
151
{
155
152
foreach ( CommandBreakpointDetails commandBreakpointDetails in breakpoints )
156
153
{
157
154
try
158
155
{
159
- BreakpointApiUtils . SetBreakpoint ( _editorServicesHost . Runspace . Debugger , commandBreakpointDetails , _debugStateService . RunspaceId ) ;
156
+ BreakpointApiUtils . SetBreakpoint (
157
+ _editorServicesHost . Runspace . Debugger ,
158
+ commandBreakpointDetails ,
159
+ _debugStateService . RunspaceId ) ;
160
160
}
161
- catch ( InvalidOperationException e )
161
+ catch ( InvalidOperationException e )
162
162
{
163
163
commandBreakpointDetails . Message = e . Message ;
164
164
commandBreakpointDetails . Verified = false ;
165
165
}
166
166
}
167
-
168
167
return breakpoints ;
169
168
}
170
169
171
170
// Legacy behavior
172
171
PSCommand psCommand = null ;
173
- List < CommandBreakpointDetails > configuredBreakpoints = new List < CommandBreakpointDetails > ( ) ;
172
+ List < CommandBreakpointDetails > configuredBreakpoints = new ( ) ;
174
173
foreach ( CommandBreakpointDetails breakpoint in breakpoints )
175
174
{
176
175
// On first iteration psCommand will be null, every subsequent
177
176
// iteration will need to start a new statement.
178
- if ( psCommand == null )
177
+ if ( psCommand is null )
179
178
{
180
179
psCommand = new PSCommand ( ) ;
181
180
}
@@ -208,20 +207,18 @@ public async Task<IEnumerable<CommandBreakpointDetails>> SetCommandBreakpoints(I
208
207
configuredBreakpoints . Add ( breakpoint ) ;
209
208
continue ;
210
209
}
211
-
212
210
psCommand . AddParameter ( "Action" , actionScriptBlock ) ;
213
211
}
214
212
}
215
213
216
214
// If no PSCommand was created then there are no breakpoints to set.
217
- if ( psCommand != null )
215
+ if ( psCommand is not null )
218
216
{
219
- IEnumerable < Breakpoint > setBreakpoints =
220
- await _executionService . ExecutePSCommandAsync < Breakpoint > ( psCommand , CancellationToken . None ) . ConfigureAwait ( false ) ;
221
- configuredBreakpoints . AddRange (
222
- setBreakpoints . Select ( CommandBreakpointDetails . Create ) ) ;
217
+ IEnumerable < Breakpoint > setBreakpoints = await _executionService
218
+ . ExecutePSCommandAsync < Breakpoint > ( psCommand , CancellationToken . None )
219
+ . ConfigureAwait ( false ) ;
220
+ configuredBreakpoints . AddRange ( setBreakpoints . Select ( CommandBreakpointDetails . Create ) ) ;
223
221
}
224
-
225
222
return configuredBreakpoints ;
226
223
}
227
224
@@ -238,30 +235,26 @@ public async Task RemoveAllBreakpointsAsync(string scriptPath = null)
238
235
_editorServicesHost . Runspace . Debugger ,
239
236
_debugStateService . RunspaceId ) )
240
237
{
241
- if ( scriptPath == null || scriptPath == breakpoint . Script )
238
+ if ( scriptPath is null || scriptPath == breakpoint . Script )
242
239
{
243
240
BreakpointApiUtils . RemoveBreakpoint (
244
241
_editorServicesHost . Runspace . Debugger ,
245
242
breakpoint ,
246
243
_debugStateService . RunspaceId ) ;
247
244
}
248
245
}
249
-
250
246
return ;
251
247
}
252
248
253
249
// Legacy behavior
254
-
255
- PSCommand psCommand = new PSCommand ( ) ;
256
- psCommand . AddCommand ( @"Microsoft.PowerShell.Utility\Get-PSBreakpoint" ) ;
250
+ var psCommand = new PSCommand ( ) . AddCommand ( @"Microsoft.PowerShell.Utility\Get-PSBreakpoint" ) ;
257
251
258
252
if ( ! string . IsNullOrEmpty ( scriptPath ) )
259
253
{
260
254
psCommand . AddParameter ( "Script" , scriptPath ) ;
261
255
}
262
256
263
257
psCommand . AddCommand ( @"Microsoft.PowerShell.Utility\Remove-PSBreakpoint" ) ;
264
-
265
258
await _executionService . ExecutePSCommandAsync < object > ( psCommand , CancellationToken . None ) . ConfigureAwait ( false ) ;
266
259
}
267
260
catch ( Exception e )
@@ -281,37 +274,26 @@ public async Task RemoveBreakpointsAsync(IEnumerable<Breakpoint> breakpoints)
281
274
breakpoint ,
282
275
_debugStateService . RunspaceId ) ;
283
276
284
- switch ( breakpoint )
277
+ _ = breakpoint switch
285
278
{
286
- case CommandBreakpoint commandBreakpoint :
287
- CommandBreakpoints . Remove ( commandBreakpoint ) ;
288
- break ;
289
- case LineBreakpoint lineBreakpoint :
290
- if ( BreakpointsPerFile . TryGetValue ( lineBreakpoint . Script , out HashSet < Breakpoint > bps ) )
291
- {
292
- bps . Remove ( lineBreakpoint ) ;
293
- }
294
- break ;
295
- default :
296
- throw new ArgumentException ( "Unsupported breakpoint type." ) ;
297
- }
279
+ CommandBreakpoint commandBreakpoint => CommandBreakpoints . Remove ( commandBreakpoint ) ,
280
+ LineBreakpoint lineBreakpoint =>
281
+ BreakpointsPerFile . TryGetValue ( lineBreakpoint . Script , out HashSet < Breakpoint > bps ) && bps . Remove ( lineBreakpoint ) ,
282
+ _ => throw new NotImplementedException ( "Other breakpoints not supported yet" ) ,
283
+ } ;
298
284
}
299
-
300
285
return ;
301
286
}
302
287
303
288
// Legacy behavior
304
- var breakpointIds = breakpoints . Select ( b => b . Id ) . ToArray ( ) ;
305
- if ( breakpointIds . Length > 0 )
289
+ var breakpointIds = breakpoints . Select ( b => b . Id ) ;
290
+ if ( breakpointIds . Any ( ) )
306
291
{
307
- PSCommand psCommand = new PSCommand ( ) ;
308
- psCommand . AddCommand ( @"Microsoft.PowerShell.Utility\Remove-PSBreakpoint" ) ;
309
- psCommand . AddParameter ( "Id" , breakpoints . Select ( b => b . Id ) . ToArray ( ) ) ;
310
-
292
+ PSCommand psCommand = new PSCommand ( )
293
+ . AddCommand ( @"Microsoft.PowerShell.Utility\Remove-PSBreakpoint" )
294
+ . AddParameter ( "Id" , breakpoints . Select ( b => b . Id ) . ToArray ( ) ) ;
311
295
await _executionService . ExecutePSCommandAsync < object > ( psCommand , CancellationToken . None ) . ConfigureAwait ( false ) ;
312
296
}
313
297
}
314
-
315
-
316
298
}
317
299
}
0 commit comments