提交 74a82d86 编写于 作者: C CyrusNajmabadi

Simplify code now that we natively support reading/writing locations in a SymbolKey.

上级 793aa0d6
using System.Diagnostics;
// 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.Diagnostics;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
internal partial struct SymbolKey
{
/// <summary>
/// Anonymous functions and anonymous-delegates (the special VB synthesized delegate types),
/// only come into existence when someone has explicitly written a lambda in their source
/// code. So to appropriately round-trip this symbol we store the location that the lambda
/// was at so that we can find the symbol again when we resolve the key.
/// </summary>
private static class AnonymousFunctionOrDelegateSymbolKey
{
private enum LocalSymbolType
{
AnonymousFunction,
AnonymousDelegate,
}
public static void Create(ISymbol symbol, SymbolKeyWriter visitor)
{
// var reference = symbol.DeclaringSyntaxReferences.FirstOrDefault();
//var filePath = reference?.SyntaxTree.FilePath ?? "";
//var textSpan = reference?.Span ?? new TextSpan();
Debug.Assert(symbol.IsAnonymousDelegateType() ||
symbol.IsAnonymousFunction());
var type = symbol.IsAnonymousDelegateType()
? LocalSymbolType.AnonymousDelegate
: LocalSymbolType.AnonymousFunction;
var location = symbol.Locations.FirstOrDefault();
var filePath = location?.SourceTree.FilePath ?? "";
var textSpan = location?.SourceSpan ?? new TextSpan();
Debug.Assert(symbol.IsAnonymousDelegateType() || symbol.IsAnonymousFunction());
visitor.WriteInteger((int)type);
visitor.WriteString(filePath);
visitor.WriteInteger(textSpan.Start);
visitor.WriteInteger(textSpan.Length);
visitor.WriteBoolean(symbol.IsAnonymousDelegateType());
visitor.WriteLocation(symbol.Locations.FirstOrDefault());
}
public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
{
var type = (LocalSymbolType)reader.ReadInteger();
var filePath = reader.ReadString();
var start = reader.ReadInteger();
var length = reader.ReadInteger();
var isAnonymousDelegateType = reader.ReadBoolean();
var location = reader.ReadLocation();
var syntaxTree = reader.GetSyntaxTree(filePath);
var syntaxTree = location.SourceTree;
if (syntaxTree != null)
{
var semanticModel = reader.Compilation.GetSemanticModel(syntaxTree);
var root = syntaxTree.GetRoot(reader.CancellationToken);
var node = root.FindNode(new TextSpan(start, length), getInnermostNodeForTie: true);
var node = root.FindNode(location.SourceSpan, getInnermostNodeForTie: true);
var symbol = semanticModel.GetSymbolInfo(node, reader.CancellationToken)
.GetAnySymbol();
if (type == LocalSymbolType.AnonymousDelegate)
if (isAnonymousDelegateType)
{
var anonymousDelegate = (symbol as IMethodSymbol).AssociatedAnonymousDelegate;
symbol = anonymousDelegate;
......
......@@ -484,7 +484,7 @@
<Compile Include="SolutionCrawler\PredefinedInvocationReasons.cs" />
<Compile Include="FindSymbols\StreamingProgressCollector.cs" />
<Compile Include="FindSymbols\SymbolFinder.ServerCallback.cs" />
<Compile Include="SymbolKey\SymbolKey.AnonymousSymbolKey.cs" />
<Compile Include="SymbolKey\SymbolKey.AnonymousFunctionOrDelegateSymbolKey.cs" />
<Compile Include="SymbolKey\SymbolKey.AnonymousTypeSymbolKey.cs" />
<Compile Include="SymbolKey\SymbolKey.ComparisonOptions.cs" />
<Compile Include="SymbolKey\SymbolKey.SymbolKeyComparer.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册