From dc76661a53230db4e54499d27622bc060ca921d1 Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Wed, 5 Dec 2018 00:20:46 +0100 Subject: [PATCH] ui: fixed multiple multiline comments within one line not correctly highlighted --- src/lib_gui/qt/utility/QtHighlighter.cpp | 27 +++++++-- testing/code_view/code_view_tests.srctrlprj | 1 + .../data/syntax_highlighter_tests.cpp | 58 +++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 testing/code_view/data/syntax_highlighter_tests.cpp diff --git a/src/lib_gui/qt/utility/QtHighlighter.cpp b/src/lib_gui/qt/utility/QtHighlighter.cpp index 31a0f127..bc87cce0 100644 --- a/src/lib_gui/qt/utility/QtHighlighter.cpp +++ b/src/lib_gui/qt/utility/QtHighlighter.cpp @@ -270,7 +270,7 @@ void QtHighlighter::createRanges( std::vector> QtHighlighter::createMultiLineCommentRanges( QTextDocument* doc, std::vector>* ranges) { - QRegExp commentStartExpression = QRegExp("(^([^/]|/[^/])*)/\\*"); + QRegExp commentStartExpression = QRegExp("/\\*"); QRegExp commentEndExpression = QRegExp("\\*/"); QTextCursor cursorStart(doc); @@ -280,15 +280,34 @@ std::vector> QtHighlighter::createMultiLineCommentRanges( while (true) { - do + while (true) { cursorStart = document()->find(commentStartExpression, cursorStart); - if (!cursorStart.isNull()) + if (cursorStart.isNull()) + { + break; + } + + // ignore if within single line comment + QTextCursor inlineCommentStart = document()->find(s_commentRule.pattern, QTextCursor(cursorStart.block())); + if (!inlineCommentStart.isNull() && + inlineCommentStart.blockNumber() == cursorStart.blockNumber() && + inlineCommentStart.selectionStart() < cursorStart.selectionStart()) + { + cursorStart = QTextCursor(inlineCommentStart.block().next()); + continue; + } + + if (!isInRange(cursorStart.selectionEnd(), *ranges)) { cursorStart.setPosition(cursorStart.selectionEnd() - 2); + break; + } + else + { + cursorStart.setPosition(cursorStart.selectionEnd() + 2); } } - while (isInRange(cursorStart.selectionEnd(), *ranges)); if (cursorStart.isNull()) { diff --git a/testing/code_view/code_view_tests.srctrlprj b/testing/code_view/code_view_tests.srctrlprj index 21c27481..5dbac82d 100644 --- a/testing/code_view/code_view_tests.srctrlprj +++ b/testing/code_view/code_view_tests.srctrlprj @@ -8,6 +8,7 @@ Test Suites:\n\n [::\tmSINGLE_FILE_TESTS\ts\tp]\n [::\tmFILE_BAR_TESTS\ts\tp]\n [::\tmERROR_TESTS\ts\tp]\n +[::\tmSYNTAX_HIGHLIGHTER_TESTS\ts\tp]\n diff --git a/testing/code_view/data/syntax_highlighter_tests.cpp b/testing/code_view/data/syntax_highlighter_tests.cpp new file mode 100644 index 00000000..bbd84545 --- /dev/null +++ b/testing/code_view/data/syntax_highlighter_tests.cpp @@ -0,0 +1,58 @@ + +namespace SYNTAX_HIGHLIGHTER_TESTS +{ + +// TEST: multiline comments +// START ---------------------------------------------------------------------- + +/**/ + +//* + +///* + +// /* /* + +//* /* + +/* comment */ + +/* comment *//* comment */ + +/* comment */ /* comment */ + +/* comment /* inside comment */ + +/* comment + * comment + * comment + */ + +/* start + comment +// end */ + +/* comment */ int no_comment; /* comment */ + +int no_comment2; /* + * comment + * comment + */ int no_comment3; + +// /* +int no_comment4; +// */ + +//* +int no_comment5; +//* + +const char no_commentStr1[] = " /* string */ "/* comment */; +const char no_commentStr2[] = " /* "; /* comment */ +const char no_commentStr3[] = /* const char[] a = " no string */" this is a string "; // <- highlight broken + +// RESULT: All no_comment variable are not highlighted as comment + +// END ------------------------------------------------------------------------ + +}