-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return the SignatureHelp items nearest to the cursor #73606
Changes from all commits
4f3a760
593792b
6da3b0f
5940e8c
819706a
5df81cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// 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.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Extensions; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
using Roslyn.Utilities; | ||
|
||
namespace Microsoft.CodeAnalysis.SignatureHelp; | ||
|
||
/// <summary> | ||
/// A service that is used to determine the appropriate signature help for a position in a document. | ||
/// </summary> | ||
[method: ImportingConstructor] | ||
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] | ||
[Export(typeof(SignatureHelpService)), Shared] | ||
internal sealed class SignatureHelpService([ImportMany] IEnumerable<Lazy<ISignatureHelpProvider, OrderableLanguageMetadata>> allProviders) | ||
{ | ||
private readonly ConcurrentDictionary<string, ImmutableArray<ISignatureHelpProvider>> _providersByLanguage = []; | ||
private readonly IEnumerable<Lazy<ISignatureHelpProvider, OrderableLanguageMetadata>> _allProviders = allProviders; | ||
|
||
private ImmutableArray<ISignatureHelpProvider> GetProviders(string language) | ||
{ | ||
return _providersByLanguage.GetOrAdd(language, language => | ||
_allProviders | ||
.Where(p => p.Metadata.Language == language) | ||
.SelectAsArray(p => p.Value)); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the <see cref="ISignatureHelpProvider"/> and <see cref="SignatureHelpItems"/> associated with | ||
/// the position in the document. | ||
/// </summary> | ||
public Task<(ISignatureHelpProvider? provider, SignatureHelpItems? bestItems)> GetSignatureHelpAsync( | ||
Document document, | ||
int position, | ||
SignatureHelpTriggerInfo triggerInfo, | ||
SignatureHelpOptions options, | ||
CancellationToken cancellationToken) | ||
{ | ||
return GetSignatureHelpAsync( | ||
GetProviders(document.Project.Language), | ||
document, | ||
position, | ||
triggerInfo, | ||
options, | ||
cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the <see cref="ISignatureHelpProvider"/> and <see cref="SignatureHelpItems"/> associated with | ||
/// the position in the document. | ||
/// </summary> | ||
public static async Task<(ISignatureHelpProvider? provider, SignatureHelpItems? bestItems)> GetSignatureHelpAsync( | ||
ImmutableArray<ISignatureHelpProvider> providers, | ||
Document document, | ||
int position, | ||
SignatureHelpTriggerInfo triggerInfo, | ||
SignatureHelpOptions options, | ||
CancellationToken cancellationToken) | ||
{ | ||
var extensionManager = document.Project.Solution.Services.GetRequiredService<IExtensionManager>(); | ||
|
||
ISignatureHelpProvider? bestProvider = null; | ||
SignatureHelpItems? bestItems = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'm assuming this was a move, and didn't change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, yeah I meant more generally the logic moved but not verbatim. On a suggestion from DavidB it uses the extension manager when invoking providers as well as inlines the |
||
|
||
// returns the first non-empty quick info found (based on provider order) | ||
foreach (var provider in providers) | ||
{ | ||
var items = await extensionManager.PerformFunctionAsync( | ||
provider, | ||
cancellationToken => provider.GetItemsAsync(document, position, triggerInfo, options, cancellationToken), | ||
defaultValue: null, | ||
cancellationToken).ConfigureAwait(false); | ||
|
||
if (items is null || !items.ApplicableSpan.IntersectsWith(position)) | ||
{ | ||
continue; | ||
} | ||
|
||
// If another provider provides sig help items, then only take them if they | ||
// start after the last batch of items. i.e. we want the set of items that | ||
// conceptually are closer to where the caret position is. This way if you have: | ||
// | ||
// Goo(new Bar($$ | ||
// | ||
// Then invoking sig help will only show the items for "new Bar(" and not also | ||
// the items for "Goo(..." | ||
if (bestItems is not null && items.ApplicableSpan.Start < bestItems.ApplicableSpan.Start) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the moved |
||
{ | ||
continue; | ||
} | ||
|
||
bestProvider = provider; | ||
bestItems = items; | ||
} | ||
|
||
return (bestProvider, bestItems); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved into the SignatureHelpService.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm assuming it didn't change (beyond anything mechanical). if it did, please call that out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will do a better job of this going forward. Added comments below.