mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 10:58:30 -05:00
Ensure Keywork and DataType options are used
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user