forked from OmniSharp/omnisharp-roslyn
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for GoToDefinition on source-generated files
This adds a new response element to gotodefinition responses: SourceGeneratedFileInfo. This is similar to MetadataSource, except that unlike MetadataSource it's not tracked on a type/project basis, but rather as a document/project basis. Retrieving info about a source generated file can be done through the SourceGeneratedFileService endpoints: * SourceGeneratedFileInfo - Gets the file content of a source generated file. * UpdateSourceGeneratedFileInfo - Gets the updated content of a source generated file, if it has changed since the last time information was returned. * SourceGeneratedFileClosed - Sent to the server to inform it that the editor has closed the generated file and it can stop tracking Document version info for that file. Currently, the only way to get the info needed to open a source-generated file is via the gotodefinition endpoint. We'll want to add info to find-usages as well, but that's a job for another day. Contributes to OmniSharp#1934.
- Loading branch information
Showing
19 changed files
with
446 additions
and
14 deletions.
There are no files selected for viewing
8 changes: 6 additions & 2 deletions
8
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 MetadataSource? MetadataSource { get; set; } | ||
public SourceGeneratedFileInfo? SourceGeneratedInfo { get; set; } | ||
public bool IsEmpty => string.IsNullOrWhiteSpace(FileName) && MetadataSource == 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.