Kusto Format Fix (#1251)

* Added Format function to DataSourceFactory. Removed unsued code / commented code in TSqlFormatterService.

* Renamed TSqlFormatterService to FormatterService in Kusto
This commit is contained in:
Justin M
2021-09-29 13:07:56 -07:00
committed by GitHub
parent e1356f76cc
commit e7b17fb660
2 changed files with 17 additions and 62 deletions

View File

@@ -2,16 +2,19 @@
using System.Collections.Generic;
using System.Composition;
using Kusto.Data;
using Kusto.Language;
using Kusto.Language.Editor;
using Microsoft.Kusto.ServiceLayer.Connection.Contracts;
using Microsoft.Kusto.ServiceLayer.DataSource.Contracts;
using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection;
using Microsoft.Kusto.ServiceLayer.DataSource.Intellisense;
using Microsoft.Kusto.ServiceLayer.DataSource.Kusto;
using Microsoft.Kusto.ServiceLayer.DataSource.Monitor;
using Microsoft.Kusto.ServiceLayer.Formatter;
using Microsoft.Kusto.ServiceLayer.LanguageServices;
using Microsoft.Kusto.ServiceLayer.LanguageServices.Contracts;
using Microsoft.Kusto.ServiceLayer.Utility;
using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
using CompletionItem = Microsoft.Kusto.ServiceLayer.LanguageServices.Contracts.CompletionItem;
namespace Microsoft.Kusto.ServiceLayer.DataSource
{
@@ -157,5 +160,13 @@ namespace Microsoft.Kusto.ServiceLayer.DataSource
{
return Program.ServiceName.Contains("Kusto") ? KustoProviderDescription : LogAnalyticsProviderDescription;
}
public static string Format(string input, FormatOptions options)
{
var code = KustoCode.ParseAndAnalyze(input);
var kustoCodeService = new KustoCodeService(code);
var formattedText = kustoCodeService.GetFormattedText();
return formattedText.Text;
}
}
}

View File

@@ -6,8 +6,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Kusto.ServiceLayer.DataSource;
using Microsoft.SqlTools.Extensibility;
using Microsoft.SqlTools.Hosting;
using Microsoft.SqlTools.Hosting.Protocol;
@@ -18,18 +18,19 @@ using Microsoft.Kusto.ServiceLayer.SqlContext;
using Microsoft.Kusto.ServiceLayer.Workspace;
using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
using Microsoft.SqlTools.Utility;
using FormattingOptions = Microsoft.Kusto.ServiceLayer.Formatter.Contracts.FormattingOptions;
using Range = Microsoft.Kusto.ServiceLayer.Workspace.Contracts.Range;
namespace Microsoft.Kusto.ServiceLayer.Formatter
{
public class TSqlFormatterService : HostedService<TSqlFormatterService>, IComposableService
public class FormatterService : HostedService<FormatterService>, IComposableService
{
private FormatterSettings settings;
/// <summary>
/// The default constructor is required for MEF-based composable services
/// </summary>
public TSqlFormatterService()
public FormatterService()
{
settings = new FormatterSettings();
}
@@ -164,8 +165,7 @@ namespace Microsoft.Kusto.ServiceLayer.Formatter
FormatOptions options = GetOptions(docFormatParams);
List<TextEdit> edits = new List<TextEdit>();
edit.NewText = Format(text, options, false);
// TODO do not add if no formatting needed?
edit.NewText = DataSourceFactory.Format(text, options);
edits.Add(edit);
return edits.ToArray();
}
@@ -239,62 +239,6 @@ namespace Microsoft.Kusto.ServiceLayer.Formatter
await requestContext.SendError(ex.ToString());
}
}
public string Format(TextReader input)
{
string originalSql = input.ReadToEnd();
return Format(originalSql, new FormatOptions());
}
public string Format(string input, FormatOptions options)
{
return Format(input, options, true);
}
public string Format(string input, FormatOptions options, bool verifyOutput)
{
string result = null;
//TODOKusto: Implement formatting for Kusto generically here.
//var kustoCodeService = new KustoCodeService(input, GlobalState.Default);
//var formattedText = kustoCodeService.GetFormattedText();
//DoFormat(input, options, verifyOutput, visitor =>
//{
//result = formattedText.Text;
//});
return result;
}
/*public void Format(string input, FormatOptions options, bool verifyOutput, Replacement.OnReplace replace)
{
DoFormat(input, options, verifyOutput, visitor =>
{
foreach (Replacement r in visitor.Context.Replacements)
{
r.Apply(replace);
}
});
}
private void DoFormat(string input, FormatOptions options, bool verifyOutput, Action<FormatterVisitor> postFormatAction)
{
Validate.IsNotNull(nameof(input), input);
Validate.IsNotNull(nameof(options), options);
ParseResult result = Parser.Parse(input);
FormatContext context = new FormatContext(result.Script, options);
FormatterVisitor visitor = new FormatterVisitor(context, ServiceProvider);
result.Script.Accept(visitor);
if (verifyOutput)
{
visitor.VerifyFormat();
}
postFormatAction?.Invoke(visitor);
}*/
}
internal static class RangeExtensions