Skip to content
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

Return generated file info for find references #2434

Merged
merged 2 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/OmniSharp.Roslyn.CSharp/Helpers/LocationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using OmniSharp.Extensions;
using OmniSharp.Models;

namespace OmniSharp.Helpers
Expand All @@ -24,20 +24,26 @@ public static QuickFix GetQuickFix(this Location location, OmniSharpWorkspace wo
var sourceText = GetSourceText(location, documents, lineSpan.HasMappedPath);
var text = GetLineText(location, sourceText, lineSpan.StartLinePosition.Line);

var fileName = Path.IsPathRooted(lineSpan.Path)
var generatedInfo = workspace.CurrentSolution.GetSourceGeneratedFileInfo(location);

var fileName = Path.IsPathRooted(lineSpan.Path) || generatedInfo != null
// If there is generated file information, the path is not rooted, but we don't want to try and locate it as it doesn't
// exist on disk
? lineSpan.Path
// when a #line directive maps into a separate file using a relative path, get the full path relative to the folder containing the source tree
: Path.GetFullPath(Path.Combine(Path.GetDirectoryName(location.SourceTree.FilePath), lineSpan.Path));

return new QuickFix

return new SymbolLocation
{
Text = text.Trim(),
FileName = fileName,
Line = lineSpan.StartLinePosition.Line,
Column = lineSpan.HasMappedPath ? 0 : lineSpan.StartLinePosition.Character, // when a #line directive maps into a separate file, assume columns (0,0)
EndLine = lineSpan.EndLinePosition.Line,
EndColumn = lineSpan.HasMappedPath ? 0 : lineSpan.EndLinePosition.Character,
Projects = documents.Select(document => document.Project.Name).ToArray()
Projects = documents.Select(document => document.Project.Name).ToArray(),
GeneratedFileInfo = generatedInfo
};

static SourceText GetSourceText(Location location, IEnumerable<Document> documents, bool hasMappedPath)
Expand Down
2 changes: 1 addition & 1 deletion src/OmniSharp.Roslyn/Extensions/SolutionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private static QuickFix ConvertSymbol(Solution solution, ISymbol symbol, Locatio
};
}

internal static SourceGeneratedFileInfo? GetSourceGeneratedFileInfo(Solution solution, Location location)
internal static SourceGeneratedFileInfo? GetSourceGeneratedFileInfo(this Solution solution, Location location)
{
Debug.Assert(location.IsInSource);
var document = solution.GetDocument(location.SourceTree);
Expand Down
47 changes: 47 additions & 0 deletions tests/OmniSharp.Roslyn.CSharp.Tests/FindReferencesFacts.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using OmniSharp.Models;
using OmniSharp.Models.FindUsages;
using OmniSharp.Models.v1.SourceGeneratedFile;
using OmniSharp.Roslyn.CSharp.Services.Navigation;
using Roslyn.Test.Utilities;
using TestUtility;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -545,6 +549,49 @@ public class Foo
Assert.Null(exception);
}

[Fact]
public async Task ReturnsGeneratedReferences()
{
const string Source = @"
public partial class Generated
{
public int Property { get; set; }
}
";
const string FileName = "real.cs";
var testFile = new TestFile(FileName, @"
class C
{
Generate$$d G;
}
");

TestHelpers.AddProjectToWorkspace(SharedOmniSharpTestHost.Workspace,
"project.csproj",
new[] { "netcoreapp3.1" },
new[] { testFile },
analyzerRefs: ImmutableArray.Create<AnalyzerReference>(new TestGeneratorReference(
context => context.AddSource("GeneratedFile", Source))));

var response = await FindUsagesAsync(testFile, onlyThisFile: false, excludeDefinition: false);

var result = response.QuickFixes.Cast<SymbolLocation>().Single(s => s.GeneratedFileInfo is not null);
Assert.NotNull(result);
AssertEx.Equal(@"OmniSharp.Roslyn.CSharp.Tests\OmniSharp.Roslyn.CSharp.Tests.TestSourceGenerator\GeneratedFile.cs", result.FileName.Replace("/", @"\"));

var sourceGeneratedFileHandler = SharedOmniSharpTestHost.GetRequestHandler<SourceGeneratedFileService>(OmniSharpEndpoints.SourceGeneratedFile);
var sourceGeneratedRequest = new SourceGeneratedFileRequest
{
DocumentGuid = result.GeneratedFileInfo.DocumentGuid,
ProjectGuid = result.GeneratedFileInfo.ProjectGuid
};

var sourceGeneratedFileResponse = await sourceGeneratedFileHandler.Handle(sourceGeneratedRequest);
Assert.NotNull(sourceGeneratedFileResponse);
AssertEx.Equal(Source, sourceGeneratedFileResponse.Source);
AssertEx.Equal(@"OmniSharp.Roslyn.CSharp.Tests\OmniSharp.Roslyn.CSharp.Tests.TestSourceGenerator\GeneratedFile.cs", sourceGeneratedFileResponse.SourceName.Replace("/", @"\"));
}

private Task<QuickFixResponse> FindUsagesAsync(string code, bool excludeDefinition = false)
{
return FindUsagesAsync(new[] { new TestFile("dummy.cs", code) }, false, excludeDefinition);
Expand Down