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

Semantic snippets: don't provide snippets when dotting of a type name (floow up) #74893

Merged
merged 1 commit into from
Aug 24, 2024
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 @@ -566,4 +566,21 @@ void M()
}
""");
}

[Fact]
public async Task NoInlineSnippetForTypeItselfTest_BeforeContextualKeyword()
{
await VerifySnippetIsAbsentAsync("""
using System.Threading.Tasks;

class C
{
async void M()
{
bool.$$
await Task.Delay(10);
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -582,4 +582,21 @@ void M()
}
""");
}

[Fact]
public async Task NoInlineDoSnippetForTypeItselfTest_BeforeContextualKeyword()
{
await VerifySnippetIsAbsentAsync("""
using System.Threading.Tasks;

class C
{
async void M()
{
bool.$$
await Task.Delay(10);
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,24 @@ void M()
""");
}

[Theory]
[MemberData(nameof(CommonSnippetTestData.CommonEnumerableTypes), MemberType = typeof(CommonSnippetTestData))]
public async Task NoInlineForEachSnippetForTypeItselfTest_BeforeContextualKeyword(string collectionType)
{
await VerifySnippetIsAbsentAsync($$"""
using System.Threading.Tasks;

class C
{
async void M()
{
{{collectionType}}.$$
await Task.Delay(10);
}
}
""");
}

[Theory]
[InlineData("ArrayList")]
[InlineData("IEnumerable")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -877,32 +877,68 @@ public class MyType
}

[Theory]
[InlineData("MyType")]
[MemberData(nameof(CommonSnippetTestData.IntegerTypes), MemberType = typeof(CommonSnippetTestData))]
public async Task NoInlineForSnippetForTypeItselfTest(string integerType)
public async Task NoInlineForSnippetForTypeItselfTest(string validTypes)
{
await VerifySnippetIsAbsentAsync($$"""
class C
{
void M()
{
{{integerType}}.$$
{{validTypes}}.$$
}
}

class MyType
{
public int Count => 0;
}
""");
}

[Theory]
[InlineData("MyType")]
[MemberData(nameof(CommonSnippetTestData.IntegerTypes), MemberType = typeof(CommonSnippetTestData))]
public async Task NoInlineForSnippetForTypeItselfTest_Parenthesized(string integerType)
public async Task NoInlineForSnippetForTypeItselfTest_Parenthesized(string validTypes)
{
await VerifySnippetIsAbsentAsync($$"""
class C
{
void M()
{
({{integerType}}).$$
({{validTypes}}).$$
}
}

class MyType
{
public int Count => 0;
}
""");
}

[Theory]
[InlineData("MyType")]
[MemberData(nameof(CommonSnippetTestData.IntegerTypes), MemberType = typeof(CommonSnippetTestData))]
public async Task NoInlineForSnippetForTypeItselfTest_BeforeContextualKeyword(string validTypes)
{
await VerifySnippetIsAbsentAsync($$"""
using System.Threading.Tasks;

class C
{
async void M()
{
{{validTypes}}.$$
await Task.Delay(10);
}
}

class MyType
{
public int Count => 0;
}
""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -879,32 +879,68 @@ public class MyType
}

[Theory]
[InlineData("MyType")]
[MemberData(nameof(CommonSnippetTestData.IntegerTypes), MemberType = typeof(CommonSnippetTestData))]
public async Task NoInlineReversedForSnippetForTypeItselfTest(string integerType)
public async Task NoInlineReversedForSnippetForTypeItselfTest(string validTypes)
{
await VerifySnippetIsAbsentAsync($$"""
class C
{
void M()
{
{{integerType}}.$$
{{validTypes}}.$$
}
}

class MyType
{
public int Count => 0;
}
""");
}

[Theory]
[InlineData("MyType")]
[MemberData(nameof(CommonSnippetTestData.IntegerTypes), MemberType = typeof(CommonSnippetTestData))]
public async Task NoInlineReversedForSnippetForTypeItselfTest_Parenthesized(string integerType)
public async Task NoInlineReversedForSnippetForTypeItselfTest_Parenthesized(string validTypes)
{
await VerifySnippetIsAbsentAsync($$"""
class C
{
void M()
{
({{integerType}}).$$
({{validTypes}}).$$
}
}

class MyType
{
public int Count => 0;
}
""");
}

[Theory]
[InlineData("MyType")]
[MemberData(nameof(CommonSnippetTestData.IntegerTypes), MemberType = typeof(CommonSnippetTestData))]
public async Task NoInlineReversedForSnippetForTypeItselfTest_BeforeContextualKeyword(string validTypes)
{
await VerifySnippetIsAbsentAsync($$"""
using System.Threading.Tasks;

class C
{
async void M()
{
{{validTypes}}.$$
await Task.Delay(10);
}
}

class MyType
{
public int Count => 0;
}
""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ private static bool TryGetInlineExpressionInfo(SyntaxToken targetToken, ISyntaxF
if (syntaxFacts.IsQualifiedName(parentNode))
{
syntaxFacts.GetPartsOfQualifiedName(parentNode, out var expression, out _, out _);
var symbolInfo = semanticModel.GetSymbolInfo(expression, cancellationToken);

// Forbid a case when we are dotting of a type, e.g. `string.$$`.
// Inline statement snippets are not valid in this context
if (symbolInfo.Symbol is ITypeSymbol)
{
expressionInfo = null;
return false;
}

var typeInfo = semanticModel.GetSpeculativeTypeInfo(expression.SpanStart, expression, SpeculativeBindingOption.BindAsExpression);
expressionInfo = new(expression, typeInfo);
return true;
Expand Down
Loading