forked from qt-creator/qt-creator
Markdown: Reuse Markdown highlighter from change log viewer
Change-Id: Ief1b0c135a34bfd5e9b5220e9fbf93f281d8e95a Reviewed-by: David Schulz <david.schulz@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
@@ -3,10 +3,9 @@
|
||||
|
||||
#include "stringutils.h"
|
||||
|
||||
#include "algorithm.h"
|
||||
#include "hostosinfo.h"
|
||||
#include "qtcassert.h"
|
||||
#include "filepath.h"
|
||||
#include "qtcassert.h"
|
||||
#include "theme/theme.h"
|
||||
#include "utilstr.h"
|
||||
|
||||
#ifdef QT_WIDGETS_LIB
|
||||
@@ -15,11 +14,13 @@
|
||||
#endif
|
||||
|
||||
#include <QDir>
|
||||
#include <QFontMetrics>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonValue>
|
||||
#include <QLocale>
|
||||
#include <QRegularExpression>
|
||||
#include <QSet>
|
||||
#include <QTextList>
|
||||
#include <QTime>
|
||||
|
||||
#include <limits.h>
|
||||
@@ -568,4 +569,60 @@ QTCREATOR_UTILS_EXPORT int endOfNextWord(const QString &string, int position)
|
||||
return std::distance(string.begin(), it);
|
||||
}
|
||||
|
||||
MarkdownHighlighter::MarkdownHighlighter(QTextDocument *parent)
|
||||
: QSyntaxHighlighter(parent)
|
||||
, h2Brush(Qt::NoBrush)
|
||||
{
|
||||
parent->setIndentWidth(30); // default value is 40
|
||||
}
|
||||
|
||||
void MarkdownHighlighter::highlightBlock(const QString &text)
|
||||
{
|
||||
if (text.isEmpty())
|
||||
return;
|
||||
|
||||
QTextBlockFormat fmt = currentBlock().blockFormat();
|
||||
QTextCursor cur(currentBlock());
|
||||
if (fmt.hasProperty(QTextFormat::HeadingLevel)) {
|
||||
fmt.setTopMargin(10);
|
||||
fmt.setBottomMargin(10);
|
||||
|
||||
// Draw an underline for Heading 2, by creating a texture brush
|
||||
// with the last pixel visible
|
||||
if (fmt.property(QTextFormat::HeadingLevel) == 2) {
|
||||
QTextCharFormat charFmt = currentBlock().charFormat();
|
||||
charFmt.setBaselineOffset(15);
|
||||
setFormat(0, text.length(), charFmt);
|
||||
|
||||
if (h2Brush.style() == Qt::NoBrush) {
|
||||
const int height = QFontMetrics(charFmt.font()).height();
|
||||
QImage image(1, height, QImage::Format_ARGB32);
|
||||
|
||||
image.fill(QColor(0, 0, 0, 0).rgba());
|
||||
image.setPixel(0,
|
||||
height - 1,
|
||||
Utils::creatorTheme()->color(Theme::TextColorDisabled).rgba());
|
||||
|
||||
h2Brush = QBrush(image);
|
||||
}
|
||||
fmt.setBackground(h2Brush);
|
||||
}
|
||||
cur.setBlockFormat(fmt);
|
||||
} else if (fmt.hasProperty(QTextFormat::BlockCodeLanguage) && fmt.indent() == 0) {
|
||||
// set identation for code blocks
|
||||
fmt.setIndent(1);
|
||||
cur.setBlockFormat(fmt);
|
||||
}
|
||||
|
||||
// Show the bulet points as filled circles
|
||||
QTextList *list = cur.currentList();
|
||||
if (list) {
|
||||
QTextListFormat listFmt = list->format();
|
||||
if (listFmt.indent() == 1 && listFmt.style() == QTextListFormat::ListCircle) {
|
||||
listFmt.setStyle(QTextListFormat::ListDisc);
|
||||
list->setFormat(listFmt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Utils
|
||||
|
||||
Reference in New Issue
Block a user