Skip to content

Commit

Permalink
Implement an adapter interface to access LanguageServerSnippetExpande…
Browse files Browse the repository at this point in the history
…r via the EditorFeatures layer
  • Loading branch information
sharwell committed May 25, 2022
1 parent a24ef90 commit fb46e1d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<char> PotentialCommitCharacters
{
Expand All @@ -63,7 +61,7 @@ internal CommitManager(
RecentItemsManager recentItemsManager,
IGlobalOptionService globalOptions,
IThreadingContext threadingContext,
LanguageServerSnippetExpander languageServerSnippetExpander)
ILanguageServerSnippetExpander? languageServerSnippetExpander)
{
_globalOptions = globalOptions;
_threadingContext = threadingContext;
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,15 +22,15 @@ 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)]
public CommitManagerProvider(
IThreadingContext threadingContext,
RecentItemsManager recentItemsManager,
IGlobalOptionService globalOptions,
LanguageServerSnippetExpander languageServerSnippetExpander)
[Import(AllowDefault = true)] ILanguageServerSnippetExpander? languageServerSnippetExpander)
{
_threadingContext = threadingContext;
_recentItemsManager = recentItemsManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit fb46e1d

Please sign in to comment.