From e7b17fb6607193726826956dbfea2e47949cf795 Mon Sep 17 00:00:00 2001 From: Justin M <63619224+JustinMDotNet@users.noreply.github.com> Date: Wed, 29 Sep 2021 13:07:56 -0700 Subject: [PATCH] Kusto Format Fix (#1251) * Added Format function to DataSourceFactory. Removed unsued code / commented code in TSqlFormatterService. * Renamed TSqlFormatterService to FormatterService in Kusto --- .../DataSource/DataSourceFactory.cs | 13 +++- ...ormatterService.cs => FormatterService.cs} | 66 ++----------------- 2 files changed, 17 insertions(+), 62 deletions(-) rename src/Microsoft.Kusto.ServiceLayer/Formatter/{TSqlFormatterService.cs => FormatterService.cs} (82%) diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceFactory.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceFactory.cs index d76f324d..0683211d 100644 --- a/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceFactory.cs +++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceFactory.cs @@ -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; + } } } \ No newline at end of file diff --git a/src/Microsoft.Kusto.ServiceLayer/Formatter/TSqlFormatterService.cs b/src/Microsoft.Kusto.ServiceLayer/Formatter/FormatterService.cs similarity index 82% rename from src/Microsoft.Kusto.ServiceLayer/Formatter/TSqlFormatterService.cs rename to src/Microsoft.Kusto.ServiceLayer/Formatter/FormatterService.cs index 6feec8fb..42c0b578 100644 --- a/src/Microsoft.Kusto.ServiceLayer/Formatter/TSqlFormatterService.cs +++ b/src/Microsoft.Kusto.ServiceLayer/Formatter/FormatterService.cs @@ -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, IComposableService + public class FormatterService : HostedService, IComposableService { private FormatterSettings settings; /// /// The default constructor is required for MEF-based composable services /// - public TSqlFormatterService() + public FormatterService() { settings = new FormatterSettings(); } @@ -164,8 +165,7 @@ namespace Microsoft.Kusto.ServiceLayer.Formatter FormatOptions options = GetOptions(docFormatParams); List edits = new List(); - 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 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