-
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
Recommend init
keyword
#43865
Merged
Merged
Recommend init
keyword
#43865
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
300 changes: 300 additions & 0 deletions
300
src/EditorFeatures/CSharpTest2/Recommendations/InitKeywordRecommenderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; $$"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/Features/CSharp/Portable/Completion/KeywordRecommenders/InitKeywordRecommender.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
don't we need:
as well?
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.
No, we brainstormed that, but that wasn't decided yet.