diff --git a/src/ServiceHost/Hosting/ServiceHost.cs b/src/ServiceHost/Hosting/ServiceHost.cs index f8808af6..fcaaffcd 100644 --- a/src/ServiceHost/Hosting/ServiceHost.cs +++ b/src/ServiceHost/Hosting/ServiceHost.cs @@ -4,9 +4,9 @@ // using System; +using System.Linq; using System.Threading.Tasks; using System.Collections.Generic; -using System.Linq; using Microsoft.SqlTools.EditorServices.Utility; using Microsoft.SqlTools.ServiceLayer.Hosting.Contracts; using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol; @@ -27,12 +27,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Hosting private static readonly Lazy instance = new Lazy(() => new ServiceHost()); /// - /// Creates or retrieves the current instance of the ServiceHost + /// Current instance of the ServiceHost /// - /// Instance of the service host - public static ServiceHost Create() + public static ServiceHost Instance { - return instance.Value; + get { return instance.Value; } } /// @@ -42,8 +41,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Hosting private ServiceHost() : base(new StdioServerChannel()) { // Initialize the shutdown activities - shutdownActivities = new List(); - initializeActivities = new List(); + shutdownCallbacks = new List(); + initializeCallbacks = new List(); // Register the requests that this service host will handle this.SetRequestHandler(InitializeRequest.Type, this.HandleInitializeRequest); @@ -54,34 +53,34 @@ namespace Microsoft.SqlTools.ServiceLayer.Hosting #region Member Variables - public delegate Task ShutdownHandler(object shutdownParams, RequestContext shutdownRequestContext); + public delegate Task ShutdownCallback(object shutdownParams, RequestContext shutdownRequestContext); - public delegate Task InitializeHandler(InitializeRequest startupParams, RequestContext requestContext); + public delegate Task InitializeCallback(InitializeRequest startupParams, RequestContext requestContext); - private readonly List shutdownActivities; + private readonly List shutdownCallbacks; - private readonly List initializeActivities; + private readonly List initializeCallbacks; #endregion #region Public Methods /// - /// Adds a new method to be called when the shutdown request is submitted + /// Adds a new callback to be called when the shutdown request is submitted /// - /// - public void RegisterShutdownTask(ShutdownHandler activity) + /// Callback to perform when a shutdown request is submitted + public void RegisterShutdownTask(ShutdownCallback callback) { - shutdownActivities.Add(activity); + shutdownCallbacks.Add(callback); } /// /// Add a new method to be called when the initialize request is submitted /// - /// - public void RegisterInitializeTask(InitializeHandler activity) + /// Callback to perform when an initialize request is submitted + public void RegisterInitializeTask(InitializeCallback callback) { - initializeActivities.Add(activity); + initializeCallbacks.Add(callback); } #endregion @@ -96,7 +95,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Hosting Logger.Write(LogLevel.Normal, "Service host is shutting down..."); // Call all the shutdown methods provided by the service components - Task[] shutdownTasks = shutdownActivities.Select(t => t(shutdownParams, requestContext)).ToArray(); + Task[] shutdownTasks = shutdownCallbacks.Select(t => t(shutdownParams, requestContext)).ToArray(); await Task.WhenAll(shutdownTasks); } @@ -111,7 +110,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Hosting Logger.Write(LogLevel.Verbose, "HandleInitializationRequest"); // Call all tasks that registered on the initialize request - var initializeTasks = initializeActivities.Select(t => t(initializeParams, requestContext)); + var initializeTasks = initializeCallbacks.Select(t => t(initializeParams, requestContext)); await Task.WhenAll(initializeTasks); // TODO: Figure out where this needs to go to be agnostic of the language diff --git a/src/ServiceHost/LanguageServices/LanguageService.cs b/src/ServiceHost/LanguageServices/LanguageService.cs index 05f42fb3..42bbdec5 100644 --- a/src/ServiceHost/LanguageServices/LanguageService.cs +++ b/src/ServiceHost/LanguageServices/LanguageService.cs @@ -84,7 +84,7 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices }); // Register the configuration update handler - WorkspaceService.Instance.RegisterDidChangeConfigurationNotificationTask(HandleDidChangeConfigurationNotification); + WorkspaceService.Instance.RegisterConfigChangeCallback(HandleDidChangeConfigurationNotification); // Store the SqlToolsContext for future use Context = context; diff --git a/src/ServiceHost/Program.cs b/src/ServiceHost/Program.cs index 6879aabb..53c15d3a 100644 --- a/src/ServiceHost/Program.cs +++ b/src/ServiceHost/Program.cs @@ -34,8 +34,8 @@ namespace Microsoft.SqlTools.ServiceLayer var profilePaths = new ProfilePaths(hostProfileId, "baseAllUsersPath", "baseCurrentUserPath"); SqlToolsContext sqlToolsContext = new SqlToolsContext(hostDetails, profilePaths); - // Create the service host - ServiceHost serviceHost = ServiceHost.Create(); + // Grab the instance of the service host + ServiceHost serviceHost = ServiceHost.Instance; // Initialize the services that will be hosted here WorkspaceService.Instance.InitializeService(serviceHost); diff --git a/src/ServiceHost/WorkspaceServices/WorkspaceService.cs b/src/ServiceHost/WorkspaceServices/WorkspaceService.cs index 7be53481..6eb0c8c9 100644 --- a/src/ServiceHost/WorkspaceServices/WorkspaceService.cs +++ b/src/ServiceHost/WorkspaceServices/WorkspaceService.cs @@ -5,13 +5,13 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.SqlTools.EditorServices.Utility; using Microsoft.SqlTools.ServiceLayer.Hosting; using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol; using Microsoft.SqlTools.ServiceLayer.WorkspaceServices.Contracts; -using System.Linq; namespace Microsoft.SqlTools.ServiceLayer.WorkspaceServices { @@ -29,8 +29,8 @@ namespace Microsoft.SqlTools.ServiceLayer.WorkspaceServices private WorkspaceService() { - ConfigurationNotificationHandlers = new List(); - TextDocumentChangeHandlers = new List(); + ConfigChangeCallbacks = new List(); + TextDocChangeCallbacks = new List(); } #endregion @@ -41,12 +41,31 @@ namespace Microsoft.SqlTools.ServiceLayer.WorkspaceServices public TConfig CurrentSettings { get; private set; } - public delegate Task DidChangeConfigurationNotificationHandler(TConfig newSettings, TConfig oldSettings, EventContext eventContext); + /// + /// Delegate for callbacks that occur when the configuration for the workspace changes + /// + /// The settings that were just set + /// The settings before they were changed + /// Context of the event that triggered the callback + /// + public delegate Task ConfigChangeCallback(TConfig newSettings, TConfig oldSettings, EventContext eventContext); - public delegate Task DidChangeTextDocumentNotificationTask(ScriptFile[] changedFiles, EventContext eventContext); + /// + /// Delegate for callbacks that occur when the current text document changes + /// + /// Array of files that changed + /// Context of the event raised for the changed files + public delegate Task TextDocChangeCallback(ScriptFile[] changedFiles, EventContext eventContext); - private List ConfigurationNotificationHandlers { get; set; } - private List TextDocumentChangeHandlers { get; set; } + /// + /// List of callbacks to call when the configuration of the workspace changes + /// + private List ConfigChangeCallbacks { get; set; } + + /// + /// List of callbacks to call when the current text document changes + /// + private List TextDocChangeCallbacks { get; set; } #endregion @@ -95,18 +114,18 @@ namespace Microsoft.SqlTools.ServiceLayer.WorkspaceServices /// handle changing configuration and changing the current configuration. /// /// Task to handle the request - public void RegisterDidChangeConfigurationNotificationTask(DidChangeConfigurationNotificationHandler task) + public void RegisterConfigChangeCallback(ConfigChangeCallback task) { - ConfigurationNotificationHandlers.Add(task); + ConfigChangeCallbacks.Add(task); } /// /// Adds a new task to be called when the text of a document changes. /// /// Delegate to call when the document changes - public void RegisterDidChangeTextDocumentNotificationTask(DidChangeTextDocumentNotificationTask task) + public void RegisterTextDocChangeCallback(TextDocChangeCallback task) { - TextDocumentChangeHandlers.Add(task); + TextDocChangeCallbacks.Add(task); } #endregion @@ -145,7 +164,7 @@ namespace Microsoft.SqlTools.ServiceLayer.WorkspaceServices Logger.Write(LogLevel.Verbose, msg.ToString()); - var handlers = TextDocumentChangeHandlers.Select(t => t(changedFiles.ToArray(), eventContext)); + var handlers = TextDocChangeCallbacks.Select(t => t(changedFiles.ToArray(), eventContext)); return Task.WhenAll(handlers); } @@ -177,7 +196,7 @@ namespace Microsoft.SqlTools.ServiceLayer.WorkspaceServices Logger.Write(LogLevel.Verbose, "HandleDidChangeConfigurationNotification"); // Propagate the changes to the event handlers - var configUpdateTasks = ConfigurationNotificationHandlers.Select( + var configUpdateTasks = ConfigChangeCallbacks.Select( t => t(configChangeParams.Settings, CurrentSettings, eventContext)); await Task.WhenAll(configUpdateTasks); }