Skip to content

Commit

Permalink
Explicitly pass formatting options to Roslyn APIs (via External Acces…
Browse files Browse the repository at this point in the history
…s) (#6166)

Requires:
- [x] dotnet/roslyn#59679
  • Loading branch information
davidwengier authored Jul 8, 2024
2 parents ca5327e + c5de662 commit b17a0e8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
using Microsoft.CodeAnalysis.Razor;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Text;
Expand Down Expand Up @@ -83,10 +84,7 @@ private static async Task<TextEdit[]> GetFormattingEditsAsync(FormattingContext
var root = await context.CSharpWorkspaceDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
Assumes.NotNull(root);

var workspace = context.CSharpWorkspace;

// Formatting options will already be set in the workspace.
var changes = CodeAnalysis.Formatting.Formatter.GetFormattedTextChanges(root, spanToFormat, workspace, cancellationToken: cancellationToken);
var changes = RazorCSharpFormattingInteractionService.GetFormattedTextChanges(context.CSharpWorkspace.Services, root, spanToFormat, context.Options.GetIndentationOptions(), cancellationToken);

var edits = changes.Select(c => c.ToTextEdit(csharpSourceText)).ToArray();
return edits;
Expand All @@ -108,7 +106,7 @@ private static async Task<Dictionary<int, int>> GetCSharpIndentationCoreAsync(Fo

// At this point, we have added all the necessary markers and attached annotations.
// Let's invoke the C# formatter and hope for the best.
var formattedRoot = CodeAnalysis.Formatting.Formatter.Format(root, context.CSharpWorkspace, cancellationToken: cancellationToken);
var formattedRoot = RazorCSharpFormattingInteractionService.Format(context.CSharpWorkspace.Services, root, context.Options.GetIndentationOptions(), cancellationToken);
var formattedText = formattedRoot.GetText();

var desiredIndentationMap = new Dictionary<int, int>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,14 @@ public async override Task<FormattingResult> ExecuteAsync(FormattingContext cont
}

// 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,
context.Options.GetIndentationOptions(),
autoFormattingOptions,
indentStyle: CodeAnalysis.Formatting.FormattingOptions.IndentStyle.Smart,
cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,7 @@ public Document CSharpWorkspaceDocument
}
}

public AdhocWorkspace CSharpWorkspace
{
get
{
if (_csharpWorkspace is null)
{
var adhocWorkspace = _workspaceFactory.Create();
var csharpOptions = GetChangedOptionSet(adhocWorkspace.Options);
adhocWorkspace.TryApplyChanges(adhocWorkspace.CurrentSolution.WithOptions(csharpOptions));
_csharpWorkspace = adhocWorkspace;
}

return _csharpWorkspace;
}
}

public CodeAnalysis.Options.OptionSet GetChangedOptionSet(CodeAnalysis.Options.OptionSet optionsSet)
{
return optionsSet.WithChangedOption(CodeAnalysis.Formatting.FormattingOptions.TabSize, LanguageNames.CSharp, Options.TabSize)
.WithChangedOption(CodeAnalysis.Formatting.FormattingOptions.IndentationSize, LanguageNames.CSharp, Options.TabSize)
.WithChangedOption(CodeAnalysis.Formatting.FormattingOptions.UseTabs, LanguageNames.CSharp, !Options.InsertSpaces);
}
public AdhocWorkspace CSharpWorkspace => _csharpWorkspace ??= _workspaceFactory.Create();

/// <summary>A Dictionary of int (line number) to IndentationContext.</summary>
/// <remarks>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using Microsoft.CodeAnalysis.ExternalAccess.Razor;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting;

internal static class FormattingOptionsExtensions
{
public static RazorIndentationOptions GetIndentationOptions(this FormattingOptions options)
=> new(
UseTabs: !options.InsertSpaces,
TabSize: options.TabSize,
IndentationSize: options.TabSize);
}

0 comments on commit b17a0e8

Please sign in to comment.