This commit is contained in:
David Wengier 2024-09-05 13:09:33 +10:00
Родитель a795f32f17
Коммит d87ad016dd
5 изменённых файлов: 17 добавлений и 21 удалений

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

@ -41,11 +41,7 @@ internal class DocumentOnTypeFormattingEndpoint(
public void ApplyCapabilities(VSInternalServerCapabilities serverCapabilities, VSInternalClientCapabilities clientCapabilities)
{
serverCapabilities.DocumentOnTypeFormattingProvider = new DocumentOnTypeFormattingOptions
{
FirstTriggerCharacter = RazorFormattingService.AllTriggerCharacters[0],
MoreTriggerCharacter = RazorFormattingService.AllTriggerCharacters.AsSpan()[1..].ToArray(),
};
serverCapabilities.DocumentOnTypeFormattingProvider = new DocumentOnTypeFormattingOptions().EnableOnTypeFormattingTriggerCharacters();
}
public TextDocumentIdentifier GetTextDocumentIdentifier(DocumentOnTypeFormattingParams request)

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

@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.SemanticTokens;
using Microsoft.VisualStudio.LanguageServer.Protocol;
@ -84,4 +85,12 @@ internal static class LspInitializationHelpers
return options;
}
public static DocumentOnTypeFormattingOptions EnableOnTypeFormattingTriggerCharacters(this DocumentOnTypeFormattingOptions options)
{
options.FirstTriggerCharacter = RazorFormattingService.FirstTriggerCharacter;
options.MoreTriggerCharacter = RazorFormattingService.MoreTriggerCharacters.ToArray();
return options;
}
}

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

@ -23,8 +23,9 @@ namespace Microsoft.CodeAnalysis.Razor.Formatting;
internal class RazorFormattingService : IRazorFormattingService
{
public static readonly ImmutableArray<string> AllTriggerCharacters = ["}", ";", "\n", "{"];
public static readonly FrozenSet<string> AllTriggerCharacterSet = AllTriggerCharacters.ToFrozenSet(StringComparer.Ordinal);
public static readonly string FirstTriggerCharacter = "}";
public static readonly ImmutableArray<string> MoreTriggerCharacters = [";", "\n", "{"];
public static readonly FrozenSet<string> AllTriggerCharacterSet = FrozenSet.ToFrozenSet([FirstTriggerCharacter, .. MoreTriggerCharacters], StringComparer.Ordinal);
private static readonly FrozenSet<string> s_csharpTriggerCharacterSet = FrozenSet.ToFrozenSet(["}", ";"], StringComparer.Ordinal);
private static readonly FrozenSet<string> s_htmlTriggerCharacterSet = FrozenSet.ToFrozenSet(["\n", "{", "}", ";"], StringComparer.Ordinal);

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

@ -8,6 +8,7 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor;
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost;
using Microsoft.CodeAnalysis.Razor.Formatting;
@ -55,9 +56,7 @@ internal sealed class CohostOnTypeFormattingEndpoint(
RegisterOptions = new DocumentOnTypeFormattingRegistrationOptions()
{
DocumentSelector = filter,
FirstTriggerCharacter = RazorFormattingService.AllTriggerCharacters[0],
MoreTriggerCharacter = RazorFormattingService.AllTriggerCharacters.AsSpan()[1..].ToArray(),
}
}.EnableOnTypeFormattingTriggerCharacters()
};
}

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

@ -46,7 +46,7 @@ public class Foo{}
{
foreach (var character in RazorFormattingService.TestAccessor.GetCSharpTriggerCharacterSet())
{
Assert.Contains(character, RazorFormattingService.AllTriggerCharacters);
Assert.Contains(character, RazorFormattingService.AllTriggerCharacterSet);
}
}
@ -55,16 +55,7 @@ public class Foo{}
{
foreach (var character in RazorFormattingService.TestAccessor.GetHtmlTriggerCharacterSet())
{
Assert.Contains(character, RazorFormattingService.AllTriggerCharacters);
Assert.Contains(character, RazorFormattingService.AllTriggerCharacterSet);
}
}
[Fact]
public void AllTriggerCharacters_ContainsUniqueCharacters()
{
var allChars = RazorFormattingService.AllTriggerCharacters;
var distinctChars = allChars.Distinct().ToArray();
Assert.Equal(distinctChars, allChars);
}
}