1 /**************************************************************************** 2 ** 3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). 4 ** All rights reserved. 5 ** Contact: Nokia Corporation (qt-info@nokia.com) 6 ** 7 ** This file is part of the QtGui module of the Qt Toolkit. 8 ** 9 ** $QT_BEGIN_LICENSE:LGPL$ 10 ** No Commercial Usage 11 ** This file contains pre-release code and may not be distributed. 12 ** You may use this file in accordance with the terms and conditions 13 ** contained in the Technology Preview License Agreement accompanying 14 ** this package. 15 ** 16 ** GNU Lesser General Public License Usage 17 ** Alternatively, this file may be used under the terms of the GNU Lesser 18 ** General Public License version 2.1 as published by the Free Software 19 ** Foundation and appearing in the file LICENSE.LGPL included in the 20 ** packaging of this file. Please review the following information to 21 ** ensure the GNU Lesser General Public License version 2.1 requirements 22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 23 ** 24 ** In addition, as a special exception, Nokia gives you certain additional 25 ** rights. These rights are described in the Nokia Qt LGPL Exception 26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 27 ** 28 ** If you have questions regarding the use of this file, please contact 29 ** Nokia at qt-info@nokia.com. 30 ** 31 ** 32 ** 33 ** 34 ** 35 ** 36 ** 37 ** 38 ** $QT_END_LICENSE$ 39 ** 40 ****************************************************************************/ 41 42 #ifndef TEXTEDITOR_SYNTAXHIGHLIGHTER_H 43 #define TEXTEDITOR_SYNTAXHIGHLIGHTER_H 44 45 #include "texteditor_global.h" 46 47 #include <QtCore/QObject> 48 #include <QtGui/QTextLayout> 49 50 QT_BEGIN_NAMESPACE 51 class QTextDocument; 52 class QSyntaxHighlighterPrivate; 53 class QTextCharFormat; 54 class QFont; 55 class QColor; 56 class QTextBlockUserData; 57 class QTextEdit; 58 QT_END_NAMESPACE 59 60 namespace TextEditor { 61 62 class SyntaxHighlighterPrivate; 63 64 struct SyntaxComment { SyntaxCommentSyntaxComment65 SyntaxComment() : isCommentAfterWhiteSpaces(true), isCommentAfterWhiteSpacesAddSpace (true) 66 {} isEmptySyntaxComment67 bool isEmpty() const { 68 return singleLineComment.isEmpty() && 69 multiLineCommentStart.isEmpty() && 70 multiLineCommentEnd.isEmpty(); 71 } 72 QString singleLineComment; 73 QString multiLineCommentStart; 74 QString multiLineCommentEnd; 75 bool isCommentAfterWhiteSpaces; 76 bool isCommentAfterWhiteSpacesAddSpace; 77 }; 78 79 class TEXTEDITOR_EXPORT SyntaxHighlighter : public QObject 80 { 81 Q_OBJECT 82 Q_DECLARE_PRIVATE(SyntaxHighlighter) 83 public: 84 enum TextFormatId { 85 Normal = 1, 86 VisualWhitespace, 87 Keyword, 88 DataType, 89 Decimal, 90 BaseN, 91 Float, 92 Char, 93 String, 94 Comment, 95 Alert, 96 Error, 97 Function, 98 RegionMarker, 99 Others, 100 Symbol, 101 BuiltinFunc, 102 Predeclared, 103 FuncDecl, 104 Placeholder, 105 ToDo, 106 PreprocessorFormat, 107 TextFormatId_MAX 108 }; 109 public: 110 SyntaxHighlighter(QObject *parent); 111 SyntaxHighlighter(QTextDocument *parent); 112 SyntaxHighlighter(QTextEdit *parent); 113 virtual ~SyntaxHighlighter(); 114 115 void setDocument(QTextDocument *doc); 116 QTextDocument *document() const; 117 setContextData(const QString & id,const QString & value)118 void setContextData(const QString &id, const QString &value) { 119 m_contextMap[id] = value; 120 } contextData(const QString & id)121 QString contextData(const QString &id) { 122 return m_contextMap[id]; 123 } 124 125 void setExtraAdditionalFormats(const QTextBlock& block, const QList<QTextLayout::FormatRange> &formats); 126 void configureFormat(TextFormatId id, const QTextCharFormat &format); 127 virtual void setTabSize(int tabSize); 128 public: 129 SyntaxComment comment() const; 130 void setupComment(const SyntaxComment &comment); 131 signals: 132 void foldIndentChanged(QTextBlock block); 133 public Q_SLOTS: 134 void rehighlight(); 135 void rehighlightBlock(const QTextBlock &block); 136 137 protected: 138 virtual void highlightBlock(const QString &text) = 0; 139 140 void setFormat(int start, int count, const QTextCharFormat &format, int id = 0); 141 //void setFormat(int start, int count, const QColor &color); 142 //void setFormat(int start, int count, const QFont &font); 143 QTextCharFormat format(int pos) const; 144 145 void applyFormatToSpaces(const QString &text, const QTextCharFormat &format); 146 147 int previousBlockState() const; 148 int currentBlockState() const; 149 void setCurrentBlockState(int newState); 150 151 void setCurrentBlockUserData(QTextBlockUserData *data); 152 QTextBlockUserData *currentBlockUserData() const; 153 154 QTextBlock currentBlock() const; 155 QMap<QString,QString> m_contextMap; 156 protected: 157 struct KateFormatMap 158 { 159 KateFormatMap(); 160 QHash<QString, TextFormatId> m_ids; 161 }; 162 static const KateFormatMap m_kateFormats; 163 QTextCharFormat m_creatorFormats[TextFormatId_MAX]; 164 private: 165 Q_DISABLE_COPY(SyntaxHighlighter) 166 Q_PRIVATE_SLOT(d_ptr, void _q_reformatBlocks(int from, int charsRemoved, int charsAdded)) 167 Q_PRIVATE_SLOT(d_ptr, void _q_delayedRehighlight()) 168 169 QScopedPointer<SyntaxHighlighterPrivate> d_ptr; 170 }; 171 172 } // namespace TextEditor 173 174 #endif // TEXTEDITOR_SYNTAXHIGHLIGHTER_H 175