未验证 提交 44495558 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #46030 from sharwell/get-service

Fix confusing use of GetService extensions with different behavior
......@@ -114,7 +114,7 @@ private void InitializeCore()
return;
}
var vsShell = _serviceProvider.GetService<IVsShell, SVsShell>();
var vsShell = _serviceProvider.GetService<SVsShell, IVsShell>();
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<IVsRegisterPriorityCommandTarget, SVsRegisterPriorityCommandTarget>();
var priorityCommandTargetRegistrar = _serviceProvider.GetService<SVsRegisterPriorityCommandTarget, IVsRegisterPriorityCommandTarget>();
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<IOleCommandTarget, SUIHostCommandDispatcher>();
_oleCommandTarget = _serviceProvider.GetService<SUIHostCommandDispatcher, IOleCommandTarget>();
}
}
......@@ -345,7 +345,7 @@ private void RestoreVsKeybindings()
if (_uiShell == null)
{
_uiShell = _serviceProvider.GetService<IVsUIShell, SVsUIShell>();
_uiShell = _serviceProvider.GetService<SVsUIShell, IVsUIShell>();
}
ErrorHandler.ThrowOnFailure(_uiShell.PostExecCommand(
......@@ -432,7 +432,7 @@ private async Task ShutdownAsync()
if (_priorityCommandTargetCookie != VSConstants.VSCOOKIE_NIL)
{
var priorityCommandTargetRegistrar = _serviceProvider.GetService<IVsRegisterPriorityCommandTarget, SVsRegisterPriorityCommandTarget>();
var priorityCommandTargetRegistrar = _serviceProvider.GetService<SVsRegisterPriorityCommandTarget, IVsRegisterPriorityCommandTarget>();
var cookie = _priorityCommandTargetCookie;
_priorityCommandTargetCookie = VSConstants.VSCOOKIE_NIL;
var hr = priorityCommandTargetRegistrar.UnregisterPriorityCommandTarget(cookie);
......
// 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<TService, TInterface>(this IServiceProvider serviceProvider)
{
var service = (TInterface)serviceProvider.GetService(typeof(TService));
Debug.Assert(service != null);
return service;
}
}
}
......@@ -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;
......
......@@ -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<IVsUIShellOpenDocument, SVsUIShellOpenDocument>();
var openDocumentService = ServiceProvider.GlobalProvider.GetService<SVsUIShellOpenDocument, IVsUIShellOpenDocument>();
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<IVsUIShellOpenDocument, SVsUIShellOpenDocument>();
var openDocumentService = ServiceProvider.GlobalProvider.GetService<SVsUIShellOpenDocument, IVsUIShellOpenDocument>();
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?
......
......@@ -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;
......
......@@ -3,19 +3,18 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
namespace Microsoft.VisualStudio.LanguageServices.Utilities
{
internal static class IServiceProviderExtensions
{
/// <summary>
/// Returns the specified interface from the service. This is useful when the service and interface differ
/// </summary>
public static TInterfaceType GetService<TInterfaceType, TServiceType>(this IServiceProvider sp)
where TInterfaceType : class
where TServiceType : class
/// <inheritdoc cref="Shell.ServiceExtensions.GetService{TService, TInterface}(IServiceProvider, bool)"/>
public static TInterface GetService<TService, TInterface>(this IServiceProvider sp)
{
return (TInterfaceType)sp.GetService(typeof(TServiceType));
var service = (TInterface)sp.GetService(typeof(TService));
Debug.Assert(service != null);
return service;
}
/// <summary>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册