Skip to content
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 @@ -6,7 +6,7 @@
using BenchmarkDotNet.Attributes;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Syntax;
using Microsoft.CodeAnalysis.Razor.Completion;
using Microsoft.CodeAnalysis.Remote.Razor.Completion;
using Roslyn.LanguageServer.Protocol;

namespace Microsoft.AspNetCore.Razor.Microbenchmarks.Serialization;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public ImmutableArray<Registration> GetRegistrations(VSInternalClientCapabilitie

var rewrittenResponse = DelegatedCompletionHelper.RewriteHtmlResponse(result, razorCompletionOptions);

var resolutionContext = new DelegatedCompletionResolutionContext(RazorLanguageKind.Html, rewrittenResponse.Data ?? rewrittenResponse.ItemDefaults?.Data, ProvisionalTextEdit: null);
var resolutionContext = new DelegatedCompletionResolutionContext(RazorLanguageKind.Html, rewrittenResponse.Data ?? rewrittenResponse.ItemDefaults?.Data, ProvisionalTextEdit: null, InDeclDocument: false);
var resultId = _completionListCache.Add(rewrittenResponse, resolutionContext);
rewrittenResponse.SetResultId(resultId, _clientCapabilitiesService.ClientCapabilities);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Syntax;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.LanguageServer.Handler.Completion;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.Formatting;
Expand Down Expand Up @@ -344,13 +345,18 @@ static bool IsAfterColon(string? attributeName, int nameSpanStart, int absoluteI
public static async Task<VSInternalCompletionItem> FormatCSharpCompletionItemAsync(
VSInternalCompletionItem resolvedCompletionItem,
DocumentContext documentContext,
Solution solution,
bool declarationDocument,
RazorFormattingOptions options,
IRazorFormattingService formattingService,
IDocumentMappingService documentMappingService,
bool supportsVisualStudioExtensions,
ILogger logger,
CancellationToken cancellationToken)
{
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
var csharpDocument = codeDocument.GetRequiredCSharpDocument(declarationDocument);

// In VS Code, Roslyn does resolve via a custom command. Thats fine, but we have to modify the text edit sitting within it,
// rather than the one LSP knows about.
if (resolvedCompletionItem.Command is { CommandIdentifier: CompletionResultFactory.CompleteComplexEditCommand, Arguments: var args })
Expand All @@ -363,9 +369,18 @@ public static async Task<VSInternalCompletionItem> FormatCSharpCompletionItemAsy
}

// In cohosting case, command parameters will be of the correct types (or deserialized by now in LSP case)
if (args is [TextDocumentIdentifier, TextEdit complexEdit, _, int nextCursorPosition])
if (args is [TextDocumentIdentifier textDocumentIdentifier, TextEdit complexEdit, _, int nextCursorPosition])
{
var formattedTextEdit = await FormatTextEditsAsync([complexEdit], documentContext, options, formattingService, cancellationToken).ConfigureAwait(false);
var commandGeneratedDocumentUri = textDocumentIdentifier.DocumentUri.GetRequiredSystemUri();
// Just in case the edit is for a different document, however unlikely, we'll use the uri as the source of truth
if (!codeDocument.TryGetCSharpDocumentForGeneratedUri(solution, commandGeneratedDocumentUri, out var commandCSharpDocument))
{
logger.LogError($"Unable to find a generated Razor C# document for URI '{commandGeneratedDocumentUri}'.");
resolvedCompletionItem.Command = null;
return resolvedCompletionItem;
}

var formattedTextEdit = await FormatTextEditsAsync([complexEdit], documentContext, commandCSharpDocument, options, formattingService, cancellationToken).ConfigureAwait(false);
if (formattedTextEdit is null)
{
resolvedCompletionItem.Command = null;
Expand All @@ -380,8 +395,7 @@ public static async Task<VSInternalCompletionItem> FormatCSharpCompletionItemAsy
if (nextCursorPosition >= 0)
{
// nextCursorPosition is where VS Code will navigate to, so we translate it to our document, or set to 0 to do nothing.
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
args[3] = documentMappingService.TryMapToRazorDocumentPosition(codeDocument.GetRequiredImplCSharpDocument(), nextCursorPosition, out _, out nextCursorPosition)
args[3] = documentMappingService.TryMapToRazorDocumentPosition(commandCSharpDocument, nextCursorPosition, out _, out nextCursorPosition)
? nextCursorPosition
: 0;
}
Expand All @@ -405,7 +419,7 @@ public static async Task<VSInternalCompletionItem> FormatCSharpCompletionItemAsy
{
if (resolvedCompletionItem.TextEdit.Value.TryGetFirst(out var textEdit))
{
var formattedTextChange = await FormatTextEditsAsync([textEdit], documentContext, options, formattingService, cancellationToken).ConfigureAwait(false);
var formattedTextChange = await FormatTextEditsAsync([textEdit], documentContext, csharpDocument, options, formattingService, cancellationToken).ConfigureAwait(false);
if (formattedTextChange is not null)
{
resolvedCompletionItem.TextEdit = formattedTextChange;
Expand All @@ -421,7 +435,7 @@ public static async Task<VSInternalCompletionItem> FormatCSharpCompletionItemAsy

if (resolvedCompletionItem.AdditionalTextEdits is not null)
{
var formattedTextChange = await FormatTextEditsAsync(resolvedCompletionItem.AdditionalTextEdits, documentContext, options, formattingService, cancellationToken).ConfigureAwait(false);
var formattedTextChange = await FormatTextEditsAsync(resolvedCompletionItem.AdditionalTextEdits, documentContext, csharpDocument, options, formattingService, cancellationToken).ConfigureAwait(false);
resolvedCompletionItem.AdditionalTextEdits = formattedTextChange is { } change ? [change] : null;
}

Expand All @@ -438,16 +452,15 @@ static string GetArgumentTypesLogString(VSInternalCompletionItem resolvedComplet
}
}

private static async Task<TextEdit?> FormatTextEditsAsync(TextEdit[] textEdits, DocumentContext documentContext, RazorFormattingOptions options, IRazorFormattingService formattingService, CancellationToken cancellationToken)
private static async Task<TextEdit?> FormatTextEditsAsync(TextEdit[] textEdits, DocumentContext documentContext, RazorCSharpDocument csharpDocument, RazorFormattingOptions options, IRazorFormattingService formattingService, CancellationToken cancellationToken)
{
var sourceText = await documentContext.GetSourceTextAsync(cancellationToken).ConfigureAwait(false);
var csharpSourceText = await documentContext.GetCSharpSourceTextAsync(cancellationToken).ConfigureAwait(false);

var changes = textEdits.SelectAsArray(csharpSourceText.GetTextChange);
var changes = textEdits.SelectAsArray(csharpDocument.Text.GetTextChange);
var formattedTextChange = await formattingService.TryGetCSharpSnippetFormattingEditAsync(
documentContext,
changes,
declarationDocument: false, // PROTOTYPE(sonic): Pass in the right value to this
csharpDocument.IsDeclarationDocument,
options,
cancellationToken).ConfigureAwait(false);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
<Namespace>$(ProjectName).Resources</Namespace>
</EmbeddedResource>

<EmbeddedResource Update="Resources\HtmlDescriptions.resx">
<LogicalName>Microsoft.CodeAnalysis.Razor.Workspaces.Resources.HtmlDescriptions.resources</LogicalName>
</EmbeddedResource>

<Using Include="$(ProjectName).Resources" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ namespace Microsoft.CodeAnalysis.Razor.Protocol;
internal record DelegatedCompletionResolutionContext(
[property: JsonPropertyName("projectedKind")] RazorLanguageKind ProjectedKind,
[property: JsonPropertyName("originalCompletionListData")] object? OriginalCompletionListData,
[property: JsonPropertyName("provisionalTextEdit")] TextEdit? ProvisionalTextEdit) : ICompletionResolveContext;
[property: JsonPropertyName("provisionalTextEdit")] TextEdit? ProvisionalTextEdit,
[property: JsonPropertyName("inDeclDocument")] bool InDeclDocument) : ICompletionResolveContext;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading