highlighter fix

This commit is contained in:
Le Tan 2018-04-20 19:55:28 +08:00
parent 67e40cbe87
commit 6fe82d3db0
6 changed files with 59 additions and 42 deletions

View File

@ -856,7 +856,7 @@ VMarkdownTab::VMarkdownTab(QWidget *p_parent)
{ {
// Default note open mode. // Default note open mode.
m_openModeCombo = VUtils::getComboBox(); m_openModeCombo = VUtils::getComboBox();
m_openModeCombo->setToolTip(tr("Default mode to open a note")); m_openModeCombo->setToolTip(tr("Default mode to open an internal note"));
m_openModeCombo->addItem(tr("Read Mode"), (int)OpenFileMode::Read); m_openModeCombo->addItem(tr("Read Mode"), (int)OpenFileMode::Read);
m_openModeCombo->addItem(tr("Edit Mode"), (int)OpenFileMode::Edit); m_openModeCombo->addItem(tr("Edit Mode"), (int)OpenFileMode::Edit);
@ -865,7 +865,7 @@ VMarkdownTab::VMarkdownTab(QWidget *p_parent)
m_headingSequenceTypeCombo->setToolTip(tr("Enable auto sequence for all headings (in the form like 1.2.3.4.)")); m_headingSequenceTypeCombo->setToolTip(tr("Enable auto sequence for all headings (in the form like 1.2.3.4.)"));
m_headingSequenceTypeCombo->addItem(tr("Disabled"), (int)HeadingSequenceType::Disabled); m_headingSequenceTypeCombo->addItem(tr("Disabled"), (int)HeadingSequenceType::Disabled);
m_headingSequenceTypeCombo->addItem(tr("Enabled"), (int)HeadingSequenceType::Enabled); m_headingSequenceTypeCombo->addItem(tr("Enabled"), (int)HeadingSequenceType::Enabled);
m_headingSequenceTypeCombo->addItem(tr("Enabled for notes only"), (int)HeadingSequenceType::EnabledNoteOnly); m_headingSequenceTypeCombo->addItem(tr("Enabled for intrenal notes only"), (int)HeadingSequenceType::EnabledNoteOnly);
m_headingSequenceLevelCombo = VUtils::getComboBox(); m_headingSequenceLevelCombo = VUtils::getComboBox();
m_headingSequenceLevelCombo->setToolTip(tr("Base level to start heading sequence")); m_headingSequenceLevelCombo->setToolTip(tr("Base level to start heading sequence"));

View File

@ -36,6 +36,7 @@ HGMarkdownHighlighter::HGMarkdownHighlighter(const QVector<HighlightingStyle> &s
m_blockHLResultReady(false), m_blockHLResultReady(false),
waitInterval(waitInterval), waitInterval(waitInterval),
m_enableMathjax(false), m_enableMathjax(false),
m_signalOut(false),
content(NULL), content(NULL),
capacity(0), capacity(0),
result(NULL) result(NULL)
@ -82,15 +83,12 @@ HGMarkdownHighlighter::HGMarkdownHighlighter(const QVector<HighlightingStyle> &s
startParseAndHighlight(false); startParseAndHighlight(false);
}); });
static const int completeWaitTime = 500; const int completeWaitTime = 400;
m_completeTimer = new QTimer(this); m_completeTimer = new QTimer(this);
m_completeTimer->setSingleShot(true); m_completeTimer->setSingleShot(true);
m_completeTimer->setInterval(completeWaitTime); m_completeTimer->setInterval(completeWaitTime);
connect(m_completeTimer, &QTimer::timeout, connect(m_completeTimer, &QTimer::timeout,
this, [this]() { this, &HGMarkdownHighlighter::completeHighlight);
updateMathjaxBlocks();
emit highlightCompleted();
});
connect(document, &QTextDocument::contentsChange, connect(document, &QTextDocument::contentsChange,
this, &HGMarkdownHighlighter::handleContentChange); this, &HGMarkdownHighlighter::handleContentChange);
@ -331,7 +329,6 @@ void HGMarkdownHighlighter::initImageRegionsFromResult()
m_imageRegions.clear(); m_imageRegions.clear();
if (!result) { if (!result) {
emit imageLinksUpdated(m_imageRegions);
return; return;
} }
@ -345,10 +342,6 @@ void HGMarkdownHighlighter::initImageRegionsFromResult()
m_imageRegions.push_back(VElementRegion(elem->pos, elem->end)); m_imageRegions.push_back(VElementRegion(elem->pos, elem->end));
elem = elem->next; elem = elem->next;
} }
qDebug() << "highlighter: parse" << m_imageRegions.size() << "image regions";
emit imageLinksUpdated(m_imageRegions);
} }
void HGMarkdownHighlighter::initVerbatimBlocksFromResult() void HGMarkdownHighlighter::initVerbatimBlocksFromResult()
@ -383,7 +376,6 @@ void HGMarkdownHighlighter::initHeaderRegionsFromResult()
m_headerRegions.clear(); m_headerRegions.clear();
if (!result) { if (!result) {
emit headersUpdated(m_headerRegions);
return; return;
} }
@ -410,10 +402,6 @@ void HGMarkdownHighlighter::initHeaderRegionsFromResult()
} }
std::sort(m_headerRegions.begin(), m_headerRegions.end()); std::sort(m_headerRegions.begin(), m_headerRegions.end());
qDebug() << "highlighter: parse" << m_headerRegions.size() << "header regions";
emit headersUpdated(m_headerRegions);
} }
void HGMarkdownHighlighter::initBlockHighlihgtOne(unsigned long pos, void HGMarkdownHighlighter::initBlockHighlihgtOne(unsigned long pos,
@ -756,7 +744,7 @@ void HGMarkdownHighlighter::highlightLinkWithSpacesInURL(const QString &p_text)
} }
} }
void HGMarkdownHighlighter::parse(bool p_fast) void HGMarkdownHighlighter::parse()
{ {
if (!parsing.testAndSetRelaxed(0, 1)) { if (!parsing.testAndSetRelaxed(0, 1)) {
return; return;
@ -776,21 +764,19 @@ void HGMarkdownHighlighter::parse(bool p_fast)
m_blockHLResultReady = true; m_blockHLResultReady = true;
if (!p_fast) { initHtmlCommentRegionsFromResult();
initHtmlCommentRegionsFromResult();
initImageRegionsFromResult(); initImageRegionsFromResult();
initHeaderRegionsFromResult(); initHeaderRegionsFromResult();
initVerbatimBlocksFromResult(); initVerbatimBlocksFromResult();
initInlineCodeRegionsFromResult(); initInlineCodeRegionsFromResult();
initBoldItalicRegionsFromResult(); initBoldItalicRegionsFromResult();
initLinkRegionsFromResult(); initLinkRegionsFromResult();
}
if (result) { if (result) {
pmh_free_elements(result); pmh_free_elements(result);
@ -834,6 +820,8 @@ void HGMarkdownHighlighter::handleContentChange(int /* position */, int charsRem
return; return;
} }
m_signalOut = false;
timer->stop(); timer->stop();
timer->start(); timer->start();
} }
@ -841,17 +829,15 @@ void HGMarkdownHighlighter::handleContentChange(int /* position */, int charsRem
void HGMarkdownHighlighter::startParseAndHighlight(bool p_fast) void HGMarkdownHighlighter::startParseAndHighlight(bool p_fast)
{ {
qDebug() << "HGMarkdownHighlighter start a new parse (fast" << p_fast << ")"; qDebug() << "HGMarkdownHighlighter start a new parse (fast" << p_fast << ")";
parse(p_fast); parse();
if (p_fast) { m_signalOut = !p_fast;
rehighlight();
} else {
if (!updateCodeBlocks()) {
rehighlight();
}
highlightChanged(); if (!p_fast) {
updateCodeBlocks();
} }
rehighlight();
} }
void HGMarkdownHighlighter::updateHighlight() void HGMarkdownHighlighter::updateHighlight()
@ -1341,3 +1327,15 @@ bool HGMarkdownHighlighter::isValidMathjaxRegion(int p_blockNumber,
return true; return true;
} }
void HGMarkdownHighlighter::completeHighlight()
{
if (m_signalOut) {
m_signalOut = false;
updateMathjaxBlocks();
emit imageLinksUpdated(m_imageRegions);
emit headersUpdated(m_headerRegions);
}
emit highlightCompleted();
}

View File

@ -241,6 +241,8 @@ private slots:
// @p_fast: if true, just parse and update styles. // @p_fast: if true, just parse and update styles.
void startParseAndHighlight(bool p_fast = false); void startParseAndHighlight(bool p_fast = false);
void completeHighlight();
private: private:
struct HeaderBlockInfo struct HeaderBlockInfo
{ {
@ -330,6 +332,9 @@ private:
// Comment regions for each block. // Comment regions for each block.
QHash<int, QVector<VElementRegion>> m_commentRegions; QHash<int, QVector<VElementRegion>> m_commentRegions;
// Whether need to signal out changes when highlight completes.
bool m_signalOut;
char *content; char *content;
int capacity; int capacity;
pmh_element **result; pmh_element **result;
@ -347,7 +352,7 @@ private:
// intended to complement this. // intended to complement this.
void highlightLinkWithSpacesInURL(const QString &p_text); void highlightLinkWithSpacesInURL(const QString &p_text);
void parse(bool p_fast = false); void parse();
void parseInternal(); void parseInternal();

View File

@ -82,8 +82,16 @@ void VListWidget::resizeEvent(QResizeEvent *p_event)
void VListWidget::handleSearchModeTriggered(bool p_inSearchMode, bool p_focus) void VListWidget::handleSearchModeTriggered(bool p_inSearchMode, bool p_focus)
{ {
setSearchInputVisible(p_inSearchMode); if (p_inSearchMode) {
if (!p_inSearchMode) { setSearchInputVisible(p_inSearchMode);
} else {
// Hiding search input will make QWebEngine get focus which will consume
// the Esc key sequence by mistake.
if (p_focus) {
setFocus();
}
setSearchInputVisible(p_inSearchMode);
clearItemsHighlight(); clearItemsHighlight();
} }

View File

@ -496,7 +496,7 @@ void VMdEditor::updateHeaders(const QVector<VElementRegion> &p_headerRegions)
<< block.text(); << block.text();
} }
if ((block.userState() == HighlightBlockState::Normal) if ((block.userState() == HighlightBlockState::Header)
&& headerReg.exactMatch(block.text())) { && headerReg.exactMatch(block.text())) {
int level = headerReg.cap(1).length(); int level = headerReg.cap(1).length();
VTableOfContentItem header(headerReg.cap(2).trimmed(), VTableOfContentItem header(headerReg.cap(2).trimmed(),

View File

@ -323,6 +323,7 @@ int VPreviewManager::calculateBlockMargin(const QTextBlock &p_block, int p_tabSt
void VPreviewManager::updateBlockPreviewInfo(TS p_timeStamp, void VPreviewManager::updateBlockPreviewInfo(TS p_timeStamp,
const QVector<ImageLinkInfo> &p_imageLinks) const QVector<ImageLinkInfo> &p_imageLinks)
{ {
QSet<int> affectedBlocks;
for (auto const & link : p_imageLinks) { for (auto const & link : p_imageLinks) {
QTextBlock block = m_document->findBlockByNumber(link.m_blockNumber); QTextBlock block = m_document->findBlockByNumber(link.m_blockNumber);
if (!block.isValid()) { if (!block.isValid()) {
@ -345,16 +346,21 @@ void VPreviewManager::updateBlockPreviewInfo(TS p_timeStamp,
!link.m_isBlock, !link.m_isBlock,
name, name,
m_editor->imageSize(name)); m_editor->imageSize(name));
blockData->insertPreviewInfo(info); bool tsUpdated = blockData->insertPreviewInfo(info);
imageCache(PreviewSource::ImageLink).insert(name, p_timeStamp); imageCache(PreviewSource::ImageLink).insert(name, p_timeStamp);
if (!tsUpdated) {
// No need to relayout the block if only timestamp is updated.
affectedBlocks.insert(link.m_blockNumber);
m_highlighter->addPossiblePreviewBlock(link.m_blockNumber);
}
qDebug() << "block" << link.m_blockNumber qDebug() << "block" << link.m_blockNumber
<< imageCache(PreviewSource::ImageLink).size() << imageCache(PreviewSource::ImageLink).size()
<< blockData->toString(); << blockData->toString();
} }
// TODO: may need to call m_editor->update()? m_editor->relayout(affectedBlocks);
m_editor->update();
} }
void VPreviewManager::updateBlockPreviewInfo(TS p_timeStamp, void VPreviewManager::updateBlockPreviewInfo(TS p_timeStamp,