Skip to content

Commit

Permalink
Include XML docs in method highlights
Browse files Browse the repository at this point in the history
  • Loading branch information
OoLunar committed Oct 31, 2024
1 parent c8b880f commit 0aa6d89
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/Commands/Common/SourceCodeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static SourceCodeCommand()
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(SourceText.From(manifestStream), CSharpParseOptions.Default);

// Get all the classes in the file
foreach (ClassDeclarationSyntax classDeclaration in GetClasses(syntaxTree.GetRoot().DescendantNodes()).Distinct())
foreach (ClassDeclarationSyntax classDeclaration in syntaxTree.GetRoot().DescendantNodes().GetNodes<ClassDeclarationSyntax>().Distinct())
{
// Get the FQN of the class
string className = $"{GetNamespace(classDeclaration)}.{classDeclaration.Identifier}";
Expand Down Expand Up @@ -145,7 +145,23 @@ static SourceCodeCommand()
}

// Find the beginning and ending lines of the method, starting as early as the XML docs and ending at the closing brace
int start = methodDeclaration.GetLocation().GetLineSpan().StartLinePosition.Line + 1;
int start = -1;
foreach (SyntaxTrivia syntaxTrivia in methodDeclaration.GetLeadingTrivia())
{
if (syntaxTrivia.GetStructure() is not DocumentationCommentTriviaSyntax documentationCommentTriviaSyntax)
{
continue;
}

start = documentationCommentTriviaSyntax.Content[0].GetLocation().GetLineSpan().StartLinePosition.Line + 1;
break;
}

if (start == -1)
{
start = methodDeclaration.GetLocation().GetLineSpan().StartLinePosition.Line + 1;
}

int end = methodDeclaration.GetLocation().GetLineSpan().EndLinePosition.Line + 1;

StringBuilder commandNameBuilder = new();
Expand Down Expand Up @@ -179,18 +195,18 @@ static SourceCodeCommand()
_commandLinks = commandLinks.ToFrozenDictionary();
}

private static IEnumerable<ClassDeclarationSyntax> GetClasses(IEnumerable<SyntaxNode> nodes)
private static IEnumerable<T> GetNodes<T>(this IEnumerable<SyntaxNode> nodes) where T : SyntaxNode
{
foreach (SyntaxNode node in nodes)
{
if (node is ClassDeclarationSyntax classDeclaration)
if (node is T desiredNode)
{
yield return classDeclaration;
yield return desiredNode;
}

foreach (ClassDeclarationSyntax classDeclarationSyntax in GetClasses(node.ChildNodes()))
foreach (T matchingNode in GetNodes<T>(node.ChildNodes()))
{
yield return classDeclarationSyntax;
yield return matchingNode;
}
}
}
Expand Down

0 comments on commit 0aa6d89

Please sign in to comment.