From d1076af43be49e83a253cbd9525261142a78ce43 Mon Sep 17 00:00:00 2001 From: Kevin Cunnane Date: Wed, 15 Feb 2017 12:59:27 -0800 Subject: [PATCH] Ensure Keywork and DataType options are used --- .../Formatter/TSqlFormatterService.cs | 33 ++++++++-------- .../Formatter/FormatterSettingsTests.cs | 39 +++++++++++++++++-- 2 files changed, 53 insertions(+), 19 deletions(-) diff --git a/src/Microsoft.SqlTools.ServiceLayer/Formatter/TSqlFormatterService.cs b/src/Microsoft.SqlTools.ServiceLayer/Formatter/TSqlFormatterService.cs index c6afa157..58ffdb08 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Formatter/TSqlFormatterService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Formatter/TSqlFormatterService.cs @@ -33,12 +33,16 @@ namespace Microsoft.SqlTools.ServiceLayer.Formatter { settings = new FormatterSettings(); } - + + + public override void InitializeService(IProtocolEndpoint serviceHost) { serviceHost.SetRequestHandler(DocumentFormattingRequest.Type, HandleDocFormatRequest); serviceHost.SetRequestHandler(DocumentRangeFormattingRequest.Type, HandleDocRangeFormatRequest); - + + + WorkspaceService?.RegisterConfigChangeCallback(HandleDidChangeConfigurationNotification); } @@ -122,25 +126,22 @@ namespace Microsoft.SqlTools.ServiceLayer.Formatter } private FormatOptions GetOptions(DocumentFormattingParams docFormatParams) + { + return MergeFormatOptions(docFormatParams.Options, settings); + } + + internal static FormatOptions MergeFormatOptions(FormattingOptions formatRequestOptions, FormatterSettings settings) + { FormatOptions options = new FormatOptions(); - if (docFormatParams.Options != null) + if (formatRequestOptions != null) { - options.UseSpaces = docFormatParams.Options.InsertSpaces; - options.SpacesPerIndent = docFormatParams.Options.TabSize; - } - if (settings != null) - { - if (settings.AlignColumnDefinitionsInColumns.HasValue) { options.AlignColumnDefinitionsInColumns = settings.AlignColumnDefinitionsInColumns.Value; } - - if (settings.PlaceCommasBeforeNextStatement.HasValue) { options.PlaceCommasBeforeNextStatement = settings.PlaceCommasBeforeNextStatement.Value; } - - if (settings.PlaceSelectStatementReferencesOnNewLine.HasValue) { options.PlaceEachReferenceOnNewLineInQueryStatements = settings.PlaceSelectStatementReferencesOnNewLine.Value; } - - if (settings.UseBracketForIdentifiers.HasValue) { options.EncloseIdentifiersInSquareBrackets = settings.UseBracketForIdentifiers.Value; } - + options.UseSpaces = formatRequestOptions.InsertSpaces; + options.SpacesPerIndent = formatRequestOptions.TabSize; } + UpdateFormatOptionsFromSettings(options, settings); return options; + } internal static void UpdateFormatOptionsFromSettings(FormatOptions options, FormatterSettings settings) diff --git a/test/Microsoft.SqlTools.ServiceLayer.Test/Formatter/FormatterSettingsTests.cs b/test/Microsoft.SqlTools.ServiceLayer.Test/Formatter/FormatterSettingsTests.cs index 3ce0f58e..78859f6a 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.Test/Formatter/FormatterSettingsTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.Test/Formatter/FormatterSettingsTests.cs @@ -4,6 +4,7 @@ // using Microsoft.SqlTools.ServiceLayer.Formatter; +using Microsoft.SqlTools.ServiceLayer.Formatter.Contracts; using Microsoft.SqlTools.ServiceLayer.SqlContext; using Newtonsoft.Json.Linq; using Xunit; @@ -84,6 +85,16 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Formatter [Fact] public void CanCopyAlteredFormatSettingsToOptions() + { + SqlToolsSettings sqlToolsSettings = CreateNonDefaultFormatSettings(); + + FormatOptions options = new FormatOptions(); + TSqlFormatterService.UpdateFormatOptionsFromSettings(options, sqlToolsSettings.SqlTools.Format); + + AssertOptionsHaveExpectedNonDefaultValues(options); + } + + private static SqlToolsSettings CreateNonDefaultFormatSettings() { var sqlToolsSettings = new SqlToolsSettings(); sqlToolsSettings.SqlTools.Format.AlignColumnDefinitionsInColumns = true; @@ -92,17 +103,39 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Formatter sqlToolsSettings.SqlTools.Format.PlaceCommasBeforeNextStatement = true; sqlToolsSettings.SqlTools.Format.PlaceSelectStatementReferencesOnNewLine = true; sqlToolsSettings.SqlTools.Format.UseBracketForIdentifiers = true; + return sqlToolsSettings; + } - FormatOptions options = new FormatOptions(); - TSqlFormatterService.UpdateFormatOptionsFromSettings(options, sqlToolsSettings.SqlTools.Format); - + private static void AssertOptionsHaveExpectedNonDefaultValues(FormatOptions options) + { Assert.True(options.AlignColumnDefinitionsInColumns); Assert.Equal(CasingOptions.Lowercase, options.DatatypeCasing); Assert.Equal(CasingOptions.Uppercase, options.KeywordCasing); Assert.True(options.PlaceCommasBeforeNextStatement); Assert.True(options.PlaceEachReferenceOnNewLineInQueryStatements); Assert.True(options.EncloseIdentifiersInSquareBrackets); + + Assert.False(options.UppercaseDataTypes); + Assert.True(options.UppercaseKeywords); + Assert.True(options.LowercaseDataTypes); + Assert.False(options.LowercaseKeywords); + Assert.False(options.DoNotFormatDataTypes); + Assert.False(options.DoNotFormatKeywords); } + [Fact] + public void CanMergeRequestOptionsAndSettings() + { + var sqlToolsSettings = CreateNonDefaultFormatSettings(); + + FormatOptions options = TSqlFormatterService.MergeFormatOptions( + new FormattingOptions { InsertSpaces = true, TabSize = 2 }, + sqlToolsSettings.SqlTools.Format); + + AssertOptionsHaveExpectedNonDefaultValues(options); + Assert.False(options.UseTabs); + Assert.True(options.UseSpaces); + Assert.Equal(2, options.SpacesPerIndent); + } } }