2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-04-26 14:02:09 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2010-04-26 14:02:09 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-04-26 14:02:09 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2010-04-26 14:02:09 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-10-02 09:12:39 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-04-26 14:02:09 +02:00
|
|
|
|
2015-01-02 11:31:16 +02:00
|
|
|
#ifndef TEXTDOCUMENTLAYOUT_H
|
|
|
|
|
#define TEXTDOCUMENTLAYOUT_H
|
2010-04-26 14:02:09 +02:00
|
|
|
|
|
|
|
|
#include "texteditor_global.h"
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
#include "texteditor.h"
|
2014-07-19 11:27:28 +02:00
|
|
|
#include "textmark.h"
|
2010-04-26 14:02:09 +02:00
|
|
|
|
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; }
|
2014-07-19 11:27:28 +02:00
|
|
|
void addMark(TextMark *mark);
|
|
|
|
|
inline bool removeMark(TextMark *mark) { return m_marks.removeAll(mark); }
|
2012-02-14 16:25:52 +01:00
|
|
|
|
2012-03-30 11:40:31 +02:00
|
|
|
inline TextMarks documentClosing() {
|
|
|
|
|
TextMarks marks = m_marks;
|
2014-07-19 11:27:28 +02:00
|
|
|
foreach (TextMark *mrk, m_marks)
|
2014-07-18 15:29:04 +02:00
|
|
|
mrk->setBaseTextDocument(0);
|
2012-03-30 11:40:31 +02:00
|
|
|
m_marks.clear();
|
|
|
|
|
return marks;
|
|
|
|
|
}
|
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;
|
2014-01-23 22:16:43 +02:00
|
|
|
uint m_lexerState : 8;
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
class TEXTEDITOR_EXPORT TextDocumentLayout : public QPlainTextDocumentLayout
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
|
|
public:
|
2014-09-26 09:14:03 +02:00
|
|
|
TextDocumentLayout(QTextDocument *doc);
|
|
|
|
|
~TextDocumentLayout();
|
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();
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
void setup(TextDocumentLayout *layout);
|
2011-08-12 12:13:23 +02:00
|
|
|
void reset();
|
|
|
|
|
void process(QTextBlock block);
|
|
|
|
|
void finalize();
|
|
|
|
|
|
|
|
|
|
private:
|
2014-09-26 09:14:03 +02:00
|
|
|
TextDocumentLayout *m_layout;
|
2011-08-12 12:13:23 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-17 14:59:14 +01:00
|
|
|
void requestExtraAreaUpdate();
|
2010-04-26 14:02:09 +02:00
|
|
|
|
|
|
|
|
void emitDocumentSizeChanged() { emit documentSizeChanged(documentSize()); }
|
2012-02-15 13:06:16 +01:00
|
|
|
|
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
|
|
|
|
2010-07-02 13:47:14 +02:00
|
|
|
void setRequiredWidth(int width);
|
|
|
|
|
|
|
|
|
|
QSizeF documentSize() const;
|
2010-07-12 11:16:10 +02:00
|
|
|
|
2012-03-30 11:40:31 +02:00
|
|
|
TextMarks documentClosing();
|
2014-09-22 18:43:31 +02:00
|
|
|
void documentReloaded(TextMarks marks, TextDocument *baseextDocument);
|
2012-02-14 19:27:15 +01:00
|
|
|
void updateMarksLineNumber();
|
|
|
|
|
void updateMarksBlock(const QTextBlock &block);
|
2012-02-17 14:59:14 +01:00
|
|
|
|
|
|
|
|
signals:
|
|
|
|
|
void updateExtraArea();
|
2010-04-26 14:02:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace TextEditor
|
|
|
|
|
|
2015-01-02 11:31:16 +02:00
|
|
|
#endif // TEXTDOCUMENTLAYOUT_H
|