VisualStudioDebugStateChangeListener.cs 2.6 KB
Newer Older
1 2
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

3 4
#nullable enable

5 6
using System;
using System.ComponentModel.Composition;
7
using Microsoft.CodeAnalysis;
8 9 10 11 12 13
using Microsoft.CodeAnalysis.Debugging;
using Microsoft.CodeAnalysis.EditAndContinue;
using Microsoft.VisualStudio.Debugger;
using Microsoft.VisualStudio.Debugger.Clr;
using Microsoft.VisualStudio.Debugger.ComponentInterfaces;
using Microsoft.VisualStudio.Debugger.UI.Interfaces;
14
using Roslyn.Utilities;
15 16 17 18 19 20

namespace Microsoft.VisualStudio.LanguageServices.EditAndContinue
{
    [Export(typeof(IDebugStateChangeListener))]
    internal sealed class VisualStudioDebugStateChangeListener : IDebugStateChangeListener
    {
21
        private readonly Workspace _workspace;
22
        private readonly IDebuggingWorkspaceService _debuggingService;
23 24 25

        // EnC service or null if EnC is disabled for the debug session.
        private IEditAndContinueWorkspaceService? _encService;
26 27

        [ImportingConstructor]
28
        public VisualStudioDebugStateChangeListener(VisualStudioWorkspace workspace)
29
        {
30
            _workspace = workspace;
31 32 33
            _debuggingService = workspace.Services.GetRequiredService<IDebuggingWorkspaceService>();
        }

34 35 36 37
        /// <summary>
        /// Called by the debugger when a debugging session starts and managed debugging is being used.
        /// </summary>
        public void StartDebugging(DebugSessionOptions options)
38
        {
39 40 41
            _debuggingService.OnBeforeDebuggingStateChanged(DebuggingState.Design, DebuggingState.Run);

            if ((options & DebugSessionOptions.EditAndContinueDisabled) == 0)
42
            {
43 44 45 46 47 48 49
                _encService = _workspace.Services.GetRequiredService<IEditAndContinueWorkspaceService>();
                _encService.StartDebuggingSession();
            }
            else
            {
                _encService = null;
            }
50 51
        }

52
        public void EnterBreakState()
53 54
        {
            _debuggingService.OnBeforeDebuggingStateChanged(DebuggingState.Run, DebuggingState.Break);
55
            _encService?.StartEditSession();
56 57 58 59 60
        }

        public void ExitBreakState()
        {
            _debuggingService.OnBeforeDebuggingStateChanged(DebuggingState.Break, DebuggingState.Run);
61
            _encService?.EndEditSession();
62 63 64 65 66
        }

        public void StopDebugging()
        {
            _debuggingService.OnBeforeDebuggingStateChanged(DebuggingState.Run, DebuggingState.Design);
67
            _encService?.EndDebuggingSession();
68 69 70
        }
    }
}