未验证 提交 c4932a97 编写于 作者: M msftbot[bot] 提交者: GitHub

Merge pull request #48763 from CyrusNajmabadi/inlineHintDeclExpr

Support inline hints for declaration expressions.
......@@ -45,14 +45,14 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.InlineHints
AssertEx.Equal(expectedTags, producedTags)
End Sub
Protected Async Function VerifyTypeHints(test As XElement, Optional optionIsEnabled As Boolean = True) As Task
Protected Async Function VerifyTypeHints(test As XElement, Optional optionIsEnabled As Boolean = True, Optional ephemeral As Boolean = False) As Task
Using workspace = TestWorkspace.Create(test)
WpfTestRunner.RequireWpfFact($"{NameOf(AbstractInlineHintsTests)}.{NameOf(Me.VerifyTypeHints)} creates asynchronous taggers")
workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(workspace.Options.WithChangedOption(
InlineHintsOptions.EnabledForTypes,
workspace.CurrentSolution.Projects().First().Language,
optionIsEnabled)))
Dim language = workspace.CurrentSolution.Projects().First().Language
workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(
workspace.Options.WithChangedOption(InlineHintsOptions.EnabledForTypes, language, optionIsEnabled AndAlso Not ephemeral).
WithChangedOption(InlineHintsOptions.DisplayAllOverride, ephemeral)))
Dim hostDocument = workspace.Documents.Single()
Dim snapshot = hostDocument.GetTextBuffer().CurrentSnapshot
......
......@@ -46,6 +46,26 @@ class A
Await VerifyTypeHints(input)
End Function
<WpfFact, Trait(Traits.Feature, Traits.Features.InlineHints)>
Public Async Function TestOnLocalVariableWithVarType_Ephemeral() As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class A
{
void Main()
{
{|int:var|} i = 0;
}
}
</Document>
</Project>
</Workspace>
Await VerifyTypeHints(input, ephemeral:=True)
End Function
<WpfFact, Trait(Traits.Feature, Traits.Features.InlineHints)>
Public Async Function TestOnDeconstruction() As Task
Dim input =
......@@ -86,6 +106,26 @@ class A
Await VerifyTypeHints(input)
End Function
<WpfFact, Trait(Traits.Feature, Traits.Features.InlineHints)>
Public Async Function TestWithForeachVar_Ephemeral() As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class A
{
void Main(string[] args)
{
foreach ({|string:var|} j in args) {}
}
}
</Document>
</Project>
</Workspace>
Await VerifyTypeHints(input, ephemeral:=True)
End Function
<WpfFact, Trait(Traits.Feature, Traits.Features.InlineHints)>
Public Async Function TestNotWithForeachType() As Task
Dim input =
......@@ -126,6 +166,26 @@ class A
Await VerifyTypeHints(input)
End Function
<WpfFact, Trait(Traits.Feature, Traits.Features.InlineHints)>
Public Async Function TestWithPatternVar_Ephemeral() As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class A
{
void Main(string[] args)
{
if (args is { Length: {|int:var|} goo }) { }
}
}
</Document>
</Project>
</Workspace>
Await VerifyTypeHints(input, ephemeral:=True)
End Function
<WpfFact, Trait(Traits.Feature, Traits.Features.InlineHints)>
Public Async Function TestNotWithPatternType() As Task
Dim input =
......@@ -208,5 +268,49 @@ class A
Await VerifyTypeHints(input)
End Function
<WpfFact, Trait(Traits.Feature, Traits.Features.InlineHints)>
Public Async Function TestWithDeclarationExpression() As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class A
{
void Main(string[] args)
{
if (int.TryParse("", out var {|int:|}x))
{
}
}
}
</Document>
</Project>
</Workspace>
Await VerifyTypeHints(input)
End Function
<WpfFact, Trait(Traits.Feature, Traits.Features.InlineHints)>
Public Async Function TestWithDeclarationExpression_Ephemeral() As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class A
{
void Main(string[] args)
{
if (int.TryParse("", out {|int:var|} x))
{
}
}
}
</Document>
</Project>
</Workspace>
Await VerifyTypeHints(input, ephemeral:=True)
End Function
End Class
End Namespace
......@@ -33,8 +33,7 @@ public CSharpInlineTypeHintsService()
{
if (forImplicitVariableTypes || displayAllOverride)
{
if (node is VariableDeclarationSyntax variableDeclaration &&
variableDeclaration.Type.IsVar &&
if (node is VariableDeclarationSyntax { Type: { IsVar: true } } variableDeclaration &&
variableDeclaration.Variables.Count == 1 &&
!variableDeclaration.Variables[0].Identifier.IsMissing)
{
......@@ -42,7 +41,13 @@ public CSharpInlineTypeHintsService()
if (IsValidType(type))
return (type, GetSpan(displayAllOverride, forImplicitVariableTypes, variableDeclaration.Type, variableDeclaration.Variables[0].Identifier));
}
else if (node is SingleVariableDesignationSyntax { Parent: not DeclarationPatternSyntax } variableDesignation)
if (node is DeclarationExpressionSyntax { Type: { IsVar: true } } declarationExpression)
{
var type = semanticModel.GetTypeInfo(declarationExpression.Type, cancellationToken).Type;
if (IsValidType(type))
return (type, GetSpan(displayAllOverride, forImplicitVariableTypes, declarationExpression.Type, declarationExpression.Designation));
}
else if (node is SingleVariableDesignationSyntax { Parent: not DeclarationPatternSyntax and not DeclarationExpressionSyntax } variableDesignation)
{
var local = semanticModel.GetDeclaredSymbol(variableDesignation, cancellationToken) as ILocalSymbol;
var type = local?.Type;
......@@ -53,8 +58,7 @@ public CSharpInlineTypeHintsService()
: (type, new TextSpan(variableDesignation.Identifier.SpanStart, 0));
}
}
else if (node is ForEachStatementSyntax forEachStatement &&
forEachStatement.Type.IsVar)
else if (node is ForEachStatementSyntax { Type: { IsVar: true } } forEachStatement)
{
var info = semanticModel.GetForEachStatementInfo(forEachStatement);
var type = info.ElementType;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册