diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/AutoInsert/OnAutoInsertEndpoint.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/AutoInsert/OnAutoInsertEndpoint.cs index d71c507cb57..bdad71ad41b 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/AutoInsert/OnAutoInsertEndpoint.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/AutoInsert/OnAutoInsertEndpoint.cs @@ -143,7 +143,7 @@ public void ApplyCapabilities(VSInternalServerCapabilities serverCapabilities, V // Therefore we are just going to no-op if the user has turned off on type formatting. Maybe one day we can make this // smarter, but at least the user can always turn the setting back on, type their "///", and turn it back off, without // having to restart VS. Not the worst compromise (hopefully!) - if (!_optionsMonitor.CurrentValue.FormatOnType) + if (!_optionsMonitor.CurrentValue.Formatting.IsOnTypeEnabled()) { Logger.LogInformation($"Formatting on type disabled, so auto insert is a no-op for C#."); return SpecializedTasks.Null(); diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/DefaultRazorConfigurationService.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/DefaultRazorConfigurationService.cs index 6010dde3ebd..d7d3abcab15 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/DefaultRazorConfigurationService.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/DefaultRazorConfigurationService.cs @@ -96,10 +96,10 @@ internal RazorLSPOptions BuildOptions(JsonObject[] result) } else { - ExtractVSCodeOptions(result, out var enableFormatting, out var autoClosingTags, out var commitElementsWithSpace, out var codeBlockBraceOnNextLine); + ExtractVSCodeOptions(result, out var formatting, out var autoClosingTags, out var commitElementsWithSpace, out var codeBlockBraceOnNextLine); return RazorLSPOptions.Default with { - EnableFormatting = enableFormatting, + Formatting = formatting, AutoClosingTags = autoClosingTags, CommitElementsWithSpace = commitElementsWithSpace, CodeBlockBraceOnNextLine = codeBlockBraceOnNextLine @@ -109,7 +109,7 @@ internal RazorLSPOptions BuildOptions(JsonObject[] result) private void ExtractVSCodeOptions( JsonObject[] result, - out bool enableFormatting, + out FormattingFlags formatting, out bool autoClosingTags, out bool commitElementsWithSpace, out bool codeBlockBraceOnNextLine) @@ -117,7 +117,7 @@ private void ExtractVSCodeOptions( var razor = result[0]; var html = result[1]; - enableFormatting = RazorLSPOptions.Default.EnableFormatting; + formatting = RazorLSPOptions.Default.Formatting; autoClosingTags = RazorLSPOptions.Default.AutoClosingTags; codeBlockBraceOnNextLine = RazorLSPOptions.Default.CodeBlockBraceOnNextLine; // Deliberately not using the "default" here because we want a different default for VS Code, as @@ -130,7 +130,15 @@ private void ExtractVSCodeOptions( if (parsedFormat.TryGetPropertyValue("enable", out var parsedEnableFormatting) && parsedEnableFormatting is not null) { - enableFormatting = GetObjectOrDefault(parsedEnableFormatting, enableFormatting); + var formattingEnabled = GetObjectOrDefault(parsedEnableFormatting, formatting.IsEnabled()); + if (formattingEnabled) + { + formatting |= FormattingFlags.Enabled; + } + else + { + formatting = FormattingFlags.Disabled; + } } if (parsedFormat.TryGetPropertyValue("codeBlockBraceOnNextLine", out var parsedCodeBlockBraceOnNextLine) && diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentFormattingEndpoint.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentFormattingEndpoint.cs index 1564a8d4129..6358afb3c34 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentFormattingEndpoint.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentFormattingEndpoint.cs @@ -37,7 +37,7 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(DocumentFormattingParams public async Task HandleRequestAsync(DocumentFormattingParams request, RazorRequestContext requestContext, CancellationToken cancellationToken) { - if (!_optionsMonitor.CurrentValue.EnableFormatting) + if (!_optionsMonitor.CurrentValue.Formatting.IsEnabled()) { return null; } diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentOnTypeFormattingEndpoint.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentOnTypeFormattingEndpoint.cs index 04c740f1e6e..df1062b30f7 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentOnTypeFormattingEndpoint.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentOnTypeFormattingEndpoint.cs @@ -49,13 +49,13 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(DocumentOnTypeFormatting { _logger.LogInformation($"Starting OnTypeFormatting request for {request.TextDocument.Uri}."); - if (!_optionsMonitor.CurrentValue.EnableFormatting) + if (!_optionsMonitor.CurrentValue.Formatting.IsEnabled()) { _logger.LogInformation($"Formatting option disabled."); return null; } - if (!_optionsMonitor.CurrentValue.FormatOnType) + if (!_optionsMonitor.CurrentValue.Formatting.IsOnTypeEnabled()) { _logger.LogInformation($"Formatting on type disabled."); return null; diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentRangeFormattingEndpoint.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentRangeFormattingEndpoint.cs index 7ba97f9f9fb..da8823a0f2a 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentRangeFormattingEndpoint.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/DocumentRangeFormattingEndpoint.cs @@ -37,7 +37,7 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(DocumentRangeFormattingP public async Task HandleRequestAsync(DocumentRangeFormattingParams request, RazorRequestContext requestContext, CancellationToken cancellationToken) { - if (!_optionsMonitor.CurrentValue.EnableFormatting) + if (!_optionsMonitor.CurrentValue.Formatting.IsEnabled()) { return null; } @@ -54,6 +54,16 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(DocumentRangeFormattingP return null; } + if (request.Options.OtherOptions is not null && + request.Options.OtherOptions.TryGetValue("fromPaste", out var fromPasteObj) && + fromPasteObj is bool fromPaste) + { + if (fromPaste && !_optionsMonitor.CurrentValue.Formatting.IsOnPasteEnabled()) + { + return null; + } + } + var options = RazorFormattingOptions.From(request.Options, _optionsMonitor.CurrentValue.CodeBlockBraceOnNextLine); var htmlChanges = await _htmlFormatter.GetDocumentFormattingEditsAsync(documentContext.Snapshot, documentContext.Uri, request.Options, cancellationToken).ConfigureAwait(false); diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Hosting/FormattingFlagExtensions.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Hosting/FormattingFlagExtensions.cs new file mode 100644 index 00000000000..0ac02c7ba6c --- /dev/null +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Hosting/FormattingFlagExtensions.cs @@ -0,0 +1,16 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT license. See License.txt in the project root for license information. + +namespace Microsoft.AspNetCore.Razor.LanguageServer.Hosting; + +internal static class FormattingFlagExtensions +{ + public static bool IsEnabled(this FormattingFlags flags) + => flags.IsFlagSet(FormattingFlags.Enabled); + + public static bool IsOnTypeEnabled(this FormattingFlags flags) + => flags.IsEnabled() && flags.IsFlagSet(FormattingFlags.OnType); + + public static bool IsOnPasteEnabled(this FormattingFlags flags) + => flags.IsEnabled() && flags.IsFlagSet(FormattingFlags.OnPaste); +} diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Hosting/FormattingFlags.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Hosting/FormattingFlags.cs new file mode 100644 index 00000000000..b14073c22e3 --- /dev/null +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Hosting/FormattingFlags.cs @@ -0,0 +1,16 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT license. See License.txt in the project root for license information. + +using System; + +namespace Microsoft.AspNetCore.Razor.LanguageServer.Hosting; + +[Flags] +internal enum FormattingFlags +{ + Disabled = 0, + Enabled = 1, + OnPaste = 1 << 1, + OnType = 1 << 2, + All = Enabled | OnPaste | OnType +}; diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Hosting/RazorLSPOptions.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Hosting/RazorLSPOptions.cs index 1931a4a1055..c8d1a284810 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Hosting/RazorLSPOptions.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Hosting/RazorLSPOptions.cs @@ -1,30 +1,29 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT license. See License.txt in the project root for license information. +using System; using Microsoft.CodeAnalysis.Razor.Settings; namespace Microsoft.AspNetCore.Razor.LanguageServer.Hosting; internal record RazorLSPOptions( - bool EnableFormatting, + FormattingFlags Formatting, bool AutoClosingTags, bool InsertSpaces, int TabSize, bool AutoShowCompletion, bool AutoListParams, - bool FormatOnType, bool AutoInsertAttributeQuotes, bool ColorBackground, bool CodeBlockBraceOnNextLine, bool CommitElementsWithSpace) { - public readonly static RazorLSPOptions Default = new(EnableFormatting: true, + public readonly static RazorLSPOptions Default = new(Formatting: FormattingFlags.All, AutoClosingTags: true, AutoListParams: true, InsertSpaces: true, TabSize: 4, AutoShowCompletion: true, - FormatOnType: true, AutoInsertAttributeQuotes: true, ColorBackground: false, CodeBlockBraceOnNextLine: false, @@ -35,15 +34,30 @@ internal record RazorLSPOptions( /// not defined in client settings. /// internal static RazorLSPOptions From(ClientSettings settings) - => new(Default.EnableFormatting, + => new(GetFormattingFlags(settings), settings.AdvancedSettings.AutoClosingTags, !settings.ClientSpaceSettings.IndentWithTabs, settings.ClientSpaceSettings.IndentSize, settings.ClientCompletionSettings.AutoShowCompletion, settings.ClientCompletionSettings.AutoListParams, - settings.AdvancedSettings.FormatOnType, settings.AdvancedSettings.AutoInsertAttributeQuotes, settings.AdvancedSettings.ColorBackground, settings.AdvancedSettings.CodeBlockBraceOnNextLine, settings.AdvancedSettings.CommitElementsWithSpace); + + private static FormattingFlags GetFormattingFlags(ClientSettings settings) + { + var flags = FormattingFlags.Enabled; + if (settings.AdvancedSettings.FormatOnPaste) + { + flags |= FormattingFlags.OnPaste; + } + + if (settings.AdvancedSettings.FormatOnType) + { + flags |= FormattingFlags.OnType; + } + + return flags; + } } diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Settings/ClientSettings.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Settings/ClientSettings.cs index 568b7f38da7..f0e9036a619 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Settings/ClientSettings.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Settings/ClientSettings.cs @@ -32,7 +32,7 @@ internal sealed record ClientSpaceSettings(bool IndentWithTabs, int IndentSize) public int IndentSize { get; } = IndentSize >= 0 ? IndentSize : throw new ArgumentOutOfRangeException(nameof(IndentSize)); } -internal sealed record ClientAdvancedSettings(bool FormatOnType, bool AutoClosingTags, bool AutoInsertAttributeQuotes, bool ColorBackground, bool CodeBlockBraceOnNextLine, bool CommitElementsWithSpace, SnippetSetting SnippetSetting, LogLevel LogLevel) +internal sealed record ClientAdvancedSettings(bool FormatOnType, bool AutoClosingTags, bool AutoInsertAttributeQuotes, bool ColorBackground, bool CodeBlockBraceOnNextLine, bool CommitElementsWithSpace, SnippetSetting SnippetSetting, LogLevel LogLevel, bool FormatOnPaste) { - public static readonly ClientAdvancedSettings Default = new(FormatOnType: true, AutoClosingTags: true, AutoInsertAttributeQuotes: true, ColorBackground: false, CodeBlockBraceOnNextLine: false, CommitElementsWithSpace: true, SnippetSetting.All, LogLevel.Warning); + public static readonly ClientAdvancedSettings Default = new(FormatOnType: true, AutoClosingTags: true, AutoInsertAttributeQuotes: true, ColorBackground: false, CodeBlockBraceOnNextLine: false, CommitElementsWithSpace: true, SnippetSetting.All, LogLevel.Warning, FormatOnPaste: true); } diff --git a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Cohost/CohostRangeFormattingEndpoint.cs b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Cohost/CohostRangeFormattingEndpoint.cs index 610679471c7..a849a6d05c5 100644 --- a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Cohost/CohostRangeFormattingEndpoint.cs +++ b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Cohost/CohostRangeFormattingEndpoint.cs @@ -70,6 +70,14 @@ public ImmutableArray GetRegistrations(VSInternalClientCapabilitie private async Task HandleRequestAsync(DocumentRangeFormattingParams request, TextDocument razorDocument, CancellationToken cancellationToken) { + if (request.Options.OtherOptions is not null && request.Options.OtherOptions.TryGetValue("fromPaste", out var fromPasteObj) && fromPasteObj is bool fromPaste) + { + if (fromPaste && !_clientSettingsManager.GetClientSettings().AdvancedSettings.FormatOnPaste) + { + return null; + } + } + _logger.LogDebug($"Getting Html formatting changes for {razorDocument.FilePath}"); var htmlResult = await TryGetHtmlFormattingEditsAsync(request, razorDocument, cancellationToken).ConfigureAwait(false); diff --git a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Options/OptionsStorage.cs b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Options/OptionsStorage.cs index e49ecb22145..5e28a880bae 100644 --- a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Options/OptionsStorage.cs +++ b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Options/OptionsStorage.cs @@ -77,6 +77,12 @@ public LogLevel LogLevel set => SetInt(SettingsNames.LogLevel.LegacyName, (int)value); } + public bool FormatOnPaste + { + get => GetBool(SettingsNames.FormatOnPaste.LegacyName, defaultValue: true); + set => SetBool(SettingsNames.FormatOnPaste.LegacyName, value); + } + [ImportingConstructor] public OptionsStorage( SVsServiceProvider synchronousServiceProvider, @@ -107,7 +113,7 @@ public async Task OnChangedAsync(Action changed) private EventHandler? _changed; - public ClientAdvancedSettings GetAdvancedSettings() => new(FormatOnType, AutoClosingTags, AutoInsertAttributeQuotes, ColorBackground, CodeBlockBraceOnNextLine, CommitElementsWithSpace, Snippets, LogLevel); + public ClientAdvancedSettings GetAdvancedSettings() => new(FormatOnType, AutoClosingTags, AutoInsertAttributeQuotes, ColorBackground, CodeBlockBraceOnNextLine, CommitElementsWithSpace, Snippets, LogLevel, FormatOnPaste); public bool GetBool(string name, bool defaultValue) { diff --git a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Options/SettingsNames.cs b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Options/SettingsNames.cs index 75d10a365a9..0aba1107461 100644 --- a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Options/SettingsNames.cs +++ b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Options/SettingsNames.cs @@ -18,6 +18,7 @@ public record Setting(string LegacyName, string UnifiedName); public static readonly Setting CommitElementsWithSpace = new("CommitElementsWithSpace", UnifiedCollection + ".commitCharactersWithSpace"); public static readonly Setting Snippets = new("Snippets", UnifiedCollection + ".snippets"); public static readonly Setting LogLevel = new("LogLevel", UnifiedCollection + ".logLevel"); + public static readonly Setting FormatOnPaste = new("FormatOnPaste", UnifiedCollection + ".formatOnPaste"); public static readonly Setting[] AllSettings = [ @@ -29,5 +30,6 @@ public record Setting(string LegacyName, string UnifiedName); CommitElementsWithSpace, Snippets, LogLevel, + FormatOnPaste, ]; } diff --git a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/Options/AdvancedOptionPage.cs b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/Options/AdvancedOptionPage.cs index f8a290761a7..264135fa679 100644 --- a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/Options/AdvancedOptionPage.cs +++ b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/Options/AdvancedOptionPage.cs @@ -25,6 +25,7 @@ internal class AdvancedOptionPage : DialogPage private bool? _commitElementsWithSpace; private SnippetSetting? _snippets; private LogLevel? _logLevel; + private bool? _formatOnPaste; public AdvancedOptionPage() { @@ -91,6 +92,15 @@ public bool CodeBlockBraceOnNextLine set => _codeBlockBraceOnNextLine = value; } + [LocCategory(nameof(VSPackage.Formatting))] + [LocDescription(nameof(VSPackage.Setting_FormattingOnPasteDescription))] + [LocDisplayName(nameof(VSPackage.Setting_FormattingOnPasteDisplayName))] + public bool FormatOnPaste + { + get => _formatOnPaste ?? true; + set => _formatOnPaste = value; + } + [LocCategory(nameof(VSPackage.Completion))] [LocDescription(nameof(VSPackage.Setting_SnippetsDescription))] [LocDisplayName(nameof(VSPackage.Setting_SnippetsDisplayName))] diff --git a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/UnifiedSettings/razor.registration.json b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/UnifiedSettings/razor.registration.json index 46dc8be958d..3420468a7db 100644 --- a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/UnifiedSettings/razor.registration.json +++ b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/UnifiedSettings/razor.registration.json @@ -17,179 +17,192 @@ "path": "Razor\\FormatOnType" } } - } - }, - "textEditor.razor.advanced.autoClosingTags": { - "type": "boolean", - "default": true, - "title": "@Setting_AutoClosingTagsDisplayName;{13b72f58-279e-49e0-a56d-296be02f0805}", - "description": "@Setting_AutoClosingTagsDescription;{13b72f58-279e-49e0-a56d-296be02f0805}", - "migration": { - "pass": { - "input": { - "store": "VsUserSettingsRegistry", - "path": "Razor\\AutoClosingTags" + }, + "textEditor.razor.advanced.formatOnPaste": { + "type": "boolean", + "default": true, + "title": "@Setting_FormattingOnPasteDisplayName;{13b72f58-279e-49e0-a56d-296be02f0805}", + "description": "@Setting_FormattingOnPasteDescription;{13b72f58-279e-49e0-a56d-296be02f0805}", + "migration": { + "pass": { + "input": { + "store": "VsUserSettingsRegistry", + "path": "Razor\\FormatOnPaste" + } } } - } - }, - "textEditor.razor.advanced.autoInsertAttributeQuotes": { - "type": "boolean", - "default": true, - "title": "@Setting_AutoInsertAttributeQuotesDisplayName;{13b72f58-279e-49e0-a56d-296be02f0805}", - "description": "@Setting_AutoInsertAttributeQuotesDescription;{13b72f58-279e-49e0-a56d-296be02f0805}", - "migration": { - "pass": { - "input": { - "store": "VsUserSettingsRegistry", - "path": "Razor\\AutoInsertAttributeQuotes" + }, + "textEditor.razor.advanced.autoClosingTags": { + "type": "boolean", + "default": true, + "title": "@Setting_AutoClosingTagsDisplayName;{13b72f58-279e-49e0-a56d-296be02f0805}", + "description": "@Setting_AutoClosingTagsDescription;{13b72f58-279e-49e0-a56d-296be02f0805}", + "migration": { + "pass": { + "input": { + "store": "VsUserSettingsRegistry", + "path": "Razor\\AutoClosingTags" + } } } - } - }, - "textEditor.razor.advanced.colorBackground": { - "type": "boolean", - "default": false, - "title": "@Setting_ColorBackgroundDisplayName;{13b72f58-279e-49e0-a56d-296be02f0805}", - "description": "@Setting_ColorBackgroundDescription;{13b72f58-279e-49e0-a56d-296be02f0805}", - "migration": { - "pass": { - "input": { - "store": "VsUserSettingsRegistry", - "path": "Razor\\ColorBackground" + }, + "textEditor.razor.advanced.autoInsertAttributeQuotes": { + "type": "boolean", + "default": true, + "title": "@Setting_AutoInsertAttributeQuotesDisplayName;{13b72f58-279e-49e0-a56d-296be02f0805}", + "description": "@Setting_AutoInsertAttributeQuotesDescription;{13b72f58-279e-49e0-a56d-296be02f0805}", + "migration": { + "pass": { + "input": { + "store": "VsUserSettingsRegistry", + "path": "Razor\\AutoInsertAttributeQuotes" + } } } - } - }, - "textEditor.razor.advanced.codeBlockBraceOnNextLine": { - "type": "boolean", - "default": false, - "title": "@Setting_CodeBlockBraceOnNextLineDisplayName;{13b72f58-279e-49e0-a56d-296be02f0805}", - "description": "@Setting_CodeBlockBraceOnNextLineDescription;{13b72f58-279e-49e0-a56d-296be02f0805}", - "migration": { - "pass": { - "input": { - "store": "VsUserSettingsRegistry", - "path": "Razor\\CodeBlockBraceOnNextLined" + }, + "textEditor.razor.advanced.colorBackground": { + "type": "boolean", + "default": false, + "title": "@Setting_ColorBackgroundDisplayName;{13b72f58-279e-49e0-a56d-296be02f0805}", + "description": "@Setting_ColorBackgroundDescription;{13b72f58-279e-49e0-a56d-296be02f0805}", + "migration": { + "pass": { + "input": { + "store": "VsUserSettingsRegistry", + "path": "Razor\\ColorBackground" + } } } - } - }, - "textEditor.razor.advanced.commitElementsWithSpace": { - "type": "boolean", - "default": true, - "title": "@Setting_CommitElementsWithSpaceDisplayName;{13b72f58-279e-49e0-a56d-296be02f0805}", - "description": "@Setting_CommitElementsWithSpaceDescription;{13b72f58-279e-49e0-a56d-296be02f0805}", - "migration": { - "pass": { - "input": { - "store": "VsUserSettingsRegistry", - "path": "Razor\\CommitElementsWithSpace" + }, + "textEditor.razor.advanced.codeBlockBraceOnNextLine": { + "type": "boolean", + "default": false, + "title": "@Setting_CodeBlockBraceOnNextLineDisplayName;{13b72f58-279e-49e0-a56d-296be02f0805}", + "description": "@Setting_CodeBlockBraceOnNextLineDescription;{13b72f58-279e-49e0-a56d-296be02f0805}", + "migration": { + "pass": { + "input": { + "store": "VsUserSettingsRegistry", + "path": "Razor\\CodeBlockBraceOnNextLined" + } } } - } - }, - "textEditor.razor.advanced.snippets": { - "type": "string", - "default": "all", - "enum": [ - "all", - "custom", - "none" - ], - "enumItemLabels": [ - "@Setting_SnippetsAll;{13b72f58-279e-49e0-a56d-296be02f0805}", - "@Setting_SnippetsCustom;{13b72f58-279e-49e0-a56d-296be02f0805}", - "@Setting_SnippetsNone;{13b72f58-279e-49e0-a56d-296be02f0805}" - ], - "title": "@Setting_SnippetsDisplayName;{13b72f58-279e-49e0-a56d-296be02f0805}", - "description": "@Setting_SnippetsDescription;{13b72f58-279e-49e0-a56d-296be02f0805}", - "migration": { - "enumIntegerToString": { - "input": { - "store": "VsUserSettingsRegistry", - "path": "Razor\\Snippets" - }, - "map": [ - { - "result": "all", - "match": 0 - }, - { - "result": "custom", - "match": 1 - }, - { - "result": "none", - "match": 2 + }, + "textEditor.razor.advanced.commitElementsWithSpace": { + "type": "boolean", + "default": true, + "title": "@Setting_CommitElementsWithSpaceDisplayName;{13b72f58-279e-49e0-a56d-296be02f0805}", + "description": "@Setting_CommitElementsWithSpaceDescription;{13b72f58-279e-49e0-a56d-296be02f0805}", + "migration": { + "pass": { + "input": { + "store": "VsUserSettingsRegistry", + "path": "Razor\\CommitElementsWithSpace" } - ] + } } - } - }, - "textEditor.razor.advanced.logLevel": { - "type": "string", - "default": "warning", - "enum": [ - "trace", - "debug", - "information", - "warning", - "error", - "critical", - "none" - ], - "enumItemLabels": [ - "@Setting_LogLevelTrace;{13b72f58-279e-49e0-a56d-296be02f0805}", - "@Setting_LogLevelDebug;{13b72f58-279e-49e0-a56d-296be02f0805}", - "@Setting_LogLevelInformation;{13b72f58-279e-49e0-a56d-296be02f0805}", - "@Setting_LogLevelWarning;{13b72f58-279e-49e0-a56d-296be02f0805}", - "@Setting_LogLevelError;{13b72f58-279e-49e0-a56d-296be02f0805}", - "@Setting_LogLevelCritical;{13b72f58-279e-49e0-a56d-296be02f0805}", - "@Setting_LogLevelNone;{13b72f58-279e-49e0-a56d-296be02f0805}" - ], - "title": "@Setting_LogLevelDisplayName;{13b72f58-279e-49e0-a56d-296be02f0805}", - "description": "@Setting_LogLevelDescription;{13b72f58-279e-49e0-a56d-296be02f0805}", - "migration": { - "enumIntegerToString": { - "input": { - "store": "VsUserSettingsRegistry", - "path": "Razor\\LogLevel" - }, - "map": [ - { - "result": "trace", - "match": 0 - }, - { - "result": "debug", - "match": 1 - }, - { - "result": "information", - "match": 2 + }, + "textEditor.razor.advanced.snippets": { + "type": "string", + "default": "all", + "enum": [ + "all", + "custom", + "none" + ], + "enumItemLabels": [ + "@Setting_SnippetsAll;{13b72f58-279e-49e0-a56d-296be02f0805}", + "@Setting_SnippetsCustom;{13b72f58-279e-49e0-a56d-296be02f0805}", + "@Setting_SnippetsNone;{13b72f58-279e-49e0-a56d-296be02f0805}" + ], + "title": "@Setting_SnippetsDisplayName;{13b72f58-279e-49e0-a56d-296be02f0805}", + "description": "@Setting_SnippetsDescription;{13b72f58-279e-49e0-a56d-296be02f0805}", + "migration": { + "enumIntegerToString": { + "input": { + "store": "VsUserSettingsRegistry", + "path": "Razor\\Snippets" }, - { - "result": "warning", - "match": 3 - }, - { - "result": "error", - "match": 4 - }, - { - "result": "critical", - "match": 5 + "map": [ + { + "result": "all", + "match": 0 + }, + { + "result": "custom", + "match": 1 + }, + { + "result": "none", + "match": 2 + } + ] + } + } + }, + "textEditor.razor.advanced.logLevel": { + "type": "string", + "default": "warning", + "enum": [ + "trace", + "debug", + "information", + "warning", + "error", + "critical", + "none" + ], + "enumItemLabels": [ + "@Setting_LogLevelTrace;{13b72f58-279e-49e0-a56d-296be02f0805}", + "@Setting_LogLevelDebug;{13b72f58-279e-49e0-a56d-296be02f0805}", + "@Setting_LogLevelInformation;{13b72f58-279e-49e0-a56d-296be02f0805}", + "@Setting_LogLevelWarning;{13b72f58-279e-49e0-a56d-296be02f0805}", + "@Setting_LogLevelError;{13b72f58-279e-49e0-a56d-296be02f0805}", + "@Setting_LogLevelCritical;{13b72f58-279e-49e0-a56d-296be02f0805}", + "@Setting_LogLevelNone;{13b72f58-279e-49e0-a56d-296be02f0805}" + ], + "title": "@Setting_LogLevelDisplayName;{13b72f58-279e-49e0-a56d-296be02f0805}", + "description": "@Setting_LogLevelDescription;{13b72f58-279e-49e0-a56d-296be02f0805}", + "migration": { + "enumIntegerToString": { + "input": { + "store": "VsUserSettingsRegistry", + "path": "Razor\\LogLevel" }, - { - "result": "none", - "match": 6 - } - ] + "map": [ + { + "result": "trace", + "match": 0 + }, + { + "result": "debug", + "match": 1 + }, + { + "result": "information", + "match": 2 + }, + { + "result": "warning", + "match": 3 + }, + { + "result": "error", + "match": 4 + }, + { + "result": "critical", + "match": 5 + }, + { + "result": "none", + "match": 6 + } + ] + } } } - } - }, + }, "categories": { "textEditor.razor": { "title": "@110;{13b72f58-279e-49e0-a56d-296be02f0805}" // Razor (ASP.NET Core) diff --git a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/VSPackage.resx b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/VSPackage.resx index 7a9e0223550..9942e572b2f 100644 --- a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/VSPackage.resx +++ b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/VSPackage.resx @@ -180,6 +180,12 @@ Commit elements with space + + Format on paste + + + If true, formatting will be enabled when pasting content + If true, formatting will be enabled while typing diff --git a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.cs.xlf b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.cs.xlf index f761260c51b..302c22420fc 100644 --- a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.cs.xlf +++ b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.cs.xlf @@ -92,6 +92,16 @@ Potvrzovat elementy mezerníkem + + If true, formatting will be enabled when pasting content + If true, formatting will be enabled when pasting content + + + + Format on paste + Format on paste + + If true, formatting will be enabled while typing Pokud je hodnota true, při psaní se povolí formátování. diff --git a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.de.xlf b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.de.xlf index c3df4f2b10f..bb58cb71614 100644 --- a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.de.xlf +++ b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.de.xlf @@ -92,6 +92,16 @@ Elemente durch Drücken der LEERTASTE committen + + If true, formatting will be enabled when pasting content + If true, formatting will be enabled when pasting content + + + + Format on paste + Format on paste + + If true, formatting will be enabled while typing Bei "true" wird die Formatierung während der Eingabe aktiviert. diff --git a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.es.xlf b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.es.xlf index 61ad53589dd..cea5f3f1064 100644 --- a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.es.xlf +++ b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.es.xlf @@ -92,6 +92,16 @@ Confirmar elementos con espacio + + If true, formatting will be enabled when pasting content + If true, formatting will be enabled when pasting content + + + + Format on paste + Format on paste + + If true, formatting will be enabled while typing Si es true, el formato se habilitará al escribir diff --git a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.fr.xlf b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.fr.xlf index 5c7c6139fef..9c7b8ec7f0f 100644 --- a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.fr.xlf +++ b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.fr.xlf @@ -92,6 +92,16 @@ Valider des éléments avec de l’espace + + If true, formatting will be enabled when pasting content + If true, formatting will be enabled when pasting content + + + + Format on paste + Format on paste + + If true, formatting will be enabled while typing Si la valeur est true, la mise en forme est activée lors de la saisie diff --git a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.it.xlf b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.it.xlf index ff83731d724..e00bc3fdc57 100644 --- a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.it.xlf +++ b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.it.xlf @@ -92,6 +92,16 @@ Eseguire il commit degli elementi con lo spazio + + If true, formatting will be enabled when pasting content + If true, formatting will be enabled when pasting content + + + + Format on paste + Format on paste + + If true, formatting will be enabled while typing Se true, la formattazione verrà abilitata durante la digitazione diff --git a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.ja.xlf b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.ja.xlf index 264240d6ed0..ecefe304f03 100644 --- a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.ja.xlf +++ b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.ja.xlf @@ -92,6 +92,16 @@ スペースを含む要素のコミット + + If true, formatting will be enabled when pasting content + If true, formatting will be enabled when pasting content + + + + Format on paste + Format on paste + + If true, formatting will be enabled while typing true の場合、入力中に書式設定が有効になります diff --git a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.ko.xlf b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.ko.xlf index c844ba635ac..25a1f9a374e 100644 --- a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.ko.xlf +++ b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.ko.xlf @@ -92,6 +92,16 @@ 공백을 사용하여 요소 커밋 + + If true, formatting will be enabled when pasting content + If true, formatting will be enabled when pasting content + + + + Format on paste + Format on paste + + If true, formatting will be enabled while typing true이면 입력하는 동안 서식을 사용할 수 있습니다. diff --git a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.pl.xlf b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.pl.xlf index dc3dfbd58a3..15e1b0445e6 100644 --- a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.pl.xlf +++ b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.pl.xlf @@ -92,6 +92,16 @@ Zatwierdź elementy za pomocą klawisza Spacja + + If true, formatting will be enabled when pasting content + If true, formatting will be enabled when pasting content + + + + Format on paste + Format on paste + + If true, formatting will be enabled while typing Jeśli ma wartość „prawda”, formatowanie będzie włączone podczas pisania diff --git a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.pt-BR.xlf b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.pt-BR.xlf index c2ad5566dfc..451e2164153 100644 --- a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.pt-BR.xlf +++ b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.pt-BR.xlf @@ -92,6 +92,16 @@ Confirmar elementos com espaço + + If true, formatting will be enabled when pasting content + If true, formatting will be enabled when pasting content + + + + Format on paste + Format on paste + + If true, formatting will be enabled while typing Se verdadeiro, a formatação será habilitada durante a digitação diff --git a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.ru.xlf b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.ru.xlf index 534feeab666..3eb1da386aa 100644 --- a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.ru.xlf +++ b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.ru.xlf @@ -92,6 +92,16 @@ Фиксация элементов клавишей ПРОБЕЛ + + If true, formatting will be enabled when pasting content + If true, formatting will be enabled when pasting content + + + + Format on paste + Format on paste + + If true, formatting will be enabled while typing Если задано значение true, при вводе текста будет включено форматирование diff --git a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.tr.xlf b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.tr.xlf index 2d871d26639..3026631fe92 100644 --- a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.tr.xlf +++ b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.tr.xlf @@ -92,6 +92,16 @@ Öğeleri boşluk çubuğuyla işle + + If true, formatting will be enabled when pasting content + If true, formatting will be enabled when pasting content + + + + Format on paste + Format on paste + + If true, formatting will be enabled while typing True ise, yazma sırasında biçimlendirme etkinleştirilir diff --git a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.zh-Hans.xlf b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.zh-Hans.xlf index 1a428d7836c..0d8454bc47f 100644 --- a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.zh-Hans.xlf +++ b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.zh-Hans.xlf @@ -92,6 +92,16 @@ 使用空格键提交元素 + + If true, formatting will be enabled when pasting content + If true, formatting will be enabled when pasting content + + + + Format on paste + Format on paste + + If true, formatting will be enabled while typing 如果为 true,则在键入时将启用格式设置 diff --git a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.zh-Hant.xlf b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.zh-Hant.xlf index 1779abb2bac..238c43570cd 100644 --- a/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.zh-Hant.xlf +++ b/src/Razor/src/Microsoft.VisualStudio.RazorExtension/xlf/VSPackage.zh-Hant.xlf @@ -92,6 +92,16 @@ 透過空格鍵認可元素 + + If true, formatting will be enabled when pasting content + If true, formatting will be enabled when pasting content + + + + Format on paste + Format on paste + + If true, formatting will be enabled while typing 如果為 True,則會在輸入時啟用格式化 diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CodeActionEndToEndTest.NetFx.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CodeActionEndToEndTest.NetFx.cs index 4b9e5380267..523591dd0ee 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CodeActionEndToEndTest.NetFx.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CodeActionEndToEndTest.NetFx.cs @@ -802,13 +802,12 @@ public async Task Handle_GenerateMethod_VaryIndentSize(bool insertSpaces, int ta """; var razorLSPOptions = new RazorLSPOptions( - EnableFormatting: true, + FormattingFlags.All, AutoClosingTags: true, insertSpaces, tabSize, AutoShowCompletion: true, AutoListParams: true, - FormatOnType: true, AutoInsertAttributeQuotes: true, ColorBackground: false, CodeBlockBraceOnNextLine: false, diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/DefaultRazorConfigurationServiceTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/DefaultRazorConfigurationServiceTest.cs index 29048394810..9be85770208 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/DefaultRazorConfigurationServiceTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/DefaultRazorConfigurationServiceTest.cs @@ -22,7 +22,7 @@ public async Task GetLatestOptionsAsync_ReturnsExpectedOptions() { // Arrange var expectedOptions = new RazorLSPOptions( - EnableFormatting: false, AutoClosingTags: false, InsertSpaces: true, TabSize: 4, AutoShowCompletion: true, AutoListParams: true, FormatOnType: true, AutoInsertAttributeQuotes: true, ColorBackground: false, CodeBlockBraceOnNextLine: true, CommitElementsWithSpace: false); + FormattingFlags.Disabled, AutoClosingTags: false, InsertSpaces: true, TabSize: 4, AutoShowCompletion: true, AutoListParams: true, AutoInsertAttributeQuotes: true, ColorBackground: false, CodeBlockBraceOnNextLine: true, CommitElementsWithSpace: false); var razorJsonString = """ @@ -94,7 +94,7 @@ public void BuildOptions_VSCodeOptionsOnly_ReturnsExpected() { // Arrange - purposely choosing options opposite of default var expectedOptions = new RazorLSPOptions( - EnableFormatting: false, AutoClosingTags: false, InsertSpaces: true, TabSize: 4, AutoShowCompletion: true, AutoListParams: true, FormatOnType: true, AutoInsertAttributeQuotes: true, ColorBackground: false, CodeBlockBraceOnNextLine: true, CommitElementsWithSpace: false); + FormattingFlags.Disabled, AutoClosingTags: false, InsertSpaces: true, TabSize: 4, AutoShowCompletion: true, AutoListParams: true, AutoInsertAttributeQuotes: true, ColorBackground: false, CodeBlockBraceOnNextLine: true, CommitElementsWithSpace: false); var razorJsonString = """ { "format": { @@ -131,7 +131,7 @@ public void BuildOptions_VSOptionsOnly_ReturnsExpected() { // Arrange - purposely choosing options opposite of default var expectedOptions = new RazorLSPOptions( - EnableFormatting: true, AutoClosingTags: false, InsertSpaces: false, TabSize: 8, AutoShowCompletion: true, AutoListParams: true, FormatOnType: false, AutoInsertAttributeQuotes: false, ColorBackground: false, CodeBlockBraceOnNextLine: false, CommitElementsWithSpace: false); + FormattingFlags.Enabled, AutoClosingTags: false, InsertSpaces: false, TabSize: 8, AutoShowCompletion: true, AutoListParams: true, AutoInsertAttributeQuotes: false, ColorBackground: false, CodeBlockBraceOnNextLine: false, CommitElementsWithSpace: false); var razorJsonString = """ { } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/DocumentRangeFormattingEndpointTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/DocumentRangeFormattingEndpointTest.cs index bd667e180a8..550db0cc4a2 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/DocumentRangeFormattingEndpointTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/DocumentRangeFormattingEndpointTest.cs @@ -79,7 +79,7 @@ public async Task Handle_UnsupportedCodeDocument_ReturnsNull() var endpoint = new DocumentRangeFormattingEndpoint(formattingService, htmlFormatter, optionsMonitor); var @params = new DocumentRangeFormattingParams() { - TextDocument = new TextDocumentIdentifier { Uri = uri, } + TextDocument = new TextDocumentIdentifier { Uri = uri, }, }; var requestContext = CreateRazorRequestContext(documentContext); @@ -107,4 +107,32 @@ public async Task Handle_FormattingDisabled_ReturnsNull() // Assert Assert.Null(result); } + + [Fact] + public async Task Handle_FormattingOnPasteDisabled_ReturnsNull() + { + // Arrange + var formattingService = new DummyRazorFormattingService(); + var optionsMonitor = GetOptionsMonitor(formatOnPaste: false); + var htmlFormatter = new TestHtmlFormatter(); + var endpoint = new DocumentRangeFormattingEndpoint(formattingService, htmlFormatter, optionsMonitor); + var @params = new DocumentRangeFormattingParams() + { + Options = new() + { + OtherOptions = new() + { + { "fromPaste", true } + } + } + }; + + var requestContext = CreateRazorRequestContext(documentContext: null); + + // Act + var result = await endpoint.HandleRequestAsync(@params, requestContext, DisposalToken); + + // Assert + Assert.Null(result); + } } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/RazorFormattingTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/RazorFormattingTest.cs index aa1400fbe62..baa075aaa1f 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/RazorFormattingTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/RazorFormattingTest.cs @@ -814,7 +814,7 @@ private void IncrementCount() } """, triggerCharacter: '}', - razorLSPOptions: RazorLSPOptions.Default with { FormatOnType = true }); + razorLSPOptions: RazorLSPOptions.Default); } [Fact] diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorLSPOptionsMonitorTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorLSPOptionsMonitorTest.cs index 923c290ca46..973d319eb9e 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorLSPOptionsMonitorTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorLSPOptionsMonitorTest.cs @@ -23,7 +23,7 @@ public RazorLSPOptionsMonitorTest(ITestOutputHelper testOutput) public async Task UpdateAsync_Invokes_OnChangeRegistration() { // Arrange - var expectedOptions = new RazorLSPOptions(EnableFormatting: false, AutoClosingTags: true, InsertSpaces: true, TabSize: 4, AutoShowCompletion: true, AutoListParams: true, FormatOnType: true, AutoInsertAttributeQuotes: true, ColorBackground: false, CodeBlockBraceOnNextLine: false, CommitElementsWithSpace: true); + var expectedOptions = new RazorLSPOptions(FormattingFlags.Disabled, AutoClosingTags: true, InsertSpaces: true, TabSize: 4, AutoShowCompletion: true, AutoListParams: true, AutoInsertAttributeQuotes: true, ColorBackground: false, CodeBlockBraceOnNextLine: false, CommitElementsWithSpace: true); var configService = Mock.Of( f => f.GetLatestOptionsAsync(DisposalToken) == Task.FromResult(expectedOptions), MockBehavior.Strict); @@ -45,7 +45,7 @@ public async Task UpdateAsync_Invokes_OnChangeRegistration() public async Task UpdateAsync_DoesNotInvoke_OnChangeRegistration_AfterDispose() { // Arrange - var expectedOptions = new RazorLSPOptions(EnableFormatting: false, AutoClosingTags: true, InsertSpaces: true, TabSize: 4, AutoShowCompletion: true, AutoListParams: true, FormatOnType: true, AutoInsertAttributeQuotes: true, ColorBackground: false, CodeBlockBraceOnNextLine: false, CommitElementsWithSpace: true); + var expectedOptions = new RazorLSPOptions(FormattingFlags.Disabled, AutoClosingTags: true, InsertSpaces: true, TabSize: 4, AutoShowCompletion: true, AutoListParams: true, AutoInsertAttributeQuotes: true, ColorBackground: false, CodeBlockBraceOnNextLine: false, CommitElementsWithSpace: true); var configService = Mock.Of( f => f.GetLatestOptionsAsync(DisposalToken) == Task.FromResult(expectedOptions), MockBehavior.Strict); @@ -91,7 +91,7 @@ public async Task UpdateAsync_ConfigReturnsNull_DoesNotInvoke_OnChangeRegistrati public void InitializedOptionsAreCurrent() { // Arrange - var expectedOptions = new RazorLSPOptions(EnableFormatting: false, AutoClosingTags: true, InsertSpaces: true, TabSize: 4, AutoShowCompletion: true, AutoListParams: true, FormatOnType: true, AutoInsertAttributeQuotes: true, ColorBackground: false, CodeBlockBraceOnNextLine: false, CommitElementsWithSpace: true); + var expectedOptions = new RazorLSPOptions(FormattingFlags.Disabled, AutoClosingTags: true, InsertSpaces: true, TabSize: 4, AutoShowCompletion: true, AutoListParams: true, AutoInsertAttributeQuotes: true, ColorBackground: false, CodeBlockBraceOnNextLine: false, CommitElementsWithSpace: true); var configService = Mock.Of( f => f.GetLatestOptionsAsync(DisposalToken) == Task.FromResult(expectedOptions), MockBehavior.Strict); diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/LanguageServer/LanguageServerTestBase.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/LanguageServer/LanguageServerTestBase.cs index 7bd6d140ce2..9b9c878a08f 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/LanguageServer/LanguageServerTestBase.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/LanguageServer/LanguageServerTestBase.cs @@ -127,15 +127,56 @@ private protected static DocumentContext CreateDocumentContext(Uri uri, IDocumen return new DocumentContext(uri, snapshot, projectContext: null); } - private protected static RazorLSPOptionsMonitor GetOptionsMonitor(bool enableFormatting = true, bool autoShowCompletion = true, bool autoListParams = true, bool formatOnType = true, bool autoInsertAttributeQuotes = true, bool colorBackground = false, bool codeBlockBraceOnNextLine = false, bool commitElementsWithSpace = true) + private protected static RazorLSPOptionsMonitor GetOptionsMonitor( + bool enableFormatting = true, + bool autoShowCompletion = true, + bool autoListParams = true, + bool formatOnType = true, + bool autoInsertAttributeQuotes = true, + bool colorBackground = false, + bool codeBlockBraceOnNextLine = false, + bool commitElementsWithSpace = true, + bool formatOnPaste = true) { var configService = StrictMock.Of(); - var options = new RazorLSPOptions(enableFormatting, true, InsertSpaces: true, TabSize: 4, autoShowCompletion, autoListParams, formatOnType, autoInsertAttributeQuotes, colorBackground, codeBlockBraceOnNextLine, commitElementsWithSpace); + var options = new RazorLSPOptions( + GetFormattingFlags(enableFormatting, formatOnType, formatOnPaste), + true, + InsertSpaces: true, + TabSize: 4, + autoShowCompletion, + autoListParams, + autoInsertAttributeQuotes, + colorBackground, + codeBlockBraceOnNextLine, + commitElementsWithSpace); var optionsMonitor = new RazorLSPOptionsMonitor(configService, options); return optionsMonitor; } + private static FormattingFlags GetFormattingFlags(bool enableFormatting, bool formatOnType, bool formatOnPaste) + { + var flags = FormattingFlags.Disabled; + + if (enableFormatting) + { + flags |= FormattingFlags.Enabled; + } + + if (formatOnType) + { + flags |= FormattingFlags.OnType; + } + + if (formatOnPaste) + { + flags |= FormattingFlags.OnPaste; + } + + return flags; + } + private class ThrowingRazorSpanMappingService : IRazorSpanMappingService { public Task> MapSpansAsync(Document document, IEnumerable spans, CancellationToken cancellationToken) diff --git a/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Settings/ClientSettingsManagerTest.cs b/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Settings/ClientSettingsManagerTest.cs index 498fb92da12..0edfde8f751 100644 --- a/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Settings/ClientSettingsManagerTest.cs +++ b/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Settings/ClientSettingsManagerTest.cs @@ -82,7 +82,7 @@ public void Update_TriggersChangedIfAdvancedSettingsAreDifferent() var manager = new ClientSettingsManager(_clientSettingsChangeTriggers); var called = false; manager.ClientSettingsChanged += (caller, args) => called = true; - var settings = new ClientAdvancedSettings(FormatOnType: false, AutoClosingTags: true, AutoInsertAttributeQuotes: true, ColorBackground: true, CodeBlockBraceOnNextLine: false, CommitElementsWithSpace: false, SnippetSetting: default, LogLevel: default); + var settings = new ClientAdvancedSettings(FormatOnType: false, AutoClosingTags: true, AutoInsertAttributeQuotes: true, ColorBackground: true, CodeBlockBraceOnNextLine: false, CommitElementsWithSpace: false, SnippetSetting: default, LogLevel: default, FormatOnPaste: false); // Act manager.Update(settings);