Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6b65bbb
Move formatting files up to Remote.Razor
davidwengier Jun 10, 2026
3796b77
Move almost all of FormattingUtilities
davidwengier Jun 10, 2026
d60bd03
Move C# formatting options helper back a to shared location
davidwengier Jun 10, 2026
e4059b1
Move trigger characters to where they're actually used
davidwengier Jun 10, 2026
b63641b
Fix namespaces and usings
davidwengier Jun 10, 2026
8f4a32d
Add IVT
davidwengier Jun 10, 2026
e46ed32
Move resources
davidwengier Jun 10, 2026
51db216
Update tests
davidwengier Jun 10, 2026
24d5d26
Move MEF attributes and remote unnecessary indirection
davidwengier Jun 10, 2026
74d6760
Unskip tests
davidwengier Jun 11, 2026
62c0956
Make formatting sonic compatible
davidwengier Jun 11, 2026
df11094
Update dependent services so they at least compile
davidwengier Jun 11, 2026
a4948b7
Fix "Show Formatting Document" feature
davidwengier Jun 11, 2026
4ec51a3
Unskip tests (and add one)
davidwengier Jun 11, 2026
f1cbb12
Remove stale comment
davidwengier Jun 11, 2026
e384ef0
Fix benchmark
davidwengier Jun 11, 2026
cffd735
Make Razor inlay hints SONIC-aware
davidwengier Jun 11, 2026
e5affc8
Add SONIC inlay hint tests
davidwengier Jun 11, 2026
d758621
Make Razor folding ranges SONIC-aware
davidwengier Jun 11, 2026
2057517
Add SONIC folding range tests
davidwengier Jun 11, 2026
3eb3b12
Move files from Workspaces to Remote and unify namespaces
davidwengier Jun 11, 2026
c9d98ae
Move MEF export
davidwengier Jun 11, 2026
bff73ca
Rename file
davidwengier Jun 11, 2026
577e795
Make Razor go to definition SONIC-aware
davidwengier Jun 11, 2026
4c73cf3
Correctness
davidwengier Jun 11, 2026
87ff5ca
[Sonic] Inlay hints, folding ranges, and go to definition (#84102)
davidwengier Jun 12, 2026
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 @@ -84,7 +84,7 @@ public void Setup()
var documentMappingService = new DocumentMappingService(filePathService, snapshotManager, EmptyLoggerFactory.Instance);
var razorEditService = new RazorEditService(documentMappingService, clientSettingsManager, filePathService, snapshotManager, NoOpTelemetryReporter.Instance);

_formattingService = new RemoteRazorFormattingService(
_formattingService = new RazorFormattingService(
documentMappingService,
razorEditService,
hostServicesProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public ImmutableArray<Registration> GetRegistrations(VSInternalClientCapabilitie

protected override Task<TextEdit[]?> HandleRequestAsync(DocumentFormattingParams request, TextDocument razorDocument, CancellationToken cancellationToken)
{
var csharpSyntaxFormattingOptions = CSharpFormatter.GetCSharpSyntaxFormattingOptions(razorDocument.Project.Solution.Services, csharpSyntaxFormattingOptions: null);
var csharpSyntaxFormattingOptions = CSharpFormattingOptionsHelper.GetCSharpSyntaxFormattingOptions(razorDocument.Project.Solution.Services, csharpSyntaxFormattingOptions: null);
return HandleRequestAsync(request, razorDocument, csharpSyntaxFormattingOptions, cancellationToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
using Microsoft.CodeAnalysis.Razor.Remote;
using Microsoft.CodeAnalysis.Razor.Workspaces.Settings;
using Microsoft.CodeAnalysis.Text;
using System.Collections.Frozen;
using System;

namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;

Expand All @@ -33,6 +35,10 @@ internal sealed class CohostOnTypeFormattingEndpoint(
ILoggerFactory loggerFactory)
: AbstractCohostDocumentEndpoint<DocumentOnTypeFormattingParams, TextEdit[]?>(incompatibleProjectService), IDynamicRegistrationProvider
{
internal const string FirstTriggerCharacter = "}";
internal static readonly string[] MoreTriggerCharacters = [";", "\n", "{"];
internal static readonly FrozenSet<string> AllTriggerCharacterSet = FrozenSet.ToFrozenSet([FirstTriggerCharacter, .. MoreTriggerCharacters], StringComparer.Ordinal);

private readonly IRemoteServiceInvoker _remoteServiceInvoker = remoteServiceInvoker;
private readonly IHtmlRequestInvoker _requestInvoker = requestInvoker;
private readonly IClientSettingsManager _clientSettingsManager = clientSettingsManager;
Expand All @@ -51,8 +57,8 @@ public ImmutableArray<Registration> GetRegistrations(VSInternalClientCapabilitie
Method = Methods.TextDocumentOnTypeFormattingName,
RegisterOptions = new DocumentOnTypeFormattingRegistrationOptions()
{
FirstTriggerCharacter = RazorFormattingService.FirstTriggerCharacter,
MoreTriggerCharacter = RazorFormattingService.MoreTriggerCharacters
FirstTriggerCharacter = FirstTriggerCharacter,
MoreTriggerCharacter = MoreTriggerCharacters
}
}];
}
Expand All @@ -72,7 +78,7 @@ public ImmutableArray<Registration> GetRegistrations(VSInternalClientCapabilitie
return null;
}

if (!RazorFormattingService.AllTriggerCharacterSet.Contains(request.Character))
if (!AllTriggerCharacterSet.Contains(request.Character))
{
_logger.LogWarning($"Unexpected trigger character '{request.Character}'.");
return null;
Expand Down Expand Up @@ -112,7 +118,7 @@ public ImmutableArray<Registration> GetRegistrations(VSInternalClientCapabilitie
htmlChanges = htmlEdits.SelectAsArray(sourceText.GetTextChange);
}

var csharpSyntaxFormattingOptions = CSharpFormatter.GetCSharpSyntaxFormattingOptions(razorDocument.Project.Solution.Services, csharpSyntaxFormattingOptions: null);
var csharpSyntaxFormattingOptions = CSharpFormattingOptionsHelper.GetCSharpSyntaxFormattingOptions(razorDocument.Project.Solution.Services, csharpSyntaxFormattingOptions: null);
var options = RazorFormattingOptions.From(request.Options, clientSettings.AdvancedSettings.CodeBlockBraceOnNextLine, clientSettings.AdvancedSettings.AttributeIndentStyle, csharpSyntaxFormattingOptions);

_logger.LogDebug($"Calling OOP with the {htmlChanges.Length} html edits, so it can fill in the rest");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public ImmutableArray<Registration> GetRegistrations(VSInternalClientCapabilitie
var sourceText = await razorDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);
var htmlChanges = htmlEdits.SelectAsArray(sourceText.GetTextChange);

var csharpSyntaxFormattingOptions = CSharpFormatter.GetCSharpSyntaxFormattingOptions(razorDocument.Project.Solution.Services, csharpSyntaxFormattingOptions: null);
var csharpSyntaxFormattingOptions = CSharpFormattingOptionsHelper.GetCSharpSyntaxFormattingOptions(razorDocument.Project.Solution.Services, csharpSyntaxFormattingOptions: null);
var options = RazorFormattingOptions.From(
request.Options,
_clientSettingsManager.GetClientSettings().AdvancedSettings.CodeBlockBraceOnNextLine,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal class CohostInlayHintResolveEndpoint(

var hint = await _remoteServiceInvoker.TryInvokeAsync<IRemoteInlayHintService, InlayHint>(
razorDocument.Project.Solution,
(service, solutionInfo, cancellationToken) => service.ResolveHintAsync(solutionInfo, razorDocument.Id, request, cancellationToken),
(service, solutionInfo, cancellationToken) => service.ResolveHintAsync(solutionInfo, razorDocument.Id, request, razorData.InDeclDocument, cancellationToken),
cancellationToken).ConfigureAwait(false);

if (hint is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ static string GetArgumentTypesLogString(VSInternalCompletionItem resolvedComplet
var formattedTextChange = await formattingService.TryGetCSharpSnippetFormattingEditAsync(
documentContext,
changes,
declarationDocument: false, // PROTOTYPE(sonic): Pass in the right value to this
options,
cancellationToken).ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,33 @@ public static ImmutableArray<TextChange> GetTextChangesArray(this SourceText new

return list.ToImmutableArray();
}

/// <summary>
/// Sometimes the Html language server will send back an edit that contains a tilde, because the generated
/// document we send them has lots of tildes. In those cases, we need to do some extra work to compute the
/// minimal text edits
/// </summary>
public static TextEdit[] FixHtmlTextEdits(this SourceText htmlSourceText, TextEdit[] edits)
{
// Avoid computing a minimal diff if we don't need to
if (!edits.Any(static e => e.NewText.Contains('~')))
return edits;

var changes = edits.SelectAsArray(htmlSourceText.GetTextChange);

var fixedChanges = htmlSourceText.MinimizeTextChanges(changes);
return fixedChanges.SelectAsPlainArray(htmlSourceText.GetTextEdit);
}

public static SumType<TextEdit, AnnotatedTextEdit>[] FixHtmlTextEdits(this SourceText htmlSourceText, SumType<TextEdit, AnnotatedTextEdit>[] edits)
{
// Avoid computing a minimal diff if we don't need to
if (!edits.Any(static e => ((TextEdit)e).NewText.Contains('~')))
return edits;

var changes = edits.SelectAsArray(e => htmlSourceText.GetTextChange((TextEdit)e));

var fixedChanges = htmlSourceText.MinimizeTextChanges(changes);
return fixedChanges.SelectAsPlainArray<TextChange, SumType<TextEdit, AnnotatedTextEdit>>(c => htmlSourceText.GetTextEdit(c));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.Razor.Formatting;

internal static class CSharpFormattingOptionsHelper
{
internal static CSharpSyntaxFormattingOptions GetCSharpSyntaxFormattingOptions(
SolutionServices services,
CSharpSyntaxFormattingOptions? csharpSyntaxFormattingOptions)
{
csharpSyntaxFormattingOptions
??= (CSharpSyntaxFormattingOptions)(services.GetService<ILegacyGlobalOptionsWorkspaceService>()?.GetSyntaxFormattingOptions(services.GetLanguageServices(LanguageNames.CSharp))
?? CSharpSyntaxFormattingOptions.Default);

return csharpSyntaxFormattingOptions;
}

internal static CSharpSyntaxFormattingOptions GetResolvedCSharpSyntaxFormattingOptions(
SolutionServices services,
RazorFormattingOptions options,
CSharpSyntaxFormattingOptions? csharpSyntaxFormattingOptions = null)
{
csharpSyntaxFormattingOptions = GetCSharpSyntaxFormattingOptions(
services,
csharpSyntaxFormattingOptions ?? options.CSharpSyntaxFormattingOptions);

return csharpSyntaxFormattingOptions with
{
LineFormatting = csharpSyntaxFormattingOptions.LineFormatting with
{
UseTabs = !options.InsertSpaces,
TabSize = options.TabSize,
IndentationSize = options.TabSize,
NewLine = CSharpSyntaxFormattingOptions.Default.NewLine
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,27 @@ Task<ImmutableArray<TextChange>> GetCSharpOnTypeFormattingChangesAsync(
RazorFormattingOptions options,
int hostDocumentIndex,
char triggerCharacter,
bool declarationDocument,
CancellationToken cancellationToken);

Task<TextChange?> TryGetSingleCSharpEditAsync(
DocumentContext documentContext,
TextChange csharpEdit,
bool declarationDocument,
RazorFormattingOptions options,
CancellationToken cancellationToken);

Task<TextChange?> TryGetCSharpCodeActionEditAsync(
DocumentContext documentContext,
ImmutableArray<TextChange> csharpEdits,
bool declarationDocument,
RazorFormattingOptions options,
CancellationToken cancellationToken);

Task<TextChange?> TryGetCSharpSnippetFormattingEditAsync(
DocumentContext documentContext,
ImmutableArray<TextChange> csharpEdits,
bool declarationDocument,
RazorFormattingOptions options,
CancellationToken cancellationToken);

Expand Down

This file was deleted.

Loading