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

Streamline brace matcher code #73722

Merged
merged 1 commit into from
May 26, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,52 @@
namespace Microsoft.CodeAnalysis.CSharp.BraceMatching;

[ExportBraceMatcher(LanguageNames.CSharp), Shared]
internal class BlockCommentBraceMatcher : IBraceMatcher
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class BlockCommentBraceMatcher() : IBraceMatcher
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public BlockCommentBraceMatcher()
{
}

public async Task<BraceMatchingResult?> FindBracesAsync(Document document, int position, BraceMatchingOptions options, CancellationToken cancellationToken)
{
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var token = root.FindToken(position, findInsideTrivia: false);
if (token == default)
return null;

var span = token.Span;
if (span.Contains(position))
return null;

var text = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
CyrusNajmabadi marked this conversation as resolved.
Show resolved Hide resolved
var trivia = root.FindTrivia(position);
Copy link
Member Author

Choose a reason for hiding this comment

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

it seems like findTrivia is just not that very good. Looking at the impl, it's pretty darn complex. FindToken seems to be much simpler and much more optimized. So changing our code here to just get the token, and walk the start/end trivia of it

Copy link
Contributor

Choose a reason for hiding this comment

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

I've never really internalized how structured trivia works.

Nodes can have child nodes/tokens
Tokens have leading/trailing trivia
Trivia may have structure (which is a node)

Can those nodes inside the trivia repeat this cycle?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nodes can have child nodes/tokens
Tokens have leading/trailing trivia
Trivia may have structure (which is a node)

100% correct.

Can those nodes inside the trivia repeat this cycle?

No. trivia only goes one more level deep. So within a top levle trivia (like a #if false directive trivia), you have:

  1. Nodes (the directive trivia, its expressions etc)
  2. tokens 9the #, if and false tokens
  3. trivia (the whitespace between those)

However, those final trivia are never structured themselves, so it bottoms out.

We were explicit int he syntax design that no operations go from top level to strucutred trivia automatically, and you always pass the findInsideTrivia flag to control that. And, because there's no more recursion, there's no confusion abuot which level you're diving down into. It's either the top level, or the single structured-trivia level.

Does that make sense?


if (trivia.Kind() is SyntaxKind.MultiLineCommentTrivia &&
trivia.ToString() is ['/', '*', .., '*', '/'])
{
return new BraceMatchingResult(new TextSpan(trivia.SpanStart, "/*".Length), TextSpan.FromBounds(trivia.Span.End - "*/".Length, trivia.Span.End));
}
else if (trivia.Kind() is SyntaxKind.MultiLineDocumentationCommentTrivia)
{
var startBrace = new TextSpan(trivia.FullSpan.Start, "/**".Length);
var endBrace = TextSpan.FromBounds(trivia.FullSpan.End - "*/".Length, trivia.FullSpan.End);
if (text.ToString(startBrace) == "/**" && text.ToString(endBrace) == "*/")
return new BraceMatchingResult(startBrace, endBrace);
}
if (position < token.SpanStart)
return FindBraces(token.LeadingTrivia);
else if (position >= token.Span.End)
return FindBraces(token.TrailingTrivia);

return null;
Copy link
Contributor

Choose a reason for hiding this comment

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

return null;

this should never hit, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah. this was paranoia.


BraceMatchingResult? FindBraces(SyntaxTriviaList triviaList)
{
foreach (var trivia in triviaList)
{
if (trivia.FullSpan.Contains(position))
{
if (trivia.Kind() is SyntaxKind.MultiLineCommentTrivia &&
trivia.ToString() is ['/', '*', .., '*', '/'])
{
return new BraceMatchingResult(new TextSpan(trivia.SpanStart, "/*".Length), TextSpan.FromBounds(trivia.Span.End - "*/".Length, trivia.Span.End));
}
else if (trivia.Kind() is SyntaxKind.MultiLineDocumentationCommentTrivia)
{
var startBrace = new TextSpan(trivia.FullSpan.Start, "/**".Length);
var endBrace = TextSpan.FromBounds(trivia.FullSpan.End - "*/".Length, trivia.FullSpan.End);
if (text.ToString(startBrace) == "/**" && text.ToString(endBrace) == "*/")
return new BraceMatchingResult(startBrace, endBrace);
}
}
}

return null;
}
}
}
Loading