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

Recommend init keyword #43865

Merged
merged 3 commits into from
May 3, 2020
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
@@ -0,0 +1,300 @@
// 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.Threading.Tasks;
using Microsoft.CodeAnalysis.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Recommendations
{
[Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
public class InitKeywordRecommenderTests : KeywordRecommenderTests
{
[Fact]
public async Task TestNotAtRoot_Interactive()
{
await VerifyAbsenceAsync(SourceCodeKind.Script,
@"$$");
}

[Fact]
public async Task TestNotAfterClass_Interactive()
{
await VerifyAbsenceAsync(SourceCodeKind.Script,
@"class C { }
$$");
}

[Fact]
public async Task TestNotAfterGlobalStatement_Interactive()
{
await VerifyAbsenceAsync(SourceCodeKind.Script,
@"System.Console.WriteLine();
$$");
}

[Fact]
public async Task TestNotAfterGlobalVariableDeclaration_Interactive()
{
await VerifyAbsenceAsync(SourceCodeKind.Script,
@"int i = 0;
$$");
}

[Fact]
public async Task TestNotInUsingAlias()
{
await VerifyAbsenceAsync(
@"using Goo = $$");
}

[Fact]
public async Task TestNotInEmptyStatement()
{
await VerifyAbsenceAsync(AddInsideMethod(
@"$$"));
}

[Fact]
public async Task TestAfterProperty()
{
await VerifyKeywordAsync(
@"class C {
int Goo { $$");
}

[Fact]
public async Task TestAfterPropertyPrivate()
{
await VerifyKeywordAsync(
@"class C {
int Goo { private $$");
}

[Fact]
public async Task TestAfterPropertyAttribute()
{
await VerifyKeywordAsync(
@"class C {
int Goo { [Bar] $$");
}

[Fact]
public async Task TestAfterPropertyAttributeAndPrivate()
{
await VerifyKeywordAsync(
@"class C {
int Goo { [Bar] private $$");
}

[Fact]
public async Task TestAfterPropertyGet()
{
await VerifyKeywordAsync(
@"class C {
int Goo { get; $$");
}

[Fact]
public async Task TestAfterPropertyGetAndPrivate()
{
await VerifyKeywordAsync(
@"class C {
int Goo { get; private $$");
}

[Fact]
public async Task TestAfterPropertyGetAndAttribute()
{
await VerifyKeywordAsync(
@"class C {
int Goo { get; [Bar] $$");
}

[Fact]
public async Task TestAfterPropertyGetAndAttributeAndPrivate()
{
await VerifyKeywordAsync(
@"class C {
int Goo { get; [Bar] private $$");
}

[Fact]
public async Task TestAfterGetAccessorBlock()
{
await VerifyKeywordAsync(
@"class C {
int Goo { get { } $$");
}


[Fact]
public async Task TestAfterSetAccessorBlock()
{
await VerifyKeywordAsync(
@"class C {
int Goo { set { } $$");
}

[Fact]
public async Task TestAfterGetAccessorBlockAndPrivate()
{
await VerifyKeywordAsync(
@"class C {
int Goo { get { } private $$");
}

[Fact]
public async Task TestAfterGetAccessorBlockAndAttribute()
{
await VerifyKeywordAsync(
@"class C {
int Goo { get { } [Bar] $$");
}

[Fact]
public async Task TestAfterGetAccessorBlockAndAttributeAndPrivate()
{
await VerifyKeywordAsync(
@"class C {
int Goo { get { } [Bar] private $$");
}

[Fact]
public async Task TestNotAfterPropertySetKeyword()
{
await VerifyAbsenceAsync(
@"class C {
int Goo { set $$");
}

[Fact]
public async Task TestAfterPropertySetAccessor()
{
await VerifyKeywordAsync(
@"class C {
int Goo { set; $$");
}

[Fact]
public async Task TestNotInEvent()
{
await VerifyAbsenceAsync(
@"class C {
event Goo E { $$");
}

[Fact]
public async Task TestAfterIndexer()
{
await VerifyKeywordAsync(
@"class C {
int this[int i] { $$");
}

[Fact]
public async Task TestAfterIndexerPrivate()
{
await VerifyKeywordAsync(
@"class C {
int this[int i] { private $$");
}

[Fact]
public async Task TestAfterIndexerAttribute()
{
await VerifyKeywordAsync(
@"class C {
int this[int i] { [Bar] $$");
}

[Fact]
public async Task TestAfterIndexerAttributeAndPrivate()
{
await VerifyKeywordAsync(
@"class C {
int this[int i] { [Bar] private $$");
}

[Fact]
public async Task TestAfterIndexerGet()
{
await VerifyKeywordAsync(
@"class C {
int this[int i] { get; $$");
}

[Fact]
public async Task TestAfterIndexerGetAndPrivate()
{
await VerifyKeywordAsync(
@"class C {
int this[int i] { get; private $$");
}

[Fact]
public async Task TestAfterIndexerGetAndAttribute()
{
await VerifyKeywordAsync(
@"class C {
int this[int i] { get; [Bar] $$");
}

[Fact]
public async Task TestAfterIndexerGetAndAttributeAndPrivate()
{
await VerifyKeywordAsync(
@"class C {
int this[int i] { get; [Bar] private $$");
}

[Fact]
public async Task TestAfterIndexerGetBlock()
{
await VerifyKeywordAsync(
@"class C {
int this[int i] { get { } $$");
}

[Fact]
public async Task TestAfterIndexerGetBlockAndPrivate()
{
await VerifyKeywordAsync(
@"class C {
int this[int i] { get { } private $$");
}

[Fact]
public async Task TestAfterIndexerGetBlockAndAttribute()
{
await VerifyKeywordAsync(
@"class C {
int this[int i] { get { } [Bar] $$");
}

[Fact]
public async Task TestAfterIndexerGetBlockAndAttributeAndPrivate()
{
await VerifyKeywordAsync(
@"class C {
int this[int i] { get { } [Bar] private $$");
}

[Fact]
public async Task TestNotAfterIndexerSetKeyword()
{
await VerifyAbsenceAsync(
@"class C {
int this[int i] { set $$");
}

[Fact]
public async Task TestAfterIndexerSetAccessor()
{
await VerifyKeywordAsync(
@"class C {
int this[int i] { set; $$");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,44 @@ class Usages
End Function

<WpfTheory, CombinatorialData, Trait(Traits.Feature, Traits.Features.FindReferences)>
Public Async Function TestCSharpAccessor_FromProp_Feature1(host As TestHost) As Task
Public Async Function TestCSharpAccessor_Init_Feature1(host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
interface IC
{
int Prop { get; {|Definition:$$init|}; }
}

class C : IC
{
public virtual int Prop { get; {|Definition:init|}; }
}

class D : C
{
public override int Prop { get => base.Prop; {|Definition:init|} => base.[|Prop|] = value; }

D()
{
this.[|Prop|] = 1;
this.[|Prop|]++;
}

void M()
{
_ = new D() { [|Prop|] = 1 };
}
}
</Document>
</Project>
</Workspace>
Await TestStreamingFeature(input, host)
End Function

<WpfTheory, CombinatorialData, Trait(Traits.Feature, Traits.Features.FindReferences)>
Public Async Function TestCSharpAccessor_Init_FromProp_Feature1(host As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ private static ImmutableArray<IKeywordRecommender<CSharpSyntaxContext>> GetKeywo
new HiddenKeywordRecommender(),
new IfKeywordRecommender(),
new ImplicitKeywordRecommender(),
new InitKeywordRecommender(),
new InKeywordRecommender(),
new InterfaceKeywordRecommender(),
new InternalKeywordRecommender(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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.Threading;
using Microsoft.CodeAnalysis.CSharp.Extensions.ContextQuery;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Microsoft.CodeAnalysis.CSharp.Completion.KeywordRecommenders
{
internal class InitKeywordRecommender : AbstractSyntacticSingleKeywordRecommender
{
public InitKeywordRecommender()
: base(SyntaxKind.InitKeyword)
{
}

protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
{
return
context.TargetToken.IsAccessorDeclarationContext<PropertyDeclarationSyntax>(position, SyntaxKind.InitKeyword) ||
context.TargetToken.IsAccessorDeclarationContext<IndexerDeclarationSyntax>(position, SyntaxKind.InitKeyword);
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi May 1, 2020

Choose a reason for hiding this comment

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

don't we need:

class C
{
    init 
    {
    }
}

as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, we brainstormed that, but that wasn't decided yet.

}
}
}