-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tmat
commented
Jun 15, 2022
|
||
internal static class FormattingRuleUtilities | ||
{ | ||
public static ImmutableArray<AbstractFormattingRule> GetFormattingRules(DocumentSyntax document, HostLanguageServices languageServices, TextSpan span, IEnumerable<AbstractFormattingRule>? additionalRules) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved from DocumentExtensions
tmat
commented
Jun 15, 2022
@@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to FormattingRuleUtilities
dibarbet
approved these changes
Jun 15, 2022
src/EditorFeatures/CSharp/Formatting/CSharpFormattingInteractionService.cs
Show resolved
Hide resolved
src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Utilities/SyntacticDocument.cs
Show resolved
Hide resolved
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Formatting services have async methods that are asynchronous only because they need to retrieve
SourceText
andSyntaxTree
. 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 retrievingSourceText
andSyntaxTree
asynchronously in these services, retrieve the text and tree upfront and make the formatting services synchronous. The text and the syntax root are packaged inDocumentSyntax
struct to simplify passing them both along.Unlike
SyntacticDocument
theParsedDocument
struct does not have a reference toDocument
. This makes the formatting code completely independent of solution snapshot and prevents from potential accidental async reads of data fromDocument
. 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:
SyntacticDocument
, doc difference fromParsedDocument
if we keep it