Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,10 @@ async Task<C> $$
}
""", "GetCAsync");

[Fact(Skip = "not yet implemented")]
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/17989")]
public Task NonAsyncTaskOfT()
=> VerifyItemExistsAsync("""
using System.Threading.Tasks;
public class C
{
Task<C> $$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public override async Task ProvideCompletionsAsync(CompletionContext completionC
var sortValue = 0;
foreach (var recommender in Recommenders)
{
var names = await recommender.Value.ProvideRecommendedNamesAsync(completionContext, document, context, nameInfo, cancellationToken).ConfigureAwait(false);
var names = await recommender.Value.ProvideRecommendedNamesAsync(
completionContext, document, context, nameInfo, cancellationToken).ConfigureAwait(false);

foreach (var (name, glyph) in names)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ private static bool IsPropertyDeclaration(SyntaxToken token, SemanticModel seman
return result.Type != null;
}

private static bool IsMethodDeclaration(SyntaxToken token, SemanticModel semanticModel,
CancellationToken cancellationToken, out NameDeclarationInfo result)
private static bool IsMethodDeclaration(
SyntaxToken token, SemanticModel semanticModel, CancellationToken cancellationToken, out NameDeclarationInfo result)
{
result = IsLastTokenOfType<MethodDeclarationSyntax>(
token,
Expand All @@ -220,45 +220,30 @@ private static NameDeclarationInfo IsFollowingTypeOrComma<TSyntaxNode>(
Func<DeclarationModifiers, ImmutableArray<SymbolKindOrTypeKind>> possibleDeclarationComputer,
CancellationToken cancellationToken) where TSyntaxNode : SyntaxNode
{
if (!IsPossibleTypeToken(token) && !token.IsKind(SyntaxKind.CommaToken))
{
var afterComma = token.IsKind(SyntaxKind.CommaToken);
if (!IsPossibleTypeToken(token) && !afterComma)
return default;
}

var target = token.GetAncestor<TSyntaxNode>();
if (target == null)
{
return default;
}

if (token.IsKind(SyntaxKind.CommaToken) && token.Parent != target)
{
if (afterComma && token.Parent != target)
return default;
}

var typeSyntax = typeSyntaxGetter(target);
if (typeSyntax == null)
{
return default;
}

if (!token.IsKind(SyntaxKind.CommaToken) && token != typeSyntax.GetLastToken())
{
if (!afterComma && token != typeSyntax.GetLastToken())
return default;
}

var modifiers = modifierGetter(target);
if (modifiers == null)
{
return default;
}

return new NameDeclarationInfo(
possibleDeclarationComputer(GetDeclarationModifiers(modifiers.Value)),
GetAccessibility(modifiers.Value),
GetDeclarationModifiers(modifiers.Value),
semanticModel.GetTypeInfo(typeSyntax, cancellationToken).Type,
semanticModel.GetAliasInfo(typeSyntax, cancellationToken));
return ComputeInfo(
semanticModel, possibleDeclarationComputer, typeSyntax, modifiers.Value, tryInferAsync: !afterComma, cancellationToken);
}

private static NameDeclarationInfo IsLastTokenOfType<TSyntaxNode>(
Expand Down Expand Up @@ -295,11 +280,36 @@ private static NameDeclarationInfo IsLastTokenOfType<TSyntaxNode>(

var modifiers = modifierGetter(target);

return ComputeInfo(semanticModel, possibleDeclarationComputer, typeSyntax, modifiers, tryInferAsync: true, cancellationToken);
}

private static NameDeclarationInfo ComputeInfo(
SemanticModel semanticModel,
Func<DeclarationModifiers, ImmutableArray<SymbolKindOrTypeKind>> possibleDeclarationComputer,
SyntaxNode typeSyntax,
SyntaxTokenList modifiers,
bool tryInferAsync,
CancellationToken cancellationToken)
{
var declarationModifiers = GetDeclarationModifiers(modifiers);
var possibleDeclarations = possibleDeclarationComputer(declarationModifiers);

// Treat a declaration as async if if it is explicitly marked as 'async' or if it is a method that returns a
// Task-like type. The latter ensures that even if the user didn't have an explicit 'async' modifier, we still
// name the member in an appropriate fashion (e.g. `GetCustomerAsync` for `public Task<Customer> $$`).
var type = semanticModel.GetTypeInfo(typeSyntax, cancellationToken).Type;
if (!declarationModifiers.IsAsync && tryInferAsync)
{
var knownTaskTypes = new KnownTaskTypes(semanticModel.Compilation);
if (knownTaskTypes.IsTaskLike(type))
declarationModifiers = declarationModifiers.WithAsync(true);
}

return new NameDeclarationInfo(
possibleDeclarationComputer(GetDeclarationModifiers(modifiers)),
possibleDeclarations,
GetAccessibility(modifiers),
GetDeclarationModifiers(modifiers),
semanticModel.GetTypeInfo(typeSyntax, cancellationToken).Type,
declarationModifiers,
type,
semanticModel.GetAliasInfo(typeSyntax, cancellationToken));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,22 +215,14 @@ private static void GetRecommendedNames(

foreach (var kind in declarationInfo.PossibleSymbolKinds)
{
ProcessRules(rules, firstMatchOnly: true, kind, baseNames, declarationInfo, context, result, semanticFactsService, seenBaseNames, seenUniqueNames, cancellationToken);
ProcessRules(supplementaryRules, firstMatchOnly: false, kind, baseNames, declarationInfo, context, result, semanticFactsService, seenBaseNames, seenUniqueNames, cancellationToken);
ProcessRules(rules, firstMatchOnly: true, kind);
ProcessRules(supplementaryRules, firstMatchOnly: false, kind);
}

static void ProcessRules(
void ProcessRules(
ImmutableArray<NamingRule> rules,
bool firstMatchOnly,
SymbolSpecification.SymbolKindOrTypeKind kind,
ImmutableArray<ImmutableArray<string>> baseNames,
NameDeclarationInfo declarationInfo,
CSharpSyntaxContext context,
ArrayBuilder<(string, Glyph)> result,
ISemanticFactsService semanticFactsService,
PooledHashSet<string> seenBaseNames,
PooledHashSet<string> seenUniqueNames,
CancellationToken cancellationToken)
SymbolSpecification.SymbolKindOrTypeKind kind)
{
var modifiers = declarationInfo.Modifiers;
foreach (var rule in rules)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// 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.Diagnostics.CodeAnalysis;

namespace Microsoft.CodeAnalysis.Shared.Extensions;

internal readonly struct KnownTaskTypes(Compilation compilation)
Expand All @@ -17,22 +19,25 @@ internal readonly struct KnownTaskTypes(Compilation compilation)
public readonly INamedTypeSymbol? IAsyncEnumerableOfTType = compilation.IAsyncEnumerableOfTType();
public readonly INamedTypeSymbol? IAsyncEnumeratorOfTType = compilation.IAsyncEnumeratorOfTType();

public bool IsTaskLike(ITypeSymbol returnType)
public bool IsTaskLike([NotNullWhen(true)] ITypeSymbol? returnType)
{
if (returnType.Equals(this.TaskType))
return true;
if (returnType is not null)
{
if (returnType.Equals(this.TaskType))
return true;

if (returnType.Equals(this.ValueTaskType))
return true;
if (returnType.Equals(this.ValueTaskType))
return true;

if (returnType.OriginalDefinition.Equals(this.TaskOfTType))
return true;
if (returnType.OriginalDefinition.Equals(this.TaskOfTType))
return true;

if (returnType.OriginalDefinition.Equals(this.ValueTaskOfTType))
return true;
if (returnType.OriginalDefinition.Equals(this.ValueTaskOfTType))
return true;

if (returnType.IsErrorType())
return returnType.Name is "Task" or "ValueTask";
if (returnType.IsErrorType())
return returnType.Name is "Task" or "ValueTask";
}

return false;
}
Expand Down
Loading