forked from OmniSharp/omnisharp-roslyn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(OmniSharp#2209) implemented /v2/gotodefinition for Cake
- Loading branch information
Showing
6 changed files
with
471 additions
and
86 deletions.
There are no files selected for viewing
This file contains 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
113 changes: 113 additions & 0 deletions
113
src/OmniSharp.Cake/Services/RequestHandlers/Navigation/GotoDefinitionHandlerHelper.cs
This file contains 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,113 @@ | ||
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; | ||
|
||
namespace OmniSharp.Cake.Services.RequestHandlers.Navigation | ||
{ | ||
public static class GotoDefinitionHandlerHelper | ||
{ | ||
private const int MethodLineOffset = 3; | ||
private const int PropertyLineOffset = 7; | ||
|
||
internal static async Task<IEnumerable<Alias>> GetAliasFromMetadataAsync( | ||
OmniSharpWorkspace workspace, | ||
string fileName, | ||
int line, | ||
int timeout, | ||
MetadataExternalSourceService metadataExternalSourceService) | ||
{ | ||
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 result = new List<Alias>(); | ||
foreach (var location in symbol.Locations) | ||
{ | ||
if (!location.IsInMetadata) | ||
{ | ||
continue; | ||
} | ||
|
||
var cancellationSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(timeout)); | ||
var (metadataDocument, _) = await metadataExternalSourceService.GetAndAddExternalSymbolDocument(document.Project, symbol, cancellationSource.Token); | ||
if (metadataDocument == null) | ||
{ | ||
continue; | ||
} | ||
|
||
cancellationSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(timeout)); | ||
var metadataLocation = await metadataExternalSourceService.GetExternalSymbolLocation(symbol, metadataDocument, cancellationSource.Token); | ||
var lineSpan = metadataLocation.GetMappedLineSpan(); | ||
|
||
result.Add(new Alias | ||
{ | ||
Document = document, | ||
MetadataDocument = metadataDocument, | ||
Symbol = symbol, | ||
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; } | ||
} | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
src/OmniSharp.Cake/Services/RequestHandlers/Navigation/GotoDefinitionV2Handler.cs
This file contains 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,115 @@ | ||
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.V2.GotoDefinition; | ||
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; | ||
|
||
namespace OmniSharp.Cake.Services.RequestHandlers.Navigation | ||
{ | ||
[OmniSharpHandler(OmniSharpEndpoints.V2.GotoDefinition, Constants.LanguageNames.Cake), Shared] | ||
public class GotoDefinitionV2Handler : CakeRequestHandler<GotoDefinitionRequest, GotoDefinitionResponse> | ||
{ | ||
private readonly MetadataExternalSourceService _metadataExternalSourceService; | ||
|
||
[ImportingConstructor] | ||
public GotoDefinitionV2Handler( | ||
OmniSharpWorkspace workspace, | ||
MetadataExternalSourceService metadataExternalSourceService) | ||
: base(workspace) | ||
{ | ||
_metadataExternalSourceService = metadataExternalSourceService ?? throw new ArgumentNullException(nameof(metadataExternalSourceService)); | ||
} | ||
|
||
protected override async Task<GotoDefinitionResponse> TranslateResponse(GotoDefinitionResponse response, GotoDefinitionRequest request) | ||
{ | ||
var definitions = new List<Definition>(); | ||
foreach (var definition in response.Definitions ?? Enumerable.Empty<Definition>()) | ||
{ | ||
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 Definition | ||
{ | ||
MetadataSource = definition.MetadataSource, | ||
SourceGeneratedFileInfo = definition.SourceGeneratedFileInfo, | ||
Location = new Location | ||
{ | ||
FileName = file, | ||
Range = definition.Location.Range | ||
} | ||
}); | ||
|
||
continue; | ||
} | ||
|
||
if (!request.WantMetadata) | ||
{ | ||
continue; | ||
} | ||
|
||
var aliasLocations = await GotoDefinitionHandlerHelper.GetAliasFromMetadataAsync( | ||
Workspace, | ||
request.FileName, | ||
definition.Location.Range.End.Line, | ||
request.Timeout, | ||
_metadataExternalSourceService | ||
); | ||
|
||
definitions.AddRange( | ||
aliasLocations.Select(loc => | ||
new Definition | ||
{ | ||
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 GotoDefinitionResponse | ||
{ | ||
Definitions = definitions | ||
}; | ||
} | ||
} | ||
} |
This file contains 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
Oops, something went wrong.