Skip to content

Commit 1926a5d

Browse files
Merge pull request #314 from jamesc-skyward/Debugger
Added IsDebuggingAsync() and GetDebugModeAsync() methods
2 parents 2ef1853 + 2ea779c commit 1926a5d

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.VisualStudio.Shell;
4+
using Microsoft.VisualStudio.Shell.Interop;
5+
6+
namespace Community.VisualStudio.Toolkit
7+
{
8+
/// <summary>
9+
/// Handles debugging.
10+
/// </summary>
11+
public class Debugger
12+
{
13+
/// <summary>
14+
/// The mode of the debugger.
15+
/// </summary>
16+
public enum DebugMode
17+
{
18+
/// <summary>The debugger is not attached.</summary>
19+
NotDebugging,
20+
/// <summary>The debugger is stopped at a breakpoint.</summary>
21+
AtBreakpoint,
22+
/// <summary>The debugger is attached and running.</summary>
23+
Running
24+
}
25+
26+
/// <summary>
27+
/// Checks if the debugger is attached.
28+
/// </summary>
29+
public async Task<bool> IsDebuggingAsync()
30+
{
31+
DebugMode debugMode = await GetDebugModeAsync();
32+
return debugMode != DebugMode.NotDebugging;
33+
}
34+
35+
/// <summary>
36+
/// Returns the current mode for the debugger.
37+
/// </summary>
38+
public async Task<DebugMode> GetDebugModeAsync()
39+
{
40+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
41+
42+
DBGMODE dbgMode = VsShellUtilities.GetDebugMode(ServiceProvider.GlobalProvider) & ~DBGMODE.DBGMODE_EncMask;
43+
44+
return dbgMode switch
45+
{
46+
DBGMODE.DBGMODE_Design => DebugMode.NotDebugging,
47+
DBGMODE.DBGMODE_Break => DebugMode.AtBreakpoint,
48+
DBGMODE.DBGMODE_Run => DebugMode.Running,
49+
_ => throw new InvalidOperationException($"Unexpected {nameof(DebugMode)}")
50+
};
51+
}
52+
}
53+
}

src/toolkit/Community.VisualStudio.Toolkit.Shared/VS.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public static class VS
2121
/// <summary>A collection of services related to the command system.</summary>
2222
public static Commands Commands => _commands ??= new();
2323

24+
private static Debugger? _debugger;
25+
/// <summary>A collection of services related to the debugger.</summary>
26+
public static Debugger Debugger => _debugger ??= new();
27+
2428
private static Documents? _documents;
2529
/// <summary>Contains helper methods for dealing with documents.</summary>
2630
public static Documents Documents => _documents ??= new();

src/toolkit/Community.VisualStudio.Toolkit.Shared/VSSDK.Helpers.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<Import_RootNamespace>Community.VisualStudio.Toolkit.Shared</Import_RootNamespace>
1010
</PropertyGroup>
1111
<ItemGroup>
12+
<Compile Include="$(MSBuildThisFileDirectory)Debugger\Debugger.cs" />
1213
<Compile Include="$(MSBuildThisFileDirectory)\Attributes\ProvideBraceCompletionAttribute.cs" />
1314
<Compile Include="$(MSBuildThisFileDirectory)\Attributes\ProvideFileIconAttribute.cs" />
1415
<Compile Include="$(MSBuildThisFileDirectory)\Attributes\ProvideGalleryFeedAttribute.cs" />

0 commit comments

Comments
 (0)