-
Notifications
You must be signed in to change notification settings - Fork 427
Create a new GoToTypeDefinition endpoint #2315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
75bd929
Initial changes for goToTypeDefinition
jtschuster 7cf4938
Initial support for GotoTypeDefinition
jtschuster 37c0c3e
Add more tests for gotoTypeDefinition
jtschuster 3ba51bf
Remove Commented gototypedefinition test
jtschuster a60e227
Merge branch 'master' into gototypedef
jtschuster 3464747
Merge branch 'master' into gototypedef
jtschuster 8936863
Use .IsKind() instead of .Kind() == x
jtschuster c79f647
Add necessary include for IsKind()
jtschuster afd79e1
Merge branch 'master' into gototypedef
jtschuster 96c5ab6
Merge branch 'master' into gototypedef
JoeRobich 070884e
PR feedback
jtschuster e8bcc74
Merge branch 'gototypedef' of https://github.com/jtschuster/omnisharp…
jtschuster 7d3b562
Merge branch 'master' into gototypedef
JoeRobich 14129b0
Empty commit to trigger CI retry
jtschuster ffb8dee
Merge branch 'gototypedef' of https://github.com/jtschuster/omnisharp…
jtschuster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/OmniSharp.Abstractions/Models/v2/GotoTypeDefinition/GotoTypeDefinitionRequest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| using OmniSharp.Mef; | ||
|
|
||
| namespace OmniSharp.Models.GotoTypeDefinition | ||
| { | ||
| [OmniSharpEndpoint(OmniSharpEndpoints.GotoTypeDefinition, typeof(GotoTypeDefinitionRequest), typeof(GotoTypeDefinitionResponse))] | ||
| public class GotoTypeDefinitionRequest : Request | ||
| { | ||
| public int Timeout { get; init; } = 10000; | ||
| public bool WantMetadata { get; init; } | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
src/OmniSharp.Abstractions/Models/v2/GotoTypeDefinition/GotoTypeDefinitionResponse.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #nullable enable | ||
|
|
||
| using OmniSharp.Models.Metadata; | ||
| using OmniSharp.Models.v1.SourceGeneratedFile; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace OmniSharp.Models.GotoTypeDefinition | ||
| { | ||
| public record GotoTypeDefinitionResponse | ||
| { | ||
| public List<TypeDefinition>? Definitions { get; init; } | ||
| } | ||
|
|
||
| public record TypeDefinition | ||
| { | ||
| public V2.Location Location { get; init; } = null!; | ||
| public MetadataSource? MetadataSource { get; init; } | ||
| public SourceGeneratedFileInfo? SourceGeneratedFileInfo { get; init; } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/OmniSharp.Cake/Services/RequestHandlers/Navigation/GotoTypeDefinitionHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Composition; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using OmniSharp.Extensions; | ||
| using OmniSharp.Mef; | ||
| using OmniSharp.Models.GotoTypeDefinition; | ||
| using OmniSharp.Models.Metadata; | ||
| using OmniSharp.Models.v1.SourceGeneratedFile; | ||
| using OmniSharp.Models.V2; | ||
| using OmniSharp.Roslyn; | ||
| using OmniSharp.Utilities; | ||
| using Location = OmniSharp.Models.V2.Location; | ||
| using Range = OmniSharp.Models.V2.Range; | ||
| using OmniSharp.Roslyn.CSharp.Services; | ||
| using OmniSharp.Options; | ||
|
|
||
| namespace OmniSharp.Cake.Services.RequestHandlers.Navigation | ||
| { | ||
| [OmniSharpHandler(OmniSharpEndpoints.GotoTypeDefinition, Constants.LanguageNames.Cake), Shared] | ||
| public class GotoTypeDefinitionHandler : CakeRequestHandler<GotoTypeDefinitionRequest, GotoTypeDefinitionResponse> | ||
| { | ||
| private readonly IExternalSourceService _externalSourceService; | ||
|
|
||
| [ImportingConstructor] | ||
| public GotoTypeDefinitionHandler( | ||
| OmniSharpWorkspace workspace, | ||
| ExternalSourceServiceFactory externalSourceServiceFactory, | ||
| OmniSharpOptions omniSharpOptions) | ||
| : base(workspace) | ||
| { | ||
| _externalSourceService = externalSourceServiceFactory?.Create(omniSharpOptions) ?? throw new ArgumentNullException(nameof(externalSourceServiceFactory)); | ||
| } | ||
|
|
||
| protected override async Task<GotoTypeDefinitionResponse> TranslateResponse(GotoTypeDefinitionResponse response, GotoTypeDefinitionRequest request) | ||
| { | ||
| var definitions = new List<TypeDefinition>(); | ||
| foreach (var definition in response.Definitions ?? Enumerable.Empty<TypeDefinition>()) | ||
| { | ||
| var file = definition.Location.FileName; | ||
|
|
||
| if (string.IsNullOrEmpty(file) || !file.Equals(Constants.Paths.Generated)) | ||
| { | ||
| if (PlatformHelper.IsWindows && !string.IsNullOrEmpty(file)) | ||
| { | ||
| file = file.Replace('/', '\\'); | ||
| } | ||
|
|
||
| definitions.Add(new TypeDefinition | ||
| { | ||
| MetadataSource = definition.MetadataSource, | ||
| SourceGeneratedFileInfo = definition.SourceGeneratedFileInfo, | ||
| Location = new Location | ||
| { | ||
| FileName = file, | ||
| Range = definition.Location.Range | ||
| } | ||
| }); | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| if (!request.WantMetadata) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var aliasLocations = await GotoTypeDefinitionHandlerHelper.GetAliasFromExternalSourceAsync( | ||
| Workspace, | ||
| request.FileName, | ||
| definition.Location.Range.End.Line, | ||
| request.Timeout, | ||
| _externalSourceService | ||
| ); | ||
|
|
||
| definitions.AddRange( | ||
| aliasLocations.Select(loc => | ||
| new TypeDefinition | ||
| { | ||
| Location = new Location | ||
| { | ||
| FileName = loc.MetadataDocument.FilePath ?? loc.MetadataDocument.Name, | ||
| Range = new Range | ||
| { | ||
| Start = new Point | ||
| { | ||
| Column = loc.LineSpan.StartLinePosition.Character, | ||
| Line = loc.LineSpan.StartLinePosition.Line | ||
| }, | ||
| End = new Point | ||
| { | ||
| Column = loc.LineSpan.EndLinePosition.Character, | ||
| Line = loc.LineSpan.EndLinePosition.Line | ||
| }, | ||
| } | ||
| }, | ||
| MetadataSource = new MetadataSource | ||
| { | ||
| AssemblyName = loc.Symbol.ContainingAssembly.Name, | ||
| ProjectName = loc.Document.Project.Name, | ||
| TypeName = loc.Symbol.GetSymbolName() | ||
| }, | ||
| SourceGeneratedFileInfo = new SourceGeneratedFileInfo | ||
| { | ||
| DocumentGuid = loc.Document.Id.Id, | ||
| ProjectGuid = loc.Document.Id.ProjectId.Id | ||
| } | ||
| }) | ||
| .ToList()); | ||
| } | ||
|
|
||
| return new GotoTypeDefinitionResponse | ||
| { | ||
| Definitions = definitions | ||
| }; | ||
| } | ||
| } | ||
| } |
126 changes: 126 additions & 0 deletions
126
src/OmniSharp.Cake/Services/RequestHandlers/Navigation/GotoTypeDefinitionHandlerHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.FindSymbols; | ||
| using Microsoft.CodeAnalysis.Text; | ||
| using OmniSharp.Roslyn; | ||
| using OmniSharp.Roslyn.CSharp.Services; | ||
|
|
||
| namespace OmniSharp.Cake.Services.RequestHandlers.Navigation | ||
| { | ||
| public static class GotoTypeDefinitionHandlerHelper | ||
| { | ||
| private const int MethodLineOffset = 3; | ||
| private const int PropertyLineOffset = 7; | ||
|
|
||
| internal static async Task<IEnumerable<Alias>> GetAliasFromExternalSourceAsync( | ||
| OmniSharpWorkspace workspace, | ||
| string fileName, | ||
| int line, | ||
| int timeout, | ||
| IExternalSourceService externalSourceService) | ||
| { | ||
| var document = workspace.GetDocument(fileName); | ||
| var lineIndex = line + MethodLineOffset; | ||
| int column; | ||
|
|
||
| if (document == null) | ||
| { | ||
| return Enumerable.Empty<Alias>(); | ||
| } | ||
|
|
||
| var semanticModel = await document.GetSemanticModelAsync(); | ||
| var sourceText = await document.GetTextAsync(); | ||
| var sourceLine = sourceText.Lines[lineIndex].ToString(); | ||
| if (sourceLine.Contains("(Context")) | ||
| { | ||
| column = sourceLine.IndexOf("(Context", StringComparison.Ordinal); | ||
| } | ||
| else | ||
| { | ||
| lineIndex = line + PropertyLineOffset; | ||
| sourceLine = sourceText.Lines[lineIndex].ToString(); | ||
| if (sourceLine.Contains("(Context")) | ||
| { | ||
| column = sourceLine.IndexOf("(Context", StringComparison.Ordinal); | ||
| } | ||
| else | ||
| { | ||
| return Enumerable.Empty<Alias>(); | ||
| } | ||
| } | ||
|
|
||
| if (column > 0 && sourceLine[column - 1] == '>') | ||
| { | ||
| column = sourceLine.LastIndexOf("<", column, StringComparison.Ordinal); | ||
| } | ||
|
|
||
| var position = sourceText.Lines.GetPosition(new LinePosition(lineIndex, column)); | ||
| var symbol = await SymbolFinder.FindSymbolAtPositionAsync(semanticModel, position, workspace); | ||
|
|
||
| if (symbol == null || symbol is INamespaceSymbol) | ||
| { | ||
| return Enumerable.Empty<Alias>(); | ||
| } | ||
| if (symbol is IMethodSymbol method) | ||
| { | ||
| symbol = method.PartialImplementationPart ?? symbol; | ||
| } | ||
|
|
||
| var typeSymbol = symbol switch | ||
| { | ||
| ILocalSymbol localSymbol => localSymbol.Type, | ||
| IFieldSymbol fieldSymbol => fieldSymbol.Type, | ||
| IPropertySymbol propertySymbol => propertySymbol.Type, | ||
| IParameterSymbol parameterSymbol => parameterSymbol.Type, | ||
| _ => null | ||
| }; | ||
|
|
||
| if (typeSymbol == null) | ||
| return Enumerable.Empty<Alias>(); | ||
|
|
||
| var result = new List<Alias>(); | ||
| foreach (var location in typeSymbol.Locations) | ||
| { | ||
| if (!location.IsInMetadata) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var cancellationSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(timeout)); | ||
| var (metadataDocument, _) = await externalSourceService.GetAndAddExternalSymbolDocument(document.Project, typeSymbol, cancellationSource.Token); | ||
| if (metadataDocument == null) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| cancellationSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(timeout)); | ||
| var metadataLocation = await externalSourceService.GetExternalSymbolLocation(typeSymbol, metadataDocument, cancellationSource.Token); | ||
| var lineSpan = metadataLocation.GetMappedLineSpan(); | ||
|
|
||
| result.Add(new Alias | ||
| { | ||
| Document = document, | ||
| MetadataDocument = metadataDocument, | ||
| Symbol = typeSymbol, | ||
| Location = location, | ||
| LineSpan = lineSpan | ||
| }); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| internal class Alias | ||
| { | ||
| public Document Document { get; set; } | ||
| public ISymbol Symbol { get; set; } | ||
| public Location Location { get; set; } | ||
| public FileLinePositionSpan LineSpan { get; set; } | ||
| public Document MetadataDocument { get; set; } | ||
| } | ||
| } | ||
| } | ||
64 changes: 64 additions & 0 deletions
64
src/OmniSharp.Roslyn.CSharp/Services/Navigation/GotoTypeDefinitionHelpers.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| #nullable enable | ||
|
|
||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.FindSymbols; | ||
| using Microsoft.CodeAnalysis.Operations; | ||
| using OmniSharp.Extensions; | ||
| using OmniSharp.Models.v1.SourceGeneratedFile; | ||
| using System.Diagnostics; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace OmniSharp.Roslyn.CSharp.Services.Navigation | ||
| { | ||
| internal static class GotoTypeDefinitionHelpers | ||
| { | ||
| internal static async Task<ITypeSymbol?> GetTypeOfSymbol(Document document, int line, int column, CancellationToken cancellationToken) | ||
| { | ||
| var sourceText = await document.GetTextAsync(cancellationToken); | ||
| var position = sourceText.GetPositionFromLineAndOffset(line, column); | ||
| var symbol = await SymbolFinder.FindSymbolAtPositionAsync(document, position, cancellationToken); | ||
|
|
||
| return symbol switch | ||
| { | ||
| ILocalSymbol localSymbol => localSymbol.Type, | ||
| IFieldSymbol fieldSymbol => fieldSymbol.Type, | ||
| IPropertySymbol propertySymbol => propertySymbol.Type, | ||
| IParameterSymbol parameterSymbol => parameterSymbol.Type, | ||
| _ => null | ||
| }; | ||
| } | ||
|
|
||
| internal static async Task<FileLinePositionSpan?> GetMetadataMappedSpan( | ||
| Document document, | ||
| ISymbol symbol, | ||
| IExternalSourceService externalSourceService, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| var (metadataDocument, _) = await externalSourceService.GetAndAddExternalSymbolDocument(document.Project, symbol, cancellationToken); | ||
| if (metadataDocument != null) | ||
| { | ||
| var metadataLocation = await externalSourceService.GetExternalSymbolLocation(symbol, metadataDocument, cancellationToken); | ||
| return metadataLocation.GetMappedLineSpan(); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| internal static SourceGeneratedFileInfo? GetSourceGeneratedFileInfo(OmniSharpWorkspace workspace, Location location) | ||
| { | ||
| Debug.Assert(location.IsInSource); | ||
| var document = workspace.CurrentSolution.GetDocument(location.SourceTree); | ||
| if (document is not SourceGeneratedDocument) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return new SourceGeneratedFileInfo | ||
| { | ||
| ProjectGuid = document.Project.Id.Id, | ||
| DocumentGuid = document.Id.Id | ||
| }; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.