Include singleline comments outside batch for nb convert (#1139)

* Include singleline comments outside batch for nb convert

* Remove extra space
This commit is contained in:
Charles Gagnon
2020-12-14 15:16:07 -08:00
committed by GitHub
parent a47fa8bf87
commit 5c7211e82a
3 changed files with 120 additions and 6 deletions

View File

@@ -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()));
}
}

View File

@@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="System.Text.Encoding.CodePages" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="Moq" />
<PackageReference Include="nunit" />
<PackageReference Include="nunit3testadapter" />
<PackageReference Include="nunit.console" />

View File

@@ -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);
}
}
}