未验证 提交 c094c74f 编写于 作者: J Jason Malinowski 提交者: GitHub

Merge pull request #34861 from jasonmalinowski/cleanups-to-callers-of-get-semantic-info

Cleanups to callers of GetSemanticInfo
...@@ -1251,6 +1251,12 @@ public async Task TestNullLiteral() ...@@ -1251,6 +1251,12 @@ public async Task TestNullLiteral()
MainDescription("class System.String")); MainDescription("class System.String"));
} }
[Fact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
public async Task TestNullLiteralWithVar()
{
await TestInMethodAsync(@"var f = null$$");
}
[WorkItem(26027, "https://github.com/dotnet/roslyn/issues/26027")] [WorkItem(26027, "https://github.com/dotnet/roslyn/issues/26027")]
[Fact, Trait(Traits.Feature, Traits.Features.QuickInfo)] [Fact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
public async Task TestDefaultLiteral() public async Task TestDefaultLiteral()
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
Imports System.Threading Imports System.Threading
Imports System.Threading.Tasks Imports System.Threading.Tasks
Imports Microsoft.CodeAnalysis Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.FindSymbols
Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
<[UseExportProvider]> <[UseExportProvider]>
...@@ -15,12 +16,8 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces ...@@ -15,12 +16,8 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Dim cursorPosition = cursorDocument.CursorPosition.Value Dim cursorPosition = cursorDocument.CursorPosition.Value
Dim document = workspace.CurrentSolution.GetDocument(cursorDocument.Id) Dim document = workspace.CurrentSolution.GetDocument(cursorDocument.Id)
Dim tree = Await document.GetSyntaxTreeAsync()
Dim commonSyntaxToken = Await tree.GetTouchingTokenAsync(cursorPosition, Nothing)
Dim semanticModel = Await document.GetSemanticModelAsync() Dim semanticModel = Await document.GetSemanticModelAsync()
Dim symbol = semanticModel.GetSemanticInfo(commonSyntaxToken, document.Project.Solution.Workspace, Nothing). Dim symbol = Await SymbolFinder.FindSymbolAtPositionAsync(document, cursorPosition)
GetAnySymbol(includeType:=False)
Dim namedTypeSymbol = semanticModel.GetEnclosingNamedType(cursorPosition, CancellationToken.None) Dim namedTypeSymbol = semanticModel.GetEnclosingNamedType(cursorPosition, CancellationToken.None)
Dim actualVisible = symbol.IsAccessibleWithin(namedTypeSymbol) Dim actualVisible = symbol.IsAccessibleWithin(namedTypeSymbol)
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
Imports System.Threading Imports System.Threading
Imports System.Threading.Tasks Imports System.Threading.Tasks
Imports Microsoft.CodeAnalysis Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.FindSymbols
Imports Microsoft.CodeAnalysis.Host Imports Microsoft.CodeAnalysis.Host
Imports Microsoft.CodeAnalysis.LanguageServices Imports Microsoft.CodeAnalysis.LanguageServices
...@@ -19,21 +20,8 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces ...@@ -19,21 +20,8 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Dim cursorBuffer = cursorDocument.TextBuffer Dim cursorBuffer = cursorDocument.TextBuffer
Dim document = workspace.CurrentSolution.GetDocument(cursorDocument.Id) Dim document = workspace.CurrentSolution.GetDocument(cursorDocument.Id)
' using GetTouchingWord instead of FindToken allows us to test scenarios where cursor is at the end of token (E.g: Goo$$)
Dim tree = Await document.GetSyntaxTreeAsync()
Dim commonSyntaxToken = Await tree.GetTouchingWordAsync(cursorPosition, languageServiceProvider.GetService(Of ISyntaxFactsService), Nothing)
' For String Literals GetTouchingWord returns Nothing, we still need this for Quick Info. Quick Info code does exactly the following.
' caveat: The comment above the previous line of code. Do not put the cursor at the end of the token.
If commonSyntaxToken = Nothing Then
commonSyntaxToken = (Await document.GetSyntaxRootAsync()).FindToken(cursorPosition)
End If
Dim semanticModel = Await document.GetSemanticModelAsync() Dim semanticModel = Await document.GetSemanticModelAsync()
Dim symbol = semanticModel.GetSemanticInfo(commonSyntaxToken, document.Project.Solution.Workspace, CancellationToken.None). Dim symbol = Await SymbolFinder.FindSymbolAtPositionAsync(document, cursorPosition)
GetSymbols(includeType:=True).
AsImmutable()
Dim symbolDescriptionService = languageServiceProvider.GetService(Of ISymbolDisplayService)() Dim symbolDescriptionService = languageServiceProvider.GetService(Of ISymbolDisplayService)()
...@@ -108,44 +96,6 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces ...@@ -108,44 +96,6 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Await TestCSharpAsync(workspace, $"({FeaturesResources.local_constant}) int x = 2") Await TestCSharpAsync(workspace, $"({FeaturesResources.local_constant}) int x = 2")
End Function End Function
<Fact>
Public Async Function TestCSharpNullLiteralVar() As Task
Dim workspace =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class Goo
{
void Method()
{
var x = nu$$ll
}
}
</Document>
</Project>
</Workspace>
Await TestCSharpAsync(workspace, "")
End Function
<Fact>
Public Async Function TestCSharpNullLiteralString() As Task
Dim workspace =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class Goo
{
void Method()
{
string x = nu$$ll
}
}
</Document>
</Project>
</Workspace>
Await TestCSharpAsync(workspace, "class System.String")
End Function
<Fact> <Fact>
Public Async Function TestCSharpStaticField() As Task Public Async Function TestCSharpStaticField() As Task
Dim workspace = Dim workspace =
...@@ -649,125 +599,6 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces ...@@ -649,125 +599,6 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Await TestBasicAsync(workspace, $"({FeaturesResources.local_variable}) x As String") Await TestBasicAsync(workspace, $"({FeaturesResources.local_variable}) x As String")
End Function End Function
<Fact>
Public Async Function TestStringLiteral() As Task
Dim workspace =
<Workspace>
<Project Language="Visual Basic" CommonReferences="true">
<Document>
Class Goo
Sub Method()
Dim x As String = "Hel$$lo"
End Sub
End Class
</Document>
</Project>
</Workspace>
Await TestBasicAsync(workspace, "Class System.String")
End Function
<Fact>
Public Async Function TestIntegerLiteral() As Task
Dim workspace =
<Workspace>
<Project Language="Visual Basic" CommonReferences="true">
<Document>
Class Goo
Sub Method()
Dim x = 4$$2
End Sub
End Class
</Document>
</Project>
</Workspace>
Await TestBasicAsync(workspace, "Structure System.Int32")
End Function
<Fact>
Public Async Function TestDateLiteral() As Task
Dim workspace =
<Workspace>
<Project Language="Visual Basic" CommonReferences="true">
<Document>
Class Goo
Sub Method()
Dim d As Date
d = #8/23/1970 $$3:45:39 AM#
End Sub
End Class
</Document>
</Project>
</Workspace>
Await TestBasicAsync(workspace, "Structure System.DateTime")
End Function
<Fact>
Public Async Function TestNothingLiteralDim() As Task
Dim workspace =
<Workspace>
<Project Language="Visual Basic" CommonReferences="true">
<Document>
Class Goo
Sub Method()
Dim x = Nothin$$g
End Sub
End Class
</Document>
</Project>
</Workspace>
Await TestBasicAsync(workspace, "Class System.Object")
End Function
<Fact>
Public Async Function TestNothingLiteralDimAsString() As Task
Dim workspace =
<Workspace>
<Project Language="Visual Basic" CommonReferences="true">
<Document>
Class Goo
Sub Method()
Dim x As String = Nothin$$g
End Sub
End Class
</Document>
</Project>
</Workspace>
Await TestBasicAsync(workspace, "Class System.String")
End Function
<Fact>
Public Async Function TestNothingLiteralFieldDimOptionStrict() As Task
Dim workspace =
<Workspace>
<Project Language="Visual Basic" CommonReferences="true">
<Document>
Option Strict On
Class Goo
Dim x = Nothin$$g
End Class
</Document>
</Project>
</Workspace>
Await TestBasicAsync(workspace, "Class System.Object")
End Function
<Fact>
Public Async Function TestTrueKeyword() As Task
Dim workspace =
<Workspace>
<Project Language="Visual Basic" CommonReferences="true">
<Document>
Class Goo
Sub Method()
Dim x = Tr$$ue
End Sub
End Class
</Document>
</Project>
</Workspace>
Await TestBasicAsync(workspace, "Structure System.Boolean")
End Function
<WorkItem(538732, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/538732")> <WorkItem(538732, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/538732")>
<Fact> <Fact>
Public Async Function TestMethod() As Task Public Async Function TestMethod() As Task
......
...@@ -1751,6 +1751,12 @@ End Class]]></text>.NormalizedValue(), ...@@ -1751,6 +1751,12 @@ End Class]]></text>.NormalizedValue(),
MainDescription("Structure System.Int32")) MainDescription("Structure System.Int32"))
End Function End Function
<Fact, Trait(Traits.Feature, Traits.Features.QuickInfo)>
Public Async Function TestDateLiteral() As Task
Await TestInMethodAsync("Dim f = #8/23/1970 $$3:45:39 AM#",
MainDescription("Structure System.DateTime"))
End Function
<Fact, Trait(Traits.Feature, Traits.Features.QuickInfo)> <Fact, Trait(Traits.Feature, Traits.Features.QuickInfo)>
Public Async Function TestTrueKeyword() As Task Public Async Function TestTrueKeyword() As Task
Await TestInMethodAsync("Dim f = True$$", Await TestInMethodAsync("Dim f = True$$",
...@@ -1770,6 +1776,22 @@ End Class]]></text>.NormalizedValue(), ...@@ -1770,6 +1776,22 @@ End Class]]></text>.NormalizedValue(),
MainDescription("Class System.String")) MainDescription("Class System.String"))
End Function End Function
<Fact, Trait(Traits.Feature, Traits.Features.QuickInfo)>
Public Async Function TestNothingLiteralWithNoType() As Task
Await TestInMethodAsync("Dim f = Nothing$$",
MainDescription("Class System.Object"))
End Function
<Fact, Trait(Traits.Feature, Traits.Features.QuickInfo)>
Public Async Function TestNothingLiteralFieldDimOptionStrict() As Task
Await TestAsync("
Option Strict On
Class C
Dim f = Nothing$$
End Class",
MainDescription("Class System.Object"))
End Function
''' <Remarks> ''' <Remarks>
''' As a part of fix for 756226, quick info for VB Await keyword now displays the type inferred from the AwaitExpression. This is C# behavior. ''' As a part of fix for 756226, quick info for VB Await keyword now displays the type inferred from the AwaitExpression. This is C# behavior.
''' In Dev12, quick info for VB Await keyword was the syntactic help "Await &lt;expression&gt;". ''' In Dev12, quick info for VB Await keyword was the syntactic help "Await &lt;expression&gt;".
......
...@@ -6,62 +6,11 @@ ...@@ -6,62 +6,11 @@
using System.Threading; using System.Threading;
using Microsoft.CodeAnalysis.LanguageServices; using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Operations; using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Utilities; using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities; using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Shared.Extensions namespace Microsoft.CodeAnalysis.Shared.Extensions
{ {
internal struct TokenSemanticInfo
{
public static readonly TokenSemanticInfo Empty = new TokenSemanticInfo(
null, null, ImmutableArray<ISymbol>.Empty, null, null, default(TextSpan));
public readonly ISymbol DeclaredSymbol;
public readonly IAliasSymbol AliasSymbol;
public readonly ImmutableArray<ISymbol> ReferencedSymbols;
public readonly ITypeSymbol Type;
public readonly ITypeSymbol ConvertedType;
public readonly TextSpan Span;
public TokenSemanticInfo(
ISymbol declaredSymbol,
IAliasSymbol aliasSymbol,
ImmutableArray<ISymbol> referencedSymbols,
ITypeSymbol type,
ITypeSymbol convertedType,
TextSpan span)
{
DeclaredSymbol = declaredSymbol;
AliasSymbol = aliasSymbol;
ReferencedSymbols = referencedSymbols;
Type = type;
ConvertedType = convertedType;
Span = span;
}
public ImmutableArray<ISymbol> GetSymbols(bool includeType)
{
var result = ArrayBuilder<ISymbol>.GetInstance();
result.AddIfNotNull(DeclaredSymbol);
result.AddIfNotNull(AliasSymbol);
result.AddRange(ReferencedSymbols);
if (includeType)
{
result.AddIfNotNull(Type ?? ConvertedType);
}
return result.ToImmutableAndFree();
}
public ISymbol GetAnySymbol(bool includeType)
{
return GetSymbols(includeType).FirstOrDefault();
}
}
internal static class SemanticModelExtensions internal static class SemanticModelExtensions
{ {
public static SemanticMap GetSemanticMap(this SemanticModel semanticModel, SyntaxNode node, CancellationToken cancellationToken) public static SemanticMap GetSemanticMap(this SemanticModel semanticModel, SyntaxNode node, CancellationToken cancellationToken)
...@@ -124,26 +73,6 @@ public static INamespaceSymbol GetEnclosingNamespace(this SemanticModel semantic ...@@ -124,26 +73,6 @@ public static INamespaceSymbol GetEnclosingNamespace(this SemanticModel semantic
return typeInfo.Type ?? symbolInfo.GetAnySymbol().ConvertToType(semanticModel.Compilation); return typeInfo.Type ?? symbolInfo.GetAnySymbol().ConvertToType(semanticModel.Compilation);
} }
public static TokenSemanticInfo GetSemanticInfo(
this SemanticModel semanticModel,
SyntaxToken token,
Workspace workspace,
CancellationToken cancellationToken)
{
var languageServices = workspace.Services.GetLanguageServices(token.Language);
var syntaxFacts = languageServices.GetService<ISyntaxFactsService>();
if (!syntaxFacts.IsBindableToken(token))
{
return TokenSemanticInfo.Empty;
}
var semanticFacts = languageServices.GetService<ISemanticFactsService>();
return GetSemanticInfo(
semanticModel, semanticFacts, syntaxFacts,
token, cancellationToken);
}
private static ISymbol MapSymbol(ISymbol symbol, ITypeSymbol type) private static ISymbol MapSymbol(ISymbol symbol, ITypeSymbol type)
{ {
if (symbol.IsConstructor() && symbol.ContainingType.IsAnonymousType) if (symbol.IsConstructor() && symbol.ContainingType.IsAnonymousType)
...@@ -192,13 +121,21 @@ private static ISymbol MapSymbol(ISymbol symbol, ITypeSymbol type) ...@@ -192,13 +121,21 @@ private static ISymbol MapSymbol(ISymbol symbol, ITypeSymbol type)
return symbol; return symbol;
} }
private static TokenSemanticInfo GetSemanticInfo( public static TokenSemanticInfo GetSemanticInfo(
SemanticModel semanticModel, this SemanticModel semanticModel,
ISemanticFactsService semanticFacts,
ISyntaxFactsService syntaxFacts,
SyntaxToken token, SyntaxToken token,
Workspace workspace,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
var languageServices = workspace.Services.GetLanguageServices(token.Language);
var syntaxFacts = languageServices.GetService<ISyntaxFactsService>();
if (!syntaxFacts.IsBindableToken(token))
{
return TokenSemanticInfo.Empty;
}
var semanticFacts = languageServices.GetService<ISemanticFactsService>();
IAliasSymbol aliasSymbol; IAliasSymbol aliasSymbol;
ITypeSymbol type; ITypeSymbol type;
ITypeSymbol convertedType; ITypeSymbol convertedType;
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Shared.Extensions
{
internal struct TokenSemanticInfo
{
public static readonly TokenSemanticInfo Empty = new TokenSemanticInfo(
null, null, ImmutableArray<ISymbol>.Empty, null, null, default(TextSpan));
public readonly ISymbol DeclaredSymbol;
public readonly IAliasSymbol AliasSymbol;
public readonly ImmutableArray<ISymbol> ReferencedSymbols;
public readonly ITypeSymbol Type;
public readonly ITypeSymbol ConvertedType;
public readonly TextSpan Span;
public TokenSemanticInfo(
ISymbol declaredSymbol,
IAliasSymbol aliasSymbol,
ImmutableArray<ISymbol> referencedSymbols,
ITypeSymbol type,
ITypeSymbol convertedType,
TextSpan span)
{
DeclaredSymbol = declaredSymbol;
AliasSymbol = aliasSymbol;
ReferencedSymbols = referencedSymbols;
Type = type;
ConvertedType = convertedType;
Span = span;
}
public ImmutableArray<ISymbol> GetSymbols(bool includeType)
{
var result = ArrayBuilder<ISymbol>.GetInstance();
result.AddIfNotNull(DeclaredSymbol);
result.AddIfNotNull(AliasSymbol);
result.AddRange(ReferencedSymbols);
if (includeType)
{
result.AddIfNotNull(Type ?? ConvertedType);
}
return result.ToImmutableAndFree();
}
public ISymbol GetAnySymbol(bool includeType)
{
return GetSymbols(includeType).FirstOrDefault();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册