<commit_before>// Visual Studio Shared Project
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN  *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.PythonTools.Infrastructure;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudioTools.Navigation;
using Microsoft.VisualStudioTools.Project;
using Task = System.Threading.Tasks.Task;

namespace Microsoft.VisualStudioTools {
    public abstract class CommonPackage : AsyncPackage, IOleComponent {
        private uint _componentID;
        private LibraryManager _libraryManager;
        private IOleComponentManager _compMgr;
        private UIThreadBase _uiThread;
        private static readonly object _commandsLock = new object();
        private static readonly Dictionary<Command, MenuCommand> _commands = new Dictionary<Command, MenuCommand>();

        #region Language-specific abstracts

        public abstract Type GetLibraryManagerType();
        internal abstract LibraryManager CreateLibraryManager();
        public abstract bool IsRecognizedFile(string filename);

        // TODO:
        // public abstract bool TryGetStartupFileAndDirectory(out string filename, out string dir);

        #endregion

        internal CommonPackage() {
#if DEBUG
            AppDomain.CurrentDomain.UnhandledException += (sender, e) => {
                if (e.IsTerminating) {
                    var ex = e.ExceptionObject as Exception;
                    if (ex is SEHException) {
                        return;
                    }

                    if (ex != null) {
                        Debug.Fail(
                            string.Format("An unhandled exception is about to terminate the process:\n\n{0}", ex.Message),
                            ex.ToString()
                        );
                    } else {
                        Debug.Fail(string.Format(
                            "An unhandled exception is about to terminate the process:\n\n{0}",
                            e.ExceptionObject
                        ));
                    }
                }
            };
#endif
        }


        internal static Dictionary<Command, MenuCommand> Commands => _commands;
        internal static object CommandsLock => _commandsLock;

        protected override void Dispose(bool disposing) {
            if (_uiThread != null)
            {
                _uiThread.InvokeAsync(() =>
                {
                    if (_componentID != 0)
                    {
                        _compMgr.FRevokeComponent(_componentID);
                        _componentID = 0;
                    }

                    if (_libraryManager != null)
                    {
                        _libraryManager.Dispose();
                        _libraryManager = null;
                    }
                });
            }
            base.Dispose(disposing);
        }

        private object CreateLibraryManager(IServiceContainer container, Type serviceType) {
            if (GetLibraryManagerType() != serviceType) {
                return null;
            }

            return _libraryManager = CreateLibraryManager();
        }

        internal void RegisterCommands(Guid cmdSet, params Command[] commands) {
            _uiThread.MustBeCalledFromUIThreadOrThrow();
            if (GetService(typeof(IMenuCommandService)) is OleMenuCommandService mcs) {
                lock (_commandsLock) {
                    foreach (var command in commands) {
                        var beforeQueryStatus = command.BeforeQueryStatus;
                        CommandID toolwndCommandID = new CommandID(cmdSet, command.CommandId);
                        OleMenuCommand menuToolWin = new OleMenuCommand(command.DoCommand, toolwndCommandID);
                        if (beforeQueryStatus != null) {
                            menuToolWin.BeforeQueryStatus += beforeQueryStatus;
                        }
                        mcs.AddCommand(menuToolWin);
                        _commands[command] = menuToolWin;
                    }<commit_msg>Put back commonpackage logic<commit_after>// Visual Studio Shared Project
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN  *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.PythonTools.Infrastructure;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudioTools.Navigation;
using Microsoft.VisualStudioTools.Project;
using Task = System.Threading.Tasks.Task;

namespace Microsoft.VisualStudioTools {
    public abstract class CommonPackage : AsyncPackage, IOleComponent {
        private uint _componentID;
        private LibraryManager _libraryManager;
        private IOleComponentManager _compMgr;
        private UIThreadBase _uiThread;
        private static readonly object _commandsLock = new object();
        private static readonly Dictionary<Command, MenuCommand> _commands = new Dictionary<Command, MenuCommand>();

        #region Language-specific abstracts

        public abstract Type GetLibraryManagerType();
        internal abstract LibraryManager CreateLibraryManager();
        public abstract bool IsRecognizedFile(string filename);

        // TODO:
        // public abstract bool TryGetStartupFileAndDirectory(out string filename, out string dir);

        #endregion

        internal CommonPackage() {
#if DEBUG
            AppDomain.CurrentDomain.UnhandledException += (sender, e) => {
                if (e.IsTerminating) {
                    var ex = e.ExceptionObject as Exception;
                    if (ex is SEHException) {
                        return;
                    }

                    if (ex != null) {
                        Debug.Fail(
                            string.Format("An unhandled exception is about to terminate the process:\n\n{0}", ex.Message),
                            ex.ToString()
                        );
                    } else {
                        Debug.Fail(string.Format(
                            "An unhandled exception is about to terminate the process:\n\n{0}",
                            e.ExceptionObject
                        ));
                    }
                }
            };
#endif
        }


        internal static Dictionary<Command, MenuCommand> Commands => _commands;
        internal static object CommandsLock => _commandsLock;

        protected override void Dispose(bool disposing) {
            _uiThread?.MustBeCalledFromUIThreadOrThrow();
            if (_componentID != 0)
            {
                _compMgr.FRevokeComponent(_componentID);
                _componentID = 0;
            }

            if (_libraryManager != null)
            {
                _libraryManager.Dispose();
                _libraryManager = null;
            }
            base.Dispose(disposing);
        }

        private object CreateLibraryManager(IServiceContainer container, Type serviceType) {
            if (GetLibraryManagerType() != serviceType) {
                return null;
            }

            return _libraryManager = CreateLibraryManager();
        }

        internal void RegisterCommands(Guid cmdSet, params Command[] commands) {
            _uiThread.MustBeCalledFromUIThreadOrThrow();
            if (GetService(typeof(IMenuCommandService)) is OleMenuCommandService mcs) {
                lock (_commandsLock) {
                    foreach (var command in commands) {
                        var beforeQueryStatus = command.BeforeQueryStatus;
                        CommandID toolwndCommandID = new CommandID(cmdSet, command.CommandId);
                        OleMenuCommand menuToolWin = new OleMenuCommand(command.DoCommand, toolwndCommandID);
                        if (beforeQueryStatus != null) {
                            menuToolWin.BeforeQueryStatus += beforeQueryStatus;
                        }
                        mcs.AddCommand(menuToolWin);
                        _commands[command] = menuToolWin;
                    }
{'fraction_non_alphanumeric': 0.07195315601429139, 'fraction_numerical': 0.001984914648670107, 'mean_word_length': 1.9910952804986644, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1360250', 'n_tokens_mistral': 2352, 'n_tokens_neox': 2220, 'n_words': 662}