Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
427e60e
Make Razor spell check SONIC-aware
davidwengier Jun 9, 2026
9f61e1d
Add SONIC spell check tests
davidwengier Jun 9, 2026
b546c73
Make Razor inline completion SONIC-aware
davidwengier Jun 9, 2026
3f2934d
Add SONIC inline completion tests
davidwengier Jun 9, 2026
e363e6a
Make Razor type hierarchy SONIC-aware
davidwengier Jun 9, 2026
e8c348a
Add SONIC type hierarchy tests
davidwengier Jun 9, 2026
5413ad3
Make Razor go to implementation SONIC-aware
davidwengier Jun 9, 2026
495fb29
Add SONIC go to implementation tests
davidwengier Jun 9, 2026
8d540df
Make Razor find all references SONIC-aware
davidwengier Jun 9, 2026
bfc7b72
Add SONIC find all references tests
davidwengier Jun 9, 2026
b171b00
Move document and edit mapping code up to Remote.Razor
davidwengier Jun 10, 2026
844035f
Move resource
davidwengier Jun 10, 2026
c9b000f
Delete unused file
davidwengier Jun 10, 2026
db79a4d
Move MEF exports to the real classes, and remove indirection
davidwengier Jun 10, 2026
43d93eb
Rename file
davidwengier Jun 10, 2026
1e8434a
Strongly type and simplify logic, now that there is no abstraction to…
davidwengier Jun 10, 2026
9fa0075
Make edit service and mapping service handle multiple C# documents
davidwengier Jun 10, 2026
db803ac
Move document position info code
davidwengier Jun 10, 2026
b41ce6b
Remove unused Razor using directive
davidwengier Jun 10, 2026
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
11d5cb6
[Sonic] Support multiple C# documents in formatting (#84097)
davidwengier Jun 12, 2026
d00caf2
[Sonic] Move edit service up to Remote and make it sonic aware (#84086)
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 @@ -48,20 +48,13 @@ public static SourceText GetCSharpSourceText(this RazorCodeDocument document)
/// keeps the behavior conservative.
/// </remarks>
public static RazorCSharpDocument GetCSharpDocumentForHintName(this RazorCodeDocument document, string hintName)
{
return GetCSharpDocumentForHintName(document, hintName, out _);
}

public static RazorCSharpDocument GetCSharpDocumentForHintName(this RazorCodeDocument document, string hintName, out bool isDeclDocument)
{
if (hintName.EndsWith(".decl.g.cs", System.StringComparison.Ordinal) &&
document.GetCSharpDocument(declarationDocument: true) is { } declDocument)
{
isDeclDocument = true;
return declDocument;
}

isDeclDocument = false;
return document.GetRequiredCSharpDocument(declarationDocument: false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal interface IRemoteInlineCompletionService
ValueTask<FormattedInlineCompletionInfo?> FormatInlineCompletionAsync(
RazorSolutionWrapper solutionInfo,
DocumentId documentId,
bool inDeclDocument,
RazorFormattingOptions options,
LinePositionSpan span,
string text,
Expand All @@ -30,7 +31,8 @@ internal interface IRemoteInlineCompletionService
[DataContract]
internal record struct InlineCompletionRequestInfo(
[property: DataMember(Order = 0)] Uri GeneratedDocumentUri,
[property: DataMember(Order = 1)] LinePosition Position);
[property: DataMember(Order = 1)] LinePosition Position,
[property: DataMember(Order = 2)] bool InDeclDocument);

[DataContract]
internal record struct FormattedInlineCompletionInfo(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
Expand All @@ -15,7 +17,6 @@
using Microsoft.CodeAnalysis.Razor.Remote;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Remote.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Remote.Razor.FindAllReferences;
using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.CodeAnalysis.Text;
Expand Down Expand Up @@ -73,7 +74,7 @@ protected override IRemoteFindAllReferencesService CreateService(in ServiceArgs

// Finally, call into C#.
var generatedDocument = await context.Snapshot
.GetGeneratedDocumentAsync(cancellationToken)
.GetGeneratedDocumentAsync(positionInfo.InDeclDocument, cancellationToken)
.ConfigureAwait(false);
var globalOptions = generatedDocument.Project.Solution.Services.ExportProvider.GetService<IGlobalOptionService>();
var metadataAsSourceFileService = generatedDocument.Project.Solution.Services.ExportProvider.GetService<IMetadataAsSourceFileService>();
Expand Down Expand Up @@ -113,7 +114,14 @@ await FindAllReferencesHandler.FindReferencesAsync(
continue;
}

var (mappedUri, mappedRange) = await DocumentMappingService.MapToHostDocumentUriAndRangeAsync(context.Snapshot, UriExtensions.GetRequiredSystemUri(location.DocumentUri), location.Range.ToLinePositionSpan(), cancellationToken).ConfigureAwait(false);
var generatedDocumentUri = UriExtensions.GetRequiredSystemUri(location.DocumentUri);
var (mappedUri, mappedRange) = await DocumentMappingService
.MapToHostDocumentUriAndRangeAsync(
context.Snapshot,
generatedDocumentUri,
location.Range.ToLinePositionSpan(),
cancellationToken)
.ConfigureAwait(false);

if (_filePathService.IsVirtualCSharpFile(mappedUri))
{
Expand All @@ -132,7 +140,13 @@ await FindAllReferencesHandler.FindReferencesAsync(
referenceItem.DisplayPath = mappedUri.AbsolutePath;
referenceItem.DocumentName = mappedUri.AbsolutePath;

var fixedResultText = await FindAllReferencesHelper.GetResultTextAsync(DocumentMappingService, context.GetSolutionQueryOperations(), mappedRange.Start.Line, mappedUri.GetDocumentFilePathFromUri(), cancellationToken).ConfigureAwait(false);
var fixedResultText = await GetResultTextAsync(
context,
generatedDocumentUri,
mappedRange.Start.Line,
mappedUri.GetDocumentFilePathFromUri(),
cancellationToken)
.ConfigureAwait(false);
referenceItem.Text = fixedResultText ?? referenceItem.Text;
}
}
Expand All @@ -145,4 +159,54 @@ await FindAllReferencesHandler.FindReferencesAsync(

return Results(mappedResults.ToArrayAndClear());
}

private async Task<string?> GetResultTextAsync(
RemoteDocumentContext context,
Uri generatedDocumentUri,
int lineNumber,
string filePath,
CancellationToken cancellationToken)
{
// Roslyn will have sent us back text that comes from the .g.cs file, but that is often not helpful. For example give:
//
// <SurveyPrompt Title="Blah" />
//
// A FAR for the Title property will return just the word "Title" in the Text of the reference item, which does not
// help the user reason about the result. For such cases, its better to return the text from the Razor file, even
// though it will be unclassified, as it will help the user.
//
// However, for cases where the result comes from a C# block, for example:
//
// @code {
// public string Title { get; set; }
// }
//
// A FAR for the Title property here will return the full line of code, classified by Roslyn, so we don't want to
// do anything for those.
//
// To identify which situation we're in, we try to map the start and the end of the line to C#, as an indicator. If
// either start or end fail to map, it means the entire line is not C#

// TODO: Note the call to ISolutionQueryOperations.GetProjectsContainingDocument(...) will be removed with the introduction of solution snapshots.
if (context.GetSolutionQueryOperations().GetProjectsContainingDocument(filePath).FirstOrDefault() is { } project &&
project.TryGetDocument(filePath, out var document))
{
var codeDoc = await document.GetGeneratedOutputAsync(cancellationToken).ConfigureAwait(false);
var line = codeDoc.Source.Text.Lines[lineNumber];
if (!context.Snapshot.TextDocument.Project.Solution.TryGetSourceGeneratedDocumentIdentity(generatedDocumentUri, out var identity))
{
return null;
}

var csharpDocument = codeDoc.GetCSharpDocumentForHintName(identity.HintName);
if (!DocumentMappingService.TryMapToCSharpDocumentPosition(csharpDocument, line.Start, out _, out _) ||
!DocumentMappingService.TryMapToCSharpDocumentPosition(csharpDocument, line.End, out _, out _))
{
var start = line.GetFirstNonWhitespacePosition() ?? line.Start;
return codeDoc.Source.Text.ToString(TextSpan.FromBounds(start, line.End));
}
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.PooledObjects;
Expand Down Expand Up @@ -67,7 +69,7 @@ protected override IRemoteGoToImplementationService CreateService(in ServiceArgs

// Finally, call into C#.
var generatedDocument = await context.Snapshot
.GetGeneratedDocumentAsync(cancellationToken)
.GetGeneratedDocumentAsync(positionInfo.InDeclDocument, cancellationToken)
.ConfigureAwait(false);

var supportsVisualStudioExtensions = _clientCapabilitiesService.ClientCapabilities.SupportsVisualStudioExtensions;
Expand All @@ -88,6 +90,7 @@ protected override IRemoteGoToImplementationService CreateService(in ServiceArgs

// Map the C# locations back to the Razor file.
using var mappedLocations = new PooledArrayBuilder<LspLocation>(locations.Length);
var seenLocations = new HashSet<(Uri DocumentUri, LinePositionSpan Range)>();

foreach (var location in locations)
{
Expand All @@ -97,6 +100,12 @@ protected override IRemoteGoToImplementationService CreateService(in ServiceArgs
.MapToHostDocumentUriAndRangeAsync(context.Snapshot, uri, range.ToLinePositionSpan(), cancellationToken)
.ConfigureAwait(false);

// Impl and decl generated documents can both contain a generated class declaration that maps to the same Razor location.
if (!seenLocations.Add((mappedDocumentUri, mappedRange)))
{
continue;
}

var mappedLocation = LspFactory.CreateLocation(mappedDocumentUri.CreateDocumentUriFromSystemUri(), mappedRange);

mappedLocations.Add(mappedLocation);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading;
Expand Down Expand Up @@ -32,35 +32,37 @@ protected override IRemoteInlineCompletionService CreateService(in ServiceArgs a
public async ValueTask<InlineCompletionRequestInfo?> GetInlineCompletionInfoAsync(RemoteDocumentContext context, LinePosition linePosition, CancellationToken cancellationToken)
{
var codeDocument = await context.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
var csharpDocument = codeDocument.GetRequiredImplCSharpDocument();

if (!codeDocument.Source.Text.TryGetAbsoluteIndex(linePosition, out var hostDocumentPosition))
{
return null;
}

if (!_documentMappingService.TryMapToCSharpDocumentPosition(csharpDocument, hostDocumentPosition, out var mappedPosition, out _))
// Keep track of which generated C# document the request maps into so the client asks Roslyn for completions in
// that document, and the formatting step maps Roslyn's result back through the same document's source mappings.
if (!_documentMappingService.TryMapToCSharpDocumentLinePosition(codeDocument, hostDocumentPosition, out var mappedPosition, out _, out var inDeclDocument))
{
return null;
}

var generatedDocument = await context.Snapshot.GetGeneratedDocumentAsync(cancellationToken).ConfigureAwait(false);
var generatedDocument = await context.Snapshot.GetGeneratedDocumentAsync(inDeclDocument, cancellationToken).ConfigureAwait(false);
return new InlineCompletionRequestInfo(
GeneratedDocumentUri: generatedDocument.CreateSystemUri(),
Position: mappedPosition);
Position: mappedPosition,
InDeclDocument: inDeclDocument);
}

public ValueTask<FormattedInlineCompletionInfo?> FormatInlineCompletionAsync(RazorSolutionWrapper solutionInfo, DocumentId documentId, RazorFormattingOptions options, LinePositionSpan span, string text, CancellationToken cancellationToken)
public ValueTask<FormattedInlineCompletionInfo?> FormatInlineCompletionAsync(RazorSolutionWrapper solutionInfo, DocumentId documentId, bool inDeclDocument, RazorFormattingOptions options, LinePositionSpan span, string text, CancellationToken cancellationToken)
=> RunServiceAsync(
solutionInfo,
documentId,
context => FormatInlineCompletionAsync(context, options, span, text, cancellationToken),
context => FormatInlineCompletionAsync(context, inDeclDocument, options, span, text, cancellationToken),
cancellationToken);

private async ValueTask<FormattedInlineCompletionInfo?> FormatInlineCompletionAsync(RemoteDocumentContext context, RazorFormattingOptions options, LinePositionSpan span, string text, CancellationToken cancellationToken)
private async ValueTask<FormattedInlineCompletionInfo?> FormatInlineCompletionAsync(RemoteDocumentContext context, bool inDeclDocument, RazorFormattingOptions options, LinePositionSpan span, string text, CancellationToken cancellationToken)
{
var codeDocument = await context.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
var csharpDocument = codeDocument.GetRequiredImplCSharpDocument();
var csharpDocument = codeDocument.GetRequiredCSharpDocument(inDeclDocument);

if (!_documentMappingService.TryMapToRazorDocumentRange(csharpDocument, span, out var razorRange))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.CodeAnalysis.LanguageServer;
using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Shared.Extensions;
Expand All @@ -18,13 +19,28 @@ internal sealed class CSharpSpellCheckRangeProvider() : ICSharpSpellCheckRangePr
{
public async Task<ImmutableArray<SpellCheckRange>> GetCSharpSpellCheckRangesAsync(RemoteDocumentContext documentContext, CancellationToken cancellationToken)
{
// We have a razor document, lets find the generated C# document
var snapshot = documentContext.Snapshot;
var generatedDocument = await snapshot.GetGeneratedDocumentAsync(cancellationToken).ConfigureAwait(false);
using var ranges = new PooledArrayBuilder<SpellCheckRange>();

var csharpRanges = await GetSpellCheckSpansAsync(generatedDocument, cancellationToken).ConfigureAwait(false);
// Spell-check spans can come from either generated C# document. Keep track of which document produced each range
// so the service can map it back through the matching source mappings later.
var implGeneratedDocument = await snapshot.GetGeneratedDocumentAsync(declarationDocument: false, cancellationToken).ConfigureAwait(false);
var implCsharpRanges = await GetSpellCheckSpansAsync(implGeneratedDocument, cancellationToken).ConfigureAwait(false);
foreach (var range in implCsharpRanges)
{
ranges.Add(new((int)range.Kind, range.StartIndex, range.Length, InDeclDocument: false));
}

if (await snapshot.TryGetGeneratedDocumentAsync(declarationDocument: true, cancellationToken).ConfigureAwait(false) is { } declGeneratedDocument)
{
var declCsharpRanges = await GetSpellCheckSpansAsync(declGeneratedDocument, cancellationToken).ConfigureAwait(false);
foreach (var range in declCsharpRanges)
{
ranges.Add(new((int)range.Kind, range.StartIndex, range.Length, InDeclDocument: true));
}
}

return csharpRanges.SelectAsArray(static r => new SpellCheckRange((int)r.Kind, r.StartIndex, r.Length));
return ranges.ToImmutable();
}

private static async Task<ImmutableArray<SpellCheckSpan>> GetSpellCheckSpansAsync(Document document, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

namespace Microsoft.CodeAnalysis.Remote.Razor.SpellCheck;

internal readonly record struct SpellCheckRange(int Kind, int AbsoluteStartIndex, int Length);
internal readonly record struct SpellCheckRange(int Kind, int AbsoluteStartIndex, int Length, bool InDeclDocument);
Loading