Move workspace creation down even lower so the lifetime is contrained to a single formatting operation

This commit is contained in:
David Wengier 2024-09-17 11:39:52 +10:00
Родитель 26fb68c103
Коммит 763f6994ac
11 изменённых файлов: 95 добавлений и 116 удалений

Просмотреть файл

@ -29,7 +29,6 @@ internal sealed class InlineCompletionEndpoint(
IDocumentMappingService documentMappingService,
IClientConnection clientConnection,
IFormattingCodeDocumentProvider formattingCodeDocumentProvider,
IHostServicesProvider hostServicesProvider,
RazorLSPOptionsMonitor optionsMonitor,
ILoggerFactory loggerFactory)
: IRazorRequestHandler<VSInternalInlineCompletionRequest, VSInternalInlineCompletionList?>, ICapabilitiesProvider
@ -42,7 +41,6 @@ internal sealed class InlineCompletionEndpoint(
private readonly IDocumentMappingService _documentMappingService = documentMappingService;
private readonly IClientConnection _clientConnection = clientConnection;
private readonly IFormattingCodeDocumentProvider _formattingCodeDocumentProvider = formattingCodeDocumentProvider;
private readonly IHostServicesProvider _hostServicesProvider = hostServicesProvider;
private readonly RazorLSPOptionsMonitor _optionsMonitor = optionsMonitor;
private readonly ILogger _logger = loggerFactory.GetOrCreateLogger<InlineCompletionEndpoint>();
@ -63,10 +61,7 @@ internal sealed class InlineCompletionEndpoint(
public async Task<VSInternalInlineCompletionList?> HandleRequestAsync(VSInternalInlineCompletionRequest request, RazorRequestContext requestContext, CancellationToken cancellationToken)
{
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}
ArgHelper.ThrowIfNull(request);
_logger.LogInformation($"Starting request for {request.TextDocument.Uri} at {request.Position}.");
@ -118,7 +113,6 @@ internal sealed class InlineCompletionEndpoint(
using var items = new PooledArrayBuilder<VSInternalInlineCompletionItem>(list.Items.Length);
foreach (var item in list.Items)
{
var containsSnippet = item.TextFormat == InsertTextFormat.Snippet;
var range = item.Range ?? projectedPosition.ToZeroWidthRange();
if (!_documentMappingService.TryMapToHostDocumentRange(codeDocument.GetCSharpDocument(), range, out var rangeInRazorDoc))
@ -128,13 +122,11 @@ internal sealed class InlineCompletionEndpoint(
}
var options = RazorFormattingOptions.From(request.Options, _optionsMonitor.CurrentValue.CodeBlockBraceOnNextLine);
using var workspaceProvider = new AdhocWorkspaceProvider(_hostServicesProvider);
var formattingContext = FormattingContext.Create(
documentContext.Snapshot,
codeDocument,
options,
_formattingCodeDocumentProvider,
workspaceProvider);
_formattingCodeDocumentProvider);
if (!TryGetSnippetWithAdjustedIndentation(formattingContext, item.Text, hostDocumentIndex, out var newSnippetText))
{
continue;

Просмотреть файл

@ -1,35 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.
using System;
namespace Microsoft.CodeAnalysis.Razor.Workspaces;
internal sealed class AdhocWorkspaceProvider(IHostServicesProvider hostServicesProvider) : IDisposable
{
private readonly Lazy<AdhocWorkspace> _lazyWorkspace = new(() => CreateWorkspace(hostServicesProvider));
private static AdhocWorkspace CreateWorkspace(IHostServicesProvider hostServicesProvider)
{
var fallbackServices = hostServicesProvider.GetServices();
var services = AdhocServices.Create(
workspaceServices: [],
languageServices: [],
fallbackServices);
return new AdhocWorkspace(services);
}
public AdhocWorkspace GetOrCreate()
{
return _lazyWorkspace.Value;
}
public void Dispose()
{
if (_lazyWorkspace.IsValueCreated)
{
_lazyWorkspace.Value.Dispose();
}
}
}

Просмотреть файл

@ -12,6 +12,7 @@ using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;
@ -24,14 +25,14 @@ internal sealed class CSharpFormatter(IDocumentMappingService documentMappingSer
private readonly IDocumentMappingService _documentMappingService = documentMappingService;
public async Task<ImmutableArray<TextChange>> FormatAsync(FormattingContext context, LinePositionSpan spanToFormat, CancellationToken cancellationToken)
public async Task<ImmutableArray<TextChange>> FormatAsync(HostWorkspaceServices hostWorkspaceServices, Document csharpDocument, FormattingContext context, LinePositionSpan spanToFormat, CancellationToken cancellationToken)
{
if (!_documentMappingService.TryMapToGeneratedDocumentRange(context.CodeDocument.GetCSharpDocument(), spanToFormat, out var projectedSpan))
{
return [];
}
var edits = await GetFormattingEditsAsync(context, projectedSpan, cancellationToken).ConfigureAwait(false);
var edits = await GetFormattingEditsAsync(hostWorkspaceServices, csharpDocument, projectedSpan, context.Options.ToIndentationOptions(), cancellationToken).ConfigureAwait(false);
var mappedEdits = MapEditsToHostDocument(context.CodeDocument, edits);
return mappedEdits;
}
@ -39,13 +40,14 @@ internal sealed class CSharpFormatter(IDocumentMappingService documentMappingSer
public static async Task<IReadOnlyDictionary<int, int>> GetCSharpIndentationAsync(
FormattingContext context,
HashSet<int> projectedDocumentLocations,
HostWorkspaceServices hostWorkspaceServices,
CancellationToken cancellationToken)
{
// Sorting ensures we count the marker offsets correctly.
// We also want to ensure there are no duplicates to avoid duplicate markers.
var filteredLocations = projectedDocumentLocations.OrderAsArray();
var indentations = await GetCSharpIndentationCoreAsync(context, filteredLocations, cancellationToken).ConfigureAwait(false);
var indentations = await GetCSharpIndentationCoreAsync(context, filteredLocations, hostWorkspaceServices, cancellationToken).ConfigureAwait(false);
return indentations;
}
@ -56,18 +58,18 @@ internal sealed class CSharpFormatter(IDocumentMappingService documentMappingSer
return actualEdits.ToImmutableArray();
}
private static async Task<ImmutableArray<TextChange>> GetFormattingEditsAsync(FormattingContext context, LinePositionSpan projectedSpan, CancellationToken cancellationToken)
private static async Task<ImmutableArray<TextChange>> GetFormattingEditsAsync(HostWorkspaceServices hostWorkspaceServices, Document csharpDocument, LinePositionSpan projectedSpan, RazorIndentationOptions indentationOptions, CancellationToken cancellationToken)
{
var csharpSourceText = context.CodeDocument.GetCSharpSourceText();
var root = await csharpDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var csharpSourceText = await csharpDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);
var spanToFormat = csharpSourceText.GetTextSpan(projectedSpan);
var root = await context.CSharpWorkspaceDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
Assumes.NotNull(root);
var changes = RazorCSharpFormattingInteractionService.GetFormattedTextChanges(context.CSharpWorkspace.Services, root, spanToFormat, context.Options.ToIndentationOptions(), cancellationToken);
var changes = RazorCSharpFormattingInteractionService.GetFormattedTextChanges(hostWorkspaceServices, root, spanToFormat, indentationOptions, cancellationToken);
return changes.ToImmutableArray();
}
private static async Task<Dictionary<int, int>> GetCSharpIndentationCoreAsync(FormattingContext context, ImmutableArray<int> projectedDocumentLocations, CancellationToken cancellationToken)
private static async Task<Dictionary<int, int>> GetCSharpIndentationCoreAsync(FormattingContext context, ImmutableArray<int> projectedDocumentLocations, HostWorkspaceServices hostWorkspaceServices, CancellationToken cancellationToken)
{
// No point calling the C# formatting if we won't be interested in any of its work anyway
if (projectedDocumentLocations.Length == 0)
@ -83,7 +85,7 @@ internal sealed class CSharpFormatter(IDocumentMappingService documentMappingSer
// 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 = RazorCSharpFormattingInteractionService.Format(context.CSharpWorkspace.Services, root, context.Options.ToIndentationOptions(), cancellationToken);
var formattedRoot = RazorCSharpFormattingInteractionService.Format(hostWorkspaceServices, root, context.Options.ToIndentationOptions(), cancellationToken);
var formattedText = formattedRoot.GetText();
var desiredIndentationMap = new Dictionary<int, int>();

Просмотреть файл

@ -12,7 +12,6 @@ using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Syntax;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;
@ -20,17 +19,13 @@ namespace Microsoft.CodeAnalysis.Razor.Formatting;
internal sealed class FormattingContext
{
private readonly AdhocWorkspaceProvider _workspaceProvider;
private readonly IFormattingCodeDocumentProvider _codeDocumentProvider;
private Document? _csharpWorkspaceDocument;
private IReadOnlyList<FormattingSpan>? _formattingSpans;
private IReadOnlyDictionary<int, IndentationContext>? _indentations;
private FormattingContext(
IFormattingCodeDocumentProvider codeDocumentProvider,
AdhocWorkspaceProvider workspaceProvider,
IDocumentSnapshot originalSnapshot,
RazorCodeDocument codeDocument,
RazorFormattingOptions options,
@ -39,7 +34,6 @@ internal sealed class FormattingContext
char triggerCharacter)
{
_codeDocumentProvider = codeDocumentProvider;
_workspaceProvider = workspaceProvider;
OriginalSnapshot = originalSnapshot;
CodeDocument = codeDocument;
Options = options;
@ -63,24 +57,6 @@ internal sealed class FormattingContext
public string NewLineString => Environment.NewLine;
public Document CSharpWorkspaceDocument
{
get
{
if (_csharpWorkspaceDocument is null)
{
var workspace = CSharpWorkspace;
var project = workspace.CurrentSolution.AddProject("TestProject", "TestProject", LanguageNames.CSharp);
var csharpSourceText = CodeDocument.GetCSharpSourceText();
_csharpWorkspaceDocument = project.AddDocument("TestDocument", csharpSourceText);
}
return _csharpWorkspaceDocument;
}
}
public AdhocWorkspace CSharpWorkspace => _workspaceProvider.GetOrCreate();
/// <summary>A Dictionary of int (line number) to IndentationContext.</summary>
/// <remarks>
/// Don't use this to discover the indentation level you should have, use
@ -258,7 +234,6 @@ internal sealed class FormattingContext
var newContext = new FormattingContext(
_codeDocumentProvider,
_workspaceProvider,
OriginalSnapshot,
codeDocument,
Options,
@ -292,14 +267,12 @@ internal sealed class FormattingContext
RazorCodeDocument codeDocument,
RazorFormattingOptions options,
IFormattingCodeDocumentProvider codeDocumentProvider,
AdhocWorkspaceProvider workspaceProvider,
bool automaticallyAddUsings,
int hostDocumentIndex,
char triggerCharacter)
{
return new FormattingContext(
codeDocumentProvider,
workspaceProvider,
originalSnapshot,
codeDocument,
options,
@ -312,12 +285,10 @@ internal sealed class FormattingContext
IDocumentSnapshot originalSnapshot,
RazorCodeDocument codeDocument,
RazorFormattingOptions options,
IFormattingCodeDocumentProvider codeDocumentProvider,
AdhocWorkspaceProvider workspaceProvider)
IFormattingCodeDocumentProvider codeDocumentProvider)
{
return new FormattingContext(
codeDocumentProvider,
workspaceProvider,
originalSnapshot,
codeDocument,
options,

Просмотреть файл

@ -8,8 +8,10 @@ using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.Logging;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.Razor.Formatting;
@ -19,13 +21,14 @@ namespace Microsoft.CodeAnalysis.Razor.Formatting;
/// </summary>
internal sealed class CSharpFormattingPass(
IDocumentMappingService documentMappingService,
IHostServicesProvider hostServicesProvider,
ILoggerFactory loggerFactory)
: CSharpFormattingPassBase(documentMappingService, isFormatOnType: false)
: CSharpFormattingPassBase(documentMappingService, hostServicesProvider, isFormatOnType: false)
{
private readonly CSharpFormatter _csharpFormatter = new CSharpFormatter(documentMappingService);
private readonly CSharpFormatter _csharpFormatter = new(documentMappingService);
private readonly ILogger _logger = loggerFactory.GetOrCreateLogger<CSharpFormattingPass>();
public async override Task<ImmutableArray<TextChange>> ExecuteAsync(FormattingContext context, ImmutableArray<TextChange> changes, CancellationToken cancellationToken)
protected async override Task<ImmutableArray<TextChange>> ExecuteCoreAsync(FormattingContext context, RoslynWorkspaceHelper roslynWorkspaceHelper, ImmutableArray<TextChange> changes, CancellationToken cancellationToken)
{
// Apply previous edits if any.
var originalText = context.SourceText;
@ -40,7 +43,8 @@ internal sealed class CSharpFormattingPass(
cancellationToken.ThrowIfCancellationRequested();
// Apply original C# edits
var csharpChanges = await FormatCSharpAsync(changedContext, cancellationToken).ConfigureAwait(false);
var csharpDocument = roslynWorkspaceHelper.CreateCSharpDocument(changedContext.CodeDocument);
var csharpChanges = await FormatCSharpAsync(changedContext, roslynWorkspaceHelper.HostWorkspaceServices, csharpDocument, cancellationToken).ConfigureAwait(false);
if (csharpChanges.Length > 0)
{
changedText = changedText.WithChanges(csharpChanges);
@ -51,7 +55,7 @@ internal sealed class CSharpFormattingPass(
cancellationToken.ThrowIfCancellationRequested();
var indentationChanges = await AdjustIndentationAsync(changedContext, startLine: 0, endLineInclusive: changedText.Lines.Count - 1, cancellationToken).ConfigureAwait(false);
var indentationChanges = await AdjustIndentationAsync(changedContext, startLine: 0, endLineInclusive: changedText.Lines.Count - 1, roslynWorkspaceHelper.HostWorkspaceServices, cancellationToken).ConfigureAwait(false);
if (indentationChanges.Length > 0)
{
// Apply the edits that modify indentation.
@ -65,7 +69,7 @@ internal sealed class CSharpFormattingPass(
return changedText.GetTextChangesArray(originalText);
}
private async Task<ImmutableArray<TextChange>> FormatCSharpAsync(FormattingContext context, CancellationToken cancellationToken)
private async Task<ImmutableArray<TextChange>> FormatCSharpAsync(FormattingContext context, HostWorkspaceServices hostWorkspaceServices, Document csharpDocument, CancellationToken cancellationToken)
{
var sourceText = context.SourceText;
@ -81,7 +85,8 @@ internal sealed class CSharpFormattingPass(
// These should already be remapped.
var spanToFormat = sourceText.GetLinePositionSpan(span);
var changes = await _csharpFormatter.FormatAsync(context, spanToFormat, cancellationToken).ConfigureAwait(false);
var changes = await _csharpFormatter.FormatAsync(hostWorkspaceServices, csharpDocument, context, spanToFormat, cancellationToken).ConfigureAwait(false);
csharpChanges.AddRange(changes.Where(e => spanToFormat.Contains(sourceText.GetLinePositionSpan(e.Span))));
}

Просмотреть файл

@ -0,0 +1,45 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Razor.Workspaces;
namespace Microsoft.CodeAnalysis.Razor.Formatting;
internal abstract partial class CSharpFormattingPassBase
{
protected class RoslynWorkspaceHelper(IHostServicesProvider hostServicesProvider) : IDisposable
{
private readonly Lazy<AdhocWorkspace> _lazyWorkspace = new(() => CreateWorkspace(hostServicesProvider));
public HostWorkspaceServices HostWorkspaceServices => _lazyWorkspace.Value.Services;
public Document CreateCSharpDocument(RazorCodeDocument codeDocument)
{
var project = _lazyWorkspace.Value.CurrentSolution.AddProject("TestProject", "TestProject", LanguageNames.CSharp);
var csharpSourceText = codeDocument.GetCSharpSourceText();
return project.AddDocument("TestDocument", csharpSourceText);
}
private static AdhocWorkspace CreateWorkspace(IHostServicesProvider hostServicesProvider)
{
var fallbackServices = hostServicesProvider.GetServices();
var services = AdhocServices.Create(
workspaceServices: [],
languageServices: [],
fallbackServices);
return new AdhocWorkspace(services);
}
public void Dispose()
{
if (_lazyWorkspace.IsValueCreated)
{
_lazyWorkspace.Value.Dispose();
}
}
}
}

Просмотреть файл

@ -13,22 +13,31 @@ using Microsoft.AspNetCore.Razor.Language.Components;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.Syntax;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using RazorSyntaxNode = Microsoft.AspNetCore.Razor.Language.Syntax.SyntaxNode;
namespace Microsoft.CodeAnalysis.Razor.Formatting;
internal abstract class CSharpFormattingPassBase(IDocumentMappingService documentMappingService, bool isFormatOnType) : IFormattingPass
internal abstract partial class CSharpFormattingPassBase(IDocumentMappingService documentMappingService, IHostServicesProvider hostServicesProvider, bool isFormatOnType) : IFormattingPass
{
private readonly bool _isFormatOnType = isFormatOnType;
protected IDocumentMappingService DocumentMappingService { get; } = documentMappingService;
public abstract Task<ImmutableArray<TextChange>> ExecuteAsync(FormattingContext context, ImmutableArray<TextChange> changes, CancellationToken cancellationToken);
public async Task<ImmutableArray<TextChange>> ExecuteAsync(FormattingContext context, ImmutableArray<TextChange> changes, CancellationToken cancellationToken)
{
using var roslynWorkspaceHelper = new RoslynWorkspaceHelper(hostServicesProvider);
protected async Task<ImmutableArray<TextChange>> AdjustIndentationAsync(FormattingContext context, int startLine, int endLineInclusive, CancellationToken cancellationToken)
return await ExecuteCoreAsync(context, roslynWorkspaceHelper, changes, cancellationToken).ConfigureAwait(false);
}
protected abstract Task<ImmutableArray<TextChange>> ExecuteCoreAsync(FormattingContext context, RoslynWorkspaceHelper roslynWorkspaceHelper, ImmutableArray<TextChange> changes, CancellationToken cancellationToken);
protected async Task<ImmutableArray<TextChange>> AdjustIndentationAsync(FormattingContext context, int startLine, int endLineInclusive, HostWorkspaceServices hostWorkspaceServices, CancellationToken cancellationToken)
{
// In this method, the goal is to make final adjustments to the indentation of each line.
// We will take into account the following,
@ -113,7 +122,7 @@ internal abstract class CSharpFormattingPassBase(IDocumentMappingService documen
}
// Now, invoke the C# formatter to obtain the CSharpDesiredIndentation for all significant locations.
var significantLocationIndentation = await CSharpFormatter.GetCSharpIndentationAsync(context, significantLocations, cancellationToken).ConfigureAwait(false);
var significantLocationIndentation = await CSharpFormatter.GetCSharpIndentationAsync(context, significantLocations, hostWorkspaceServices, cancellationToken).ConfigureAwait(false);
// Build source mapping indentation scopes.
var sourceMappingIndentations = new SortedDictionary<int, IndentationData>();

Просмотреть файл

@ -17,6 +17,7 @@ using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.Logging;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;
@ -27,12 +28,13 @@ namespace Microsoft.CodeAnalysis.Razor.Formatting;
/// </summary>
internal sealed class CSharpOnTypeFormattingPass(
IDocumentMappingService documentMappingService,
IHostServicesProvider hostServicesProvider,
ILoggerFactory loggerFactory)
: CSharpFormattingPassBase(documentMappingService, isFormatOnType: true)
: CSharpFormattingPassBase(documentMappingService, hostServicesProvider, isFormatOnType: true)
{
private readonly ILogger _logger = loggerFactory.GetOrCreateLogger<CSharpOnTypeFormattingPass>();
public async override Task<ImmutableArray<TextChange>> ExecuteAsync(FormattingContext context, ImmutableArray<TextChange> changes, CancellationToken cancellationToken)
protected async override Task<ImmutableArray<TextChange>> ExecuteCoreAsync(FormattingContext context, RoslynWorkspaceHelper roslynWorkspaceHelper, ImmutableArray<TextChange> changes, CancellationToken cancellationToken)
{
// Normalize and re-map the C# edits.
var codeDocument = context.CodeDocument;
@ -51,7 +53,7 @@ internal sealed class CSharpOnTypeFormattingPass(
formatOnReturn: true, formatOnTyping: true, formatOnSemicolon: true, formatOnCloseBrace: true);
var formattingChanges = await RazorCSharpFormattingInteractionService.GetFormattingChangesAsync(
context.CSharpWorkspaceDocument,
roslynWorkspaceHelper.CreateCSharpDocument(context.CodeDocument),
typedChar: context.TriggerCharacter,
projectedIndex,
context.Options.ToIndentationOptions(),
@ -167,7 +169,7 @@ internal sealed class CSharpOnTypeFormattingPass(
Debug.Assert(cleanedText.Lines.Count > endLineInclusive, "Invalid range. This is unexpected.");
var indentationChanges = await AdjustIndentationAsync(changedContext, startLine, endLineInclusive, cancellationToken).ConfigureAwait(false);
var indentationChanges = await AdjustIndentationAsync(changedContext, startLine, endLineInclusive, roslynWorkspaceHelper.HostWorkspaceServices, cancellationToken).ConfigureAwait(false);
if (indentationChanges.Length > 0)
{
// Apply the edits that modify indentation.

Просмотреть файл

@ -30,7 +30,6 @@ internal class RazorFormattingService : IRazorFormattingService
private static readonly FrozenSet<string> s_htmlTriggerCharacterSet = FrozenSet.ToFrozenSet(["\n", "{", "}", ";"], StringComparer.Ordinal);
private readonly IFormattingCodeDocumentProvider _codeDocumentProvider;
private readonly IHostServicesProvider _hostServicesProvider;
private readonly ImmutableArray<IFormattingPass> _documentFormattingPasses;
private readonly ImmutableArray<IFormattingPass> _validationPasses;
@ -44,10 +43,9 @@ internal class RazorFormattingService : IRazorFormattingService
ILoggerFactory loggerFactory)
{
_codeDocumentProvider = codeDocumentProvider;
_hostServicesProvider = hostServicesProvider;
_htmlOnTypeFormattingPass = new HtmlOnTypeFormattingPass(loggerFactory);
_csharpOnTypeFormattingPass = new CSharpOnTypeFormattingPass(documentMappingService, loggerFactory);
_csharpOnTypeFormattingPass = new CSharpOnTypeFormattingPass(documentMappingService, hostServicesProvider, loggerFactory);
_validationPasses =
[
new FormattingDiagnosticValidationPass(loggerFactory),
@ -57,7 +55,7 @@ internal class RazorFormattingService : IRazorFormattingService
[
new HtmlFormattingPass(loggerFactory),
new RazorFormattingPass(),
new CSharpFormattingPass(documentMappingService, loggerFactory),
new CSharpFormattingPass(documentMappingService, hostServicesProvider, loggerFactory),
.. _validationPasses
];
}
@ -97,13 +95,11 @@ internal class RazorFormattingService : IRazorFormattingService
var uri = documentContext.Uri;
var documentSnapshot = documentContext.Snapshot;
var hostDocumentVersion = documentContext.Snapshot.Version;
using var workspaceProvider = new AdhocWorkspaceProvider(_hostServicesProvider);
var context = FormattingContext.Create(
documentSnapshot,
codeDocument,
options,
_codeDocumentProvider,
workspaceProvider);
_codeDocumentProvider);
var originalText = context.SourceText;
var result = htmlChanges;
@ -224,13 +220,11 @@ internal class RazorFormattingService : IRazorFormattingService
var documentSnapshot = documentContext.Snapshot;
var codeDocument = await _codeDocumentProvider.GetCodeDocumentAsync(documentSnapshot).ConfigureAwait(false);
using var workspaceProvider = new AdhocWorkspaceProvider(_hostServicesProvider);
var context = FormattingContext.CreateForOnTypeFormatting(
documentSnapshot,
codeDocument,
options,
_codeDocumentProvider,
workspaceProvider,
automaticallyAddUsings,
hostDocumentIndex,
triggerCharacter);

Просмотреть файл

@ -10,7 +10,6 @@ using Microsoft.AspNetCore.Razor.Test.Common;
using Microsoft.AspNetCore.Razor.Test.Common.LanguageServer;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Text;
using Moq;
using Xunit;
@ -84,13 +83,11 @@ public class FormattingContentValidationPassTest(ITestOutputHelper testOutput) :
InsertSpaces = insertSpaces,
};
using var workspaceProvider = new AdhocWorkspaceProvider(new DefaultHostServicesProvider());
var context = FormattingContext.Create(
documentSnapshot,
codeDocument,
options,
new LspFormattingCodeDocumentProvider(),
workspaceProvider);
new LspFormattingCodeDocumentProvider());
return context;
}

Просмотреть файл

@ -9,7 +9,6 @@ using Microsoft.AspNetCore.Razor.Test.Common;
using Microsoft.AspNetCore.Razor.Test.Common.LanguageServer;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Text;
using Xunit;
using Xunit.Abstractions;
@ -82,13 +81,11 @@ public class FormattingDiagnosticValidationPassTest(ITestOutputHelper testOutput
InsertSpaces = insertSpaces,
};
using var workspaceProvider = new AdhocWorkspaceProvider(new DefaultHostServicesProvider());
var context = FormattingContext.Create(
documentSnapshot,
codeDocument,
options,
new LspFormattingCodeDocumentProvider(),
workspaceProvider);
new LspFormattingCodeDocumentProvider());
return context;
}