| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | /**************************************************************************
 | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | ** | 
					
						
							|  |  |  | ** This file is part of Qt Creator | 
					
						
							|  |  |  | ** | 
					
						
							| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2009-06-17 00:01:27 +10:00
										 |  |  | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | ** Commercial Usage | 
					
						
							| 
									
										
										
										
											2008-12-02 14:17:16 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | ** Licensees holding valid Qt Commercial licenses may use this file in | 
					
						
							|  |  |  | ** accordance with the Qt Commercial License Agreement provided with the | 
					
						
							|  |  |  | ** Software or, alternatively, in accordance with the terms contained in | 
					
						
							|  |  |  | ** a written agreement between you and Nokia. | 
					
						
							| 
									
										
										
										
											2008-12-02 14:17:16 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | ** GNU Lesser General Public License Usage | 
					
						
							| 
									
										
										
										
											2008-12-02 14:17:16 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
					
						
							|  |  |  | ** General Public License version 2.1 as published by the Free Software | 
					
						
							|  |  |  | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
					
						
							|  |  |  | ** packaging of this file.  Please review the following information to | 
					
						
							|  |  |  | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
					
						
							|  |  |  | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 | 
					
						
							| 
									
										
										
										
											2008-12-02 14:17:16 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | ** If you are unsure which license is appropriate for your use, please | 
					
						
							| 
									
										
										
										
											2009-08-14 09:30:56 +02:00
										 |  |  | ** contact the sales department at http://qt.nokia.com/contact.
 | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | **************************************************************************/ | 
					
						
							| 
									
										
										
										
											2008-12-02 16:19:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | #include "profilehighlighter.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QtCore/QRegExp>
 | 
					
						
							|  |  |  | #include <QtGui/QColor>
 | 
					
						
							|  |  |  | #include <QtGui/QTextDocument>
 | 
					
						
							|  |  |  | #include <QtGui/QTextEdit>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace Qt4ProjectManager::Internal; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-24 19:13:37 +01:00
										 |  |  | #define MAX_VARIABLES 55
 | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | const char *const variables[MAX_VARIABLES] = { | 
					
						
							| 
									
										
										
										
											2009-09-15 13:02:39 +02:00
										 |  |  |     "CCFLAG", | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  |     "CONFIG", | 
					
						
							|  |  |  |     "DEFINES", | 
					
						
							|  |  |  |     "DEF_FILE", | 
					
						
							|  |  |  |     "DEPENDPATH", | 
					
						
							|  |  |  |     "DESTDIR", | 
					
						
							|  |  |  |     "DESTDIR_TARGET", | 
					
						
							|  |  |  |     "DISTFILES", | 
					
						
							|  |  |  |     "DLLDESTDIR", | 
					
						
							|  |  |  |     "FORMS", | 
					
						
							|  |  |  |     "HEADERS", | 
					
						
							|  |  |  |     "INCLUDEPATH", | 
					
						
							| 
									
										
										
										
											2009-09-15 13:02:39 +02:00
										 |  |  |     "INSTALLS", | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  |     "LEXSOURCES", | 
					
						
							|  |  |  |     "LIBS", | 
					
						
							|  |  |  |     "MAKEFILE", | 
					
						
							|  |  |  |     "MOC_DIR", | 
					
						
							| 
									
										
										
										
											2009-11-24 19:13:37 +01:00
										 |  |  |     "OBJECTIVE_HEADERS", | 
					
						
							|  |  |  |     "OBJECTIVE_SOURCES", | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  |     "OBJECTS", | 
					
						
							|  |  |  |     "OBJECTS_DIR", | 
					
						
							|  |  |  |     "OBJMOC", | 
					
						
							| 
									
										
										
										
											2009-07-23 11:37:08 +02:00
										 |  |  |     "OTHER_FILES", | 
					
						
							| 
									
										
										
										
											2009-04-07 15:58:02 +02:00
										 |  |  |     "PKGCONFIG", | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  |     "POST_TARGETDEPS", | 
					
						
							|  |  |  |     "PRECOMPILED_HEADER", | 
					
						
							|  |  |  |     "PRE_TARGETDEPS", | 
					
						
							|  |  |  |     "QMAKE", | 
					
						
							|  |  |  |     "QMAKESPEC", | 
					
						
							|  |  |  |     "QT", | 
					
						
							|  |  |  |     "RCC_DIR", | 
					
						
							|  |  |  |     "RC_FILE", | 
					
						
							|  |  |  |     "REQUIRES", | 
					
						
							|  |  |  |     "RESOURCES", | 
					
						
							|  |  |  |     "RES_FILE", | 
					
						
							|  |  |  |     "SOURCES", | 
					
						
							|  |  |  |     "SRCMOC", | 
					
						
							| 
									
										
										
										
											2009-09-15 13:02:39 +02:00
										 |  |  |     "STATECHARTS", | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  |     "SUBDIRS", | 
					
						
							|  |  |  |     "TARGET", | 
					
						
							|  |  |  |     "TARGET_EXT", | 
					
						
							|  |  |  |     "TARGET_x", | 
					
						
							|  |  |  |     "TARGET_x.y.z", | 
					
						
							|  |  |  |     "TEMPLATE", | 
					
						
							|  |  |  |     "TRANSLATIONS", | 
					
						
							|  |  |  |     "UI_DIR", | 
					
						
							|  |  |  |     "UI_HEADERS_DIR", | 
					
						
							|  |  |  |     "UI_SOURCES_DIR", | 
					
						
							|  |  |  |     "VER_MAJ", | 
					
						
							|  |  |  |     "VER_MIN", | 
					
						
							|  |  |  |     "VER_PAT", | 
					
						
							|  |  |  |     "VERSION", | 
					
						
							|  |  |  |     "VPATH", | 
					
						
							|  |  |  |     "YACCSOURCES", | 
					
						
							|  |  |  |     0 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define MAX_FUNCTIONS 22
 | 
					
						
							|  |  |  | const char *const functions[MAX_FUNCTIONS] = { | 
					
						
							|  |  |  |     "basename", | 
					
						
							|  |  |  |     "CONFIG", | 
					
						
							|  |  |  |     "contains", | 
					
						
							|  |  |  |     "count", | 
					
						
							|  |  |  |     "dirname", | 
					
						
							|  |  |  |     "error", | 
					
						
							|  |  |  |     "exists", | 
					
						
							|  |  |  |     "find", | 
					
						
							|  |  |  |     "for", | 
					
						
							|  |  |  |     "include", | 
					
						
							|  |  |  |     "infile", | 
					
						
							|  |  |  |     "isEmpty", | 
					
						
							|  |  |  |     "join", | 
					
						
							|  |  |  |     "member", | 
					
						
							|  |  |  |     "message", | 
					
						
							|  |  |  |     "prompt", | 
					
						
							|  |  |  |     "quote", | 
					
						
							|  |  |  |     "sprintf", | 
					
						
							|  |  |  |     "system", | 
					
						
							|  |  |  |     "unique", | 
					
						
							|  |  |  |     "warning", | 
					
						
							|  |  |  |     0 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct KeywordHelper | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     inline KeywordHelper(const QString &word) : needle(word) {} | 
					
						
							|  |  |  |     const QString needle; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool operator<(const KeywordHelper &helper, const char *kw) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return helper.needle < QLatin1String(kw); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool operator<(const char *kw, const KeywordHelper &helper) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return QLatin1String(kw) < helper.needle; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool isVariable(const QString &word) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     const char *const *start = &variables[0]; | 
					
						
							|  |  |  |     const char *const *end = &variables[MAX_VARIABLES - 1]; | 
					
						
							|  |  |  |     const char *const *kw = qBinaryFind(start, end, KeywordHelper(word)); | 
					
						
							|  |  |  |     return *kw != 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool isFunction(const QString &word) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     const char *const *start = &functions[0]; | 
					
						
							|  |  |  |     const char *const *end = &functions[MAX_FUNCTIONS - 1]; | 
					
						
							|  |  |  |     const char *const *kw = qBinaryFind(start, end, KeywordHelper(word)); | 
					
						
							|  |  |  |     return *kw != 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ProFileHighlighter::ProFileHighlighter(QTextDocument *document) : | 
					
						
							|  |  |  |     QSyntaxHighlighter(document) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ProFileHighlighter::highlightBlock(const QString &text) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (text.isEmpty()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QString buf; | 
					
						
							|  |  |  |     bool inCommentMode = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QTextCharFormat emptyFormat; | 
					
						
							|  |  |  |     int i = 0; | 
					
						
							|  |  |  |     for (;;) { | 
					
						
							|  |  |  |         const QChar c = text.at(i); | 
					
						
							|  |  |  |         if (inCommentMode) { | 
					
						
							|  |  |  |             setFormat(i, 1, m_formats[ProfileCommentFormat]); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             if (c.isLetter() || c == '_' || c == '.') { | 
					
						
							|  |  |  |                 buf += c; | 
					
						
							|  |  |  |                 setFormat(i - buf.length()+1, buf.length(), emptyFormat); | 
					
						
							|  |  |  |                 if (!buf.isEmpty() && isFunction(buf)) | 
					
						
							|  |  |  |                     setFormat(i - buf.length()+1, buf.length(), m_formats[ProfileFunctionFormat]); | 
					
						
							|  |  |  |                 else if (!buf.isEmpty() && isVariable(buf)) | 
					
						
							|  |  |  |                     setFormat(i - buf.length()+1, buf.length(), m_formats[ProfileVariableFormat]); | 
					
						
							|  |  |  |             } else if (c == '(') { | 
					
						
							|  |  |  |                 if (!buf.isEmpty() && isFunction(buf)) | 
					
						
							|  |  |  |                     setFormat(i - buf.length(), buf.length(), m_formats[ProfileFunctionFormat]); | 
					
						
							|  |  |  |                 buf.clear(); | 
					
						
							|  |  |  |             } else if (c == '#') { | 
					
						
							|  |  |  |                 inCommentMode = true; | 
					
						
							|  |  |  |                 setFormat(i, 1, m_formats[ProfileCommentFormat]); | 
					
						
							|  |  |  |                 buf.clear(); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 if (!buf.isEmpty() && isVariable(buf)) | 
					
						
							|  |  |  |                     setFormat(i - buf.length(), buf.length(), m_formats[ProfileVariableFormat]); | 
					
						
							|  |  |  |                 buf.clear(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         i++; | 
					
						
							|  |  |  |         if (i >= text.length()) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |