hgmarkdownhighlighter.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. #include <QtGui>
  2. #include <QtDebug>
  3. #include <QTextCursor>
  4. #include <algorithm>
  5. #include "hgmarkdownhighlighter.h"
  6. #include "vconfigmanager.h"
  7. #include "utils/vutils.h"
  8. #include "vtextblockdata.h"
  9. extern VConfigManager *g_config;
  10. const int HGMarkdownHighlighter::initCapacity = 1024;
  11. void HGMarkdownHighlighter::resizeBuffer(int newCap)
  12. {
  13. if (newCap == capacity) {
  14. return;
  15. }
  16. if (capacity > 0) {
  17. Q_ASSERT(content);
  18. delete [] content;
  19. }
  20. capacity = newCap;
  21. content = new char [capacity];
  22. }
  23. // Will be freeed by parent automatically
  24. HGMarkdownHighlighter::HGMarkdownHighlighter(const QVector<HighlightingStyle> &styles,
  25. const QHash<QString, QTextCharFormat> &codeBlockStyles,
  26. int waitInterval,
  27. QTextDocument *parent)
  28. : QSyntaxHighlighter(parent), highlightingStyles(styles),
  29. m_codeBlockStyles(codeBlockStyles), m_numOfCodeBlockHighlightsToRecv(0),
  30. parsing(0), waitInterval(waitInterval), content(NULL), capacity(0), result(NULL)
  31. {
  32. codeBlockStartExp = QRegExp(VUtils::c_fencedCodeBlockStartRegExp);
  33. codeBlockEndExp = QRegExp(VUtils::c_fencedCodeBlockEndRegExp);
  34. codeBlockFormat.setForeground(QBrush(Qt::darkYellow));
  35. for (int index = 0; index < styles.size(); ++index) {
  36. const pmh_element_type &eleType = styles[index].type;
  37. if (eleType == pmh_VERBATIM) {
  38. codeBlockFormat = styles[index].format;
  39. } else if (eleType == pmh_LINK) {
  40. m_linkFormat = styles[index].format;
  41. } else if (eleType == pmh_IMAGE) {
  42. m_imageFormat = styles[index].format;
  43. }
  44. }
  45. m_colorColumnFormat = codeBlockFormat;
  46. m_colorColumnFormat.setForeground(QColor(g_config->getEditorColorColumnFg()));
  47. m_colorColumnFormat.setBackground(QColor(g_config->getEditorColorColumnBg()));
  48. resizeBuffer(initCapacity);
  49. document = parent;
  50. timer = new QTimer(this);
  51. timer->setSingleShot(true);
  52. timer->setInterval(this->waitInterval);
  53. connect(timer, &QTimer::timeout, this, &HGMarkdownHighlighter::timerTimeout);
  54. static const int completeWaitTime = 500;
  55. m_completeTimer = new QTimer(this);
  56. m_completeTimer->setSingleShot(true);
  57. m_completeTimer->setInterval(completeWaitTime);
  58. connect(m_completeTimer, &QTimer::timeout,
  59. this, &HGMarkdownHighlighter::highlightCompleted);
  60. connect(document, &QTextDocument::contentsChange,
  61. this, &HGMarkdownHighlighter::handleContentChange);
  62. }
  63. HGMarkdownHighlighter::~HGMarkdownHighlighter()
  64. {
  65. if (result) {
  66. pmh_free_elements(result);
  67. result = NULL;
  68. }
  69. if (content) {
  70. delete [] content;
  71. capacity = 0;
  72. content = NULL;
  73. }
  74. }
  75. void HGMarkdownHighlighter::updateBlockUserData(int p_blockNum, const QString &p_text)
  76. {
  77. VTextBlockData *blockData = dynamic_cast<VTextBlockData *>(currentBlockUserData());
  78. if (!blockData) {
  79. blockData = new VTextBlockData();
  80. setCurrentBlockUserData(blockData);
  81. }
  82. bool contains = p_text.contains(QChar::ObjectReplacementCharacter);
  83. blockData->setContainsPreviewImage(contains);
  84. auto curIt = m_potentialPreviewBlocks.find(p_blockNum);
  85. if (curIt == m_potentialPreviewBlocks.end()) {
  86. if (contains) {
  87. m_potentialPreviewBlocks.insert(p_blockNum, true);
  88. }
  89. } else {
  90. if (!contains) {
  91. m_potentialPreviewBlocks.erase(curIt);
  92. }
  93. }
  94. // Delete elements beyond current block count.
  95. int blocks = document->blockCount();
  96. if (!m_potentialPreviewBlocks.isEmpty()) {
  97. auto it = m_potentialPreviewBlocks.end();
  98. do {
  99. --it;
  100. if (it.key() >= blocks) {
  101. it = m_potentialPreviewBlocks.erase(it);
  102. } else {
  103. break;
  104. }
  105. } while (it != m_potentialPreviewBlocks.begin());
  106. }
  107. }
  108. void HGMarkdownHighlighter::highlightBlock(const QString &text)
  109. {
  110. int blockNum = currentBlock().blockNumber();
  111. if (!parsing && blockHighlights.size() > blockNum) {
  112. const QVector<HLUnit> &units = blockHighlights[blockNum];
  113. for (int i = 0; i < units.size(); ++i) {
  114. // TODO: merge two format within the same range
  115. const HLUnit &unit = units[i];
  116. setFormat(unit.start, unit.length, highlightingStyles[unit.styleIndex].format);
  117. }
  118. }
  119. // We use PEG Markdown Highlight as the main highlighter.
  120. // We can use other highlighting methods to complement it.
  121. // Set current block's user data.
  122. updateBlockUserData(blockNum, text);
  123. // If it is a block inside HTML comment, just skip it.
  124. if (isBlockInsideCommentRegion(currentBlock())) {
  125. setCurrentBlockState(HighlightBlockState::Comment);
  126. goto exit;
  127. }
  128. // PEG Markdown Highlight does not handle the ``` code block correctly.
  129. setCurrentBlockState(HighlightBlockState::Normal);
  130. highlightCodeBlock(text);
  131. // PEG Markdown Highlight does not handle links with spaces in the URL.
  132. // Links in the URL should be encoded to %20. We just let it be here and won't
  133. // fix this.
  134. // highlightLinkWithSpacesInURL(text);
  135. // Highlight CodeBlock using VCodeBlockHighlightHelper.
  136. if (m_codeBlockHighlights.size() > blockNum) {
  137. const QVector<HLUnitStyle> &units = m_codeBlockHighlights[blockNum];
  138. // Manually simply merge the format of all the units within the same block.
  139. // Using QTextCursor to get the char format after setFormat() seems
  140. // not to work.
  141. QVector<QTextCharFormat> formats;
  142. formats.reserve(units.size());
  143. // formatIndex[i] is the index in @formats which is the format of the
  144. // ith character.
  145. QVector<int> formatIndex(currentBlock().length(), -1);
  146. for (int i = 0; i < units.size(); ++i) {
  147. const HLUnitStyle &unit = units[i];
  148. auto it = m_codeBlockStyles.find(unit.style);
  149. if (it != m_codeBlockStyles.end()) {
  150. QTextCharFormat newFormat;
  151. if (unit.start < (unsigned int)formatIndex.size() && formatIndex[unit.start] != -1) {
  152. newFormat = formats[formatIndex[unit.start]];
  153. newFormat.merge(*it);
  154. } else {
  155. newFormat = *it;
  156. }
  157. setFormat(unit.start, unit.length, newFormat);
  158. formats.append(newFormat);
  159. int idx = formats.size() - 1;
  160. unsigned int endIdx = unit.length + unit.start;
  161. for (unsigned int i = unit.start; i < endIdx && i < (unsigned int)formatIndex.size(); ++i) {
  162. formatIndex[i] = idx;
  163. }
  164. }
  165. }
  166. }
  167. highlightCodeBlockColorColumn(text);
  168. exit:
  169. highlightChanged();
  170. }
  171. void HGMarkdownHighlighter::initBlockHighlightFromResult(int nrBlocks)
  172. {
  173. blockHighlights.resize(nrBlocks);
  174. for (int i = 0; i < blockHighlights.size(); ++i) {
  175. blockHighlights[i].clear();
  176. }
  177. if (!result) {
  178. return;
  179. }
  180. for (int i = 0; i < highlightingStyles.size(); i++)
  181. {
  182. const HighlightingStyle &style = highlightingStyles[i];
  183. pmh_element *elem_cursor = result[style.type];
  184. while (elem_cursor != NULL)
  185. {
  186. // elem_cursor->pos and elem_cursor->end is the start
  187. // and end position of the element in document.
  188. if (elem_cursor->end <= elem_cursor->pos) {
  189. elem_cursor = elem_cursor->next;
  190. continue;
  191. }
  192. initBlockHighlihgtOne(elem_cursor->pos, elem_cursor->end, i);
  193. elem_cursor = elem_cursor->next;
  194. }
  195. }
  196. }
  197. void HGMarkdownHighlighter::initHtmlCommentRegionsFromResult()
  198. {
  199. // From Qt5.7, the capacity is preserved.
  200. m_commentRegions.clear();
  201. if (!result) {
  202. return;
  203. }
  204. pmh_element *elem = result[pmh_COMMENT];
  205. while (elem != NULL) {
  206. if (elem->end <= elem->pos) {
  207. elem = elem->next;
  208. continue;
  209. }
  210. m_commentRegions.push_back(VElementRegion(elem->pos, elem->end));
  211. elem = elem->next;
  212. }
  213. qDebug() << "highlighter: parse" << m_commentRegions.size() << "HTML comment regions";
  214. }
  215. void HGMarkdownHighlighter::initImageRegionsFromResult()
  216. {
  217. if (!result) {
  218. // From Qt5.7, the capacity is preserved.
  219. m_imageRegions.clear();
  220. emit imageLinksUpdated(m_imageRegions);
  221. return;
  222. }
  223. int idx = 0;
  224. int oriSize = m_imageRegions.size();
  225. pmh_element *elem = result[pmh_IMAGE];
  226. while (elem != NULL) {
  227. if (elem->end <= elem->pos) {
  228. elem = elem->next;
  229. continue;
  230. }
  231. if (idx < oriSize) {
  232. // Try to reuse the original element.
  233. VElementRegion &reg = m_imageRegions[idx];
  234. if ((int)elem->pos != reg.m_startPos || (int)elem->end != reg.m_endPos) {
  235. reg.m_startPos = (int)elem->pos;
  236. reg.m_endPos = (int)elem->end;
  237. }
  238. } else {
  239. m_imageRegions.push_back(VElementRegion(elem->pos, elem->end));
  240. }
  241. ++idx;
  242. elem = elem->next;
  243. }
  244. if (idx < oriSize) {
  245. m_imageRegions.resize(idx);
  246. }
  247. qDebug() << "highlighter: parse" << m_imageRegions.size() << "image regions";
  248. emit imageLinksUpdated(m_imageRegions);
  249. }
  250. void HGMarkdownHighlighter::initHeaderRegionsFromResult()
  251. {
  252. if (!result) {
  253. // From Qt5.7, the capacity is preserved.
  254. m_headerRegions.clear();
  255. emit headersUpdated(m_headerRegions);
  256. return;
  257. }
  258. int idx = 0;
  259. int oriSize = m_headerRegions.size();
  260. pmh_element_type hx[6] = {pmh_H1, pmh_H2, pmh_H3, pmh_H4, pmh_H5, pmh_H6};
  261. for (int i = 0; i < 6; ++i) {
  262. pmh_element *elem = result[hx[i]];
  263. while (elem != NULL) {
  264. if (elem->end <= elem->pos) {
  265. elem = elem->next;
  266. continue;
  267. }
  268. if (idx < oriSize) {
  269. // Try to reuse the original element.
  270. VElementRegion &reg = m_headerRegions[idx];
  271. if ((int)elem->pos != reg.m_startPos || (int)elem->end != reg.m_endPos) {
  272. reg.m_startPos = (int)elem->pos;
  273. reg.m_endPos = (int)elem->end;
  274. }
  275. } else {
  276. m_headerRegions.push_back(VElementRegion(elem->pos, elem->end));
  277. }
  278. ++idx;
  279. elem = elem->next;
  280. }
  281. }
  282. if (idx < oriSize) {
  283. m_headerRegions.resize(idx);
  284. }
  285. std::sort(m_headerRegions.begin(), m_headerRegions.end());
  286. qDebug() << "highlighter: parse" << m_headerRegions.size() << "header regions";
  287. emit headersUpdated(m_headerRegions);
  288. }
  289. void HGMarkdownHighlighter::initBlockHighlihgtOne(unsigned long pos, unsigned long end, int styleIndex)
  290. {
  291. int startBlockNum = document->findBlock(pos).blockNumber();
  292. int endBlockNum = document->findBlock(end).blockNumber();
  293. for (int i = startBlockNum; i <= endBlockNum; ++i)
  294. {
  295. QTextBlock block = document->findBlockByNumber(i);
  296. int blockStartPos = block.position();
  297. HLUnit unit;
  298. if (i == startBlockNum) {
  299. unit.start = pos - blockStartPos;
  300. unit.length = (startBlockNum == endBlockNum) ?
  301. (end - pos) : (block.length() - unit.start);
  302. } else if (i == endBlockNum) {
  303. unit.start = 0;
  304. unit.length = end - blockStartPos;
  305. } else {
  306. unit.start = 0;
  307. unit.length = block.length();
  308. }
  309. unit.styleIndex = styleIndex;
  310. blockHighlights[i].append(unit);
  311. }
  312. }
  313. void HGMarkdownHighlighter::highlightCodeBlock(const QString &text)
  314. {
  315. static int startLeadingSpaces = -1;
  316. int length = 0;
  317. int index = -1;
  318. int preState = previousBlockState();
  319. int state = HighlightBlockState::Normal;
  320. if (preState != HighlightBlockState::CodeBlock
  321. && preState != HighlightBlockState::CodeBlockStart) {
  322. // Need to find a new code block start.
  323. index = codeBlockStartExp.indexIn(text);
  324. if (index >= 0) {
  325. // Start a new code block.
  326. length = text.length();
  327. state = HighlightBlockState::CodeBlockStart;
  328. // The leading spaces of code block start and end must be identical.
  329. startLeadingSpaces = codeBlockStartExp.capturedTexts()[1].size();
  330. } else {
  331. // A normal block.
  332. startLeadingSpaces = -1;
  333. return;
  334. }
  335. } else {
  336. // Need to find a code block end.
  337. index = codeBlockEndExp.indexIn(text);
  338. // The closing ``` should have the same indentation as the open ```.
  339. if (index >= 0
  340. && startLeadingSpaces == codeBlockEndExp.capturedTexts()[1].size()) {
  341. // End of code block.
  342. length = text.length();
  343. state = HighlightBlockState::CodeBlockEnd;
  344. } else {
  345. // Within code block.
  346. index = 0;
  347. length = text.length();
  348. state = HighlightBlockState::CodeBlock;
  349. }
  350. }
  351. setCurrentBlockState(state);
  352. setFormat(index, length, codeBlockFormat);
  353. }
  354. void HGMarkdownHighlighter::highlightCodeBlockColorColumn(const QString &p_text)
  355. {
  356. int cc = g_config->getColorColumn();
  357. if (cc <= 0 || currentBlockState() != HighlightBlockState::CodeBlock) {
  358. return;
  359. }
  360. if (p_text.size() < cc) {
  361. return;
  362. }
  363. setFormat(cc - 1, 1, m_colorColumnFormat);
  364. }
  365. void HGMarkdownHighlighter::highlightLinkWithSpacesInURL(const QString &p_text)
  366. {
  367. if (currentBlockState() == HighlightBlockState::CodeBlock) {
  368. return;
  369. }
  370. // TODO: should select links with spaces in URL.
  371. QRegExp regExp("[\\!]?\\[[^\\]]*\\]\\(([^\\n\\)]+)\\)");
  372. int index = regExp.indexIn(p_text);
  373. while (index >= 0) {
  374. Q_ASSERT(regExp.captureCount() == 1);
  375. int length = regExp.matchedLength();
  376. QString capturedText = regExp.capturedTexts()[1];
  377. if (capturedText.contains(' ')) {
  378. if (p_text[index] == '!' && m_imageFormat.isValid()) {
  379. setFormat(index, length, m_imageFormat);
  380. } else if (m_linkFormat.isValid()) {
  381. setFormat(index, length, m_linkFormat);
  382. }
  383. }
  384. index = regExp.indexIn(p_text, index + length);
  385. }
  386. }
  387. void HGMarkdownHighlighter::parse()
  388. {
  389. if (!parsing.testAndSetRelaxed(0, 1)) {
  390. return;
  391. }
  392. if (highlightingStyles.isEmpty()) {
  393. goto exit;
  394. }
  395. {
  396. int nrBlocks = document->blockCount();
  397. parseInternal();
  398. initBlockHighlightFromResult(nrBlocks);
  399. initHtmlCommentRegionsFromResult();
  400. initImageRegionsFromResult();
  401. initHeaderRegionsFromResult();
  402. if (result) {
  403. pmh_free_elements(result);
  404. result = NULL;
  405. }
  406. }
  407. exit:
  408. parsing.store(0);
  409. }
  410. void HGMarkdownHighlighter::parseInternal()
  411. {
  412. QString text = document->toPlainText();
  413. QByteArray ba = text.toUtf8();
  414. const char *data = (const char *)ba.data();
  415. int len = ba.size();
  416. if (result) {
  417. pmh_free_elements(result);
  418. result = NULL;
  419. }
  420. if (len == 0) {
  421. return;
  422. } else if (len >= capacity) {
  423. resizeBuffer(qMax(2 * capacity, len * 2));
  424. } else if (len < (capacity >> 2)) {
  425. resizeBuffer(qMax(capacity >> 1, len * 2));
  426. }
  427. memcpy(content, data, len);
  428. content[len] = '\0';
  429. pmh_markdown_to_elements(content, pmh_EXT_NONE, &result);
  430. }
  431. void HGMarkdownHighlighter::handleContentChange(int /* position */, int charsRemoved, int charsAdded)
  432. {
  433. if (charsRemoved == 0 && charsAdded == 0) {
  434. return;
  435. }
  436. timer->stop();
  437. timer->start();
  438. }
  439. void HGMarkdownHighlighter::timerTimeout()
  440. {
  441. qDebug() << "HGMarkdownHighlighter start a new parse";
  442. parse();
  443. if (!updateCodeBlocks()) {
  444. rehighlight();
  445. }
  446. highlightChanged();
  447. }
  448. void HGMarkdownHighlighter::updateHighlight()
  449. {
  450. timer->stop();
  451. timerTimeout();
  452. }
  453. bool HGMarkdownHighlighter::updateCodeBlocks()
  454. {
  455. if (!g_config->getEnableCodeBlockHighlight()) {
  456. m_codeBlockHighlights.clear();
  457. return false;
  458. }
  459. m_codeBlockHighlights.resize(document->blockCount());
  460. for (int i = 0; i < m_codeBlockHighlights.size(); ++i) {
  461. m_codeBlockHighlights[i].clear();
  462. }
  463. QVector<VCodeBlock> codeBlocks;
  464. VCodeBlock item;
  465. bool inBlock = false;
  466. int startLeadingSpaces = -1;
  467. // Only handle complete codeblocks.
  468. QTextBlock block = document->firstBlock();
  469. while (block.isValid()) {
  470. QString text = block.text();
  471. if (inBlock) {
  472. item.m_text = item.m_text + "\n" + text;
  473. int idx = codeBlockEndExp.indexIn(text);
  474. if (idx >= 0 && codeBlockEndExp.capturedTexts()[1].size() == startLeadingSpaces) {
  475. // End block.
  476. inBlock = false;
  477. item.m_endBlock = block.blockNumber();
  478. // See if it is a code block inside HTML comment.
  479. if (!isBlockInsideCommentRegion(block)) {
  480. qDebug() << "add one code block in lang" << item.m_lang;
  481. codeBlocks.append(item);
  482. }
  483. }
  484. } else {
  485. int idx = codeBlockStartExp.indexIn(text);
  486. if (idx >= 0) {
  487. // Start block.
  488. inBlock = true;
  489. item.m_startBlock = block.blockNumber();
  490. item.m_startPos = block.position();
  491. item.m_text = text;
  492. if (codeBlockStartExp.captureCount() == 2) {
  493. item.m_lang = codeBlockStartExp.capturedTexts()[2];
  494. }
  495. startLeadingSpaces = codeBlockStartExp.capturedTexts()[1].size();
  496. }
  497. }
  498. block = block.next();
  499. }
  500. m_numOfCodeBlockHighlightsToRecv = codeBlocks.size();
  501. if (m_numOfCodeBlockHighlightsToRecv > 0) {
  502. emit codeBlocksUpdated(codeBlocks);
  503. return true;
  504. } else {
  505. return false;
  506. }
  507. }
  508. static bool HLUnitStyleComp(const HLUnitStyle &a, const HLUnitStyle &b)
  509. {
  510. if (a.start < b.start) {
  511. return true;
  512. } else if (a.start == b.start) {
  513. return a.length > b.length;
  514. } else {
  515. return false;
  516. }
  517. }
  518. void HGMarkdownHighlighter::setCodeBlockHighlights(const QVector<HLUnitPos> &p_units)
  519. {
  520. if (p_units.isEmpty()) {
  521. goto exit;
  522. }
  523. {
  524. QVector<QVector<HLUnitStyle>> highlights(m_codeBlockHighlights.size());
  525. for (auto const &unit : p_units) {
  526. int pos = unit.m_position;
  527. int end = unit.m_position + unit.m_length;
  528. int startBlockNum = document->findBlock(pos).blockNumber();
  529. int endBlockNum = document->findBlock(end).blockNumber();
  530. // Text has been changed. Abandon the obsolete parsed result.
  531. if (startBlockNum == -1 || endBlockNum >= highlights.size()) {
  532. goto exit;
  533. }
  534. for (int i = startBlockNum; i <= endBlockNum; ++i)
  535. {
  536. QTextBlock block = document->findBlockByNumber(i);
  537. int blockStartPos = block.position();
  538. HLUnitStyle hl;
  539. hl.style = unit.m_style;
  540. if (i == startBlockNum) {
  541. hl.start = pos - blockStartPos;
  542. hl.length = (startBlockNum == endBlockNum) ?
  543. (end - pos) : (block.length() - hl.start);
  544. } else if (i == endBlockNum) {
  545. hl.start = 0;
  546. hl.length = end - blockStartPos;
  547. } else {
  548. hl.start = 0;
  549. hl.length = block.length();
  550. }
  551. highlights[i].append(hl);
  552. }
  553. }
  554. // Need to highlight in order.
  555. for (int i = 0; i < highlights.size(); ++i) {
  556. QVector<HLUnitStyle> &units = highlights[i];
  557. if (!units.isEmpty()) {
  558. std::sort(units.begin(), units.end(), HLUnitStyleComp);
  559. m_codeBlockHighlights[i].append(units);
  560. }
  561. }
  562. }
  563. exit:
  564. --m_numOfCodeBlockHighlightsToRecv;
  565. if (m_numOfCodeBlockHighlightsToRecv <= 0) {
  566. rehighlight();
  567. }
  568. }
  569. bool HGMarkdownHighlighter::isBlockInsideCommentRegion(const QTextBlock &p_block) const
  570. {
  571. if (!p_block.isValid()) {
  572. return false;
  573. }
  574. int start = p_block.position();
  575. int end = start + p_block.length();
  576. for (auto const & reg : m_commentRegions) {
  577. if (reg.contains(start) && reg.contains(end)) {
  578. return true;
  579. }
  580. }
  581. return false;
  582. }
  583. void HGMarkdownHighlighter::highlightChanged()
  584. {
  585. m_completeTimer->stop();
  586. m_completeTimer->start();
  587. }