Skip to content

Commit

Permalink
Remove IFormattingCodeDocumentProvider
Browse files Browse the repository at this point in the history
Since this service was written, IDocumentSnapshot has been updated such that now, all implementations just passed true for a parameter that is part of the existing API
  • Loading branch information
davidwengier committed Oct 30, 2024
1 parent e66abd6 commit 819d965
Show file tree
Hide file tree
Showing 11 changed files with 12 additions and 96 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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 System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -18,7 +17,6 @@
using Microsoft.CodeAnalysis.Razor.Logging;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.Protocol.Completion;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;

Expand All @@ -28,7 +26,6 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.InlineCompletion;
internal sealed class InlineCompletionEndpoint(
IDocumentMappingService documentMappingService,
IClientConnection clientConnection,
IFormattingCodeDocumentProvider formattingCodeDocumentProvider,
RazorLSPOptionsMonitor optionsMonitor,
ILoggerFactory loggerFactory)
: IRazorRequestHandler<VSInternalInlineCompletionRequest, VSInternalInlineCompletionList?>, ICapabilitiesProvider
Expand All @@ -40,7 +37,6 @@ internal sealed class InlineCompletionEndpoint(

private readonly IDocumentMappingService _documentMappingService = documentMappingService;
private readonly IClientConnection _clientConnection = clientConnection;
private readonly IFormattingCodeDocumentProvider _formattingCodeDocumentProvider = formattingCodeDocumentProvider;
private readonly RazorLSPOptionsMonitor _optionsMonitor = optionsMonitor;
private readonly ILogger _logger = loggerFactory.GetOrCreateLogger<InlineCompletionEndpoint>();

Expand Down Expand Up @@ -125,8 +121,7 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(VSInternalInlineCompleti
var formattingContext = FormattingContext.Create(
documentContext.Snapshot,
codeDocument,
options,
_formattingCodeDocumentProvider);
options);
if (!TryGetSnippetWithAdjustedIndentation(formattingContext, item.Text, hostDocumentIndex, out var newSnippetText))
{
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ protected override ILspServices ConstructLspServices()
// Add the logger as a service in case anything in CLaSP pulls it out to do logging
services.AddSingleton<ILspLogger>(Logger);

services.AddSingleton<IFormattingCodeDocumentProvider, LspFormattingCodeDocumentProvider>();

var featureOptions = _featureOptions ?? new DefaultLanguageServerFeatureOptions();
services.AddSingleton(featureOptions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,17 @@ namespace Microsoft.CodeAnalysis.Razor.Formatting;

internal sealed class FormattingContext
{
private readonly IFormattingCodeDocumentProvider _codeDocumentProvider;

private IReadOnlyList<FormattingSpan>? _formattingSpans;
private IReadOnlyDictionary<int, IndentationContext>? _indentations;

private FormattingContext(
IFormattingCodeDocumentProvider codeDocumentProvider,
IDocumentSnapshot originalSnapshot,
RazorCodeDocument codeDocument,
RazorFormattingOptions options,
bool automaticallyAddUsings,
int hostDocumentIndex,
char triggerCharacter)
{
_codeDocumentProvider = codeDocumentProvider;
OriginalSnapshot = originalSnapshot;
CodeDocument = codeDocument;
Options = options;
Expand Down Expand Up @@ -229,14 +225,12 @@ public async Task<FormattingContext> WithTextAsync(SourceText changedText, Cance
{
var changedSnapshot = OriginalSnapshot.WithText(changedText);

var codeDocument = await _codeDocumentProvider
.GetCodeDocumentAsync(changedSnapshot, cancellationToken)
.ConfigureAwait(false);
// Formatting always uses design time document
var codeDocument = await changedSnapshot.GetGeneratedOutputAsync(forceDesignTimeGeneratedOutput: true, cancellationToken).ConfigureAwait(false);

DEBUG_ValidateComponents(CodeDocument, codeDocument);

var newContext = new FormattingContext(
_codeDocumentProvider,
OriginalSnapshot,
codeDocument,
Options,
Expand Down Expand Up @@ -269,13 +263,11 @@ public static FormattingContext CreateForOnTypeFormatting(
IDocumentSnapshot originalSnapshot,
RazorCodeDocument codeDocument,
RazorFormattingOptions options,
IFormattingCodeDocumentProvider codeDocumentProvider,
bool automaticallyAddUsings,
int hostDocumentIndex,
char triggerCharacter)
{
return new FormattingContext(
codeDocumentProvider,
originalSnapshot,
codeDocument,
options,
Expand All @@ -287,17 +279,14 @@ public static FormattingContext CreateForOnTypeFormatting(
public static FormattingContext Create(
IDocumentSnapshot originalSnapshot,
RazorCodeDocument codeDocument,
RazorFormattingOptions options,
IFormattingCodeDocumentProvider codeDocumentProvider)
RazorFormattingOptions options)
{
return new FormattingContext(
codeDocumentProvider,
originalSnapshot,
codeDocument,
options,
automaticallyAddUsings: false,
hostDocumentIndex: 0,
triggerCharacter: '\0'
);
triggerCharacter: '\0');
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,16 @@ internal class RazorFormattingService : IRazorFormattingService
private static readonly FrozenSet<string> s_csharpTriggerCharacterSet = FrozenSet.ToFrozenSet(["}", ";"], StringComparer.Ordinal);
private static readonly FrozenSet<string> s_htmlTriggerCharacterSet = FrozenSet.ToFrozenSet(["\n", "{", "}", ";"], StringComparer.Ordinal);

private readonly IFormattingCodeDocumentProvider _codeDocumentProvider;

private readonly ImmutableArray<IFormattingPass> _documentFormattingPasses;
private readonly ImmutableArray<IFormattingPass> _validationPasses;
private readonly CSharpOnTypeFormattingPass _csharpOnTypeFormattingPass;
private readonly HtmlOnTypeFormattingPass _htmlOnTypeFormattingPass;

public RazorFormattingService(
IFormattingCodeDocumentProvider codeDocumentProvider,
IDocumentMappingService documentMappingService,
IHostServicesProvider hostServicesProvider,
ILoggerFactory loggerFactory)
{
_codeDocumentProvider = codeDocumentProvider;

_htmlOnTypeFormattingPass = new HtmlOnTypeFormattingPass(loggerFactory);
_csharpOnTypeFormattingPass = new CSharpOnTypeFormattingPass(documentMappingService, hostServicesProvider, loggerFactory);
_validationPasses =
Expand All @@ -67,9 +62,7 @@ public async Task<ImmutableArray<TextChange>> GetDocumentFormattingChangesAsync(
RazorFormattingOptions options,
CancellationToken cancellationToken)
{
var codeDocument = await _codeDocumentProvider
.GetCodeDocumentAsync(documentContext.Snapshot, cancellationToken)
.ConfigureAwait(false);
var codeDocument = await documentContext.Snapshot.GetGeneratedOutputAsync(forceDesignTimeGeneratedOutput: true, cancellationToken).ConfigureAwait(false);

// Range formatting happens on every paste, and if there are Razor diagnostics in the file
// that can make some very bad results. eg, given:
Expand Down Expand Up @@ -100,8 +93,7 @@ public async Task<ImmutableArray<TextChange>> GetDocumentFormattingChangesAsync(
var context = FormattingContext.Create(
documentSnapshot,
codeDocument,
options,
_codeDocumentProvider);
options);
var originalText = context.SourceText;

var result = htmlChanges;
Expand Down Expand Up @@ -252,7 +244,6 @@ private async Task<ImmutableArray<TextChange>> ApplyFormattedChangesAsync(
documentSnapshot,
codeDocument,
options,
_codeDocumentProvider,
automaticallyAddUsings: automaticallyAddUsings,
hostDocumentIndex,
triggerCharacter);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.CodeAnalysis.Remote.Razor.Formatting;

[Export(typeof(IRazorFormattingService)), Shared]
[method: ImportingConstructor]
internal sealed class RemoteRazorFormattingService(IFormattingCodeDocumentProvider codeDocumentProvider, IDocumentMappingService documentMappingService, IHostServicesProvider hostServicesProvider, ILoggerFactory loggerFactory)
: RazorFormattingService(codeDocumentProvider, documentMappingService, hostServicesProvider, loggerFactory)
internal sealed class RemoteRazorFormattingService(IDocumentMappingService documentMappingService, IHostServicesProvider hostServicesProvider, ILoggerFactory loggerFactory)
: RazorFormattingService(documentMappingService, hostServicesProvider, loggerFactory)
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ private static FormattingContext CreateFormattingContext(TestCode input, int tab
var context = FormattingContext.Create(
documentSnapshot,
codeDocument,
options,
new LspFormattingCodeDocumentProvider());
options);
return context;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ private static FormattingContext CreateFormattingContext(TestCode input, int tab
var context = FormattingContext.Create(
documentSnapshot,
codeDocument,
options,
new LspFormattingCodeDocumentProvider());
options);
return context;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ public static async Task<IRazorFormattingService> CreateWithFullSupportAsync(
await optionsMonitor.UpdateAsync(CancellationToken.None);
}

var formattingCodeDocumentProvider = new LspFormattingCodeDocumentProvider();
var hostServicesProvider = new DefaultHostServicesProvider();

return new RazorFormattingService(formattingCodeDocumentProvider, mappingService, hostServicesProvider, loggerFactory);
return new RazorFormattingService(mappingService, hostServicesProvider, loggerFactory);
}
}

0 comments on commit 819d965

Please sign in to comment.