From 2e14de437097de14cc17e0336627db148a345ab3 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Wed, 15 Jul 2020 18:56:06 -0700 Subject: [PATCH 1/3] Rename GetService to avoid confusion --- .../Experimentation/KeybindingResetDetector.cs | 10 +++++----- .../ProjectSystem/VisualStudioWorkspaceImpl.cs | 4 ++-- .../Core/Def/Utilities/IServiceProviderExtensions.cs | 5 +++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/VisualStudio/Core/Def/Implementation/Experimentation/KeybindingResetDetector.cs b/src/VisualStudio/Core/Def/Implementation/Experimentation/KeybindingResetDetector.cs index 1762f240674dd..6d06f193d7328 100644 --- a/src/VisualStudio/Core/Def/Implementation/Experimentation/KeybindingResetDetector.cs +++ b/src/VisualStudio/Core/Def/Implementation/Experimentation/KeybindingResetDetector.cs @@ -114,7 +114,7 @@ private void InitializeCore() return; } - var vsShell = _serviceProvider.GetService(); + var vsShell = _serviceProvider.GetServiceTheWrongWay(); var hr = vsShell.IsPackageInstalled(ReSharperPackageGuid, out var extensionEnabled); if (ErrorHandler.Failed(hr)) { @@ -127,7 +127,7 @@ private void InitializeCore() if (_resharperExtensionInstalledAndEnabled) { // We need to monitor for suspend/resume commands, so create and install the command target and the modal callback. - var priorityCommandTargetRegistrar = _serviceProvider.GetService(); + var priorityCommandTargetRegistrar = _serviceProvider.GetServiceTheWrongWay(); hr = priorityCommandTargetRegistrar.RegisterPriorityCommandTarget( dwReserved: 0 /* from docs must be 0 */, pCmdTrgt: this, @@ -335,7 +335,7 @@ async Task EnsureOleCommandTargetAsync() await ThreadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); - _oleCommandTarget = _serviceProvider.GetService(); + _oleCommandTarget = _serviceProvider.GetServiceTheWrongWay(); } } @@ -345,7 +345,7 @@ private void RestoreVsKeybindings() if (_uiShell == null) { - _uiShell = _serviceProvider.GetService(); + _uiShell = _serviceProvider.GetServiceTheWrongWay(); } ErrorHandler.ThrowOnFailure(_uiShell.PostExecCommand( @@ -432,7 +432,7 @@ private async Task ShutdownAsync() if (_priorityCommandTargetCookie != VSConstants.VSCOOKIE_NIL) { - var priorityCommandTargetRegistrar = _serviceProvider.GetService(); + var priorityCommandTargetRegistrar = _serviceProvider.GetServiceTheWrongWay(); var cookie = _priorityCommandTargetCookie; _priorityCommandTargetCookie = VSConstants.VSCOOKIE_NIL; var hr = priorityCommandTargetRegistrar.UnregisterPriorityCommandTarget(cookie); diff --git a/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioWorkspaceImpl.cs b/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioWorkspaceImpl.cs index 2687b3b81cf07..fa1177a530d24 100644 --- a/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioWorkspaceImpl.cs +++ b/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioWorkspaceImpl.cs @@ -1033,7 +1033,7 @@ private bool TryGetFrame(CodeAnalysis.TextDocument document, [NotNullWhen(return // document using its ItemId. Thus, we must use OpenDocumentViaProject, which only // depends on the file path. - var openDocumentService = ServiceProvider.GlobalProvider.GetService(); + var openDocumentService = ServiceProvider.GlobalProvider.GetServiceTheWrongWay(); return ErrorHandler.Succeeded(openDocumentService.OpenDocumentViaProject( document.FilePath, VSConstants.LOGVIEWID.TextView_guid, @@ -1072,7 +1072,7 @@ public void CloseDocumentCore(DocumentId documentId) var filePath = this.GetFilePath(documentId); if (filePath != null) { - var openDocumentService = ServiceProvider.GlobalProvider.GetService(); + var openDocumentService = ServiceProvider.GlobalProvider.GetServiceTheWrongWay(); if (ErrorHandler.Succeeded(openDocumentService.IsDocumentOpen(null, 0, filePath, Guid.Empty, 0, out var uiHierarchy, null, out var frame, out var isOpen))) { // TODO: do we need save argument for CloseDocument? diff --git a/src/VisualStudio/Core/Def/Utilities/IServiceProviderExtensions.cs b/src/VisualStudio/Core/Def/Utilities/IServiceProviderExtensions.cs index 578a160088f1f..9c1777e9dcb31 100644 --- a/src/VisualStudio/Core/Def/Utilities/IServiceProviderExtensions.cs +++ b/src/VisualStudio/Core/Def/Utilities/IServiceProviderExtensions.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Diagnostics; namespace Microsoft.VisualStudio.LanguageServices.Utilities { @@ -11,7 +12,7 @@ internal static class IServiceProviderExtensions /// /// Returns the specified interface from the service. This is useful when the service and interface differ /// - public static TInterfaceType GetService(this IServiceProvider sp) + public static TInterfaceType GetServiceTheWrongWay(this IServiceProvider sp) where TInterfaceType : class where TServiceType : class { @@ -22,6 +23,6 @@ public static TInterfaceType GetService(this IServ /// Returns the specified service type from the service. /// public static TServiceType GetService(this IServiceProvider sp) where TServiceType : class - => sp.GetService(); + => sp.GetServiceTheWrongWay(); } } From dc003564f29df0cb783f4644645b734b54ac8f73 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Wed, 15 Jul 2020 18:58:56 -0700 Subject: [PATCH 2/3] Consolidate service provider extensions --- .../Extensions/ServiceProviderExtensions.cs | 19 ------------------- .../AbstractSyncClassViewCommandHandler.cs | 2 +- .../VisualStudioSymbolNavigationService.cs | 2 +- .../Utilities/IServiceProviderExtensions.cs | 8 ++++++++ 4 files changed, 10 insertions(+), 21 deletions(-) delete mode 100644 src/VisualStudio/Core/Def/Implementation/Extensions/ServiceProviderExtensions.cs diff --git a/src/VisualStudio/Core/Def/Implementation/Extensions/ServiceProviderExtensions.cs b/src/VisualStudio/Core/Def/Implementation/Extensions/ServiceProviderExtensions.cs deleted file mode 100644 index 5343351694281..0000000000000 --- a/src/VisualStudio/Core/Def/Implementation/Extensions/ServiceProviderExtensions.cs +++ /dev/null @@ -1,19 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Diagnostics; - -namespace Microsoft.VisualStudio.LanguageServices.Implementation.Extensions -{ - internal static class ServiceProviderExtensions - { - public static TInterface GetService(this IServiceProvider serviceProvider) - { - var service = (TInterface)serviceProvider.GetService(typeof(TService)); - Debug.Assert(service != null); - return service; - } - } -} diff --git a/src/VisualStudio/Core/Def/Implementation/Library/ClassView/AbstractSyncClassViewCommandHandler.cs b/src/VisualStudio/Core/Def/Implementation/Library/ClassView/AbstractSyncClassViewCommandHandler.cs index 214a2b35bf71a..a1e0ad01b23b9 100644 --- a/src/VisualStudio/Core/Def/Implementation/Library/ClassView/AbstractSyncClassViewCommandHandler.cs +++ b/src/VisualStudio/Core/Def/Implementation/Library/ClassView/AbstractSyncClassViewCommandHandler.cs @@ -9,7 +9,7 @@ using Microsoft.CodeAnalysis.LanguageServices; using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.VisualStudio.Commanding; -using Microsoft.VisualStudio.LanguageServices.Implementation.Extensions; +using Microsoft.VisualStudio.LanguageServices.Utilities; using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; using Microsoft.VisualStudio.Text.Editor.Commanding.Commands; diff --git a/src/VisualStudio/Core/Def/Implementation/Workspace/VisualStudioSymbolNavigationService.cs b/src/VisualStudio/Core/Def/Implementation/Workspace/VisualStudioSymbolNavigationService.cs index a1e3a045c5adf..2f1e4fe9db179 100644 --- a/src/VisualStudio/Core/Def/Implementation/Workspace/VisualStudioSymbolNavigationService.cs +++ b/src/VisualStudio/Core/Def/Implementation/Workspace/VisualStudioSymbolNavigationService.cs @@ -24,9 +24,9 @@ using Microsoft.CodeAnalysis.Text; using Microsoft.VisualStudio.ComponentModelHost; using Microsoft.VisualStudio.Editor; -using Microsoft.VisualStudio.LanguageServices.Implementation.Extensions; using Microsoft.VisualStudio.LanguageServices.Implementation.Library; using Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem; +using Microsoft.VisualStudio.LanguageServices.Utilities; using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; using Microsoft.VisualStudio.Text; diff --git a/src/VisualStudio/Core/Def/Utilities/IServiceProviderExtensions.cs b/src/VisualStudio/Core/Def/Utilities/IServiceProviderExtensions.cs index 9c1777e9dcb31..7b6ae46c486c9 100644 --- a/src/VisualStudio/Core/Def/Utilities/IServiceProviderExtensions.cs +++ b/src/VisualStudio/Core/Def/Utilities/IServiceProviderExtensions.cs @@ -9,6 +9,14 @@ namespace Microsoft.VisualStudio.LanguageServices.Utilities { internal static class IServiceProviderExtensions { + /// + public static TInterface GetService(this IServiceProvider sp) + { + var service = (TInterface)sp.GetService(typeof(TService)); + Debug.Assert(service != null); + return service; + } + /// /// Returns the specified interface from the service. This is useful when the service and interface differ /// From 6e51025907d836d9228df67484fb20b8654e33f5 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Wed, 15 Jul 2020 19:03:50 -0700 Subject: [PATCH 3/3] Fix usage of GetService extension --- .../Experimentation/KeybindingResetDetector.cs | 10 +++++----- .../ProjectSystem/VisualStudioWorkspaceImpl.cs | 4 ++-- .../Core/Def/Utilities/IServiceProviderExtensions.cs | 12 +----------- 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/src/VisualStudio/Core/Def/Implementation/Experimentation/KeybindingResetDetector.cs b/src/VisualStudio/Core/Def/Implementation/Experimentation/KeybindingResetDetector.cs index 6d06f193d7328..3f4eb03778a05 100644 --- a/src/VisualStudio/Core/Def/Implementation/Experimentation/KeybindingResetDetector.cs +++ b/src/VisualStudio/Core/Def/Implementation/Experimentation/KeybindingResetDetector.cs @@ -114,7 +114,7 @@ private void InitializeCore() return; } - var vsShell = _serviceProvider.GetServiceTheWrongWay(); + var vsShell = _serviceProvider.GetService(); var hr = vsShell.IsPackageInstalled(ReSharperPackageGuid, out var extensionEnabled); if (ErrorHandler.Failed(hr)) { @@ -127,7 +127,7 @@ private void InitializeCore() if (_resharperExtensionInstalledAndEnabled) { // We need to monitor for suspend/resume commands, so create and install the command target and the modal callback. - var priorityCommandTargetRegistrar = _serviceProvider.GetServiceTheWrongWay(); + var priorityCommandTargetRegistrar = _serviceProvider.GetService(); hr = priorityCommandTargetRegistrar.RegisterPriorityCommandTarget( dwReserved: 0 /* from docs must be 0 */, pCmdTrgt: this, @@ -335,7 +335,7 @@ async Task EnsureOleCommandTargetAsync() await ThreadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); - _oleCommandTarget = _serviceProvider.GetServiceTheWrongWay(); + _oleCommandTarget = _serviceProvider.GetService(); } } @@ -345,7 +345,7 @@ private void RestoreVsKeybindings() if (_uiShell == null) { - _uiShell = _serviceProvider.GetServiceTheWrongWay(); + _uiShell = _serviceProvider.GetService(); } ErrorHandler.ThrowOnFailure(_uiShell.PostExecCommand( @@ -432,7 +432,7 @@ private async Task ShutdownAsync() if (_priorityCommandTargetCookie != VSConstants.VSCOOKIE_NIL) { - var priorityCommandTargetRegistrar = _serviceProvider.GetServiceTheWrongWay(); + var priorityCommandTargetRegistrar = _serviceProvider.GetService(); var cookie = _priorityCommandTargetCookie; _priorityCommandTargetCookie = VSConstants.VSCOOKIE_NIL; var hr = priorityCommandTargetRegistrar.UnregisterPriorityCommandTarget(cookie); diff --git a/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioWorkspaceImpl.cs b/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioWorkspaceImpl.cs index fa1177a530d24..c8782d0474334 100644 --- a/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioWorkspaceImpl.cs +++ b/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioWorkspaceImpl.cs @@ -1033,7 +1033,7 @@ private bool TryGetFrame(CodeAnalysis.TextDocument document, [NotNullWhen(return // document using its ItemId. Thus, we must use OpenDocumentViaProject, which only // depends on the file path. - var openDocumentService = ServiceProvider.GlobalProvider.GetServiceTheWrongWay(); + var openDocumentService = ServiceProvider.GlobalProvider.GetService(); return ErrorHandler.Succeeded(openDocumentService.OpenDocumentViaProject( document.FilePath, VSConstants.LOGVIEWID.TextView_guid, @@ -1072,7 +1072,7 @@ public void CloseDocumentCore(DocumentId documentId) var filePath = this.GetFilePath(documentId); if (filePath != null) { - var openDocumentService = ServiceProvider.GlobalProvider.GetServiceTheWrongWay(); + var openDocumentService = ServiceProvider.GlobalProvider.GetService(); if (ErrorHandler.Succeeded(openDocumentService.IsDocumentOpen(null, 0, filePath, Guid.Empty, 0, out var uiHierarchy, null, out var frame, out var isOpen))) { // TODO: do we need save argument for CloseDocument? diff --git a/src/VisualStudio/Core/Def/Utilities/IServiceProviderExtensions.cs b/src/VisualStudio/Core/Def/Utilities/IServiceProviderExtensions.cs index 7b6ae46c486c9..a15e14b4d8407 100644 --- a/src/VisualStudio/Core/Def/Utilities/IServiceProviderExtensions.cs +++ b/src/VisualStudio/Core/Def/Utilities/IServiceProviderExtensions.cs @@ -17,20 +17,10 @@ public static TInterface GetService(this IServiceProvider return service; } - /// - /// Returns the specified interface from the service. This is useful when the service and interface differ - /// - public static TInterfaceType GetServiceTheWrongWay(this IServiceProvider sp) - where TInterfaceType : class - where TServiceType : class - { - return (TInterfaceType)sp.GetService(typeof(TServiceType)); - } - /// /// Returns the specified service type from the service. /// public static TServiceType GetService(this IServiceProvider sp) where TServiceType : class - => sp.GetServiceTheWrongWay(); + => sp.GetService(); } }