/// <summary>
        /// Creates a new CommentBlockHandler instance when a Q# file is opened in a code editor.
        /// </summary>
        /// <param name="Logger">A logger for recording event information</param>
        /// <param name="TextViewAdapter">The code editor that was just created</param>
        /// <param name="TextView">The WPF view of the code editor</param>
        /// <param name="Provider">The <see cref="CommentBlockHandlerProvider"/> that created
        /// this instance</param>
        /// <param name="Messenger">The message handler for communicating with the Q# parser
        /// application</param>
        public CommentBlockHandler(Logger Logger, IVsTextView TextViewAdapter, IWpfTextView TextView,
                                   CommentBlockHandlerProvider Provider, MessageServer Messenger)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            this.Logger    = Logger;
            this.TextView  = TextView;
            this.Provider  = Provider;
            this.Messenger = Messenger;

            // Get the relevant configuration parameters from the config file
            try
            {
                string        assemblyPath = typeof(CommentBlockHandlerProvider).Assembly.Location;
                string        basePath     = Path.GetDirectoryName(assemblyPath);
                ConfigManager config       = new ConfigManager(basePath);

                // Get lines-between-sections
                if (!config.GetConfigSetting("Comment-Structure", "lines-between-sections", out string linesBetweenSectionsString))
                {
                    LinesBetweenSections = 2;
                    Logger.Warn($"Couldn't get the lines-between-sections setting from the config file, defaulting to {LinesBetweenSections}.");
                }
                else if (!int.TryParse(linesBetweenSectionsString, out LinesBetweenSections))
                {
                    LinesBetweenSections = 2;
                    Logger.Warn($"The lines-between-sections setting from the config file is set to [{linesBetweenSectionsString}] " +
                                $"which isn't a valid integer value. Defaulting to {LinesBetweenSections}.");
                }

                // Get lines-between-parameters
                if (!config.GetConfigSetting("Comment-Structure", "lines-between-parameters", out string linesBetweenParametersString))
                {
                    LinesBetweenParameters = 2;
                    Logger.Warn($"Couldn't get the lines-between-parameters setting from the config file, defaulting to {LinesBetweenParameters}.");
                }
                else if (!int.TryParse(linesBetweenParametersString, out LinesBetweenParameters))
                {
                    LinesBetweenParameters = 2;
                    Logger.Warn($"The lines-between-sections setting from the config file is set to [{linesBetweenParametersString}] " +
                                $"which isn't a valid integer value. Defaulting to {LinesBetweenParameters}.");
                }
            }
            catch (Exception ex)
            {
                LinesBetweenSections   = 2;
                LinesBetweenParameters = 2;
                Logger.Warn($"Loading the comment structure settings from the config file failed: {ex.GetType().Name} - {ex.Message}");
                Logger.Trace(ex.StackTrace);
                Logger.Warn("Using the default settings.");
            }

            // Get a handle to the Visual Studio environment and the next handler in the chain
            Dte = (DTE)Provider.ServiceProvider.GetService(typeof(DTE));
            Assumes.Present(Dte);

            // Get a handle to the Visual Studio environment and the next handler in the chain
            TextViewAdapter.AddCommandFilter(this, out NextCommandHandler);
            Logger.Debug($"{nameof(CommentBlockHandler)} initialized.");
        }
Beispiel #2
0
        /// <summary>
        /// Creates the MessageServer singleton, or gets it if it was already initialized.
        /// </summary>
        /// <param name="Logger">A logger for recording event information</param>
        /// <returns>The MessageServer singleton</returns>
        public static MessageServer GetOrCreateServer(Logger Logger)
        {
            if (Instance == null)
            {
                Instance = new MessageServer(Logger);
            }

            return(Instance);
        }
Beispiel #3
0
        /// <summary>
        /// Creates a new CommentBlockHandlerProvider instance and initializes the
        /// <see cref="MessageServer"/> singleton if it hasn't been started yet.
        /// </summary>
        public CommentBlockHandlerProvider()
        {
            // Create a logger
            string assemblyPath = typeof(CommentBlockHandlerProvider).Assembly.Location;
            string basePath     = Path.GetDirectoryName(assemblyPath);

            Logger = Logger.GetOrCreateLogger(basePath, "Extension.log");
            Logger.Debug("Extension logger ready.");

            WrapperChannel = MessageServer.GetOrCreateServer(Logger);
            Logger.Info($"{nameof(CommentBlockHandlerProvider)} is loaded and ready.");
        }