public async override Task <FormattingResult> ExecuteAsync(FormattingContext context, FormattingResult result, CancellationToken cancellationToken) { if (result.Kind != RazorLanguageKind.Razor) { // We don't care about changes to projected documents here. return(result); } var originalDiagnostics = context.CodeDocument.GetSyntaxTree().Diagnostics; var text = context.SourceText; var edits = result.Edits; var changes = edits.Select(e => e.AsTextChange(text)); var changedText = text.WithChanges(changes); var changedContext = await context.WithTextAsync(changedText); var changedDiagnostics = changedContext.CodeDocument.GetSyntaxTree().Diagnostics; // We want to ensure diagnostics didn't change, but since we're formatting things, its expected // that some of them might have moved around. // This is not 100% correct, as the formatting technically could still cause a compile error, // but only if it also fixes one at the same time, so its probably an edge case (if indeed it's // at all possible). Also worth noting the order has to be maintained in that case. if (!originalDiagnostics.SequenceEqual(changedDiagnostics, LocationIgnoringDiagnosticComparer.Instance)) { if (DebugAssertsEnabled) { Debug.Fail("A formatting result was rejected because the formatted text produced different diagnostics compared to the original text."); } return(new FormattingResult(Array.Empty <TextEdit>())); } return(result); }
public async override Task <FormattingResult> ExecuteAsync(FormattingContext context, FormattingResult result, CancellationToken cancellationToken) { if (result.Kind != RazorLanguageKind.Razor) { // We don't care about changes to projected documents here. return(result); } var originalDiagnostics = context.CodeDocument.GetSyntaxTree().Diagnostics; var text = context.SourceText; var edits = result.Edits; var changes = edits.Select(e => e.AsTextChange(text)); var changedText = text.WithChanges(changes); var changedContext = await context.WithTextAsync(changedText); var changedDiagnostics = changedContext.CodeDocument.GetSyntaxTree().Diagnostics; if (!originalDiagnostics.SequenceEqual(changedDiagnostics)) { // Looks like we removed some non-whitespace content as part of formatting. Oops. // Discard this formatting result. if (DebugAssertsEnabled) { Debug.Fail("A formatting result was rejected because the formatted text produced different diagnostics compared to the original text."); } return(new FormattingResult(Array.Empty <TextEdit>())); } return(result); }
public async override Task <FormattingResult> ExecuteAsync(FormattingContext context, FormattingResult result, CancellationToken cancellationToken) { if (context.IsFormatOnType) { // We don't want to handle OnTypeFormatting here. return(result); } // Apply previous edits if any. var originalText = context.SourceText; var changedText = originalText; var changedContext = context; if (result.Edits.Length > 0) { var changes = result.Edits.Select(e => e.AsTextChange(originalText)).ToArray(); changedText = changedText.WithChanges(changes); changedContext = await context.WithTextAsync(changedText); cancellationToken.ThrowIfCancellationRequested(); } // Format the razor bits of the file var syntaxTree = changedContext.CodeDocument.GetSyntaxTree(); var edits = FormatRazor(changedContext, syntaxTree); // Compute the final combined set of edits var formattingChanges = edits.Select(e => e.AsTextChange(changedText)); changedText = changedText.WithChanges(formattingChanges); var finalChanges = SourceTextDiffer.GetMinimalTextChanges(originalText, changedText, lineDiffOnly: false); var finalEdits = finalChanges.Select(f => f.AsTextEdit(originalText)).ToArray(); return(new FormattingResult(finalEdits)); }
public async override Task <FormattingResult> ExecuteAsync(FormattingContext context, FormattingResult result, CancellationToken cancellationToken) { if (!context.IsFormatOnType || result.Kind != RazorLanguageKind.CSharp) { // We don't want to handle regular formatting or non-C# on type formatting here. return(result); } // Normalize and re-map the C# edits. var codeDocument = context.CodeDocument; var csharpText = SourceText.From(codeDocument.GetCSharpDocument().GeneratedCode); var normalizedEdits = NormalizeTextEdits(csharpText, result.Edits); var mappedEdits = RemapTextEdits(codeDocument, normalizedEdits, result.Kind); var filteredEdits = FilterCSharpTextEdits(context, mappedEdits); if (filteredEdits.Length == 0) { // There are no CSharp edits for us to apply. No op. return(new FormattingResult(filteredEdits)); } // Find the lines that were affected by these edits. var originalText = codeDocument.GetSourceText(); var changes = filteredEdits.Select(e => e.AsTextChange(originalText)); var changedText = originalText.WithChanges(changes); TrackEncompassingChange(originalText, changedText, out var spanBeforeChange, out var spanAfterChange); var rangeBeforeEdit = spanBeforeChange.AsRange(originalText); var rangeAfterEdit = spanAfterChange.AsRange(changedText); // Create a new formatting context for the changed razor document. var changedContext = await context.WithTextAsync(changedText); cancellationToken.ThrowIfCancellationRequested(); // Now, for each affected line in the edited version of the document, remove x amount of spaces // at the front to account for extra indentation applied by the C# formatter. // This should be based on context. // For instance, lines inside @code/@functions block should be reduced one level // and lines inside @{} should be reduced by two levels. var indentationChanges = AdjustCSharpIndentation(changedContext, (int)rangeAfterEdit.Start.Line, (int)rangeAfterEdit.End.Line); if (indentationChanges.Count > 0) { // Apply the edits that modify indentation. changedText = changedText.WithChanges(indentationChanges); changedContext = await changedContext.WithTextAsync(changedText); } // We make an optimistic attempt at fixing corner cases. changedText = CleanupDocument(changedContext, rangeAfterEdit); // Now that we have made all the necessary changes to the document. Let's diff the original vs final version and return the diff. var finalChanges = SourceTextDiffer.GetMinimalTextChanges(originalText, changedText, lineDiffOnly: false); var finalEdits = finalChanges.Select(f => f.AsTextEdit(originalText)).ToArray(); return(new FormattingResult(finalEdits)); }
public async override Task <FormattingResult> ExecuteAsync(FormattingContext context, FormattingResult result, CancellationToken cancellationToken) { if (context.IsFormatOnType || result.Kind != RazorLanguageKind.Razor) { // We don't want to handle OnTypeFormatting here. return(result); } // Apply previous edits if any. var originalText = context.SourceText; var changedText = originalText; var changedContext = context; if (result.Edits.Length > 0) { var changes = result.Edits.Select(e => e.AsTextChange(originalText)).ToArray(); changedText = changedText.WithChanges(changes); changedContext = await context.WithTextAsync(changedText); } cancellationToken.ThrowIfCancellationRequested(); // Apply original C# edits var csharpEdits = await FormatCSharpAsync(changedContext, cancellationToken); if (csharpEdits.Count > 0) { var csharpChanges = csharpEdits.Select(c => c.AsTextChange(changedText)); changedText = changedText.WithChanges(csharpChanges); changedContext = await changedContext.WithTextAsync(changedText); } cancellationToken.ThrowIfCancellationRequested(); // We make an optimistic attempt at fixing corner cases. var cleanupChanges = CleanupDocument(changedContext); changedText = changedText.WithChanges(cleanupChanges); changedContext = await changedContext.WithTextAsync(changedText); cancellationToken.ThrowIfCancellationRequested(); var indentationChanges = await AdjustIndentationAsync(changedContext, cancellationToken); if (indentationChanges.Count > 0) { // Apply the edits that modify indentation. changedText = changedText.WithChanges(indentationChanges); } var finalChanges = SourceTextDiffer.GetMinimalTextChanges(originalText, changedText, lineDiffOnly: false); var finalEdits = finalChanges.Select(f => f.AsTextEdit(originalText)).ToArray(); return(new FormattingResult(finalEdits)); }
public async override Task <FormattingResult> ExecuteAsync(FormattingContext context, FormattingResult result, CancellationToken cancellationToken) { if (context.IsFormatOnType || result.Kind != RazorLanguageKind.Razor) { // We don't want to handle OnTypeFormatting here. return(result); } // Apply previous edits if any. var originalText = context.SourceText; var changedText = originalText; var changedContext = context; if (result.Edits.Length > 0) { var changes = result.Edits.Select(e => e.AsTextChange(originalText)).ToArray(); changedText = changedText.WithChanges(changes); changedContext = await context.WithTextAsync(changedText); } cancellationToken.ThrowIfCancellationRequested(); // Apply original C# edits var csharpEdits = await FormatCSharpAsync(changedContext, cancellationToken); if (csharpEdits.Count > 0) { var csharpChanges = csharpEdits.Select(c => c.AsTextChange(changedText)); changedText = changedText.WithChanges(csharpChanges); changedContext = await changedContext.WithTextAsync(changedText); } cancellationToken.ThrowIfCancellationRequested(); var indentationChanges = await AdjustIndentationAsync(changedContext, cancellationToken); if (indentationChanges.Count > 0) { // Apply the edits that modify indentation. changedText = changedText.WithChanges(indentationChanges); } // Allow benchmarks to specify a different diff algorithm if (!context.Options.TryGetValue("UseSourceTextDiffer", out var useSourceTextDiffer)) { useSourceTextDiffer = new BooleanNumberString(false); } var finalChanges = useSourceTextDiffer.Bool ? SourceTextDiffer.GetMinimalTextChanges(originalText, changedText, lineDiffOnly: false) : changedText.GetTextChanges(originalText); var finalEdits = finalChanges.Select(f => f.AsTextEdit(originalText)).ToArray(); return(new FormattingResult(finalEdits)); }
public async override Task <FormattingResult> ExecuteAsync(FormattingContext context, FormattingResult result, CancellationToken cancellationToken) { if (context.IsFormatOnType || result.Kind != RazorLanguageKind.Razor) { // We don't want to handle OnTypeFormatting here. return(result); } // Apply previous edits if any. var originalText = context.SourceText; var changedText = originalText; var changedContext = context; if (result.Edits.Length > 0) { var changes = result.Edits.Select(e => e.AsTextChange(originalText)).ToArray(); changedText = changedText.WithChanges(changes); changedContext = await context.WithTextAsync(changedText); } cancellationToken.ThrowIfCancellationRequested(); // Apply original C# edits var csharpEdits = await FormatCSharpAsync(changedContext, cancellationToken); if (csharpEdits.Count > 0) { var csharpChanges = csharpEdits.Select(c => c.AsTextChange(changedText)); changedText = changedText.WithChanges(csharpChanges); changedContext = await changedContext.WithTextAsync(changedText); _logger.LogTestOnly($"After FormatCSharpAsync:\r\n{changedText}"); } cancellationToken.ThrowIfCancellationRequested(); var indentationChanges = await AdjustIndentationAsync(changedContext, cancellationToken); if (indentationChanges.Count > 0) { // Apply the edits that modify indentation. changedText = changedText.WithChanges(indentationChanges); _logger.LogTestOnly($"After AdjustIndentationAsync:\r\n{changedText}"); } _logger.LogTestOnly($"Generated C#:\r\n{context.CSharpSourceText}"); var finalChanges = changedText.GetTextChanges(originalText); var finalEdits = finalChanges.Select(f => f.AsTextEdit(originalText)).ToArray(); return(new FormattingResult(finalEdits)); }
public async override Task <FormattingResult> ExecuteAsync(FormattingContext context, FormattingResult result, CancellationToken cancellationToken) { if (context.IsFormatOnType) { // We don't want to handle OnTypeFormatting here. return(result); } var originalText = context.SourceText; var htmlEdits = await HtmlFormatter.FormatAsync(context, cancellationToken); // Allow benchmarks to specify a different diff algorithm if (!context.Options.TryGetValue("UseSourceTextDiffer", out var useSourceTextDiffer)) { useSourceTextDiffer = new BooleanNumberString(false); } var normalizedEdits = htmlEdits; if (useSourceTextDiffer.Bool) { normalizedEdits = NormalizeTextEdits(originalText, htmlEdits); } var mappedEdits = RemapTextEdits(context.CodeDocument, normalizedEdits, RazorLanguageKind.Html); var changes = mappedEdits.Select(e => e.AsTextChange(originalText)); var changedText = originalText; var changedContext = context; if (changes.Any()) { changedText = originalText.WithChanges(changes); // Create a new formatting context for the changed razor document. changedContext = await context.WithTextAsync(changedText); } var indentationChanges = AdjustRazorIndentation(changedContext); if (indentationChanges.Count > 0) { // Apply the edits that adjust indentation. changedText = changedText.WithChanges(indentationChanges); } var finalChanges = useSourceTextDiffer.Bool ? SourceTextDiffer.GetMinimalTextChanges(originalText, changedText, lineDiffOnly: false) : changedText.GetTextChanges(originalText); var finalEdits = finalChanges.Select(f => f.AsTextEdit(originalText)).ToArray(); return(new FormattingResult(finalEdits)); }
public async override Task <FormattingResult> ExecuteAsync(FormattingContext context, FormattingResult result, CancellationToken cancellationToken) { var originalText = context.SourceText; TextEdit[] htmlEdits; if (context.IsFormatOnType && result.Kind == RazorLanguageKind.Html) { htmlEdits = await HtmlFormatter.FormatOnTypeAsync(context, cancellationToken).ConfigureAwait(false); } else if (!context.IsFormatOnType) { htmlEdits = await HtmlFormatter.FormatAsync(context, cancellationToken).ConfigureAwait(false); } else { // We don't want to handle on type formatting requests for other languages return(result); } var changedText = originalText; var changedContext = context; _logger.LogTestOnly($"Before HTML formatter:\r\n{changedText}"); if (htmlEdits.Length > 0) { var changes = htmlEdits.Select(e => e.AsTextChange(originalText)); changedText = originalText.WithChanges(changes); // Create a new formatting context for the changed razor document. changedContext = await context.WithTextAsync(changedText); _logger.LogTestOnly($"After normalizedEdits:\r\n{changedText}"); } var indentationChanges = AdjustRazorIndentation(changedContext); if (indentationChanges.Count > 0) { // Apply the edits that adjust indentation. changedText = changedText.WithChanges(indentationChanges); _logger.LogTestOnly($"After AdjustRazorIndentation:\r\n{changedText}"); } var finalChanges = changedText.GetTextChanges(originalText); var finalEdits = finalChanges.Select(f => f.AsTextEdit(originalText)).ToArray(); return(new FormattingResult(finalEdits)); }
public async override Task <FormattingResult> ExecuteAsync(FormattingContext context, FormattingResult result, CancellationToken cancellationToken) { if (context.IsFormatOnType) { // We don't want to handle OnTypeFormatting here. return(result); } var originalText = context.SourceText; var htmlEdits = await HtmlFormatter.FormatAsync(context, context.Range, cancellationToken); var normalizedEdits = NormalizeTextEdits(originalText, htmlEdits); var mappedEdits = RemapTextEdits(context.CodeDocument, normalizedEdits, RazorLanguageKind.Html); var changes = mappedEdits.Select(e => e.AsTextChange(originalText)); var changedText = originalText; var changedContext = context; if (changes.Any()) { changedText = originalText.WithChanges(changes); // Create a new formatting context for the changed razor document. changedContext = await context.WithTextAsync(changedText); } var indentationChanges = AdjustRazorIndentation(changedContext); if (indentationChanges.Count > 0) { // Apply the edits that adjust indentation. changedText = changedText.WithChanges(indentationChanges); } var finalChanges = SourceTextDiffer.GetMinimalTextChanges(originalText, changedText, lineDiffOnly: false); var finalEdits = finalChanges.Select(f => f.AsTextEdit(originalText)).ToArray(); return(new FormattingResult(finalEdits)); }
public async override Task <FormattingResult> ExecuteAsync(FormattingContext context, FormattingResult result, CancellationToken cancellationToken) { if (!context.IsFormatOnType || result.Kind != RazorLanguageKind.CSharp) { // We don't want to handle regular formatting or non-C# on type formatting here. return(result); } // Normalize and re-map the C# edits. var codeDocument = context.CodeDocument; var csharpText = SourceText.From(codeDocument.GetCSharpDocument().GeneratedCode); var normalizedEdits = NormalizeTextEdits(csharpText, result.Edits); var mappedEdits = RemapTextEdits(codeDocument, normalizedEdits, result.Kind); var filteredEdits = FilterCSharpTextEdits(context, mappedEdits); if (filteredEdits.Length == 0) { // There are no CSharp edits for us to apply. No op. return(new FormattingResult(filteredEdits)); } // Find the lines that were affected by these edits. var originalText = codeDocument.GetSourceText(); var changes = filteredEdits.Select(e => e.AsTextChange(originalText)); // Apply the format on type edits sent over by the client. var formattedText = originalText.WithChanges(changes); var changedContext = await context.WithTextAsync(formattedText); TrackEncompassingChange(originalText, changes, out _, out var spanAfterFormatting); var rangeAfterFormatting = spanAfterFormatting.AsRange(formattedText); cancellationToken.ThrowIfCancellationRequested(); // We make an optimistic attempt at fixing corner cases. var cleanupChanges = CleanupDocument(changedContext, rangeAfterFormatting); var cleanedText = formattedText.WithChanges(cleanupChanges); changedContext = await changedContext.WithTextAsync(cleanedText); cancellationToken.ThrowIfCancellationRequested(); // At this point we should have applied all edits that adds/removes newlines. // Let's now ensure the indentation of each of those lines is correct. // We only want to adjust the range that was affected. // We need to take into account the lines affected by formatting as well as cleanup. var cleanupLineDelta = LineDelta(formattedText, cleanupChanges); var rangeToAdjust = new Range(rangeAfterFormatting.Start, new Position(rangeAfterFormatting.End.Line + cleanupLineDelta, 0)); Debug.Assert(rangeToAdjust.End.IsValid(cleanedText), "Invalid range. This is unexpected."); var indentationChanges = AdjustIndentation(changedContext, cancellationToken, rangeToAdjust); if (indentationChanges.Count > 0) { // Apply the edits that modify indentation. cleanedText = cleanedText.WithChanges(indentationChanges); } // Now that we have made all the necessary changes to the document. Let's diff the original vs final version and return the diff. var finalChanges = SourceTextDiffer.GetMinimalTextChanges(originalText, cleanedText, lineDiffOnly: false); var finalEdits = finalChanges.Select(f => f.AsTextEdit(originalText)).ToArray(); return(new FormattingResult(finalEdits)); }
public async override Task <FormattingResult> ExecuteAsync(FormattingContext context, FormattingResult result, CancellationToken cancellationToken) { if (!context.IsFormatOnType || result.Kind != RazorLanguageKind.CSharp) { // We don't want to handle regular formatting or non-C# on type formatting here. return(result); } // Normalize and re-map the C# edits. var codeDocument = context.CodeDocument; var csharpText = codeDocument.GetCSharpSourceText(); var normalizedEdits = NormalizeTextEdits(csharpText, result.Edits); var mappedEdits = RemapTextEdits(codeDocument, normalizedEdits, result.Kind); var filteredEdits = FilterCSharpTextEdits(context, mappedEdits); if (filteredEdits.Length == 0) { // There are no CSharp edits for us to apply. No op. return(new FormattingResult(filteredEdits)); } // Find the lines that were affected by these edits. var originalText = codeDocument.GetSourceText(); var changes = filteredEdits.Select(e => e.AsTextChange(originalText)); // Apply the format on type edits sent over by the client. var formattedText = ApplyChangesAndTrackChange(originalText, changes, out _, out var spanAfterFormatting); var changedContext = await context.WithTextAsync(formattedText); var rangeAfterFormatting = spanAfterFormatting.AsRange(formattedText); cancellationToken.ThrowIfCancellationRequested(); // We make an optimistic attempt at fixing corner cases. var cleanupChanges = CleanupDocument(changedContext, rangeAfterFormatting); var cleanedText = formattedText.WithChanges(cleanupChanges); changedContext = await changedContext.WithTextAsync(cleanedText); cancellationToken.ThrowIfCancellationRequested(); // At this point we should have applied all edits that adds/removes newlines. // Let's now ensure the indentation of each of those lines is correct. // We only want to adjust the range that was affected. // We need to take into account the lines affected by formatting as well as cleanup. var lineDelta = LineDelta(formattedText, cleanupChanges, out var firstPosition, out var lastPosition); // Okay hear me out, I know this looks lazy, but it totally makes sense. // This method is called with edits that the C# formatter wants to make, and from those edits we work out which // other edits to apply etc. Fine, all good so far. BUT its totally possible that the user typed a closing brace // in the same position as the C# formatter thought it should be, on the line _after_ the code that the C# formatter // reformatted. // // For example, given: // if (true){ // } // // If the C# formatter is happy with the placement of that close brace then this method will get two edits: // * On line 1 to indent the if by 4 spaces // * On line 1 to add a newline and 4 spaces in front of the opening brace // // We'll happy format lines 1 and 2, and ignore the closing brace altogether. So, by looking one line further // we won't have that problem. if (rangeAfterFormatting.End.Line + lineDelta < cleanedText.Lines.Count) { lineDelta++; } // Now we know how many lines were affected by the cleanup and formatting, but we don't know where those lines are. For example, given: // // @if (true) // { // } // else // { // $$} // // When typing that close brace, the changes would fix the previous close brace, but the line delta would be 0, so // we'd format line 6 and call it a day, even though the formatter made an edit on line 3. To fix this we use the // first and last position of edits made above, and make sure our range encompasses them as well. For convenience // we calculate these positions in the LineDelta method called above. // This is essentially: rangeToAdjust = new Range(Math.Min(firstFormattingEdit, userEdit), Math.Max(lastFormattingEdit, userEdit)) var start = rangeAfterFormatting.Start; if (firstPosition is not null && firstPosition < start) { start = firstPosition; } var end = new Position(rangeAfterFormatting.End.Line + lineDelta, 0); if (lastPosition is not null && lastPosition < start) { end = lastPosition; } var rangeToAdjust = new Range(start, end); Debug.Assert(rangeToAdjust.End.IsValid(cleanedText), "Invalid range. This is unexpected."); var indentationChanges = await AdjustIndentationAsync(changedContext, cancellationToken, rangeToAdjust); if (indentationChanges.Count > 0) { // Apply the edits that modify indentation. cleanedText = cleanedText.WithChanges(indentationChanges); } // Now that we have made all the necessary changes to the document. Let's diff the original vs final version and return the diff. var finalChanges = cleanedText.GetTextChanges(originalText); var finalEdits = finalChanges.Select(f => f.AsTextEdit(originalText)).ToArray(); return(new FormattingResult(finalEdits)); }
public async override Task <FormattingResult> ExecuteAsync(FormattingContext context, FormattingResult result, CancellationToken cancellationToken) { if (!context.IsFormatOnType || result.Kind != RazorLanguageKind.CSharp) { // We don't want to handle regular formatting or non-C# on type formatting here. return(result); } // Normalize and re-map the C# edits. var codeDocument = context.CodeDocument; var csharpText = codeDocument.GetCSharpSourceText(); var textEdits = result.Edits; if (textEdits.Length == 0) { if (!DocumentMappingService.TryMapToProjectedDocumentPosition(codeDocument, context.HostDocumentIndex, out _, out var projectedIndex)) { _logger.LogWarning($"Failed to map to projected position for document {context.Uri}."); return(result); } // Ask C# for formatting changes. var indentationOptions = new RazorIndentationOptions( UseTabs: !context.Options.InsertSpaces, TabSize: context.Options.TabSize, IndentationSize: context.Options.TabSize); var autoFormattingOptions = new RazorAutoFormattingOptions( formatOnReturn: true, formatOnTyping: true, formatOnSemicolon: true, formatOnCloseBrace: true); var formattingChanges = await RazorCSharpFormattingInteractionService.GetFormattingChangesAsync( context.CSharpWorkspaceDocument, typedChar : context.TriggerCharacter, projectedIndex, indentationOptions, autoFormattingOptions, indentStyle : CodeAnalysis.Formatting.FormattingOptions.IndentStyle.Smart, cancellationToken).ConfigureAwait(false); if (formattingChanges.IsEmpty) { _logger.LogInformation("Received no results."); return(result); } textEdits = formattingChanges.Select(change => change.AsTextEdit(csharpText)).ToArray(); _logger.LogInformation($"Received {textEdits.Length} results from C#."); } var normalizedEdits = NormalizeTextEdits(csharpText, textEdits, out var originalTextWithChanges); var mappedEdits = RemapTextEdits(codeDocument, normalizedEdits, result.Kind); var filteredEdits = FilterCSharpTextEdits(context, mappedEdits); if (filteredEdits.Length == 0) { // There are no CSharp edits for us to apply. No op. return(new FormattingResult(filteredEdits)); } // Find the lines that were affected by these edits. var originalText = codeDocument.GetSourceText(); var changes = filteredEdits.Select(e => e.AsTextChange(originalText)); // Apply the format on type edits sent over by the client. var formattedText = ApplyChangesAndTrackChange(originalText, changes, out _, out var spanAfterFormatting); var changedContext = await context.WithTextAsync(formattedText); var rangeAfterFormatting = spanAfterFormatting.AsRange(formattedText); cancellationToken.ThrowIfCancellationRequested(); // We make an optimistic attempt at fixing corner cases. var cleanupChanges = CleanupDocument(changedContext, rangeAfterFormatting); var cleanedText = formattedText.WithChanges(cleanupChanges); changedContext = await changedContext.WithTextAsync(cleanedText); cancellationToken.ThrowIfCancellationRequested(); // At this point we should have applied all edits that adds/removes newlines. // Let's now ensure the indentation of each of those lines is correct. // We only want to adjust the range that was affected. // We need to take into account the lines affected by formatting as well as cleanup. var lineDelta = LineDelta(formattedText, cleanupChanges, out var firstPosition, out var lastPosition); // Okay hear me out, I know this looks lazy, but it totally makes sense. // This method is called with edits that the C# formatter wants to make, and from those edits we work out which // other edits to apply etc. Fine, all good so far. BUT its totally possible that the user typed a closing brace // in the same position as the C# formatter thought it should be, on the line _after_ the code that the C# formatter // reformatted. // // For example, given: // if (true){ // } // // If the C# formatter is happy with the placement of that close brace then this method will get two edits: // * On line 1 to indent the if by 4 spaces // * On line 1 to add a newline and 4 spaces in front of the opening brace // // We'll happy format lines 1 and 2, and ignore the closing brace altogether. So, by looking one line further // we won't have that problem. if (rangeAfterFormatting.End.Line + lineDelta < cleanedText.Lines.Count) { lineDelta++; } // Now we know how many lines were affected by the cleanup and formatting, but we don't know where those lines are. For example, given: // // @if (true) // { // } // else // { // $$} // // When typing that close brace, the changes would fix the previous close brace, but the line delta would be 0, so // we'd format line 6 and call it a day, even though the formatter made an edit on line 3. To fix this we use the // first and last position of edits made above, and make sure our range encompasses them as well. For convenience // we calculate these positions in the LineDelta method called above. // This is essentially: rangeToAdjust = new Range(Math.Min(firstFormattingEdit, userEdit), Math.Max(lastFormattingEdit, userEdit)) var start = rangeAfterFormatting.Start; if (firstPosition is not null && firstPosition < start) { start = firstPosition; } var end = new Position(rangeAfterFormatting.End.Line + lineDelta, 0); if (lastPosition is not null && lastPosition < start) { end = lastPosition; } var rangeToAdjust = new Range(start, end); Debug.Assert(rangeToAdjust.End.IsValid(cleanedText), "Invalid range. This is unexpected."); var indentationChanges = await AdjustIndentationAsync(changedContext, cancellationToken, rangeToAdjust); if (indentationChanges.Count > 0) { // Apply the edits that modify indentation. cleanedText = cleanedText.WithChanges(indentationChanges); } // Now that we have made all the necessary changes to the document. Let's diff the original vs final version and return the diff. var finalChanges = cleanedText.GetTextChanges(originalText); var finalEdits = finalChanges.Select(f => f.AsTextEdit(originalText)).ToArray(); if (context.AutomaticallyAddUsings) { // Because we need to parse the C# code twice for this operation, lets do a quick check to see if its even necessary if (textEdits.Any(e => e.NewText.IndexOf("using") != -1)) { finalEdits = await AddUsingStatementEditsAsync(codeDocument, finalEdits, csharpText, originalTextWithChanges, cancellationToken); } } return(new FormattingResult(finalEdits)); }