提交 f3417747 编写于 作者: C CyrusNajmabadi

Use the presence/absence of services to decide if we'll show GoToDef and GoToImpl

上级 358f89b3
......@@ -109,8 +109,6 @@
<Compile Include="FindUsages\IFindUsagesContext.cs" />
<Compile Include="FindUsages\IFindUsagesService.cs" />
<Compile Include="FindUsages\SimpleFindUsagesContext.cs" />
<Compile Include="GoToDefinition\GoToDefinitionOptions.cs" />
<Compile Include="GoToImplementation\GoToImplementationOptions.cs" />
<Compile Include="Implementation\Structure\BlockTagState.cs" />
<Compile Include="Tags\ExportImageMonikerServiceAttribute.cs" />
<Compile Include="Implementation\NavigateTo\AbstractNavigateToItemDisplay.cs" />
......
......@@ -6,6 +6,7 @@
using Microsoft.CodeAnalysis.Editor.Host;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Notification;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Text;
......@@ -25,9 +26,16 @@ internal class GoToDefinitionCommandHandler :
_waitIndicator = waitIndicator;
}
private (Document, IGoToDefinitionService) GetDocumentAndService(ITextSnapshot snapshot)
{
var document = snapshot.GetOpenDocumentInCurrentContextWithChanges();
return (document, document?.GetLanguageService<IGoToDefinitionService>());
}
public CommandState GetCommandState(GoToDefinitionCommandArgs args, Func<CommandState> nextHandler)
{
return args.SubjectBuffer.GetFeatureOnOffOption(GoToDefinitionOptions.Enabled)
var (document, service) = GetDocumentAndService(args.SubjectBuffer.CurrentSnapshot);
return service != null
? CommandState.Available
: CommandState.Unavailable;
}
......@@ -35,10 +43,11 @@ public CommandState GetCommandState(GoToDefinitionCommandArgs args, Func<Command
public void ExecuteCommand(GoToDefinitionCommandArgs args, Action nextHandler)
{
var subjectBuffer = args.SubjectBuffer;
if (subjectBuffer.GetFeatureOnOffOption(GoToDefinitionOptions.Enabled))
var (document, service) = GetDocumentAndService(subjectBuffer.CurrentSnapshot);
if (service != null)
{
var caretPos = args.TextView.GetCaretPoint(subjectBuffer);
if (caretPos.HasValue && TryExecuteCommand(subjectBuffer.CurrentSnapshot, caretPos.Value))
if (caretPos.HasValue && TryExecuteCommand(document, caretPos.Value, service))
{
return;
}
......@@ -47,22 +56,13 @@ public void ExecuteCommand(GoToDefinitionCommandArgs args, Action nextHandler)
nextHandler();
}
// Internal for testing purposes only.
internal bool TryExecuteCommand(ITextSnapshot snapshot, int caretPosition)
{
var document = snapshot.GetOpenDocumentInCurrentContextWithChanges();
if (document != null)
{
var goToDefinitionService = document.Project.LanguageServices.GetService<IGoToDefinitionService>();
return TryExecuteCommand(document, caretPosition, goToDefinitionService);
}
else
{
// We didn't even have a workspace, so we can let somebody else try to handle this if they can
return false;
}
}
=> TryExecuteCommand(snapshot.GetOpenDocumentInCurrentContextWithChanges(), caretPosition);
internal bool TryExecuteCommand(Document document, int caretPosition)
=> TryExecuteCommand(document, caretPosition, document.GetLanguageService<IGoToDefinitionService>());
// Internal for testing purposes only.
internal bool TryExecuteCommand(Document document, int caretPosition, IGoToDefinitionService goToDefinitionService)
{
string errorMessage = null;
......@@ -92,4 +92,4 @@ internal bool TryExecuteCommand(Document document, int caretPosition, IGoToDefin
return true;
}
}
}
}
\ No newline at end of file
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.CodeAnalysis.Options;
namespace Microsoft.CodeAnalysis.Editor
{
internal static class GoToDefinitionOptions
{
public static readonly PerLanguageOption<bool> Enabled = new PerLanguageOption<bool>(
nameof(GoToDefinitionOptions), nameof(Enabled), defaultValue: true);
}
}
......@@ -13,6 +13,7 @@
using Microsoft.CodeAnalysis.Notification;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Text;
namespace Microsoft.CodeAnalysis.Editor.GoToImplementation
{
......@@ -32,40 +33,44 @@ internal partial class GoToImplementationCommandHandler : ICommandHandler<GoToIm
_streamingPresenters = streamingPresenters;
}
private (Document, IGoToImplementationService, IFindUsagesService) GetDocumentAndServices(ITextSnapshot snapshot)
{
var document = snapshot.GetOpenDocumentInCurrentContextWithChanges();
return (document,
document?.GetLanguageService<IGoToImplementationService>(),
document?.GetLanguageService<IFindUsagesService>());
}
public CommandState GetCommandState(GoToImplementationCommandArgs args, Func<CommandState> nextHandler)
{
// Because this is expensive to compute, we just always say yes as long as the language allows it.
return args.SubjectBuffer.GetFeatureOnOffOption(GoToImplementationOptions.Enabled)
var (document, implService, findUsagesService) = GetDocumentAndServices(args.SubjectBuffer.CurrentSnapshot);
return implService != null || findUsagesService != null
? CommandState.Available
: CommandState.Unavailable;
}
public void ExecuteCommand(GoToImplementationCommandArgs args, Action nextHandler)
{
var subjectBuffer = args.SubjectBuffer;
if (subjectBuffer.GetFeatureOnOffOption(GoToImplementationOptions.Enabled))
var (document, implService, findUsagesService) = GetDocumentAndServices(args.SubjectBuffer.CurrentSnapshot);
if (implService != null || findUsagesService != null)
{
var caret = args.TextView.GetCaretPoint(subjectBuffer);
var caret = args.TextView.GetCaretPoint(args.SubjectBuffer);
if (caret.HasValue)
{
var document = subjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();
if (document != null)
{
ExecuteCommand(document, caret.Value);
return;
}
ExecuteCommand(document, caret.Value, implService, findUsagesService);
return;
}
}
nextHandler();
}
private void ExecuteCommand(Document document, int caretPosition)
private void ExecuteCommand(
Document document, int caretPosition,
IGoToImplementationService synchronousService,
IFindUsagesService streamingService)
{
var streamingService = document.GetLanguageService<IFindUsagesService>();
var synchronousService = document.GetLanguageService<IGoToImplementationService>();
var streamingPresenter = GetStreamingPresenter();
var streamingEnabled = document.Project.Solution.Workspace.Options.GetOption(FeatureOnOffOptions.StreamingGoToImplementation, document.Project.Language);
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.CodeAnalysis.Options;
namespace Microsoft.CodeAnalysis.Editor.GoToImplementation
{
internal static class GoToImplementationOptions
{
public static readonly PerLanguageOption<bool> Enabled = new PerLanguageOption<bool>(
nameof(GoToImplementationOptions), nameof(Enabled), defaultValue: true);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册