Skip to content
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

Treat record positional parameters as properties #48329

Merged
merged 6 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -28,6 +28,20 @@ public class DeclarationNameCompletionProviderTests : AbstractCSharpCompletionPr
internal override Type GetCompletionProviderType()
=> typeof(DeclarationNameCompletionProvider);

[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
[WorkItem(48310, "https://github.com/dotnet/roslyn/issues/48310")]
public async Task TreatRecordPositionalParameterAsProperty()
{
var markup = @"
public class MyClass
{
}

public record R(MyClass $$
";
await VerifyItemExistsAsync(markup, "MyClass", glyph: (int)Glyph.PropertyPublic);
}

[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task NameWithOnlyType1()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ internal struct NameDeclarationInfo
private static readonly ImmutableArray<SymbolKindOrTypeKind> s_parameterSyntaxKind =
ImmutableArray.Create(new SymbolKindOrTypeKind(SymbolKind.Parameter));

private static readonly ImmutableArray<SymbolKindOrTypeKind> s_propertySyntaxKind =
ImmutableArray.Create(new SymbolKindOrTypeKind(SymbolKind.Property));

public NameDeclarationInfo(
ImmutableArray<SymbolKindOrTypeKind> possibleSymbolKinds,
Accessibility? accessibility,
Expand Down Expand Up @@ -52,6 +55,7 @@ internal static async Task<NameDeclarationInfo> GetDeclarationInfoAsync(Document
var typeInferenceService = document.GetLanguageService<ITypeInferenceService>();

if (IsTupleTypeElement(token, semanticModel, cancellationToken, out var result)
|| IsPrimaryConstructorParameter(token, semanticModel, cancellationToken, out result)
|| IsParameterDeclaration(token, semanticModel, cancellationToken, out result)
|| IsTypeParameterDeclaration(token, out result)
|| IsLocalFunctionDeclaration(token, semanticModel, cancellationToken, out result)
Expand Down Expand Up @@ -377,6 +381,20 @@ private static bool IsTypeParameterDeclaration(SyntaxToken token, out NameDeclar
return false;
}

private static bool IsPrimaryConstructorParameter(SyntaxToken token, SemanticModel semanticModel,
CancellationToken cancellationToken, out NameDeclarationInfo result)
{
result = IsLastTokenOfType<ParameterSyntax>(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, the method name here is very confusing. The name suggests that it returns a bool, but it doesn't.

I would suggest an analyzer to warn for things like this.
Would this be an IDExxxx rule or a CAxxxx rule?

token, semanticModel,
p => p.Type,
_ => default,
_ => s_propertySyntaxKind,
cancellationToken);

return result.Type != null &&
token.GetAncestor<ParameterSyntax>().Parent.IsParentKind(SyntaxKind.RecordDeclaration);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check should come first, and result should be default if it fails.

}

private static bool IsParameterDeclaration(SyntaxToken token, SemanticModel semanticModel,
CancellationToken cancellationToken, out NameDeclarationInfo result)
{
Expand Down