summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2020-04-19 00:39:32 +0200
committerShawn Rutledge <shawn.rutledge@qt.io>2020-04-20 21:08:23 +0200
commit0fcd782bd3690867f19420270bcc329325f04424 (patch)
treef74aef28ee2e86251a9188aabb779f339c401bc2
parentfa83b30ceb1861075e33dc8340d6754daef86b95 (diff)
Markdown writer: don't wrap code block, and detect its end
The end of a code block nested in a list item is now detected; and if the text of the list item continues after the code block, it continues to be indented. Code blocks should never be word-wrapped. Fixes: QTBUG-80603 Change-Id: I4427f8b1d4807d819616f5cb971e2d006170d9be Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
-rw-r--r--src/gui/text/qtextmarkdownwriter.cpp17
-rw-r--r--tests/auto/gui/text/qtextmarkdownwriter/data/listsAndCodeBlocks.md24
-rw-r--r--tests/auto/gui/text/qtextmarkdownwriter/tst_qtextmarkdownwriter.cpp1
3 files changed, 35 insertions, 7 deletions
diff --git a/src/gui/text/qtextmarkdownwriter.cpp b/src/gui/text/qtextmarkdownwriter.cpp
index 7bd321becc..1788999855 100644
--- a/src/gui/text/qtextmarkdownwriter.cpp
+++ b/src/gui/text/qtextmarkdownwriter.cpp
@@ -367,6 +367,14 @@ int QTextMarkdownWriter::writeBlock(const QTextBlock &block, bool wrap, bool ign
const int ColumnLimit = 80;
QTextBlockFormat blockFmt = block.blockFormat();
bool missedBlankCodeBlockLine = false;
+ const bool codeBlock = blockFmt.hasProperty(QTextFormat::BlockCodeFence) ||
+ blockFmt.stringProperty(QTextFormat::BlockCodeLanguage).length() > 0;
+ if (m_fencedCodeBlock && !codeBlock) {
+ m_stream << m_linePrefix << QString(m_wrappedLineIndent, Space)
+ << m_codeBlockFence << Newline;
+ m_fencedCodeBlock = false;
+ m_codeBlockFence.clear();
+ }
if (block.textList()) { // it's a list-item
auto fmt = block.textList()->format();
const int listLevel = fmt.indent();
@@ -427,7 +435,7 @@ int QTextMarkdownWriter::writeBlock(const QTextBlock &block, bool wrap, bool ign
} else if (blockFmt.hasProperty(QTextFormat::BlockTrailingHorizontalRulerWidth)) {
m_stream << "- - -\n"; // unambiguous horizontal rule, not an underline under a heading
return 0;
- } else if (blockFmt.hasProperty(QTextFormat::BlockCodeFence) || blockFmt.stringProperty(QTextFormat::BlockCodeLanguage).length() > 0) {
+ } else if (codeBlock) {
// It's important to preserve blank lines in code blocks. But blank lines in code blocks
// inside block quotes are getting preserved anyway (along with the "> " prefix).
if (!blockFmt.hasProperty(QTextFormat::BlockQuoteLevel))
@@ -442,13 +450,8 @@ int QTextMarkdownWriter::writeBlock(const QTextBlock &block, bool wrap, bool ign
<< Space << blockFmt.stringProperty(QTextFormat::BlockCodeLanguage) << Newline;
m_fencedCodeBlock = true;
}
+ wrap = false;
} else if (!blockFmt.indent()) {
- if (m_fencedCodeBlock) {
- m_stream << m_linePrefix << QString(m_wrappedLineIndent, Space)
- << m_codeBlockFence << Newline;
- m_fencedCodeBlock = false;
- m_codeBlockFence.clear();
- }
m_wrappedLineIndent = 0;
m_linePrefix.clear();
if (blockFmt.hasProperty(QTextFormat::BlockQuoteLevel)) {
diff --git a/tests/auto/gui/text/qtextmarkdownwriter/data/listsAndCodeBlocks.md b/tests/auto/gui/text/qtextmarkdownwriter/data/listsAndCodeBlocks.md
new file mode 100644
index 0000000000..c65e01408b
--- /dev/null
+++ b/tests/auto/gui/text/qtextmarkdownwriter/data/listsAndCodeBlocks.md
@@ -0,0 +1,24 @@
+- something happens in the debugger like this:
+
+ ```
+ 1 QQuickEventPoint::setGrabberItem qquickevents.cpp 869 0x7ffff7a963f2
+ 2 QQuickItem::grabMouse qquickitem.cpp 7599 0x7ffff7abea29
+ 3 QQuickWindowPrivate::deliverMatchingPointsToItem qquickwindow.cpp 2738 0x7ffff7aea34c
+ 4 QQuickWindowPrivate::deliverPressOrReleaseEvent qquickwindow.cpp 2692 0x7ffff7ae9e57
+ 5 QQuickWindowPrivate::deliverMouseEvent qquickwindow.cpp 1911 0x7ffff7ae561b
+ 6 QQuickWindowPrivate::deliverPointerEvent qquickwindow.cpp 2454 0x7ffff7ae888c
+ 7 QQuickWindowPrivate::handleMouseEvent qquickwindow.cpp 2282 0x7ffff7ae7f1a
+ 8 QQuickWindow::mousePressEvent qquickwindow.cpp 2249 0x7ffff7ae7bf5
+ 9 QQuickView::mousePressEvent qquickview.cpp 626 0x7ffff7bd6bad
+ 10 QWindow::event qwindow.cpp 2258 0x7ffff70b2c54
+ ```
+ and then I want to explain something about it.
+
+- something I tried to fix it:
+
+ ```
+ code goes here, probably c++
+ ```
+- still didn't fix it, expecting a breakthrough any day now
+- some sort of miracle
+- profit!
diff --git a/tests/auto/gui/text/qtextmarkdownwriter/tst_qtextmarkdownwriter.cpp b/tests/auto/gui/text/qtextmarkdownwriter/tst_qtextmarkdownwriter.cpp
index 31592c7f0f..84cf237ccc 100644
--- a/tests/auto/gui/text/qtextmarkdownwriter/tst_qtextmarkdownwriter.cpp
+++ b/tests/auto/gui/text/qtextmarkdownwriter/tst_qtextmarkdownwriter.cpp
@@ -369,6 +369,7 @@ void tst_QTextMarkdownWriter::rewriteDocument_data()
QTest::newRow("list items after headings") << "headingsAndLists.md";
QTest::newRow("word wrap") << "wordWrap.md";
QTest::newRow("links") << "links.md";
+ QTest::newRow("lists and code blocks") << "listsAndCodeBlocks.md";
}
void tst_QTextMarkdownWriter::rewriteDocument()