mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 18:47:57 -05:00
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:
@@ -15,14 +15,13 @@ using Microsoft.SqlTools.ServiceLayer.Workspace;
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Microsoft.SqlServer.TransactSql.ScriptDom;
|
using Microsoft.SqlServer.TransactSql.ScriptDom;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Microsoft.SqlTools.ServiceLayer.NotebookConvert;
|
|
||||||
using Newtonsoft.Json.Linq;
|
|
||||||
|
|
||||||
namespace Microsoft.SqlTools.ServiceLayer.NotebookConvert
|
namespace Microsoft.SqlTools.ServiceLayer.NotebookConvert
|
||||||
{
|
{
|
||||||
enum NotebookTokenType
|
enum NotebookTokenType
|
||||||
{
|
{
|
||||||
MultilineComment,
|
MultilineComment,
|
||||||
|
SinglelineComment,
|
||||||
Batch
|
Batch
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,7 +118,7 @@ namespace Microsoft.SqlTools.ServiceLayer.NotebookConvert
|
|||||||
|
|
||||||
#endregion // Convert Handlers
|
#endregion // Convert Handlers
|
||||||
|
|
||||||
private static NotebookDocument ConvertSqlToNotebook(string sql)
|
internal static NotebookDocument ConvertSqlToNotebook(string sql)
|
||||||
{
|
{
|
||||||
// Notebooks use \n so convert any other newlines now
|
// Notebooks use \n so convert any other newlines now
|
||||||
sql = sql.Replace("\r\n", "\n");
|
sql = sql.Replace("\r\n", "\n");
|
||||||
@@ -160,11 +159,11 @@ namespace Microsoft.SqlTools.ServiceLayer.NotebookConvert
|
|||||||
* cells that could break the script
|
* cells that could break the script
|
||||||
*/
|
*/
|
||||||
var multilineComments = tokens
|
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
|
// 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
|
// 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)))
|
.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
|
// Combine batches and comments into a single list of all the fragments we need to add to the Notebook
|
||||||
var allFragments = batches.Select(batch =>
|
var allFragments = batches.Select(batch =>
|
||||||
@@ -204,6 +203,16 @@ namespace Microsoft.SqlTools.ServiceLayer.NotebookConvert
|
|||||||
commentBlock = commentBlock.Remove(commentBlock.Length - 2);
|
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()));
|
doc.Cells.Add(GenerateMarkdownCell(commentBlock.Trim()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user