<commit_before>﻿using EnvDTE;
using Microsoft.VisualStudio.OLE.Interop;
using System;
using System.Runtime.InteropServices;

namespace CsDebugScript.VS
{
    /// <summary>
    /// Context for interacting with Visual Studio
    /// </summary>
    internal static class VSContext
    {
        /// <summary>
        /// The debugger events (we need to store this in order to get events)
        /// </summary>
        private static DebuggerEvents debuggerEvents;

        /// <summary>
        /// The Visual Studio debugger proxy running in Default AppDomain.
        /// </summary>
        private static VSDebuggerProxy debuggerProxy;

        /// <summary>
        /// Gets the current debugging mode.
        /// </summary>
        public static dbgDebugMode CurrentDebugMode
        {
            get
            {
                return DTE.Debugger.CurrentMode;
            }
        }

        /// <summary>
        /// Occurs when debugger enters break mode.
        /// </summary>
        public static event Action DebuggerEnteredBreakMode;

        /// <summary>
        /// Occurs when debugger enters design mode.
        /// </summary>
        public static event Action DebuggerEnteredDesignMode;

        /// <summary>
        /// Occurs when debugger enters run mode.
        /// </summary>
        public static event Action DebuggerEnteredRunMode;

        /// <summary>
        /// Gets the DTE.
        /// </summary>
        internal static DTE DTE { get; private set; }

        /// <summary>
        /// The VS debugger
        /// </summary>
        internal static VSDebugger VSDebugger { get; private set; }

        /// <summary>
        /// Initializes the <see cref="VSContext"/> class.
        /// </summary>
        static VSContext()
        {
            SetDTE(GetDTE());
        }

        /// <summary>
        /// Sets the instance of currently running Visual Studio IDE.
        /// </summary>
        /// <param name="dte">The DTE.</param>
        internal static void SetDTE(DTE dte)
        {
            DTE = dte;
            debuggerEvents = DTE.Events.DebuggerEvents;
            debuggerEvents.OnEnterBreakMode += DebuggerEvents_OnEnterBreakMode;
            debuggerEvents.OnEnterDesignMode += DebuggerEvents_OnEnterDesignMode;
            debuggerEvents.OnEnterRunMode += DebuggerEvents_OnEnterRunMode;
            debuggerProxy = (VSDebuggerProxy)AppDomain.CurrentDomain.GetData(VSDebuggerProxy.AppDomainDataName) ?? new VSDebuggerProxy();
            VSDebugger = new VSDebugger(debuggerProxy);
            Engine.Context.InitializeDebugger(VSDebugger);
        }

        /// <summary>
        /// Initializes the application domain with the debugger proxy.
        /// </summary>
        /// <param name="scriptDomain">The script domain.</param>
        internal static void InitializeAppDomain(AppDomain scriptDomain)
        {
            scriptDomain.SetData(VSDebuggerProxy.AppDomainDataName, debuggerProxy);
        }

        /// <summary>
        /// Occurs when debugger enters run mode.
        /// </summary>
        /// <param name="reason">The reason.</param>
        private static void DebuggerEvents_OnEnterRunMode(dbgEventReason reason)
        {<commit_msg>Fixing bugs in VS plug-in when DTE is not found.<commit_after>﻿using EnvDTE;
using Microsoft.VisualStudio.OLE.Interop;
using System;
using System.Runtime.InteropServices;

namespace CsDebugScript.VS
{
    /// <summary>
    /// Context for interacting with Visual Studio
    /// </summary>
    internal static class VSContext
    {
        /// <summary>
        /// The debugger events (we need to store this in order to get events)
        /// </summary>
        private static DebuggerEvents debuggerEvents;

        /// <summary>
        /// The Visual Studio debugger proxy running in Default AppDomain.
        /// </summary>
        private static VSDebuggerProxy debuggerProxy;

        /// <summary>
        /// Gets the current debugging mode.
        /// </summary>
        public static dbgDebugMode CurrentDebugMode
        {
            get
            {
                if (DTE != null)
                {
                    return DTE.Debugger.CurrentMode;
                }
                else
                {
                    return dbgDebugMode.dbgDesignMode;
                }
            }
        }

        /// <summary>
        /// Occurs when debugger enters break mode.
        /// </summary>
        public static event Action DebuggerEnteredBreakMode;

        /// <summary>
        /// Occurs when debugger enters design mode.
        /// </summary>
        public static event Action DebuggerEnteredDesignMode;

        /// <summary>
        /// Occurs when debugger enters run mode.
        /// </summary>
        public static event Action DebuggerEnteredRunMode;

        /// <summary>
        /// Gets the DTE.
        /// </summary>
        internal static DTE DTE { get; private set; }

        /// <summary>
        /// The VS debugger
        /// </summary>
        internal static VSDebugger VSDebugger { get; private set; }

        /// <summary>
        /// Initializes the <see cref="VSContext"/> class.
        /// </summary>
        static VSContext()
        {
            InitializeDTE();
        }

        /// <summary>
        /// Initializes the DTE.
        /// </summary>
        private static void InitializeDTE()
        {
            SetDTE(GetDTE());
        }

        /// <summary>
        /// Sets the instance of currently running Visual Studio IDE.
        /// </summary>
        /// <param name="dte">The DTE.</param>
        internal static void SetDTE(DTE dte)
        {
            DTE = dte;
            if (dte != null)
            {
                debuggerEvents = DTE.Events.DebuggerEvents;
                debuggerEvents.OnEnterBreakMode += DebuggerEvents_OnEnterBreakMode;
                debuggerEvents.OnEnterDesignMode += DebuggerEvents_OnEnterDesignMode;
                debuggerEvents.OnEnterRunMode += DebuggerEvents_OnEnterRunMode;
                debuggerProxy = (VSDebuggerProxy)AppDomain.CurrentDomain.GetData(VSDebuggerProxy.AppDomainDataName) ?? new VSDebuggerProxy();
                VSDebugger = new VSDebugger(debuggerProxy);
                Engine.Context.InitializeDebugger(VSDebugger);
            }
            else
            {
                // 90s all over again :)
                var timer = new System.Windows.Threading.DispatcherTimer();
                timer.Tick += (a, b) =>
                {
                    timer.Stop();
                    InitializeDTE();
                };
                timer.Interval = TimeSpan.FromSeconds(0.1);
                timer.Start();
            }
        }

        /// <summary>
        /// Initializes the application domain with the debugger proxy.
        /// </summary>
        /// <param name="scriptDomain">The script domain.</param>
        internal static void InitializeAppDomain(AppDomain scriptDomain)
        {
            scriptDomain.SetData(VSDebuggerProxy.AppDomainDataName, debuggerProxy);
        }

        /// <summary>
        /// Occurs when debugger enters run mode.
        /// </summary>
        /// <param name="reason">The reason.</param>
        private static void DebuggerEvents_OnEnterRunMode(dbgEventReason reason)
        {
{'fraction_non_alphanumeric': 0.10335424052358877, 'fraction_numerical': 0.000545404963185165, 'mean_word_length': 1.6075364379665837, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3851235', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1852, 'n_words': 479}