-
Notifications
You must be signed in to change notification settings - Fork 418
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
Add support for GoToDefinition on source-generated files #2170
Merged
+448
−15
Merged
Changes from all commits
Commits
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
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(); | ||
333fred marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when would the client call the update point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way I've done it for vscode, when the buffer is shown.