提交 39ad5b1c 编写于 作者: A Andrew Casey

Merge pull request #5123 from amcasey/Snippets

Disable snippets in the Interactive window
......@@ -11,6 +11,7 @@
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Projection;
using Roslyn.Utilities;
using Microsoft.CodeAnalysis.Editor.Shared.Options;
namespace Microsoft.CodeAnalysis.Editor.Implementation.IntelliSense.Completion
{
......@@ -94,10 +95,13 @@ internal class Model
CompletionItem updatedBuilder = builder;
CompletionItem updatedDefaultBuilder = GetDefaultBuilder(defaultTrackingSpanInSubjectBuffer);
if (completionService != null && workspace != null && triggerInfo.TriggerReason != CompletionTriggerReason.Snippets)
if (completionService != null &&
workspace != null &&
workspace.Options.GetOption(InternalFeatureOnOffOptions.Snippets) &&
triggerInfo.TriggerReason != CompletionTriggerReason.Snippets)
{
// In order to add snippet expansion notes to completion item descriptions, update
// all of the provided CompletionItems to DisplayCompletionItems which will proxy
// all of the provided CompletionItems to DescriptionModifyingCompletionItem which will proxy
// requests to the original completion items and add the snippet expansion note to
// the description if necessary. We won't do this if the list was triggered to show
// snippet shortcuts.
......
......@@ -43,8 +43,9 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.IntelliSense
Private Sub New(workspaceElement As XElement,
extraCompletionProviders As IEnumerable(Of Lazy(Of CompletionListProvider, OrderableLanguageAndRoleMetadata)),
extraSignatureHelpProviders As IEnumerable(Of Lazy(Of ISignatureHelpProvider, OrderableLanguageMetadata)),
Optional extraExportedTypes As List(Of Type) = Nothing)
MyBase.New(workspaceElement, CreatePartCatalog(extraExportedTypes))
Optional extraExportedTypes As List(Of Type) = Nothing,
Optional workspaceKind As String = Nothing)
MyBase.New(workspaceElement, CreatePartCatalog(extraExportedTypes), workspaceKind:=workspaceKind)
Dim languageServices = Me.Workspace.CurrentSolution.Projects.First().LanguageServices
Dim language = languageServices.Language
......@@ -125,12 +126,14 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.IntelliSense
workspaceElement As XElement,
Optional extraCompletionProviders As CompletionListProvider() = Nothing,
Optional extraSignatureHelpProviders As ISignatureHelpProvider() = Nothing,
Optional extraExportedTypes As List(Of Type) = Nothing) As TestState
Optional extraExportedTypes As List(Of Type) = Nothing,
Optional workspaceKind As String = Nothing) As TestState
Return New TestState(
workspaceElement,
CreateLazyProviders(extraCompletionProviders, LanguageNames.VisualBasic, roles:=Nothing),
CreateLazyProviders(extraSignatureHelpProviders, LanguageNames.VisualBasic),
extraExportedTypes)
extraExportedTypes,
workspaceKind)
End Function
#Region "IntelliSense Operations"
......
......@@ -43,9 +43,11 @@ protected override async Task<IEnumerable<CompletionItem>> GetItemsWorkerAsync(D
{
using (Logger.LogBlock(FunctionId.Completion_SnippetCompletionProvider_GetItemsWorker_CSharp, cancellationToken))
{
// TODO (https://github.com/dotnet/roslyn/issues/5107): Enable in Interactive.
var workspace = document.Project.Solution.Workspace;
if (!workspace.CanApplyChange(ApplyChangesKind.ChangeDocument) ||
workspace.Kind == WorkspaceKind.Debugger)
workspace.Kind == WorkspaceKind.Debugger ||
workspace.Kind == WorkspaceKind.Interactive)
{
return SpecializedCollections.EmptyEnumerable<CompletionItem>();
}
......
// 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.Editor.Interactive;
using Microsoft.CodeAnalysis.Editor.Shared.Options;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.SolutionCrawler;
using Microsoft.CodeAnalysis.Text;
......@@ -24,6 +25,9 @@ internal InteractiveWorkspace(InteractiveEvaluator engine, HostServices hostServ
// register work coordinator for this workspace
_registrationService = this.Services.GetService<ISolutionCrawlerRegistrationService>();
_registrationService.Register(this);
// TODO (https://github.com/dotnet/roslyn/issues/5107): Enable in Interactive.
this.Options = this.Options.WithChangedOption(InternalFeatureOnOffOptions.Snippets, false);
}
protected override void Dispose(bool finalize)
......
......@@ -49,6 +49,13 @@ public void ExecuteCommand(SurroundWithCommandArgs args, Action nextHandler)
public CommandState GetCommandState(SurroundWithCommandArgs args, Func<CommandState> nextHandler)
{
AssertIsForeground();
if (!args.SubjectBuffer.GetOption(InternalFeatureOnOffOptions.Snippets))
{
return nextHandler();
}
Workspace workspace;
if (!Workspace.TryGetWorkspace(args.SubjectBuffer.AsTextContainer(), out workspace))
{
......
......@@ -82,6 +82,12 @@ public void ExecuteCommand(TabKeyCommandArgs args, Action nextHandler)
public CommandState GetCommandState(TabKeyCommandArgs args, Func<CommandState> nextHandler)
{
AssertIsForeground();
if (!args.SubjectBuffer.GetOption(InternalFeatureOnOffOptions.Snippets))
{
return nextHandler();
}
Workspace workspace;
if (!Workspace.TryGetWorkspace(args.SubjectBuffer.AsTextContainer(), out workspace))
{
......@@ -113,6 +119,12 @@ public void ExecuteCommand(ReturnKeyCommandArgs args, Action nextHandler)
public CommandState GetCommandState(ReturnKeyCommandArgs args, Func<CommandState> nextHandler)
{
AssertIsForeground();
if (!args.SubjectBuffer.GetOption(InternalFeatureOnOffOptions.Snippets))
{
return nextHandler();
}
Workspace workspace;
if (!Workspace.TryGetWorkspace(args.SubjectBuffer.AsTextContainer(), out workspace))
{
......@@ -144,6 +156,12 @@ public void ExecuteCommand(EscapeKeyCommandArgs args, Action nextHandler)
public CommandState GetCommandState(EscapeKeyCommandArgs args, Func<CommandState> nextHandler)
{
AssertIsForeground();
if (!args.SubjectBuffer.GetOption(InternalFeatureOnOffOptions.Snippets))
{
return nextHandler();
}
Workspace workspace;
if (!Workspace.TryGetWorkspace(args.SubjectBuffer.AsTextContainer(), out workspace))
{
......@@ -175,6 +193,12 @@ public void ExecuteCommand(BackTabKeyCommandArgs args, Action nextHandler)
public CommandState GetCommandState(BackTabKeyCommandArgs args, Func<CommandState> nextHandler)
{
AssertIsForeground();
if (!args.SubjectBuffer.GetOption(InternalFeatureOnOffOptions.Snippets))
{
return nextHandler();
}
Workspace workspace;
if (!Workspace.TryGetWorkspace(args.SubjectBuffer.AsTextContainer(), out workspace))
{
......@@ -199,6 +223,13 @@ public void ExecuteCommand(InsertSnippetCommandArgs args, Action nextHandler)
public CommandState GetCommandState(InsertSnippetCommandArgs args, Func<CommandState> nextHandler)
{
AssertIsForeground();
if (!args.SubjectBuffer.GetOption(InternalFeatureOnOffOptions.Snippets))
{
return nextHandler();
}
Workspace workspace;
if (!Workspace.TryGetWorkspace(args.SubjectBuffer.AsTextContainer(), out workspace))
{
......
......@@ -2,6 +2,7 @@
Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.Completion
Imports Microsoft.CodeAnalysis.Editor.Shared.Options
Imports Microsoft.CodeAnalysis.Editor.UnitTests.IntelliSense
Imports Microsoft.CodeAnalysis.Snippets
Imports Microsoft.CodeAnalysis.Text
......@@ -74,6 +75,33 @@ class C
End Using
End Sub
<Fact, Trait(Traits.Feature, Traits.Features.Completion), Trait(Traits.Feature, Traits.Features.Interactive)>
Public Sub SnippetExpansionNoteNotAddedToDescription_Interactive()
Dim workspaceXml =
<Workspace>
<Submission Language="C#" CommonReferences="true">
$$
</Submission>
</Workspace>
Using state = TestState.CreateTestStateFromWorkspace(
workspaceXml,
New CompletionListProvider() {New MockCompletionProvider(New TextSpan(31, 10))},
Nothing,
New List(Of Type) From {GetType(TestCSharpSnippetInfoService)},
WorkspaceKind.Interactive)
Dim testSnippetInfoService = DirectCast(state.Workspace.Services.GetLanguageServices(LanguageNames.CSharp).GetService(Of ISnippetInfoService)(), TestCSharpSnippetInfoService)
testSnippetInfoService.SetSnippetShortcuts({"for"})
state.Workspace.Options = state.Workspace.Options.WithChangedOption(InternalFeatureOnOffOptions.Snippets, False)
state.SendTypeChars("for")
state.AssertCompletionSession()
state.AssertSelectedCompletionItem(description:=String.Format(FeaturesResources.Keyword, "for"))
End Using
End Sub
Private Function CreateCSharpSnippetExpansionNoteTestState(xElement As XElement, ParamArray snippetShortcuts As String()) As TestState
Dim state = TestState.CreateCSharpTestState(
xElement,
......
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.Editor
Imports Microsoft.VisualStudio.Text
Imports Roslyn.Test.Utilities
......@@ -207,5 +208,76 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.Snippets
Assert.False(testState.SnippetExpansionClient.TryInsertExpansionCalled)
End Using
End Sub
<Fact, Trait(Traits.Feature, Traits.Features.Snippets), Trait(Traits.Feature, Traits.Features.Interactive)>
Public Sub SnippetCommandHandler_Interactive_Tab()
Dim markup = "for$$"
Dim testState = SnippetTestState.CreateSubmissionTestState(markup, LanguageNames.CSharp)
Using testState
testState.SnippetExpansionClient.TryInsertExpansionReturnValue = True
testState.SendTab()
Assert.False(testState.SnippetExpansionClient.TryInsertExpansionCalled)
Assert.Equal("for ", testState.SubjectBuffer.CurrentSnapshot.GetText())
End Using
End Sub
<Fact, Trait(Traits.Feature, Traits.Features.Snippets), Trait(Traits.Feature, Traits.Features.Interactive)>
Public Sub SnippetCommandHandler_Interactive_InsertSnippetCommand()
Dim markup = "for$$"
Dim testState = SnippetTestState.CreateSubmissionTestState(markup, LanguageNames.CSharp)
Using testState
Dim delegatedToNext = False
Dim nextHandler =
Function()
delegatedToNext = True
Return CommandState.Unavailable
End Function
Dim handler = testState.SnippetCommandHandler
Dim state = handler.GetCommandState(New Commands.InsertSnippetCommandArgs(testState.TextView, testState.SubjectBuffer), nextHandler)
Assert.True(delegatedToNext)
Assert.False(state.IsAvailable)
testState.SnippetExpansionClient.TryInsertExpansionReturnValue = True
delegatedToNext = False
testState.SendInsertSnippetCommand(AddressOf handler.ExecuteCommand, nextHandler)
Assert.True(delegatedToNext)
Assert.False(testState.SnippetExpansionClient.TryInsertExpansionCalled)
Assert.Equal("for", testState.SubjectBuffer.CurrentSnapshot.GetText())
End Using
End Sub
<Fact, Trait(Traits.Feature, Traits.Features.Snippets), Trait(Traits.Feature, Traits.Features.Interactive)>
Public Sub SnippetCommandHandler_Interactive_SurroundWithCommand()
Dim markup = "for$$"
Dim testState = SnippetTestState.CreateSubmissionTestState(markup, LanguageNames.CSharp)
Using testState
Dim delegatedToNext = False
Dim nextHandler =
Function()
delegatedToNext = True
Return CommandState.Unavailable
End Function
Dim handler = CType(testState.SnippetCommandHandler, CSharp.Snippets.SnippetCommandHandler)
Dim state = handler.GetCommandState(New Commands.SurroundWithCommandArgs(testState.TextView, testState.SubjectBuffer), nextHandler)
Assert.True(delegatedToNext)
Assert.False(state.IsAvailable)
testState.SnippetExpansionClient.TryInsertExpansionReturnValue = True
delegatedToNext = False
testState.SendSurroundWithCommand(AddressOf handler.ExecuteCommand, nextHandler)
Assert.True(delegatedToNext)
Assert.False(testState.SnippetExpansionClient.TryInsertExpansionCalled)
Assert.Equal("for", testState.SubjectBuffer.CurrentSnapshot.GetText())
End Using
End Sub
End Class
End Namespace
......@@ -30,14 +30,14 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.Snippets
Inherits AbstractCommandHandlerTestState
Implements IIntelliSenseTestState
Public Sub New(workspaceElement As XElement, languageName As String, startActiveSession As Boolean, extraParts As IEnumerable(Of Type))
MyBase.New(workspaceElement, extraParts:=CreatePartCatalog(extraParts))
Public Sub New(workspaceElement As XElement, languageName As String, startActiveSession As Boolean, extraParts As IEnumerable(Of Type), Optional workspaceKind As String = Nothing)
MyBase.New(workspaceElement, extraParts:=CreatePartCatalog(extraParts), workspaceKind:=workspaceKind)
Dim optionService = Workspace.Services.GetService(Of IOptionService)()
optionService.SetOptions(optionService.GetOptions().WithChangedOption(InternalFeatureOnOffOptions.Snippets, True))
Dim mockEditorAdaptersFactoryService = New Mock(Of IVsEditorAdaptersFactoryService)
Dim mockSVsServiceProvider = New Mock(Of SVsServiceProvider)
_snippetCommandHandler = If(languageName = LanguageNames.CSharp,
SnippetCommandHandler = If(languageName = LanguageNames.CSharp,
DirectCast(New CSharp.Snippets.SnippetCommandHandler(mockEditorAdaptersFactoryService.Object, mockSVsServiceProvider.Object), AbstractSnippetCommandHandler),
New VisualBasic.Snippets.SnippetCommandHandler(mockEditorAdaptersFactoryService.Object, mockSVsServiceProvider.Object))
......@@ -62,7 +62,7 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.Snippets
TextView.Properties.AddProperty(GetType(AbstractSnippetExpansionClient), SnippetExpansionClient)
End Sub
Private ReadOnly _snippetCommandHandler As AbstractSnippetCommandHandler
Public ReadOnly SnippetCommandHandler As AbstractSnippetCommandHandler
Private ReadOnly _completionCommandHandler As CompletionCommandHandler
Private _currentCompletionPresenterSession As TestCompletionPresenterSession
Public Property SnippetExpansionClient As MockSnippetExpansionClient
......@@ -101,6 +101,19 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.Snippets
Return New SnippetTestState(workspaceXml, languageName, startActiveSession, extraParts)
End Function
Public Shared Function CreateSubmissionTestState(markup As String, languageName As String, Optional startActiveSession As Boolean = False, Optional extraParts As IEnumerable(Of Type) = Nothing) As SnippetTestState
extraParts = If(extraParts, Type.EmptyTypes)
Dim workspaceXml = <Workspace>
<Submission Language=<%= languageName %> CommonReferences="true">
<%= markup %>
</Submission>
</Workspace>
Dim state = New SnippetTestState(workspaceXml, languageName, startActiveSession, extraParts, WorkspaceKind.Interactive)
state.Workspace.Options = state.Workspace.Options.WithChangedOption(InternalFeatureOnOffOptions.Snippets, False)
Return state
End Function
Friend Overloads Sub SendTabToCompletion()
Dim handler = DirectCast(_completionCommandHandler, ICommandHandler(Of TabKeyCommandArgs))
......@@ -108,19 +121,19 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.Snippets
End Sub
Friend Overloads Sub SendTab()
SendTab(AddressOf _snippetCommandHandler.ExecuteCommand, Function() EditorOperations.InsertText(" "))
SendTab(AddressOf SnippetCommandHandler.ExecuteCommand, Function() EditorOperations.InsertText(" "))
End Sub
Friend Overloads Sub SendBackTab()
SendBackTab(AddressOf _snippetCommandHandler.ExecuteCommand, Function() EditorOperations.Unindent())
SendBackTab(AddressOf SnippetCommandHandler.ExecuteCommand, Function() EditorOperations.Unindent())
End Sub
Friend Overloads Sub SendReturn()
SendReturn(AddressOf _snippetCommandHandler.ExecuteCommand, Function() EditorOperations.InsertNewLine())
SendReturn(AddressOf SnippetCommandHandler.ExecuteCommand, Function() EditorOperations.InsertNewLine())
End Sub
Friend Overloads Sub SendEscape()
SendEscape(AddressOf _snippetCommandHandler.ExecuteCommand, Function() EditorOperations.InsertText("EscapePassedThrough!"))
SendEscape(AddressOf SnippetCommandHandler.ExecuteCommand, Function() EditorOperations.InsertText("EscapePassedThrough!"))
End Sub
Private Class MockOrderableContentTypeMetadata
......
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.Editor
Imports Microsoft.VisualStudio.Text
Imports Roslyn.Test.Utilities
......@@ -362,5 +363,47 @@ End Class
Assert.False(testState.SnippetExpansionClient.TryInsertExpansionCalled)
End Using
End Sub
<Fact, Trait(Traits.Feature, Traits.Features.Snippets), Trait(Traits.Feature, Traits.Features.Interactive)>
Public Sub SnippetCommandHandler_Interactive_Tab()
Dim markup = "for$$"
Dim testState = SnippetTestState.CreateSubmissionTestState(markup, LanguageNames.VisualBasic)
Using testState
testState.SnippetExpansionClient.TryInsertExpansionReturnValue = True
testState.SendTab()
Assert.False(testState.SnippetExpansionClient.TryInsertExpansionCalled)
Assert.Equal("for ", testState.SubjectBuffer.CurrentSnapshot.GetText())
End Using
End Sub
<Fact, Trait(Traits.Feature, Traits.Features.Snippets), Trait(Traits.Feature, Traits.Features.Interactive)>
Public Sub SnippetCommandHandler_Interactive_InsertSnippetCommand()
Dim markup = "for$$"
Dim testState = SnippetTestState.CreateSubmissionTestState(markup, LanguageNames.VisualBasic)
Using testState
Dim delegatedToNext = False
Dim nextHandler =
Function()
delegatedToNext = True
Return CommandState.Unavailable
End Function
Dim handler = testState.SnippetCommandHandler
Dim state = handler.GetCommandState(New Commands.InsertSnippetCommandArgs(testState.TextView, testState.SubjectBuffer), nextHandler)
Assert.True(delegatedToNext)
Assert.False(state.IsAvailable)
testState.SnippetExpansionClient.TryInsertExpansionReturnValue = True
delegatedToNext = False
testState.SendInsertSnippetCommand(AddressOf handler.ExecuteCommand, nextHandler)
Assert.True(delegatedToNext)
Assert.False(testState.SnippetExpansionClient.TryInsertExpansionCalled)
Assert.Equal("for", testState.SubjectBuffer.CurrentSnapshot.GetText())
End Using
End Sub
End Class
End Namespace
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册