diff --git a/src/Microsoft.SqlTools.ServiceLayer/NotebookConvert/NotebookConvertService.cs b/src/Microsoft.SqlTools.ServiceLayer/NotebookConvert/NotebookConvertService.cs index 1f64248d..460cefc7 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/NotebookConvert/NotebookConvertService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/NotebookConvert/NotebookConvertService.cs @@ -15,14 +15,13 @@ using Microsoft.SqlTools.ServiceLayer.Workspace; using Newtonsoft.Json; using Microsoft.SqlServer.TransactSql.ScriptDom; using System.IO; -using Microsoft.SqlTools.ServiceLayer.NotebookConvert; -using Newtonsoft.Json.Linq; namespace Microsoft.SqlTools.ServiceLayer.NotebookConvert { enum NotebookTokenType { MultilineComment, + SinglelineComment, Batch } @@ -119,7 +118,7 @@ namespace Microsoft.SqlTools.ServiceLayer.NotebookConvert #endregion // Convert Handlers - private static NotebookDocument ConvertSqlToNotebook(string sql) + internal static NotebookDocument ConvertSqlToNotebook(string sql) { // Notebooks use \n so convert any other newlines now sql = sql.Replace("\r\n", "\n"); @@ -160,11 +159,11 @@ namespace Microsoft.SqlTools.ServiceLayer.NotebookConvert * cells that could break the script */ var multilineComments = tokens - .Where(token => token.TokenType == TSqlTokenType.MultilineComment) + .Where(token => token.TokenType == TSqlTokenType.MultilineComment || token.TokenType == TSqlTokenType.SingleLineComment) // Ignore comments that are within a batch. This won't include comments at the start/end of a batch though - the parser is smart enough // to have the batch only contain the code and any comments that are embedded within it .Where(token => !batches.Any(batch => token.Offset > batch.StartOffset && token.Offset < (batch.StartOffset + batch.FragmentLength))) - .Select(token => new NotebookToken() { StartOffset = token.Offset, Text = token.Text.Trim(), TokenType = NotebookTokenType.MultilineComment }); + .Select(token => new NotebookToken() { StartOffset = token.Offset, Text = token.Text.Trim(), TokenType = token.TokenType == TSqlTokenType.MultilineComment ? NotebookTokenType.MultilineComment : NotebookTokenType.SinglelineComment }); // Combine batches and comments into a single list of all the fragments we need to add to the Notebook var allFragments = batches.Select(batch => @@ -204,6 +203,16 @@ namespace Microsoft.SqlTools.ServiceLayer.NotebookConvert commentBlock = commentBlock.Remove(commentBlock.Length - 2); } + doc.Cells.Add(GenerateMarkdownCell(commentBlock.Trim())); + } else if (fragment.TokenType == NotebookTokenType.SinglelineComment) + { + string commentBlock = fragment.Text; + // Trim off the starting comment token (--) + if (commentBlock.StartsWith("--")) + { + commentBlock = commentBlock.Remove(0, 2); + } + doc.Cells.Add(GenerateMarkdownCell(commentBlock.Trim())); } } diff --git a/test/Microsoft.SqlTools.ServiceLayer.UnitTests/Microsoft.SqlTools.ServiceLayer.UnitTests.csproj b/test/Microsoft.SqlTools.ServiceLayer.UnitTests/Microsoft.SqlTools.ServiceLayer.UnitTests.csproj index 46ba3d63..bdd8d1ef 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.UnitTests/Microsoft.SqlTools.ServiceLayer.UnitTests.csproj +++ b/test/Microsoft.SqlTools.ServiceLayer.UnitTests/Microsoft.SqlTools.ServiceLayer.UnitTests.csproj @@ -11,7 +11,7 @@ - + diff --git a/test/Microsoft.SqlTools.ServiceLayer.UnitTests/NotebookConvert/NotebookConvertServiceTests.cs b/test/Microsoft.SqlTools.ServiceLayer.UnitTests/NotebookConvert/NotebookConvertServiceTests.cs new file mode 100644 index 00000000..2c8c8171 --- /dev/null +++ b/test/Microsoft.SqlTools.ServiceLayer.UnitTests/NotebookConvert/NotebookConvertServiceTests.cs @@ -0,0 +1,105 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +using NUnit.Framework; +using Microsoft.SqlTools.ServiceLayer.NotebookConvert; +using Newtonsoft.Json; + +namespace Microsoft.SqlTools.ServiceLayer.UnitTests.NotebookConvert +{ + + [TestFixture] + public class NotebookConvertServiceTests + { + [Test] + public void ConvertSqlToNotebook() + { + var sql = @" +/* + * Initial multiline comment + */ + +-- Comment before batch +SELECT * FROM sys.databases + +-- Compare Row Counts in Tables From Two Different Databases With the Same Schema + +SELECT * -- inline single line comment +/* inline multiline + * comment + */ +FROM sys.databases + +-- ending single line comment +/** + * Ending multiline + * comment + */ +"; + + var expectedNotebook = @"{ + ""metadata"": { + ""kernelspec"": { + ""name"": ""SQL"", + ""display_name"": ""SQL"", + ""language"": ""sql"" + }, + ""language_info"": { + ""name"": ""sql"", + ""version"": """" + } + }, + ""nbformat_minor"": 2, + ""nbformat"": 4, + ""cells"": [ + { + ""cell_type"": ""markdown"", + ""source"": [ + ""* Initial multiline comment"" + ] + }, + { + ""cell_type"": ""markdown"", + ""source"": [ + ""Comment before batch"" + ] + }, + { + ""cell_type"": ""code"", + ""source"": [ + ""SELECT * FROM sys.databases\n"", + ""\n"", + ""-- Compare Row Counts in Tables From Two Different Databases With the Same Schema\n"", + ""\n"", + ""SELECT * -- inline single line comment\n"", + ""/* inline multiline\n"", + "" * comment\n"", + "" */\n"", + ""FROM sys.databases"" + ] + }, + { + ""cell_type"": ""markdown"", + ""source"": [ + ""ending single line comment"" + ] + }, + { + ""cell_type"": ""markdown"", + ""source"": [ + ""* Ending multiline \n"", + "" * comment"" + ] + } + ] +}"; + + var notebook = NotebookConvertService.ConvertSqlToNotebook(sql); + var notebookString = JsonConvert.SerializeObject(notebook, Formatting.Indented); + Assert.AreEqual(expectedNotebook, notebookString); + } + + } +}