This commit is contained in:
David Wengier 2024-10-24 14:32:07 +11:00
Родитель 3eacfbd845
Коммит 0f8af98913
5 изменённых файлов: 29 добавлений и 27 удалений

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

@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Razor.LanguageServer.CodeActions.Models;
using Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.VisualStudio.LanguageServer.Protocol;
@ -55,7 +56,7 @@ internal sealed class DefaultHtmlCodeActionProvider(IEditMappingService editMapp
foreach (var edit in documentEdits)
{
edit.Edits = HtmlFormatter.FixHtmlTextEdits(htmlSourceText, edit.Edits);
edit.Edits = FormattingUtilities.FixHtmlTextEdits(htmlSourceText, edit.Edits);
}
codeAction.Edit = new WorkspaceEdit

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

@ -81,22 +81,4 @@ internal sealed class HtmlFormatter(
var sourceText = await documentSnapshot.GetTextAsync(cancellationToken).ConfigureAwait(false);
return result.Edits.SelectAsArray(sourceText.GetTextChange);
}
/// <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>
// Internal for testing
public static TextEdit[] FixHtmlTextEdits(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.Select(htmlSourceText.GetTextEdit)];
}
}

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

@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Razor.Language.Syntax;
using Microsoft.AspNetCore.Razor.LanguageServer.EndpointContracts;
using Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.Logging;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Text;
@ -122,7 +123,7 @@ internal class WrapWithTagEndpoint(IClientConnection clientConnection, ILoggerFa
if (htmlResponse.TextEdits is not null)
{
var htmlSourceText = await documentContext.GetHtmlSourceTextAsync(cancellationToken).ConfigureAwait(false);
htmlResponse.TextEdits = HtmlFormatter.FixHtmlTextEdits(htmlSourceText, htmlResponse.TextEdits);
htmlResponse.TextEdits = FormattingUtilities.FixHtmlTextEdits(htmlSourceText, htmlResponse.TextEdits);
}
return htmlResponse;

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

@ -2,11 +2,13 @@
// 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.Linq;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.CodeAnalysis.Text;
using Roslyn.LanguageServer.Protocol;
using Microsoft.VisualStudio.LanguageServer.Protocol;
namespace Microsoft.CodeAnalysis.Razor.Formatting;
@ -260,4 +262,22 @@ internal static class FormattingUtilities
return builder.DrainToImmutable();
}
}
/// <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>
// Internal for testing
public static TextEdit[] FixHtmlTextEdits(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.Select(htmlSourceText.GetTextEdit)];
}
}

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

@ -3,13 +3,11 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
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.Protocol;
using Microsoft.VisualStudio.LanguageServer.Protocol;
@ -302,7 +300,7 @@ public class WrapWithTagEndpointTest(ITestOutputHelper testOutput) : LanguageSer
};
var htmlSourceText = await context.GetHtmlSourceTextAsync(DisposalToken);
var edits = HtmlFormatter.FixHtmlTextEdits(htmlSourceText, computedEdits);
var edits = FormattingUtilities.FixHtmlTextEdits(htmlSourceText, computedEdits);
Assert.Same(computedEdits, edits);
var finalText = inputSourceText.WithChanges(edits.Select(inputSourceText.GetTextChange));
@ -342,7 +340,7 @@ public class WrapWithTagEndpointTest(ITestOutputHelper testOutput) : LanguageSer
};
var htmlSourceText = await context.GetHtmlSourceTextAsync(DisposalToken);
var edits = HtmlFormatter.FixHtmlTextEdits(htmlSourceText, computedEdits);
var edits = FormattingUtilities.FixHtmlTextEdits(htmlSourceText, computedEdits);
Assert.NotSame(computedEdits, edits);
var finalText = inputSourceText.WithChanges(edits.Select(inputSourceText.GetTextChange));
@ -382,7 +380,7 @@ public class WrapWithTagEndpointTest(ITestOutputHelper testOutput) : LanguageSer
};
var htmlSourceText = await context.GetHtmlSourceTextAsync(DisposalToken);
var edits = HtmlFormatter.FixHtmlTextEdits(htmlSourceText, computedEdits);
var edits = FormattingUtilities.FixHtmlTextEdits(htmlSourceText, computedEdits);
Assert.NotSame(computedEdits, edits);
var finalText = inputSourceText.WithChanges(edits.Select(inputSourceText.GetTextChange));