Commit 162a4c13 authored by heretik's avatar heretik
Browse files

add highlightings for multiline comments

parent c1a7f507
......@@ -6,16 +6,53 @@
SyntaxHighlighter::SyntaxHighlighter( QTextDocument *parent )
: QSyntaxHighlighter( parent )
, error_format_{}
, format_error_{}
, format_multiline_comment_{}
, regex_comment_start_{ R"(/\*)" }
, regex_comment_end_{ R"(\*/)" }
{
error_format_.setUnderlineStyle( QTextCharFormat::WaveUnderline );
error_format_.setUnderlineColor( Qt::red );
// formats
format_error_.setUnderlineStyle( QTextCharFormat::WaveUnderline );
format_error_.setUnderlineColor( Qt::red );
format_multiline_comment_.setForeground( Qt::gray );
}
void SyntaxHighlighter::highlightBlock( const QString &text )
{
auto *data_block = static_cast<BlockData*>( currentBlock().userData() );
if( data_block != nullptr ) {
setFormat( 0, text.length(), error_format_ );
// prepare format to merge
auto format_to_merge = QTextCharFormat{};
// on errors, underline
auto *data_block_ptr = static_cast<BlockData*>( currentBlock().userData() );
if( data_block_ptr != nullptr ) {
setFormat( 0, text.length(), format_error_ );
format_to_merge = format_error_;
}
// compute comments zone
constexpr auto end_line_is_in_comment = 1;
constexpr auto end_line_is_not_in_comment = 0;
setCurrentBlockState( end_line_is_not_in_comment );
auto start_index = 0;
if( previousBlockState() != end_line_is_in_comment ) {
start_index = text.indexOf( regex_comment_start_ );
}
while( start_index >= 0 ) {
auto match = QRegularExpressionMatch{ regex_comment_end_.match( text, start_index ) };
auto end_index = match.capturedStart();
auto comment_length = 0;
if( end_index == -1 ) {
setCurrentBlockState( end_line_is_in_comment );
comment_length = text.length() - start_index;
} else {
comment_length = end_index - start_index + match.capturedLength();
}
auto format = format_multiline_comment_;
format.merge( format_to_merge );
setFormat( start_index, comment_length, format );
start_index = text.indexOf( regex_comment_start_, start_index + comment_length );
}
}
......@@ -2,6 +2,7 @@
#ifndef SYNTAX_HIGHLIGHTER_HPP
#define SYNTAX_HIGHLIGHTER_HPP
#include <QRegularExpression>
#include <QSyntaxHighlighter>
class SyntaxHighlighter
......@@ -16,7 +17,10 @@ protected:
void highlightBlock( const QString &text ) override;
private:
QTextCharFormat error_format_;
QTextCharFormat format_error_;
QTextCharFormat format_multiline_comment_;
QRegularExpression regex_comment_start_;
QRegularExpression regex_comment_end_;
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment