提交 6ed3b337 编写于 作者: A AlekseyTs 提交者: GitHub

Merge pull request #14320 from AlekseyTs/Issue5002

SymbolDisplayVisitor.CreateAliasMap – pop out of speculative semantic model to lookup for declared aliases.
......@@ -166,7 +166,22 @@ private void MinimallyQualify(INamedTypeSymbol symbol)
return SpecializedCollections.EmptyDictionary<INamespaceOrTypeSymbol, IAliasSymbol>();
}
var token = semanticModelOpt.SyntaxTree.GetRoot().FindToken(positionOpt);
// Walk up the ancestors from the current position. If this is a speculative
// model, walk up the corresponding ancestors in the parent model.
SemanticModel semanticModel;
int position;
if (semanticModelOpt.IsSpeculativeSemanticModel)
{
semanticModel = semanticModelOpt.ParentModel;
position = semanticModelOpt.OriginalPositionForSpeculation;
}
else
{
semanticModel = semanticModelOpt;
position = positionOpt;
}
var token = semanticModel.SyntaxTree.GetRoot().FindToken(position);
var startNode = token.Parent;
// NOTE(cyrusn): If we're currently in a block of usings, then we want to collect the
......@@ -182,7 +197,7 @@ private void MinimallyQualify(INamedTypeSymbol symbol)
.SelectMany(n => n.Usings)
.Concat(GetAncestorsOrThis<CompilationUnitSyntax>(startNode).SelectMany(c => c.Usings))
.Where(u => u.Alias != null)
.Select(u => semanticModelOpt.GetDeclaredSymbol(u) as IAliasSymbol)
.Select(u => semanticModel.GetDeclaredSymbol(u) as IAliasSymbol)
.Where(u => u != null);
var builder = ImmutableDictionary.CreateBuilder<INamespaceOrTypeSymbol, IAliasSymbol>();
......
......@@ -7,6 +7,7 @@
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
......@@ -5182,5 +5183,44 @@ public void DisplayFakeTupleTypeSymbol()
SymbolDisplayPartKind.Keyword, // string
SymbolDisplayPartKind.Punctuation);
}
[WorkItem(5002, "https://github.com/dotnet/roslyn/issues/5002")]
[Fact]
public void AliasInSpeculativeSemanticModel()
{
var text =
@"using A = N.M;
namespace N.M
{
class B
{
}
}
class C
{
static void M()
{
}
}";
var comp = CreateCompilationWithMscorlib(text);
var tree = comp.SyntaxTrees.First();
var model = comp.GetSemanticModel(tree);
var methodDecl = tree.GetCompilationUnitRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().First();
int position = methodDecl.Body.SpanStart;
tree = CSharpSyntaxTree.ParseText(@"
class C
{
static void M()
{
}
}");
methodDecl = tree.GetCompilationUnitRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().First();
Assert.True(model.TryGetSpeculativeSemanticModelForMethodBody(position, methodDecl, out model));
var symbol = comp.GetMember<NamedTypeSymbol>("N.M.B");
position = methodDecl.Body.SpanStart;
var description = symbol.ToMinimalDisplayParts(model, position, SymbolDisplayFormat.MinimallyQualifiedFormat);
Verify(description, "A.B", SymbolDisplayPartKind.AliasName, SymbolDisplayPartKind.Punctuation, SymbolDisplayPartKind.ClassName);
}
}
}
......@@ -4603,6 +4603,42 @@ class Outer
Assert.Equal(Nothing, SymbolDisplay.FormatPrimitive(New Object(), quoteStrings:=False, useHexadecimalNumbers:=False))
End Sub
<Fact>
Public Sub AliasInSpeculativeSemanticModel()
Dim text =
<compilation>
<file name="a.vb">
Imports A = N.M
Namespace N.M
Class B
End Class
End Namespace
Class C
Shared Sub M()
Dim o = 1
End Sub
End Class
</file>
</compilation>
Dim comp = CompilationUtils.CreateCompilationWithMscorlib(text)
Dim tree = comp.SyntaxTrees.First()
Dim model = comp.GetSemanticModel(tree)
Dim methodDecl = tree.GetCompilationUnitRoot().DescendantNodes().OfType(Of MethodBlockBaseSyntax)().First()
Dim position = methodDecl.Statements(0).SpanStart
tree = VisualBasicSyntaxTree.ParseText("
Class C
Shared Sub M()
Dim o = 1
End Sub
End Class")
methodDecl = tree.GetCompilationUnitRoot().DescendantNodes().OfType(Of MethodBlockBaseSyntax)().First()
Assert.True(model.TryGetSpeculativeSemanticModelForMethodBody(position, methodDecl, model))
Dim symbol = comp.GetMember(Of NamedTypeSymbol)("N.M.B")
position = methodDecl.Statements(0).SpanStart
Dim description = symbol.ToMinimalDisplayParts(model, position, SymbolDisplayFormat.MinimallyQualifiedFormat)
Verify(description, "A.B", SymbolDisplayPartKind.AliasName, SymbolDisplayPartKind.Operator, SymbolDisplayPartKind.ClassName)
End Sub
#Region "Helpers"
Private Sub TestSymbolDescription(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册