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

Explicitly pass formatting options to Roslyn APIs (via External Access) #6166

Merged
merged 7 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
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 @@ -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);
}
Loading