-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2170 from 333fred/source-generator-gotodef
Add support for GoToDefinition on source-generated files
- Loading branch information
Showing
19 changed files
with
448 additions
and
15 deletions.
There are no files selected for viewing
10 changes: 7 additions & 3 deletions
10
src/OmniSharp.Abstractions/Models/v1/GotoDefinition/GotoDefinitionResponse.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 |
---|---|---|
@@ -1,16 +1,20 @@ | ||
#nullable enable | ||
|
||
using Newtonsoft.Json; | ||
using OmniSharp.Models.Metadata; | ||
using OmniSharp.Models.v1.SourceGeneratedFile; | ||
|
||
namespace OmniSharp.Models.GotoDefinition | ||
{ | ||
public class GotoDefinitionResponse : ICanBeEmptyResponse | ||
{ | ||
public string FileName { get; set; } | ||
public string? FileName { get; set; } | ||
[JsonConverter(typeof(ZeroBasedIndexConverter))] | ||
public int Line { get; set; } | ||
[JsonConverter(typeof(ZeroBasedIndexConverter))] | ||
public int Column { get; set; } | ||
public MetadataSource MetadataSource { get; set; } | ||
public bool IsEmpty => string.IsNullOrWhiteSpace(FileName) && MetadataSource == null; | ||
public MetadataSource? MetadataSource { get; set; } | ||
public SourceGeneratedFileInfo? SourceGeneratedInfo { get; set; } | ||
public bool IsEmpty => string.IsNullOrWhiteSpace(FileName) && MetadataSource == null && SourceGeneratedInfo == null; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/OmniSharp.Abstractions/Models/v1/SourceGeneratedFile/SourceGeneratedFileClosedRequest.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,11 @@ | ||
#nullable enable | ||
|
||
using OmniSharp.Mef; | ||
|
||
namespace OmniSharp.Models.v1.SourceGeneratedFile | ||
{ | ||
[OmniSharpEndpoint(OmniSharpEndpoints.SourceGeneratedFileClosed, typeof(SourceGeneratedFileClosedRequest), typeof(SourceGeneratedFileClosedResponse))] | ||
public sealed record SourceGeneratedFileClosedRequest : SourceGeneratedFileInfo, IRequest | ||
{ | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...OmniSharp.Abstractions/Models/v1/SourceGeneratedFile/SourceGeneratedFileClosedResponse.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,10 @@ | ||
#nullable enable | ||
using System.Threading.Tasks; | ||
|
||
namespace OmniSharp.Models.v1.SourceGeneratedFile | ||
{ | ||
public sealed class SourceGeneratedFileClosedResponse | ||
{ | ||
public static readonly Task<SourceGeneratedFileClosedResponse> Instance = Task.FromResult(new SourceGeneratedFileClosedResponse()); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/OmniSharp.Abstractions/Models/v1/SourceGeneratedFile/SourceGeneratedFileInfo.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,12 @@ | ||
#nullable enable | ||
|
||
using System; | ||
|
||
namespace OmniSharp.Models.v1.SourceGeneratedFile | ||
{ | ||
public record SourceGeneratedFileInfo | ||
{ | ||
public Guid ProjectGuid { get; init; } | ||
public Guid DocumentGuid { get; init; } | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/OmniSharp.Abstractions/Models/v1/SourceGeneratedFile/SourceGeneratedFileRequest.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,12 @@ | ||
#nullable enable | ||
|
||
using OmniSharp.Mef; | ||
using System; | ||
|
||
namespace OmniSharp.Models.v1.SourceGeneratedFile | ||
{ | ||
[OmniSharpEndpoint(OmniSharpEndpoints.SourceGeneratedFile, typeof(SourceGeneratedFileRequest), typeof(SourceGeneratedFileResponse))] | ||
public sealed record SourceGeneratedFileRequest : SourceGeneratedFileInfo, IRequest | ||
{ | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/OmniSharp.Abstractions/Models/v1/SourceGeneratedFile/SourceGeneratedFileResponse.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,10 @@ | ||
#nullable enable | ||
|
||
namespace OmniSharp.Models.v1.SourceGeneratedFile | ||
{ | ||
public sealed record SourceGeneratedFileResponse | ||
{ | ||
public string? SourceName { get; init; } | ||
public string? Source { get; init; } | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/OmniSharp.Abstractions/Models/v1/SourceGeneratedFile/UpdateSourceGeneratedFileRequest.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,10 @@ | ||
#nullable enable | ||
using OmniSharp.Mef; | ||
|
||
namespace OmniSharp.Models.v1.SourceGeneratedFile | ||
{ | ||
[OmniSharpEndpoint(OmniSharpEndpoints.UpdateSourceGeneratedFile, typeof(UpdateSourceGeneratedFileRequest), typeof(UpdateSourceGeneratedFileResponse))] | ||
public sealed record UpdateSourceGeneratedFileRequest : SourceGeneratedFileInfo, IRequest | ||
{ | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...OmniSharp.Abstractions/Models/v1/SourceGeneratedFile/UpdateSourceGeneratedFileResponse.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,17 @@ | ||
#nullable enable | ||
|
||
namespace OmniSharp.Models.v1.SourceGeneratedFile | ||
{ | ||
public record UpdateSourceGeneratedFileResponse | ||
{ | ||
public UpdateType UpdateType { get; init; } | ||
public string? Source { get; init; } | ||
} | ||
|
||
public enum UpdateType | ||
{ | ||
Unchanged, | ||
Deleted, | ||
Modified | ||
} | ||
} |
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
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
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
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
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
105 changes: 105 additions & 0 deletions
105
src/OmniSharp.Roslyn.CSharp/Services/Navigation/SourceGeneratedFileService.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,105 @@ | ||
#nullable enable | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.Extensions.Logging; | ||
using OmniSharp.Mef; | ||
using OmniSharp.Models.v1.SourceGeneratedFile; | ||
using System.Collections.Generic; | ||
using System.Composition; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace OmniSharp.Roslyn.CSharp.Services.Navigation | ||
{ | ||
[Shared] | ||
[OmniSharpHandler(OmniSharpEndpoints.SourceGeneratedFile, LanguageNames.CSharp)] | ||
[OmniSharpHandler(OmniSharpEndpoints.UpdateSourceGeneratedFile, LanguageNames.CSharp)] | ||
[OmniSharpHandler(OmniSharpEndpoints.SourceGeneratedFileClosed, LanguageNames.CSharp)] | ||
public class SourceGeneratedFileService : | ||
IRequestHandler<SourceGeneratedFileRequest, SourceGeneratedFileResponse>, | ||
IRequestHandler<UpdateSourceGeneratedFileRequest, UpdateSourceGeneratedFileResponse>, | ||
IRequestHandler<SourceGeneratedFileClosedRequest, SourceGeneratedFileClosedResponse> | ||
{ | ||
private readonly OmniSharpWorkspace _workspace; | ||
private readonly ILogger _logger; | ||
private readonly Dictionary<DocumentId, VersionStamp> _lastSentVerisons = new(); | ||
private readonly object _lock = new(); | ||
|
||
[ImportingConstructor] | ||
public SourceGeneratedFileService(OmniSharpWorkspace workspace, ILoggerFactory loggerFactory) | ||
{ | ||
_workspace = workspace; | ||
_logger = loggerFactory.CreateLogger<SourceGeneratedFileService>(); | ||
} | ||
|
||
public async Task<SourceGeneratedFileResponse> Handle(SourceGeneratedFileRequest request) | ||
{ | ||
var documentId = GetId(request); | ||
|
||
var document = await _workspace.CurrentSolution.GetSourceGeneratedDocumentAsync(documentId, CancellationToken.None); | ||
|
||
if (document is null) | ||
{ | ||
_logger.LogError("Document with ID {0}:{1} was not found or not a source generated file", request.ProjectGuid, request.DocumentGuid); | ||
return new SourceGeneratedFileResponse(); | ||
} | ||
|
||
var text = await document.GetTextAsync(); | ||
|
||
var documentVerison = await document.GetTextVersionAsync(); | ||
lock (_lock) | ||
{ | ||
_lastSentVerisons[documentId] = documentVerison; | ||
} | ||
|
||
return new SourceGeneratedFileResponse | ||
{ | ||
Source = text.ToString(), | ||
SourceName = document.FilePath | ||
}; | ||
} | ||
|
||
public async Task<UpdateSourceGeneratedFileResponse> Handle(UpdateSourceGeneratedFileRequest request) | ||
{ | ||
var documentId = GetId(request); | ||
var document = await _workspace.CurrentSolution.GetSourceGeneratedDocumentAsync(documentId, CancellationToken.None); | ||
if (document == null) | ||
{ | ||
lock (_lock) | ||
{ | ||
_ = _lastSentVerisons.Remove(documentId); | ||
} | ||
return new UpdateSourceGeneratedFileResponse() { UpdateType = UpdateType.Deleted }; | ||
} | ||
|
||
var docVersion = await document.GetTextVersionAsync(); | ||
lock (_lock) | ||
{ | ||
if (_lastSentVerisons.TryGetValue(documentId, out var lastVersion) && lastVersion == docVersion) | ||
{ | ||
return new UpdateSourceGeneratedFileResponse() { UpdateType = UpdateType.Unchanged }; | ||
} | ||
|
||
_lastSentVerisons[documentId] = docVersion; | ||
} | ||
|
||
return new UpdateSourceGeneratedFileResponse() | ||
{ | ||
UpdateType = UpdateType.Modified, | ||
Source = (await document.GetTextAsync()).ToString() | ||
}; | ||
} | ||
|
||
public Task<SourceGeneratedFileClosedResponse> Handle(SourceGeneratedFileClosedRequest request) | ||
{ | ||
lock (_lock) | ||
{ | ||
_ = _lastSentVerisons.Remove(GetId(request)); | ||
} | ||
|
||
return SourceGeneratedFileClosedResponse.Instance; | ||
} | ||
|
||
private DocumentId GetId(SourceGeneratedFileInfo info) => DocumentId.CreateFromSerialized(ProjectId.CreateFromSerialized(info.ProjectGuid), info.DocumentGuid); | ||
} | ||
} |
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.