From 08b820ca1172df57e4f7a4d67123513eacdfb553 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Thu, 7 Jun 2007 21:57:32 +0200 Subject: [PATCH] Syntax highlighter: Highlight C and C++ comments. --- kdbg/sourcewnd.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++---- kdbg/sourcewnd.h | 2 +- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/kdbg/sourcewnd.cpp b/kdbg/sourcewnd.cpp index 2d38a86..ff921c8 100644 --- a/kdbg/sourcewnd.cpp +++ b/kdbg/sourcewnd.cpp @@ -773,17 +773,63 @@ HighlightCpp::HighlightCpp(SourceWindow* srcWnd) : { } -int HighlightCpp::highlightParagraph(const QString& text, int endStateOfLastPara) +enum HLState { + hlCommentLine = 1, + hlCommentBlock +}; + +int HighlightCpp::highlightParagraph(const QString& text, int state) { int row = currentParagraph(); // highlight assembly lines if (m_srcWnd->isRowDisassCode(row)) { setFormat(0, text.length(), blue); - return endStateOfLastPara; + return state; } - setFormat(0, text.length(), m_srcWnd->colorGroup().text()); - return 0; + + if (state == -2) // initial state + state = 0; + + unsigned start = 0; + while (start < text.length()) + { + int end; + switch (state) { + case hlCommentLine: + end = text.length(); + state = 0; + setFormat(start, end-start, QColor("gray50")); + break; + case hlCommentBlock: + end = text.find("*/", start); + if (end >= 0) + end += 2, state = 0; + else + end = text.length(); + setFormat(start, end-start, QColor("gray50")); + break; + default: + for (end = start; end < int(text.length()); end++) + { + if (text[end] == '/') + { + if (end+1 < int(text.length())) { + if (text[end+1] == '/') { + state = hlCommentLine; + break; + } else if (text[end+1] == '*') { + state = hlCommentBlock; + break; + } + } + } + } + setFormat(start, end-start, m_srcWnd->colorGroup().text()); + } + start = end; + } + return state; } #include "sourcewnd.moc" diff --git a/kdbg/sourcewnd.h b/kdbg/sourcewnd.h index b54ecbe..d217eb3 100644 --- a/kdbg/sourcewnd.h +++ b/kdbg/sourcewnd.h @@ -116,7 +116,7 @@ class HighlightCpp : public QSyntaxHighlighter public: HighlightCpp(SourceWindow* srcWnd); - virtual int highlightParagraph(const QString& text, int endStateOfLastPara); + virtual int highlightParagraph(const QString& text, int state); }; #endif // SOURCEWND_H -- 2.11.4.GIT