diff --git a/src/Workspaces/Core/Portable/SymbolKey/SymbolKey.AnonymousFunctionOrDelegateSymbolKey.cs b/src/Workspaces/Core/Portable/SymbolKey/SymbolKey.AnonymousFunctionOrDelegateSymbolKey.cs new file mode 100644 index 0000000000000000000000000000000000000000..3d32460507f82429bac0b2cc22d80e8089251f1d --- /dev/null +++ b/src/Workspaces/Core/Portable/SymbolKey/SymbolKey.AnonymousFunctionOrDelegateSymbolKey.cs @@ -0,0 +1,55 @@ +// 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 Microsoft.CodeAnalysis.Shared.Extensions; + +namespace Microsoft.CodeAnalysis +{ + internal partial struct SymbolKey + { + /// + /// 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. + /// + private static class AnonymousFunctionOrDelegateSymbolKey + { + public static void Create(ISymbol symbol, SymbolKeyWriter visitor) + { + Debug.Assert(symbol.IsAnonymousDelegateType() || symbol.IsAnonymousFunction()); + + visitor.WriteBoolean(symbol.IsAnonymousDelegateType()); + visitor.WriteLocation(symbol.Locations.FirstOrDefault()); + } + + public static SymbolKeyResolution Resolve(SymbolKeyReader reader) + { + var isAnonymousDelegateType = reader.ReadBoolean(); + var location = reader.ReadLocation(); + + var syntaxTree = location.SourceTree; + if (syntaxTree != null) + { + var semanticModel = reader.Compilation.GetSemanticModel(syntaxTree); + var root = syntaxTree.GetRoot(reader.CancellationToken); + var node = root.FindNode(location.SourceSpan, getInnermostNodeForTie: true); + + var symbol = semanticModel.GetSymbolInfo(node, reader.CancellationToken) + .GetAnySymbol(); + + if (isAnonymousDelegateType) + { + var anonymousDelegate = (symbol as IMethodSymbol).AssociatedAnonymousDelegate; + symbol = anonymousDelegate; + } + + return new SymbolKeyResolution(symbol); + } + + return default(SymbolKeyResolution); + } + } + } +} \ No newline at end of file diff --git a/src/Workspaces/Core/Portable/SymbolKey/SymbolKey.AnonymousSymbolKey.cs b/src/Workspaces/Core/Portable/SymbolKey/SymbolKey.AnonymousSymbolKey.cs deleted file mode 100644 index b07c9fc824edc7104aa14ecb90750a5c1985bbe0..0000000000000000000000000000000000000000 --- a/src/Workspaces/Core/Portable/SymbolKey/SymbolKey.AnonymousSymbolKey.cs +++ /dev/null @@ -1,72 +0,0 @@ -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 - { - 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(); - - visitor.WriteInteger((int)type); - visitor.WriteString(filePath); - visitor.WriteInteger(textSpan.Start); - visitor.WriteInteger(textSpan.Length); - } - - public static SymbolKeyResolution Resolve(SymbolKeyReader reader) - { - var type = (LocalSymbolType)reader.ReadInteger(); - var filePath = reader.ReadString(); - var start = reader.ReadInteger(); - var length = reader.ReadInteger(); - - var syntaxTree = reader.GetSyntaxTree(filePath); - 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 symbol = semanticModel.GetSymbolInfo(node, reader.CancellationToken) - .GetAnySymbol(); - - if (type == LocalSymbolType.AnonymousDelegate) - { - var anonymousDelegate = (symbol as IMethodSymbol).AssociatedAnonymousDelegate; - symbol = anonymousDelegate; - } - - return new SymbolKeyResolution(symbol); - } - - return default(SymbolKeyResolution); - } - } - } -} \ No newline at end of file diff --git a/src/Workspaces/Core/Portable/Workspaces.csproj b/src/Workspaces/Core/Portable/Workspaces.csproj index bd2eb102f9e06b8da71de367123d29c6da882af4..6f1c140a2f89422a4391422482d025824b820275 100644 --- a/src/Workspaces/Core/Portable/Workspaces.csproj +++ b/src/Workspaces/Core/Portable/Workspaces.csproj @@ -484,7 +484,7 @@ - +