2010-04-26 14:02:09 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2012-01-26 18:33:46 +01:00
|
|
|
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
2010-04-26 14:02:09 +02:00
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2010-04-26 14:02:09 +02:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2010-04-26 14:02:09 +02:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2010-04-26 14:02:09 +02:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifndef BASETEXTDOCUMENTLAYOUT_H
|
|
|
|
|
#define BASETEXTDOCUMENTLAYOUT_H
|
|
|
|
|
|
|
|
|
|
#include "texteditor_global.h"
|
|
|
|
|
|
|
|
|
|
#include "itexteditor.h"
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTextBlockUserData>
|
|
|
|
|
#include <QPlainTextDocumentLayout>
|
2010-04-26 14:02:09 +02:00
|
|
|
|
|
|
|
|
namespace TextEditor {
|
|
|
|
|
struct Parenthesis;
|
|
|
|
|
typedef QVector<Parenthesis> Parentheses;
|
|
|
|
|
|
|
|
|
|
struct TEXTEDITOR_EXPORT Parenthesis
|
|
|
|
|
{
|
|
|
|
|
enum Type { Opened, Closed };
|
|
|
|
|
|
|
|
|
|
inline Parenthesis() : type(Opened), pos(-1) {}
|
|
|
|
|
inline Parenthesis(Type t, QChar c, int position)
|
|
|
|
|
: type(t), chr(c), pos(position) {}
|
|
|
|
|
Type type;
|
|
|
|
|
QChar chr;
|
|
|
|
|
int pos;
|
|
|
|
|
};
|
|
|
|
|
|
2010-07-02 15:43:34 +02:00
|
|
|
class TEXTEDITOR_EXPORT CodeFormatterData
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
virtual ~CodeFormatterData();
|
|
|
|
|
};
|
2010-04-26 14:02:09 +02:00
|
|
|
|
|
|
|
|
class TEXTEDITOR_EXPORT TextBlockUserData : public QTextBlockUserData
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
inline TextBlockUserData()
|
2010-05-20 15:10:26 +02:00
|
|
|
: m_folded(false),
|
|
|
|
|
m_ifdefedOut(false),
|
|
|
|
|
m_foldingIndent(0),
|
2010-07-12 11:16:10 +02:00
|
|
|
m_lexerState(0),
|
2010-05-20 15:10:26 +02:00
|
|
|
m_foldingStartIncluded(false),
|
2010-07-02 15:43:34 +02:00
|
|
|
m_foldingEndIncluded(false),
|
|
|
|
|
m_codeFormatterData(0)
|
|
|
|
|
{}
|
2010-04-26 14:02:09 +02:00
|
|
|
~TextBlockUserData();
|
|
|
|
|
|
|
|
|
|
inline TextMarks marks() const { return m_marks; }
|
2011-03-02 18:43:26 +01:00
|
|
|
void addMark(ITextMark *mark);
|
2010-04-26 14:02:09 +02:00
|
|
|
inline bool removeMark(ITextMark *mark) { return m_marks.removeAll(mark); }
|
2012-02-14 16:25:52 +01:00
|
|
|
|
2012-02-14 17:17:40 +01:00
|
|
|
inline void documentClosing() { m_marks.clear(); }
|
2010-04-26 14:02:09 +02:00
|
|
|
|
2010-05-20 15:10:26 +02:00
|
|
|
inline void setFolded(bool b) { m_folded = b; }
|
|
|
|
|
inline bool folded() const { return m_folded; }
|
2010-04-26 14:02:09 +02:00
|
|
|
|
|
|
|
|
inline void setParentheses(const Parentheses &parentheses) { m_parentheses = parentheses; }
|
|
|
|
|
inline void clearParentheses() { m_parentheses.clear(); }
|
|
|
|
|
inline const Parentheses &parentheses() const { return m_parentheses; }
|
|
|
|
|
inline bool hasParentheses() const { return !m_parentheses.isEmpty(); }
|
|
|
|
|
int braceDepthDelta() const;
|
|
|
|
|
|
|
|
|
|
inline bool setIfdefedOut() { bool result = m_ifdefedOut; m_ifdefedOut = true; return !result; }
|
|
|
|
|
inline bool clearIfdefedOut() { bool result = m_ifdefedOut; m_ifdefedOut = false; return result;}
|
|
|
|
|
inline bool ifdefedOut() const { return m_ifdefedOut; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enum MatchType { NoMatch, Match, Mismatch };
|
|
|
|
|
static MatchType checkOpenParenthesis(QTextCursor *cursor, QChar c);
|
|
|
|
|
static MatchType checkClosedParenthesis(QTextCursor *cursor, QChar c);
|
|
|
|
|
static MatchType matchCursorBackward(QTextCursor *cursor);
|
|
|
|
|
static MatchType matchCursorForward(QTextCursor *cursor);
|
2011-10-29 13:09:33 +08:00
|
|
|
static bool findPreviousOpenParenthesis(QTextCursor *cursor, bool select = false,
|
|
|
|
|
bool onlyInCurrentBlock = false);
|
2010-04-26 14:02:09 +02:00
|
|
|
static bool findNextClosingParenthesis(QTextCursor *cursor, bool select = false);
|
|
|
|
|
|
|
|
|
|
static bool findPreviousBlockOpenParenthesis(QTextCursor *cursor, bool checkStartPosition = false);
|
|
|
|
|
static bool findNextBlockClosingParenthesis(QTextCursor *cursor);
|
|
|
|
|
|
2010-11-22 10:46:55 +01:00
|
|
|
// Get the code folding level
|
2010-07-12 11:16:10 +02:00
|
|
|
inline int foldingIndent() const { return m_foldingIndent; }
|
2010-11-22 10:46:55 +01:00
|
|
|
/* Set the code folding level.
|
|
|
|
|
*
|
|
|
|
|
* A code folding marker will appear the line *before* the one where the indention
|
|
|
|
|
* level increases. The code folding reagion will end in the last line that has the same
|
|
|
|
|
* indention level (or higher).
|
|
|
|
|
*/
|
2010-07-12 11:16:10 +02:00
|
|
|
inline void setFoldingIndent(int indent) { m_foldingIndent = indent; }
|
2011-02-25 15:27:13 +01:00
|
|
|
// Set whether the first character of the folded region will show when the code is folded.
|
2010-07-12 11:16:10 +02:00
|
|
|
inline void setFoldingStartIncluded(bool included) { m_foldingStartIncluded = included; }
|
|
|
|
|
inline bool foldingStartIncluded() const { return m_foldingStartIncluded; }
|
2011-02-25 15:27:13 +01:00
|
|
|
// Set whether the last character of the folded region will show when the code is folded.
|
2010-07-12 11:16:10 +02:00
|
|
|
inline void setFoldingEndIncluded(bool included) { m_foldingEndIncluded = included; }
|
|
|
|
|
inline bool foldingEndIncluded() const { return m_foldingEndIncluded; }
|
|
|
|
|
inline int lexerState() const { return m_lexerState; }
|
|
|
|
|
inline void setLexerState(int state) {m_lexerState = state; }
|
2010-05-20 15:10:26 +02:00
|
|
|
|
2010-07-02 15:43:34 +02:00
|
|
|
|
|
|
|
|
CodeFormatterData *codeFormatterData() const { return m_codeFormatterData; }
|
|
|
|
|
void setCodeFormatterData(CodeFormatterData *data);
|
2010-04-26 14:02:09 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
TextMarks m_marks;
|
2010-05-20 15:10:26 +02:00
|
|
|
uint m_folded : 1;
|
2010-04-26 14:02:09 +02:00
|
|
|
uint m_ifdefedOut : 1;
|
2010-05-20 15:10:26 +02:00
|
|
|
uint m_foldingIndent : 16;
|
2010-07-12 11:16:10 +02:00
|
|
|
uint m_lexerState : 4;
|
2010-05-20 15:10:26 +02:00
|
|
|
uint m_foldingStartIncluded : 1;
|
|
|
|
|
uint m_foldingEndIncluded : 1;
|
2010-04-26 14:02:09 +02:00
|
|
|
Parentheses m_parentheses;
|
2010-07-02 15:43:34 +02:00
|
|
|
CodeFormatterData *m_codeFormatterData;
|
2010-04-26 14:02:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
class TEXTEDITOR_EXPORT BaseTextDocumentLayout : public QPlainTextDocumentLayout
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
|
|
public:
|
2010-04-26 14:06:29 +02:00
|
|
|
BaseTextDocumentLayout(QTextDocument *doc);
|
|
|
|
|
~BaseTextDocumentLayout();
|
2010-04-26 14:02:09 +02:00
|
|
|
|
|
|
|
|
static void setParentheses(const QTextBlock &block, const Parentheses &parentheses);
|
|
|
|
|
static void clearParentheses(const QTextBlock &block) { setParentheses(block, Parentheses());}
|
|
|
|
|
static Parentheses parentheses(const QTextBlock &block);
|
|
|
|
|
static bool hasParentheses(const QTextBlock &block);
|
|
|
|
|
static bool setIfdefedOut(const QTextBlock &block);
|
|
|
|
|
static bool clearIfdefedOut(const QTextBlock &block);
|
|
|
|
|
static bool ifdefedOut(const QTextBlock &block);
|
|
|
|
|
static int braceDepthDelta(const QTextBlock &block);
|
|
|
|
|
static int braceDepth(const QTextBlock &block);
|
|
|
|
|
static void setBraceDepth(QTextBlock &block, int depth);
|
|
|
|
|
static void changeBraceDepth(QTextBlock &block, int delta);
|
2010-05-20 15:10:26 +02:00
|
|
|
static void setFoldingIndent(const QTextBlock &block, int indent);
|
|
|
|
|
static int foldingIndent(const QTextBlock &block);
|
2010-07-12 11:16:10 +02:00
|
|
|
static void setLexerState(const QTextBlock &block, int state);
|
|
|
|
|
static int lexerState(const QTextBlock &block);
|
2010-05-20 15:10:26 +02:00
|
|
|
static void changeFoldingIndent(QTextBlock &block, int delta);
|
|
|
|
|
static bool canFold(const QTextBlock &block);
|
|
|
|
|
static void doFoldOrUnfold(const QTextBlock& block, bool unfold);
|
|
|
|
|
static bool isFolded(const QTextBlock &block);
|
|
|
|
|
static void setFolded(const QTextBlock &block, bool folded);
|
2010-04-26 14:02:09 +02:00
|
|
|
|
2011-08-12 12:13:23 +02:00
|
|
|
class TEXTEDITOR_EXPORT FoldValidator
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
FoldValidator();
|
|
|
|
|
|
|
|
|
|
void setup(BaseTextDocumentLayout *layout);
|
|
|
|
|
void reset();
|
|
|
|
|
void process(QTextBlock block);
|
|
|
|
|
void finalize();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
BaseTextDocumentLayout *m_layout;
|
|
|
|
|
bool m_requestDocUpdate;
|
|
|
|
|
int m_insideFold;
|
|
|
|
|
};
|
|
|
|
|
|
2010-04-26 14:02:09 +02:00
|
|
|
static TextBlockUserData *testUserData(const QTextBlock &block) {
|
|
|
|
|
return static_cast<TextBlockUserData*>(block.userData());
|
|
|
|
|
}
|
|
|
|
|
static TextBlockUserData *userData(const QTextBlock &block) {
|
|
|
|
|
TextBlockUserData *data = static_cast<TextBlockUserData*>(block.userData());
|
|
|
|
|
if (!data && block.isValid())
|
|
|
|
|
const_cast<QTextBlock &>(block).setUserData((data = new TextBlockUserData));
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void emitDocumentSizeChanged() { emit documentSizeChanged(documentSize()); }
|
2012-02-15 13:06:16 +01:00
|
|
|
ITextMarkable *markableInterface();
|
|
|
|
|
|
2010-04-26 14:02:09 +02:00
|
|
|
int lastSaveRevision;
|
|
|
|
|
bool hasMarks;
|
2011-03-23 10:45:04 +01:00
|
|
|
double maxMarkWidthFactor;
|
2010-07-02 13:47:14 +02:00
|
|
|
|
|
|
|
|
int m_requiredWidth;
|
2012-02-15 13:06:16 +01:00
|
|
|
ITextMarkable *m_documentMarker;
|
|
|
|
|
|
2010-07-02 13:47:14 +02:00
|
|
|
void setRequiredWidth(int width);
|
|
|
|
|
|
|
|
|
|
QSizeF documentSize() const;
|
2010-07-12 11:16:10 +02:00
|
|
|
|
2012-02-15 13:39:10 +01:00
|
|
|
void documentClosing();
|
2012-02-14 19:27:15 +01:00
|
|
|
void updateMarksLineNumber();
|
|
|
|
|
void updateMarksBlock(const QTextBlock &block);
|
2010-04-26 14:02:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace TextEditor
|
|
|
|
|
|
|
|
|
|
#endif // BASETEXTDOCUMENTLAYOUT_H
|