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

Avoid accessing Document from formatter services #61928

Merged
merged 4 commits into from
Jun 16, 2022

Conversation

tmat
Copy link
Member

@tmat tmat commented Jun 15, 2022

Formatting services have async methods that are asynchronous only because they need to retrieve SourceText and SyntaxTree. Some callers of the services are truly async (e.g. code fixes), but others (e.g. typing command handler) are synchronous and running on UI thread. This creates potential for undesirable scheduling of parser execution and blocking of the UI thread in the latter case.

Instead of passing Document to various formatting services and then retrieving SourceText and SyntaxTree asynchronously in these services, retrieve the text and tree upfront and make the formatting services synchronous. The text and the syntax root are packaged in DocumentSyntax struct to simplify passing them both along.

Unlike SyntacticDocument the ParsedDocument struct does not have a reference to Document. This makes the formatting code completely independent of solution snapshot and prevents from potential accidental async reads of data from Document. If in future we end up removing Solution snapshot from in-proc this approach will also allow us to keep the synchronous formatter calls in-proc as long as we have the text and trees in-proc.

Contributes to #57554

Follow ups:

  • Apply this pattern for brace completion and other blocking command handlers
  • Avoid reading options asynchronously when they are available from the editor buffer
  • Consider replacing SyntacticDocument, doc difference from ParsedDocument if we keep it


internal static class FormattingRuleUtilities
{
public static ImmutableArray<AbstractFormattingRule> GetFormattingRules(DocumentSyntax document, HostLanguageServices languageServices, TextSpan span, IEnumerable<AbstractFormattingRule>? additionalRules)
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved from DocumentExtensions

@@ -147,21 +147,5 @@ public static async Task<NamingRule> GetApplicableNamingRuleAsync(this Document

throw ExceptionUtilities.Unreachable;
}

public static ImmutableArray<AbstractFormattingRule> GetFormattingRules(this Document document, TextSpan span, IEnumerable<AbstractFormattingRule>? additionalRules)
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved to FormattingRuleUtilities

@tmat tmat marked this pull request as ready for review June 15, 2022 21:38
@tmat tmat requested review from a team as code owners June 15, 2022 21:38
@tmat tmat merged commit bd2bf93 into dotnet:main Jun 16, 2022
@tmat tmat deleted the SynchronousFormatting branch June 16, 2022 00:54
@ghost ghost added this to the Next milestone Jun 16, 2022
@RikkiGibson RikkiGibson modified the milestones: Next, 17.3 P3 Jun 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants