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

Provide SourceGeneratedFileInfo for workspace symbols requests #2431

Merged
merged 3 commits into from
Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 6 additions & 2 deletions src/OmniSharp.Abstractions/Models/v1/SymbolLocation.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
namespace OmniSharp.Models
using OmniSharp.Models.v1.SourceGeneratedFile;

namespace OmniSharp.Models
{
public class SymbolLocation : QuickFix
{
public string Kind { get; set; }
public string ContainingSymbolName { get; set; }
public SourceGeneratedFileInfo GeneratedFileInfo { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public override async Task<Container<SymbolInformation>> Handle(
{
Name = x.Text,
Kind = Helpers.ToSymbolKind(x.Kind),
ContainerName = x.ContainingSymbolName,
Location = new OmniSharp.Extensions.LanguageServer.Protocol.Models.Location
{
Uri = Helpers.ToUri(x.FileName),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.FindSymbols;
using OmniSharp.Extensions;
using OmniSharp.Models.v1.SourceGeneratedFile;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -44,21 +42,5 @@ internal static class GoToDefinitionHelpers

return null;
}

internal static SourceGeneratedFileInfo? GetSourceGeneratedFileInfo(OmniSharpWorkspace workspace, Location location)
{
Debug.Assert(location.IsInSource);
var document = workspace.CurrentSolution.GetDocument(location.SourceTree);
if (document is not SourceGeneratedDocument)
{
return null;
}

return new SourceGeneratedFileInfo
{
ProjectGuid = document.Project.Id.Id,
DocumentGuid = document.Id.Id
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public async Task<GotoDefinitionResponse> Handle(GotoDefinitionRequest request)
FileName = lineSpan.Path,
Line = lineSpan.StartLinePosition.Line,
Column = lineSpan.StartLinePosition.Character,
SourceGeneratedInfo = GoToDefinitionHelpers.GetSourceGeneratedFileInfo(_workspace, location)
SourceGeneratedInfo = SolutionExtensions.GetSourceGeneratedFileInfo(document.Project.Solution, location)
};
}
else if (location.IsInMetadata && request.WantMetadata)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public async Task<GotoDefinitionResponse> Handle(GotoDefinitionRequest request)
}
else
{
sourceGeneratedFileInfo = GoToDefinitionHelpers.GetSourceGeneratedFileInfo(_workspace, location);
sourceGeneratedFileInfo = SolutionExtensions.GetSourceGeneratedFileInfo(document.Project.Solution, location);
}

return new Definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task<GotoTypeDefinitionResponse> Handle(GotoTypeDefinitionRequest r
.Select(location => new TypeDefinition
{
Location = location.GetMappedLineSpan().GetLocationFromFileLinePositionSpan(),
SourceGeneratedFileInfo = GoToDefinitionHelpers.GetSourceGeneratedFileInfo(_workspace, location)
SourceGeneratedFileInfo = SolutionExtensions.GetSourceGeneratedFileInfo(document.Project.Solution, location)
})
.ToList()
};
Expand Down
26 changes: 24 additions & 2 deletions src/OmniSharp.Roslyn/Extensions/SolutionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.FindSymbols;
using OmniSharp.Models;
using OmniSharp.Models.v1.SourceGeneratedFile;

#nullable enable

namespace OmniSharp.Extensions
{
Expand Down Expand Up @@ -67,7 +71,7 @@ private static QuickFix ConvertSymbol(Solution solution, ISymbol symbol, Locatio
var lineSpan = location.GetLineSpan();
var path = lineSpan.Path;
var projects = solution.GetDocumentIdsWithFilePath(path)
.Select(documentId => solution.GetProject(documentId.ProjectId).Name)
.Select(documentId => solution.GetProject(documentId.ProjectId)!.Name)
.ToArray();

var format = SymbolDisplayFormat.MinimallyQualifiedFormat;
Expand All @@ -86,7 +90,25 @@ private static QuickFix ConvertSymbol(Solution solution, ISymbol symbol, Locatio
Column = lineSpan.StartLinePosition.Character,
EndLine = lineSpan.EndLinePosition.Line,
EndColumn = lineSpan.EndLinePosition.Character,
Projects = projects
Projects = projects,
ContainingSymbolName = symbol.ContainingSymbol?.Name ?? "",
GeneratedFileInfo = GetSourceGeneratedFileInfo(solution, location),
};
}

internal static SourceGeneratedFileInfo? GetSourceGeneratedFileInfo(Solution solution, Location location)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy/paste from GoToDefinitionHelpers, and modified to take a Solution instead of an OmniSharpWorkspace.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this was intended to be the home for extension methods, so maybe it would be good to make this one too for consistency

{
Debug.Assert(location.IsInSource);
var document = solution.GetDocument(location.SourceTree);
if (document is not SourceGeneratedDocument)
{
return null;
}

return new SourceGeneratedFileInfo
{
ProjectGuid = document.Project.Id.Id,
DocumentGuid = document.Id.Id
};
}
}
Expand Down