diff --git a/src/EditorFeatures/Core.Wpf/AsyncCompletion/LanguageServerSnippetExpanderAdapter.cs b/src/EditorFeatures/Core.Wpf/AsyncCompletion/LanguageServerSnippetExpanderAdapter.cs new file mode 100644 index 0000000000000..b98a0d39d296c --- /dev/null +++ b/src/EditorFeatures/Core.Wpf/AsyncCompletion/LanguageServerSnippetExpanderAdapter.cs @@ -0,0 +1,30 @@ +// 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.Composition; +using Microsoft.CodeAnalysis.Host.Mef; +using Microsoft.VisualStudio.LanguageServer.Client.Snippets; +using Microsoft.VisualStudio.Text; +using Microsoft.VisualStudio.Text.Editor; + +namespace Microsoft.CodeAnalysis.Editor.Implementation.IntelliSense.AsyncCompletion +{ + [Export(typeof(ILanguageServerSnippetExpander))] + [Shared] + internal sealed class LanguageServerSnippetExpanderAdapter : ILanguageServerSnippetExpander + { + private readonly LanguageServerSnippetExpander _languageServerSnippetExpander; + + [ImportingConstructor] + [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] + public LanguageServerSnippetExpanderAdapter(LanguageServerSnippetExpander languageServerSnippetExpander) + { + _languageServerSnippetExpander = languageServerSnippetExpander; + } + + public bool TryExpand(string lspSnippetText, SnapshotSpan snapshotSpan, ITextView textView) + => _languageServerSnippetExpander.TryExpand(lspSnippetText, snapshotSpan, textView); + } +} diff --git a/src/EditorFeatures/Core/IntelliSense/AsyncCompletion/CommitManager.cs b/src/EditorFeatures/Core/IntelliSense/AsyncCompletion/CommitManager.cs index c3c57d8df5e2d..fd82f76ebb69d 100644 --- a/src/EditorFeatures/Core/IntelliSense/AsyncCompletion/CommitManager.cs +++ b/src/EditorFeatures/Core/IntelliSense/AsyncCompletion/CommitManager.cs @@ -16,11 +16,9 @@ using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Options; using Microsoft.CodeAnalysis.Shared.Extensions; -using Microsoft.CodeAnalysis.Snippets; using Microsoft.CodeAnalysis.Text; using Microsoft.CodeAnalysis.Text.Shared.Extensions; using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion; -using Microsoft.VisualStudio.LanguageServer.Client.Snippets; using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Text.Editor; using Microsoft.VisualStudio.Threading; @@ -40,7 +38,7 @@ internal sealed class CommitManager : IAsyncCompletionCommitManager private readonly ITextView _textView; private readonly IGlobalOptionService _globalOptions; private readonly IThreadingContext _threadingContext; - private readonly LanguageServerSnippetExpander _languageServerSnippetExpander; + private readonly ILanguageServerSnippetExpander? _languageServerSnippetExpander; public IEnumerable PotentialCommitCharacters { @@ -63,7 +61,7 @@ internal CommitManager( RecentItemsManager recentItemsManager, IGlobalOptionService globalOptions, IThreadingContext threadingContext, - LanguageServerSnippetExpander languageServerSnippetExpander) + ILanguageServerSnippetExpander? languageServerSnippetExpander) { _globalOptions = globalOptions; _threadingContext = threadingContext; @@ -250,6 +248,8 @@ private AsyncCompletionData.CommitResult Commit( // and if so, we call upon the LanguageServerSnippetExpander's TryExpand to insert the snippet. if (SnippetCompletionItem.IsSnippet(roslynItem)) { + Contract.ThrowIfNull(_languageServerSnippetExpander); + var lspSnippetText = change.Properties[SnippetCompletionItem.LSPSnippetKey]; if (!_languageServerSnippetExpander.TryExpand(lspSnippetText!, triggerSnapshotSpan, _textView)) diff --git a/src/EditorFeatures/Core/IntelliSense/AsyncCompletion/CommitManagerProvider.cs b/src/EditorFeatures/Core/IntelliSense/AsyncCompletion/CommitManagerProvider.cs index f1270a361628e..1781c22ffaebf 100644 --- a/src/EditorFeatures/Core/IntelliSense/AsyncCompletion/CommitManagerProvider.cs +++ b/src/EditorFeatures/Core/IntelliSense/AsyncCompletion/CommitManagerProvider.cs @@ -8,9 +8,7 @@ using Microsoft.CodeAnalysis.Editor.Shared.Utilities; using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.Options; -using Microsoft.CodeAnalysis.Snippets; using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion; -using Microsoft.VisualStudio.LanguageServer.Client.Snippets; using Microsoft.VisualStudio.Text.Editor; using Microsoft.VisualStudio.Utilities; @@ -24,7 +22,7 @@ internal class CommitManagerProvider : IAsyncCompletionCommitManagerProvider private readonly IThreadingContext _threadingContext; private readonly RecentItemsManager _recentItemsManager; private readonly IGlobalOptionService _globalOptions; - private readonly LanguageServerSnippetExpander _languageServerSnippetExpander; + private readonly ILanguageServerSnippetExpander? _languageServerSnippetExpander; [ImportingConstructor] [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] @@ -32,7 +30,7 @@ public CommitManagerProvider( IThreadingContext threadingContext, RecentItemsManager recentItemsManager, IGlobalOptionService globalOptions, - LanguageServerSnippetExpander languageServerSnippetExpander) + [Import(AllowDefault = true)] ILanguageServerSnippetExpander? languageServerSnippetExpander) { _threadingContext = threadingContext; _recentItemsManager = recentItemsManager; diff --git a/src/EditorFeatures/Core/IntelliSense/AsyncCompletion/ILanguageServerSnippetExpander.cs b/src/EditorFeatures/Core/IntelliSense/AsyncCompletion/ILanguageServerSnippetExpander.cs new file mode 100644 index 0000000000000..bdfe02aa3816b --- /dev/null +++ b/src/EditorFeatures/Core/IntelliSense/AsyncCompletion/ILanguageServerSnippetExpander.cs @@ -0,0 +1,14 @@ +// 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 Microsoft.VisualStudio.Text; +using Microsoft.VisualStudio.Text.Editor; + +namespace Microsoft.CodeAnalysis.Editor.Implementation.IntelliSense.AsyncCompletion +{ + internal interface ILanguageServerSnippetExpander + { + bool TryExpand(string lspSnippetText, SnapshotSpan snapshotSpan, ITextView textView); + } +}