From ff153d9874f728c9ec3ab40b87f55ccf0239e538 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Mon, 16 May 2022 13:32:44 +0200 Subject: Avoid ending Markdown fenced code blocks with gratuitous blank lines This caused unnecessary empty
 blocks when converting markdown to
HTML, made code blocks too large using QSyntaxHighlighter to highlight
the whole block, and caused assymmetry when rewriting markdown.

Pick-to: 6.3
Fixes: QTBUG-101031
Change-Id: I08016577ccb92edb4afae31d7df3259cb011d5c8
Reviewed-by: Qt CI Bot 
Reviewed-by: Allan Sandfeld Jensen 
---
 src/gui/text/qtextmarkdownimporter.cpp | 12 ++++++++++--
 src/gui/text/qtextmarkdownwriter.cpp   |  4 ++--
 2 files changed, 12 insertions(+), 4 deletions(-)

(limited to 'src')

diff --git a/src/gui/text/qtextmarkdownimporter.cpp b/src/gui/text/qtextmarkdownimporter.cpp
index c434bf859a..1b6222761f 100644
--- a/src/gui/text/qtextmarkdownimporter.cpp
+++ b/src/gui/text/qtextmarkdownimporter.cpp
@@ -498,6 +498,14 @@ int QTextMarkdownImporter::cbText(int textType, const char *text, unsigned size)
     case MD_BLOCK_TD:
         m_nonEmptyTableCells.append(m_tableCol);
         break;
+    case MD_BLOCK_CODE:
+        if (s == Newline) {
+            // defer a blank line until we see something else in the code block,
+            // to avoid ending every code block with a gratuitous blank line
+            m_needsInsertBlock = true;
+            s = QString();
+        }
+        break;
     default:
         break;
     }
@@ -531,12 +539,12 @@ int QTextMarkdownImporter::cbText(int textType, const char *text, unsigned size)
                     QString::number(bfmt.intProperty(QTextFormat::BlockQuoteLevel));
         if (bfmt.hasProperty(QTextFormat::BlockCodeLanguage))
             debugInfo += "in a code block"_L1;
+        if (m_cursor->currentList())
+            debugInfo += "in a list"_L1;
         qCDebug(lcMD) << textType << "in block" << m_blockType << s << qPrintable(debugInfo)
                       << "bindent" << bfmt.indent() << "tindent" << bfmt.textIndent()
                       << "margins" << bfmt.leftMargin() << bfmt.topMargin() << bfmt.bottomMargin() << bfmt.rightMargin();
     }
-    qCDebug(lcMD) << textType << "in block" << m_blockType << s << "in list?" << m_cursor->currentList()
-                  << "indent" << m_cursor->blockFormat().indent();
     return 0; // no error
 }
 
diff --git a/src/gui/text/qtextmarkdownwriter.cpp b/src/gui/text/qtextmarkdownwriter.cpp
index fe772d3944..9f651a04fd 100644
--- a/src/gui/text/qtextmarkdownwriter.cpp
+++ b/src/gui/text/qtextmarkdownwriter.cpp
@@ -156,11 +156,11 @@ void QTextMarkdownWriter::writeFrame(const QTextFrame *frame)
                 for (int col = cell.column(); col < spanEndCol; ++col)
                     m_stream << "|";
             } else if (m_fencedCodeBlock && ending) {
-                m_stream << m_linePrefix << QString(m_wrappedLineIndent, Space)
+                m_stream << Newline << m_linePrefix << QString(m_wrappedLineIndent, Space)
                          << m_codeBlockFence << Newline << Newline;
                 m_codeBlockFence.clear();
             } else if (m_indentedCodeBlock && nextIsDifferent) {
-                m_stream << Newline;
+                m_stream << Newline << Newline;
             } else if (endingCol > 0) {
                 if (block.textList() || block.blockFormat().hasProperty(QTextFormat::BlockCodeLanguage)) {
                     m_stream << Newline;
-- 
cgit v1.2.3