-
Notifications
You must be signed in to change notification settings - Fork 420
/
MetadataService.cs
56 lines (50 loc) · 2.31 KB
/
MetadataService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using OmniSharp.Mef;
using OmniSharp.Models.Metadata;
using OmniSharp.Options;
using OmniSharp.Roslyn.CSharp.Services.Decompilation;
namespace OmniSharp.Roslyn.CSharp.Services.Navigation
{
[OmniSharpHandler(OmniSharpEndpoints.Metadata, LanguageNames.CSharp)]
public class MetadataService : IRequestHandler<MetadataRequest, MetadataResponse>
{
private readonly OmniSharpOptions _omniSharpOptions;
private readonly OmniSharpWorkspace _workspace;
private readonly ExternalSourceServiceFactory _externalSourceServiceFactory;
[ImportingConstructor]
public MetadataService(OmniSharpWorkspace workspace, ExternalSourceServiceFactory externalSourceServiceFactory, OmniSharpOptions omniSharpOptions)
{
_workspace = workspace;
_externalSourceServiceFactory = externalSourceServiceFactory;
_omniSharpOptions = omniSharpOptions;
}
public async Task<MetadataResponse> Handle(MetadataRequest request)
{
var externalSourceService = _externalSourceServiceFactory.Create(_omniSharpOptions);
var response = new MetadataResponse();
foreach (var project in _workspace.CurrentSolution.Projects)
{
var compilation = await project.GetCompilationAsync();
var symbol = compilation.GetTypeByMetadataName(request.TypeName);
if (symbol != null && symbol.ContainingAssembly.Name == request.AssemblyName)
{
var cancellationToken = _externalSourceServiceFactory.CreateCancellationToken(_omniSharpOptions, request.Timeout);
var (metadataDocument, documentPath) = await externalSourceService.GetAndAddExternalSymbolDocument(project, symbol, cancellationToken);
if (metadataDocument != null)
{
var source = await metadataDocument.GetTextAsync();
response.Source = source.ToString();
response.SourceName = documentPath;
return response;
}
}
}
return response;
}
}
}