Skip to content

Commit

Permalink
Revert "Add option for format on paste (dotnet#11039)"
Browse files Browse the repository at this point in the history
This reverts commit 019b7c8.
  • Loading branch information
akhera99 committed Oct 22, 2024
1 parent 97694ba commit a187c81
Show file tree
Hide file tree
Showing 35 changed files with 199 additions and 466 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.Formatting.IsOnTypeEnabled())
if (!_optionsMonitor.CurrentValue.FormatOnType)
{
Logger.LogInformation($"Formatting on type disabled, so auto insert is a no-op for C#.");
return SpecializedTasks.Null<IDelegatedParams>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ internal RazorLSPOptions BuildOptions(JsonObject[] result)
}
else
{
ExtractVSCodeOptions(result, out var formatting, out var autoClosingTags, out var commitElementsWithSpace, out var codeBlockBraceOnNextLine);
ExtractVSCodeOptions(result, out var enableFormatting, out var autoClosingTags, out var commitElementsWithSpace, out var codeBlockBraceOnNextLine);
return RazorLSPOptions.Default with
{
Formatting = formatting,
EnableFormatting = enableFormatting,
AutoClosingTags = autoClosingTags,
CommitElementsWithSpace = commitElementsWithSpace,
CodeBlockBraceOnNextLine = codeBlockBraceOnNextLine
Expand All @@ -109,15 +109,15 @@ internal RazorLSPOptions BuildOptions(JsonObject[] result)

private void ExtractVSCodeOptions(
JsonObject[] result,
out FormattingFlags formatting,
out bool enableFormatting,
out bool autoClosingTags,
out bool commitElementsWithSpace,
out bool codeBlockBraceOnNextLine)
{
var razor = result[0];
var html = result[1];

formatting = RazorLSPOptions.Default.Formatting;
enableFormatting = RazorLSPOptions.Default.EnableFormatting;
autoClosingTags = RazorLSPOptions.Default.AutoClosingTags;
codeBlockBraceOnNextLine = RazorLSPOptions.Default.CodeBlockBraceOnNextLine;
// Deliberately not using the "default" here because we want a different default for VS Code, as
Expand All @@ -130,15 +130,7 @@ private void ExtractVSCodeOptions(
if (parsedFormat.TryGetPropertyValue("enable", out var parsedEnableFormatting) &&
parsedEnableFormatting is not null)
{
var formattingEnabled = GetObjectOrDefault(parsedEnableFormatting, formatting.IsEnabled());
if (formattingEnabled)
{
formatting |= FormattingFlags.Enabled;
}
else
{
formatting = FormattingFlags.Disabled;
}
enableFormatting = GetObjectOrDefault(parsedEnableFormatting, enableFormatting);
}

if (parsedFormat.TryGetPropertyValue("codeBlockBraceOnNextLine", out var parsedCodeBlockBraceOnNextLine) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(DocumentFormattingParams

public async Task<TextEdit[]?> HandleRequestAsync(DocumentFormattingParams request, RazorRequestContext requestContext, CancellationToken cancellationToken)
{
if (!_optionsMonitor.CurrentValue.Formatting.IsEnabled())
if (!_optionsMonitor.CurrentValue.EnableFormatting)
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(DocumentOnTypeFormatting
{
_logger.LogInformation($"Starting OnTypeFormatting request for {request.TextDocument.Uri}.");

if (!_optionsMonitor.CurrentValue.Formatting.IsEnabled())
if (!_optionsMonitor.CurrentValue.EnableFormatting)
{
_logger.LogInformation($"Formatting option disabled.");
return null;
}

if (!_optionsMonitor.CurrentValue.Formatting.IsOnTypeEnabled())
if (!_optionsMonitor.CurrentValue.FormatOnType)
{
_logger.LogInformation($"Formatting on type disabled.");
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(DocumentRangeFormattingP

public async Task<TextEdit[]?> HandleRequestAsync(DocumentRangeFormattingParams request, RazorRequestContext requestContext, CancellationToken cancellationToken)
{
if (!_optionsMonitor.CurrentValue.Formatting.IsEnabled())
if (!_optionsMonitor.CurrentValue.EnableFormatting)
{
return null;
}
Expand All @@ -54,16 +54,6 @@ 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);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
// 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(
FormattingFlags Formatting,
bool EnableFormatting,
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(Formatting: FormattingFlags.All,
public readonly static RazorLSPOptions Default = new(EnableFormatting: true,
AutoClosingTags: true,
AutoListParams: true,
InsertSpaces: true,
TabSize: 4,
AutoShowCompletion: true,
FormatOnType: true,
AutoInsertAttributeQuotes: true,
ColorBackground: false,
CodeBlockBraceOnNextLine: false,
Expand All @@ -34,30 +35,15 @@ internal record RazorLSPOptions(
/// not defined in client settings.
/// </summary>
internal static RazorLSPOptions From(ClientSettings settings)
=> new(GetFormattingFlags(settings),
=> new(Default.EnableFormatting,
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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, bool FormatOnPaste)
internal sealed record ClientAdvancedSettings(bool FormatOnType, bool AutoClosingTags, bool AutoInsertAttributeQuotes, bool ColorBackground, bool CodeBlockBraceOnNextLine, bool CommitElementsWithSpace, SnippetSetting SnippetSetting, LogLevel LogLevel)
{
public static readonly ClientAdvancedSettings Default = new(FormatOnType: true, AutoClosingTags: true, AutoInsertAttributeQuotes: true, ColorBackground: false, CodeBlockBraceOnNextLine: false, CommitElementsWithSpace: true, SnippetSetting.All, LogLevel.Warning, FormatOnPaste: true);
public static readonly ClientAdvancedSettings Default = new(FormatOnType: true, AutoClosingTags: true, AutoInsertAttributeQuotes: true, ColorBackground: false, CodeBlockBraceOnNextLine: false, CommitElementsWithSpace: true, SnippetSetting.All, LogLevel.Warning);
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ public ImmutableArray<Registration> GetRegistrations(VSInternalClientCapabilitie

private async Task<TextEdit[]?> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ 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,
Expand Down Expand Up @@ -113,7 +107,7 @@ public async Task OnChangedAsync(Action<ClientAdvancedSettings> changed)

private EventHandler<ClientAdvancedSettingsChangedEventArgs>? _changed;

public ClientAdvancedSettings GetAdvancedSettings() => new(FormatOnType, AutoClosingTags, AutoInsertAttributeQuotes, ColorBackground, CodeBlockBraceOnNextLine, CommitElementsWithSpace, Snippets, LogLevel, FormatOnPaste);
public ClientAdvancedSettings GetAdvancedSettings() => new(FormatOnType, AutoClosingTags, AutoInsertAttributeQuotes, ColorBackground, CodeBlockBraceOnNextLine, CommitElementsWithSpace, Snippets, LogLevel);

public bool GetBool(string name, bool defaultValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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 =
[
Expand All @@ -30,6 +29,5 @@ public record Setting(string LegacyName, string UnifiedName);
CommitElementsWithSpace,
Snippets,
LogLevel,
FormatOnPaste,
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ internal class AdvancedOptionPage : DialogPage
private bool? _commitElementsWithSpace;
private SnippetSetting? _snippets;
private LogLevel? _logLevel;
private bool? _formatOnPaste;

public AdvancedOptionPage()
{
Expand Down Expand Up @@ -92,15 +91,6 @@ 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))]
Expand Down
Loading

0 comments on commit a187c81

Please sign in to comment.