|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using Microsoft.CodeAnalysis.FindSymbols; |
| 8 | +using Microsoft.CodeAnalysis.Text; |
| 9 | +using OmniSharp.Roslyn; |
| 10 | +using OmniSharp.Roslyn.CSharp.Services; |
| 11 | + |
| 12 | +namespace OmniSharp.Cake.Services.RequestHandlers.Navigation |
| 13 | +{ |
| 14 | + public static class GotoTypeDefinitionHandlerHelper |
| 15 | + { |
| 16 | + private const int MethodLineOffset = 3; |
| 17 | + private const int PropertyLineOffset = 7; |
| 18 | + |
| 19 | + internal static async Task<IEnumerable<Alias>> GetAliasFromExternalSourceAsync( |
| 20 | + OmniSharpWorkspace workspace, |
| 21 | + string fileName, |
| 22 | + int line, |
| 23 | + int timeout, |
| 24 | + IExternalSourceService externalSourceService) |
| 25 | + { |
| 26 | + var document = workspace.GetDocument(fileName); |
| 27 | + var lineIndex = line + MethodLineOffset; |
| 28 | + int column; |
| 29 | + |
| 30 | + if (document == null) |
| 31 | + { |
| 32 | + return Enumerable.Empty<Alias>(); |
| 33 | + } |
| 34 | + |
| 35 | + var semanticModel = await document.GetSemanticModelAsync(); |
| 36 | + var sourceText = await document.GetTextAsync(); |
| 37 | + var sourceLine = sourceText.Lines[lineIndex].ToString(); |
| 38 | + if (sourceLine.Contains("(Context")) |
| 39 | + { |
| 40 | + column = sourceLine.IndexOf("(Context", StringComparison.Ordinal); |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + lineIndex = line + PropertyLineOffset; |
| 45 | + sourceLine = sourceText.Lines[lineIndex].ToString(); |
| 46 | + if (sourceLine.Contains("(Context")) |
| 47 | + { |
| 48 | + column = sourceLine.IndexOf("(Context", StringComparison.Ordinal); |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + return Enumerable.Empty<Alias>(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if (column > 0 && sourceLine[column - 1] == '>') |
| 57 | + { |
| 58 | + column = sourceLine.LastIndexOf("<", column, StringComparison.Ordinal); |
| 59 | + } |
| 60 | + |
| 61 | + var position = sourceText.Lines.GetPosition(new LinePosition(lineIndex, column)); |
| 62 | + var symbol = await SymbolFinder.FindSymbolAtPositionAsync(semanticModel, position, workspace); |
| 63 | + |
| 64 | + if (symbol == null || symbol is INamespaceSymbol) |
| 65 | + { |
| 66 | + return Enumerable.Empty<Alias>(); |
| 67 | + } |
| 68 | + if (symbol is IMethodSymbol method) |
| 69 | + { |
| 70 | + symbol = method.PartialImplementationPart ?? symbol; |
| 71 | + } |
| 72 | + |
| 73 | + var typeSymbol = symbol switch |
| 74 | + { |
| 75 | + ILocalSymbol localSymbol => localSymbol.Type, |
| 76 | + IFieldSymbol fieldSymbol => fieldSymbol.Type, |
| 77 | + IPropertySymbol propertySymbol => propertySymbol.Type, |
| 78 | + IParameterSymbol parameterSymbol => parameterSymbol.Type, |
| 79 | + _ => null |
| 80 | + }; |
| 81 | + |
| 82 | + if (typeSymbol == null) |
| 83 | + return Enumerable.Empty<Alias>(); |
| 84 | + |
| 85 | + var result = new List<Alias>(); |
| 86 | + foreach (var location in typeSymbol.Locations) |
| 87 | + { |
| 88 | + if (!location.IsInMetadata) |
| 89 | + { |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + var cancellationSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(timeout)); |
| 94 | + var (metadataDocument, _) = await externalSourceService.GetAndAddExternalSymbolDocument(document.Project, typeSymbol, cancellationSource.Token); |
| 95 | + if (metadataDocument == null) |
| 96 | + { |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + cancellationSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(timeout)); |
| 101 | + var metadataLocation = await externalSourceService.GetExternalSymbolLocation(typeSymbol, metadataDocument, cancellationSource.Token); |
| 102 | + var lineSpan = metadataLocation.GetMappedLineSpan(); |
| 103 | + |
| 104 | + result.Add(new Alias |
| 105 | + { |
| 106 | + Document = document, |
| 107 | + MetadataDocument = metadataDocument, |
| 108 | + Symbol = typeSymbol, |
| 109 | + Location = location, |
| 110 | + LineSpan = lineSpan |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + return result; |
| 115 | + } |
| 116 | + |
| 117 | + internal class Alias |
| 118 | + { |
| 119 | + public Document Document { get; set; } |
| 120 | + public ISymbol Symbol { get; set; } |
| 121 | + public Location Location { get; set; } |
| 122 | + public FileLinePositionSpan LineSpan { get; set; } |
| 123 | + public Document MetadataDocument { get; set; } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments