1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #ifndef BASETEXTDOCUMENTLAYOUT_H
35 #define BASETEXTDOCUMENTLAYOUT_H
37 #include "texteditor_global.h"
39 #include "itexteditor.h"
41 #include <QtGui/QTextBlockUserData>
42 #include <QtGui/QPlainTextDocumentLayout>
44 namespace TextEditor {
47 typedef QVector<Parenthesis> Parentheses;
49 struct TEXTEDITOR_EXPORT Parenthesis
51 enum Type { Opened, Closed };
53 inline Parenthesis() : type(Opened), pos(-1) {}
54 inline Parenthesis(Type t, QChar c, int position)
55 : type(t), chr(c), pos(position) {}
61 class TEXTEDITOR_EXPORT CodeFormatterData
64 virtual ~CodeFormatterData();
67 class TEXTEDITOR_EXPORT TextBlockUserData : public QTextBlockUserData
71 inline TextBlockUserData()
76 m_foldingStartIncluded(false),
77 m_foldingEndIncluded(false),
78 m_codeFormatterData(0)
82 inline TextMarks marks() const { return m_marks; }
83 void addMark(ITextMark *mark);
84 inline bool removeMark(ITextMark *mark) { return m_marks.removeAll(mark); }
85 inline bool hasMark(ITextMark *mark) const { return m_marks.contains(mark); }
86 inline void clearMarks() { m_marks.clear(); }
87 inline void documentClosing() { Q_FOREACH(ITextMark *tm, m_marks) { tm->documentClosing(); } m_marks.clear();}
89 inline void setFolded(bool b) { m_folded = b; }
90 inline bool folded() const { return m_folded; }
92 inline void setParentheses(const Parentheses &parentheses) { m_parentheses = parentheses; }
93 inline void clearParentheses() { m_parentheses.clear(); }
94 inline const Parentheses &parentheses() const { return m_parentheses; }
95 inline bool hasParentheses() const { return !m_parentheses.isEmpty(); }
96 int braceDepthDelta() const;
98 inline bool setIfdefedOut() { bool result = m_ifdefedOut; m_ifdefedOut = true; return !result; }
99 inline bool clearIfdefedOut() { bool result = m_ifdefedOut; m_ifdefedOut = false; return result;}
100 inline bool ifdefedOut() const { return m_ifdefedOut; }
103 enum MatchType { NoMatch, Match, Mismatch };
104 static MatchType checkOpenParenthesis(QTextCursor *cursor, QChar c);
105 static MatchType checkClosedParenthesis(QTextCursor *cursor, QChar c);
106 static MatchType matchCursorBackward(QTextCursor *cursor);
107 static MatchType matchCursorForward(QTextCursor *cursor);
108 static bool findPreviousOpenParenthesis(QTextCursor *cursor, bool select = false);
109 static bool findNextClosingParenthesis(QTextCursor *cursor, bool select = false);
111 static bool findPreviousBlockOpenParenthesis(QTextCursor *cursor, bool checkStartPosition = false);
112 static bool findNextBlockClosingParenthesis(QTextCursor *cursor);
114 // Get the code folding level
115 inline int foldingIndent() const { return m_foldingIndent; }
116 /* Set the code folding level.
118 * A code folding marker will appear the line *before* the one where the indention
119 * level increases. The code folding reagion will end in the last line that has the same
120 * indention level (or higher).
122 inline void setFoldingIndent(int indent) { m_foldingIndent = indent; }
123 // Set whether the first character of the folded region will show when the code is folded.
124 inline void setFoldingStartIncluded(bool included) { m_foldingStartIncluded = included; }
125 inline bool foldingStartIncluded() const { return m_foldingStartIncluded; }
126 // Set whether the last character of the folded region will show when the code is folded.
127 inline void setFoldingEndIncluded(bool included) { m_foldingEndIncluded = included; }
128 inline bool foldingEndIncluded() const { return m_foldingEndIncluded; }
129 inline int lexerState() const { return m_lexerState; }
130 inline void setLexerState(int state) {m_lexerState = state; }
133 CodeFormatterData *codeFormatterData() const { return m_codeFormatterData; }
134 void setCodeFormatterData(CodeFormatterData *data);
139 uint m_ifdefedOut : 1;
140 uint m_foldingIndent : 16;
141 uint m_lexerState : 4;
142 uint m_foldingStartIncluded : 1;
143 uint m_foldingEndIncluded : 1;
144 Parentheses m_parentheses;
145 CodeFormatterData *m_codeFormatterData;
149 class TEXTEDITOR_EXPORT BaseTextDocumentLayout : public QPlainTextDocumentLayout
154 BaseTextDocumentLayout(QTextDocument *doc);
155 ~BaseTextDocumentLayout();
157 static void setParentheses(const QTextBlock &block, const Parentheses &parentheses);
158 static void clearParentheses(const QTextBlock &block) { setParentheses(block, Parentheses());}
159 static Parentheses parentheses(const QTextBlock &block);
160 static bool hasParentheses(const QTextBlock &block);
161 static bool setIfdefedOut(const QTextBlock &block);
162 static bool clearIfdefedOut(const QTextBlock &block);
163 static bool ifdefedOut(const QTextBlock &block);
164 static int braceDepthDelta(const QTextBlock &block);
165 static int braceDepth(const QTextBlock &block);
166 static void setBraceDepth(QTextBlock &block, int depth);
167 static void changeBraceDepth(QTextBlock &block, int delta);
168 static void setFoldingIndent(const QTextBlock &block, int indent);
169 static int foldingIndent(const QTextBlock &block);
170 static void setLexerState(const QTextBlock &block, int state);
171 static int lexerState(const QTextBlock &block);
172 static void changeFoldingIndent(QTextBlock &block, int delta);
173 static bool canFold(const QTextBlock &block);
174 static void doFoldOrUnfold(const QTextBlock& block, bool unfold);
175 static bool isFolded(const QTextBlock &block);
176 static void setFolded(const QTextBlock &block, bool folded);
178 static TextBlockUserData *testUserData(const QTextBlock &block) {
179 return static_cast<TextBlockUserData*>(block.userData());
181 static TextBlockUserData *userData(const QTextBlock &block) {
182 TextBlockUserData *data = static_cast<TextBlockUserData*>(block.userData());
183 if (!data && block.isValid())
184 const_cast<QTextBlock &>(block).setUserData((data = new TextBlockUserData));
189 void emitDocumentSizeChanged() { emit documentSizeChanged(documentSize()); }
190 int lastSaveRevision;
194 void setRequiredWidth(int width);
196 QSizeF documentSize() const;
200 } // namespace TextEditor
202 #endif // BASETEXTDOCUMENTLAYOUT_H