Skip to content

Commit 146e341

Browse files
misc clean up and fixed action script blocks
1 parent 049563b commit 146e341

File tree

6 files changed

+60
-48
lines changed

6 files changed

+60
-48
lines changed

src/PowerShellEditorServices/Services/DebugAdapter/BreakpointService.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@
44
//
55

66
using System;
7-
using System.Collections.Concurrent;
87
using System.Collections.Generic;
9-
using System.Collections.ObjectModel;
108
using System.Linq;
119
using System.Management.Automation;
12-
using System.Management.Automation.Language;
13-
using System.Threading;
1410
using System.Threading.Tasks;
1511
using Microsoft.Extensions.Logging;
1612
using Microsoft.PowerShell.EditorServices.Logging;
1713
using Microsoft.PowerShell.EditorServices.Services.DebugAdapter;
18-
using Microsoft.PowerShell.EditorServices.Utility;
1914

2015
namespace Microsoft.PowerShell.EditorServices.Services
2116
{
@@ -44,7 +39,7 @@ public BreakpointService(
4439

4540
public async Task<List<Breakpoint>> GetBreakpointsAsync()
4641
{
47-
if (VersionUtils.IsPS7OrGreater)
42+
if (BreakpointApiUtils.SupportsBreakpointApis)
4843
{
4944
return BreakpointApiUtils.GetBreakpoints(
5045
_powerShellContextService.CurrentRunspace.Runspace.Debugger,
@@ -60,7 +55,7 @@ public async Task<List<Breakpoint>> GetBreakpointsAsync()
6055

6156
public async Task<IEnumerable<BreakpointDetails>> SetBreakpointsAsync(string escapedScriptPath, IEnumerable<BreakpointDetails> breakpoints)
6257
{
63-
if (VersionUtils.IsPS7OrGreater)
58+
if (BreakpointApiUtils.SupportsBreakpointApis)
6459
{
6560
foreach (BreakpointDetails breakpointDetails in breakpoints)
6661
{
@@ -150,7 +145,7 @@ public async Task<IEnumerable<BreakpointDetails>> SetBreakpointsAsync(string esc
150145

151146
public async Task<IEnumerable<CommandBreakpointDetails>> SetCommandBreakpoints(IEnumerable<CommandBreakpointDetails> breakpoints)
152147
{
153-
if (VersionUtils.IsPS7OrGreater)
148+
if (BreakpointApiUtils.SupportsBreakpointApis)
154149
{
155150
foreach (CommandBreakpointDetails commandBreakpointDetails in breakpoints)
156151
{
@@ -226,7 +221,7 @@ public async Task RemoveAllBreakpointsAsync(string scriptPath = null)
226221
{
227222
try
228223
{
229-
if (VersionUtils.IsPS7OrGreater)
224+
if (BreakpointApiUtils.SupportsBreakpointApis)
230225
{
231226
foreach (Breakpoint breakpoint in BreakpointApiUtils.GetBreakpoints(
232227
_powerShellContextService.CurrentRunspace.Runspace.Debugger,
@@ -266,7 +261,7 @@ public async Task RemoveAllBreakpointsAsync(string scriptPath = null)
266261

267262
public async Task RemoveBreakpointsAsync(IEnumerable<Breakpoint> breakpoints)
268263
{
269-
if (VersionUtils.IsPS7OrGreater)
264+
if (BreakpointApiUtils.SupportsBreakpointApis)
270265
{
271266
foreach (Breakpoint breakpoint in breakpoints)
272267
{

src/PowerShellEditorServices/Services/DebugAdapter/Debugging/BreakpointApiUtils.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55

66
using System;
77
using System.Collections.Generic;
8-
using System.Linq;
9-
using System.Linq.Expressions;
108
using System.Management.Automation;
119
using System.Management.Automation.Language;
1210
using System.Reflection;
1311
using System.Text;
1412
using System.Threading;
15-
using Microsoft.Extensions.Logging;
13+
using Microsoft.PowerShell.EditorServices.Utility;
1614

1715
namespace Microsoft.PowerShell.EditorServices.Services.DebugAdapter
1816

@@ -115,6 +113,14 @@ static BreakpointApiUtils()
115113

116114
#endregion
117115

116+
#region Public Static Properties
117+
118+
// TODO: Try to compute this more dynamically. If we're launching a script in the PSIC, there are APIs are available in PS 5.1 and up.
119+
// For now, only PS7 or greater gets this feature.
120+
public static bool SupportsBreakpointApis => VersionUtils.IsPS7OrGreater;
121+
122+
#endregion
123+
118124
#region Public Static Methods
119125

120126
public static Breakpoint SetBreakpoint(Debugger debugger, BreakpointDetailsBase breakpoint, int? runspaceId = null)
@@ -172,7 +178,7 @@ public static ScriptBlock GetBreakpointActionScriptBlock(string condition, strin
172178
// In the HitCount only case, this is simple as we can just use the HitCount
173179
// property on the breakpoint object which is represented by $_.
174180
builder.Insert(0, $"if ($_.HitCount -eq {parsedHitCount}) {{ ")
175-
.Append(" }}");
181+
.Append(" }");
176182
}
177183

178184
Interlocked.Increment(ref breakpointHitCounter);
@@ -181,7 +187,7 @@ public static ScriptBlock GetBreakpointActionScriptBlock(string condition, strin
181187
$"$global:{s_psesGlobalVariableNamePrefix}BreakHitCounter_{breakpointHitCounter}";
182188

183189
builder.Insert(0, $"if (++{globalHitCountVarName} -eq {parsedHitCount}) {{ ")
184-
.Append(" }}");
190+
.Append(" }");
185191
}
186192

187193
if (!string.IsNullOrWhiteSpace(condition))

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public async Task<SetBreakpointsResponse> Handle(SetBreakpointsArguments request
164164
string fileExtension = Path.GetExtension(scriptFile?.FilePath ?? "")?.ToLower();
165165
bool isUntitledPath = ScriptFile.IsUntitledPath(request.Source.Path);
166166
if ((!isUntitledPath && fileExtension != ".ps1" && fileExtension != ".psm1") ||
167-
(!VersionUtils.IsPS7OrGreater && isUntitledPath))
167+
(!BreakpointApiUtils.SupportsBreakpointApis && isUntitledPath))
168168
{
169169
_logger.LogWarning(
170170
$"Attempted to set breakpoints on a non-PowerShell file: {request.Source.Path}");
@@ -189,7 +189,8 @@ public async Task<SetBreakpointsResponse> Handle(SetBreakpointsArguments request
189189
(int)srcBreakpoint.Line,
190190
(int?)srcBreakpoint.Column,
191191
srcBreakpoint.Condition,
192-
srcBreakpoint.HitCondition))
192+
srcBreakpoint.HitCondition,
193+
srcBreakpoint.LogMessage))
193194
.ToArray();
194195

195196
// If this is a "run without debugging (Ctrl+F5)" session ignore requests to set breakpoints.

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
using System.Threading.Tasks;
1010
using Microsoft.Extensions.Logging;
1111
using Microsoft.PowerShell.EditorServices.Services;
12+
using Microsoft.PowerShell.EditorServices.Services.DebugAdapter;
1213
using Microsoft.PowerShell.EditorServices.Services.PowerShellContext;
1314
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
14-
using Microsoft.PowerShell.EditorServices.Utility;
1515
using OmniSharp.Extensions.DebugAdapter.Protocol.Events;
1616
using OmniSharp.Extensions.DebugAdapter.Protocol.Requests;
1717
using OmniSharp.Extensions.JsonRpc;
@@ -100,12 +100,16 @@ private async Task LaunchScriptAsync(string scriptToLaunch)
100100
{
101101
ScriptFile untitledScript = _workspaceService.GetFile(scriptToLaunch);
102102

103-
if (VersionUtils.IsPS7OrGreater)
103+
if (BreakpointApiUtils.SupportsBreakpointApis)
104104
{
105+
// Parse untitled files with their `Untitled:` URI as the file name which will cache the URI & contents within the PowerShell parser.
106+
// By doing this, we light up the ability to debug Untitled files with breakpoints.
107+
// This is only possible via the direct usage of the breakpoint APIs in PowerShell because
108+
// Set-PSBreakpoint validates that paths are actually on the filesystem.
105109
ScriptBlockAst ast = Parser.ParseInput(untitledScript.Contents, untitledScript.DocumentUri, out Token[] tokens, out ParseError[] errors);
106110

107111
// This seems to be the simplest way to invoke a script block (which contains breakpoint information) via the PowerShell API.
108-
PSCommand cmd = new PSCommand().AddScript("& $args[0]").AddArgument(ast.GetScriptBlock());
112+
var cmd = new PSCommand().AddScript("& $args[0]").AddArgument(ast.GetScriptBlock());
109113
await _powerShellContextService
110114
.ExecuteCommandAsync<object>(cmd, sendOutputToHost: true, sendErrorToHost:true)
111115
.ConfigureAwait(false);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ public async Task<InitializeResponse> Handle(InitializeRequestArguments request,
3535
// Now send the Initialize response to continue setup
3636
return new InitializeResponse
3737
{
38+
SupportsConditionalBreakpoints = true,
3839
SupportsConfigurationDoneRequest = true,
3940
SupportsFunctionBreakpoints = true,
40-
SupportsConditionalBreakpoints = true,
4141
SupportsHitConditionalBreakpoints = true,
42+
SupportsLogPoints = true,
4243
SupportsSetVariable = true
4344
};
4445
}

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

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Threading;
1111
using System.Threading.Tasks;
1212
using Microsoft.Extensions.Logging;
13+
using Microsoft.PowerShell.EditorServices.Logging;
1314
using Microsoft.PowerShell.EditorServices.Services;
1415
using Microsoft.PowerShell.EditorServices.Services.PowerShellContext;
1516
using OmniSharp.Extensions.DebugAdapter.Protocol.Events;
@@ -370,6 +371,10 @@ await _powerShellContextService.ExecuteScriptStringAsync(
370371
.ExecuteScriptStringAsync(debugRunspaceCmd)
371372
.ContinueWith(OnExecutionCompletedAsync);
372373

374+
if (runspaceVersion.Version.Major >= 7)
375+
{
376+
_jsonRpcServer.SendNotification(EventNames.Initialized);
377+
}
373378
return Unit.Value;
374379
}
375380

@@ -387,33 +392,33 @@ private async Task OnExecutionCompletedAsync(Task executeTask)
387392

388393
_logger.LogTrace("Execution completed, terminating...");
389394

390-
//_debugStateService.ExecutionCompleted = true;
391-
392-
//_debugEventHandlerService.UnregisterEventHandlers();
393-
394-
//if (_debugStateService.IsAttachSession)
395-
//{
396-
// // Pop the sessions
397-
// if (_powerShellContextService.CurrentRunspace.Context == RunspaceContext.EnteredProcess)
398-
// {
399-
// try
400-
// {
401-
// await _powerShellContextService.ExecuteScriptStringAsync("Exit-PSHostProcess");
402-
403-
// if (_debugStateService.IsRemoteAttach &&
404-
// _powerShellContextService.CurrentRunspace.Location == RunspaceLocation.Remote)
405-
// {
406-
// await _powerShellContextService.ExecuteScriptStringAsync("Exit-PSSession");
407-
// }
408-
// }
409-
// catch (Exception e)
410-
// {
411-
// _logger.LogException("Caught exception while popping attached process after debugging", e);
412-
// }
413-
// }
414-
//}
415-
416-
//_debugService.IsClientAttached = false;
395+
_debugStateService.ExecutionCompleted = true;
396+
397+
_debugEventHandlerService.UnregisterEventHandlers();
398+
399+
if (_debugStateService.IsAttachSession)
400+
{
401+
// Pop the sessions
402+
if (_powerShellContextService.CurrentRunspace.Context == RunspaceContext.EnteredProcess)
403+
{
404+
try
405+
{
406+
await _powerShellContextService.ExecuteScriptStringAsync("Exit-PSHostProcess");
407+
408+
if (_debugStateService.IsRemoteAttach &&
409+
_powerShellContextService.CurrentRunspace.Location == RunspaceLocation.Remote)
410+
{
411+
await _powerShellContextService.ExecuteScriptStringAsync("Exit-PSSession");
412+
}
413+
}
414+
catch (Exception e)
415+
{
416+
_logger.LogException("Caught exception while popping attached process after debugging", e);
417+
}
418+
}
419+
}
420+
421+
_debugService.IsClientAttached = false;
417422
_jsonRpcServer.SendNotification(EventNames.Terminated);
418423
}
419424
}

0 commit comments

Comments
 (0)