2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-04-26 14:02:09 +02:00
|
|
|
**
|
2013-01-28 17:12:19 +01:00
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
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
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/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
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt 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
|
|
|
|
|
|
|
|
#include "basetextdocumentlayout.h"
|
2012-02-15 13:06:16 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2012-02-17 14:59:14 +01:00
|
|
|
#include <QDebug>
|
2010-04-26 14:02:09 +02:00
|
|
|
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
2012-02-17 16:19:50 +01:00
|
|
|
namespace TextEditor {
|
2012-02-15 13:06:16 +01:00
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
class DocumentMarker : public ITextMarkable
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
DocumentMarker(QTextDocument *);
|
|
|
|
|
~DocumentMarker();
|
|
|
|
|
|
|
|
|
|
TextMarks marks() const { return m_marksCache; }
|
|
|
|
|
|
|
|
|
|
// ITextMarkable
|
2012-02-21 16:27:04 +01:00
|
|
|
bool addMark(ITextMark *mark);
|
2012-02-15 13:06:16 +01:00
|
|
|
TextMarks marksAt(int line) const;
|
|
|
|
|
void removeMark(ITextMark *mark);
|
|
|
|
|
void updateMark(ITextMark *mark);
|
|
|
|
|
|
2012-03-30 11:40:31 +02:00
|
|
|
void removeMarkFromMarksCache(TextEditor::ITextMark *mark);
|
2012-02-15 13:06:16 +01:00
|
|
|
private:
|
|
|
|
|
TextMarks m_marksCache; // not owned
|
|
|
|
|
QTextDocument *document;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
DocumentMarker::DocumentMarker(QTextDocument *doc)
|
|
|
|
|
: ITextMarkable(doc), document(doc)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DocumentMarker::~DocumentMarker()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-21 16:27:04 +01:00
|
|
|
bool DocumentMarker::addMark(TextEditor::ITextMark *mark)
|
2012-02-15 13:06:16 +01:00
|
|
|
{
|
2012-03-30 11:40:31 +02:00
|
|
|
if (mark->markableInterface())
|
|
|
|
|
return false;
|
2012-02-21 16:27:04 +01:00
|
|
|
QTC_ASSERT(mark->lineNumber() >= 1, return false);
|
|
|
|
|
int blockNumber = mark->lineNumber() - 1;
|
2012-02-15 13:06:16 +01:00
|
|
|
BaseTextDocumentLayout *documentLayout =
|
|
|
|
|
qobject_cast<BaseTextDocumentLayout*>(document->documentLayout());
|
|
|
|
|
QTC_ASSERT(documentLayout, return false);
|
|
|
|
|
QTextBlock block = document->findBlockByNumber(blockNumber);
|
|
|
|
|
|
|
|
|
|
if (block.isValid()) {
|
|
|
|
|
TextBlockUserData *userData = BaseTextDocumentLayout::userData(block);
|
|
|
|
|
userData->addMark(mark);
|
|
|
|
|
m_marksCache.append(mark);
|
|
|
|
|
mark->updateLineNumber(blockNumber + 1);
|
2012-03-13 16:05:16 +01:00
|
|
|
QTC_CHECK(mark->lineNumber() == blockNumber + 1); // Checks that the base class is called
|
2012-02-15 13:06:16 +01:00
|
|
|
mark->updateBlock(block);
|
2012-09-19 13:17:33 +02:00
|
|
|
mark->setMarkableInterface(this);
|
2012-10-09 14:02:52 +02:00
|
|
|
if (!mark->isVisible())
|
2012-02-17 14:59:14 +01:00
|
|
|
return true;
|
|
|
|
|
// Update document layout
|
|
|
|
|
double newMaxWidthFactor = qMax(mark->widthFactor(), documentLayout->maxMarkWidthFactor);
|
|
|
|
|
bool fullUpdate = newMaxWidthFactor > documentLayout->maxMarkWidthFactor || !documentLayout->hasMarks;
|
2012-02-15 13:06:16 +01:00
|
|
|
documentLayout->hasMarks = true;
|
2012-02-17 14:59:14 +01:00
|
|
|
documentLayout->maxMarkWidthFactor = newMaxWidthFactor;
|
|
|
|
|
if (fullUpdate)
|
|
|
|
|
documentLayout->requestUpdate();
|
|
|
|
|
else
|
|
|
|
|
documentLayout->requestExtraAreaUpdate();
|
2012-02-15 13:06:16 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextEditor::TextMarks DocumentMarker::marksAt(int line) const
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(line >= 1, return TextMarks());
|
|
|
|
|
int blockNumber = line - 1;
|
|
|
|
|
QTextBlock block = document->findBlockByNumber(blockNumber);
|
|
|
|
|
|
|
|
|
|
if (block.isValid()) {
|
|
|
|
|
if (TextBlockUserData *userData = BaseTextDocumentLayout::testUserData(block))
|
|
|
|
|
return userData->marks();
|
|
|
|
|
}
|
|
|
|
|
return TextMarks();
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-30 11:40:31 +02:00
|
|
|
void DocumentMarker::removeMarkFromMarksCache(TextEditor::ITextMark *mark)
|
|
|
|
|
{
|
|
|
|
|
BaseTextDocumentLayout *documentLayout =
|
|
|
|
|
qobject_cast<BaseTextDocumentLayout*>(document->documentLayout());
|
2012-04-17 08:01:25 +02:00
|
|
|
QTC_ASSERT(documentLayout, return);
|
2012-02-17 14:59:14 +01:00
|
|
|
m_marksCache.removeAll(mark);
|
|
|
|
|
|
2012-03-30 11:40:31 +02:00
|
|
|
if (m_marksCache.isEmpty()) {
|
|
|
|
|
documentLayout->hasMarks = false;
|
2012-02-17 14:59:14 +01:00
|
|
|
documentLayout->maxMarkWidthFactor = 1.0;
|
|
|
|
|
documentLayout->requestUpdate();
|
|
|
|
|
return;
|
2012-03-30 11:40:31 +02:00
|
|
|
}
|
|
|
|
|
|
2012-10-09 14:02:52 +02:00
|
|
|
if (!mark->isVisible())
|
2012-02-17 14:59:14 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (documentLayout->maxMarkWidthFactor == 1.0
|
|
|
|
|
|| mark->widthFactor() == 1.0
|
|
|
|
|
|| mark->widthFactor() < documentLayout->maxMarkWidthFactor) {
|
|
|
|
|
// No change in width possible
|
|
|
|
|
documentLayout->requestExtraAreaUpdate();
|
|
|
|
|
} else {
|
|
|
|
|
double maxWidthFactor = 1.0;
|
|
|
|
|
foreach (const ITextMark *mark, marks()) {
|
2012-10-09 14:02:52 +02:00
|
|
|
if (!mark->isVisible())
|
2012-02-17 14:59:14 +01:00
|
|
|
continue;
|
|
|
|
|
maxWidthFactor = qMax(mark->widthFactor(), maxWidthFactor);
|
|
|
|
|
if (maxWidthFactor == documentLayout->maxMarkWidthFactor)
|
|
|
|
|
break; // Still a mark with the maxMarkWidthFactor
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (maxWidthFactor != documentLayout->maxMarkWidthFactor) {
|
|
|
|
|
documentLayout->maxMarkWidthFactor = maxWidthFactor;
|
|
|
|
|
documentLayout->requestUpdate();
|
|
|
|
|
} else {
|
|
|
|
|
documentLayout->requestExtraAreaUpdate();
|
|
|
|
|
}
|
2012-03-30 11:40:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-15 13:06:16 +01:00
|
|
|
void DocumentMarker::removeMark(TextEditor::ITextMark *mark)
|
|
|
|
|
{
|
2012-02-17 14:59:14 +01:00
|
|
|
QTextBlock block = document->findBlockByNumber(mark->lineNumber() - 1);
|
|
|
|
|
if (TextBlockUserData *data = static_cast<TextBlockUserData *>(block.userData())) {
|
|
|
|
|
if (!data->removeMark(mark))
|
|
|
|
|
qDebug() << "Could not find mark" << mark << "on line" << mark->lineNumber();
|
2012-02-15 13:06:16 +01:00
|
|
|
}
|
2012-02-17 14:59:14 +01:00
|
|
|
|
2012-03-30 11:40:31 +02:00
|
|
|
removeMarkFromMarksCache(mark);
|
|
|
|
|
mark->setMarkableInterface(0);
|
2012-02-15 13:06:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DocumentMarker::updateMark(ITextMark *mark)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(mark)
|
|
|
|
|
BaseTextDocumentLayout *documentLayout =
|
|
|
|
|
qobject_cast<BaseTextDocumentLayout*>(document->documentLayout());
|
|
|
|
|
QTC_ASSERT(documentLayout, return);
|
|
|
|
|
documentLayout->requestUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
2012-02-17 16:19:50 +01:00
|
|
|
} // namespace TextEditor
|
2012-02-15 13:06:16 +01:00
|
|
|
|
2010-07-02 15:43:34 +02:00
|
|
|
CodeFormatterData::~CodeFormatterData()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-26 14:02:09 +02:00
|
|
|
TextBlockUserData::~TextBlockUserData()
|
|
|
|
|
{
|
2012-02-17 14:59:14 +01:00
|
|
|
foreach (ITextMark *mrk, m_marks) {
|
2012-03-30 11:40:31 +02:00
|
|
|
TextEditor::Internal::DocumentMarker *documentMarker
|
|
|
|
|
= static_cast<TextEditor::Internal::DocumentMarker *>(mrk->markableInterface());
|
|
|
|
|
documentMarker->removeMarkFromMarksCache(mrk);
|
|
|
|
|
mrk->setMarkableInterface(0);
|
2010-04-26 14:02:09 +02:00
|
|
|
mrk->removedFromEditor();
|
|
|
|
|
}
|
2010-07-02 15:43:34 +02:00
|
|
|
|
|
|
|
|
if (m_codeFormatterData)
|
|
|
|
|
delete m_codeFormatterData;
|
2010-04-26 14:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int TextBlockUserData::braceDepthDelta() const
|
|
|
|
|
{
|
|
|
|
|
int delta = 0;
|
|
|
|
|
for (int i = 0; i < m_parentheses.size(); ++i) {
|
|
|
|
|
switch (m_parentheses.at(i).chr.unicode()) {
|
|
|
|
|
case '{': case '+': case '[': ++delta; break;
|
|
|
|
|
case '}': case '-': case ']': --delta; break;
|
|
|
|
|
default: break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return delta;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextBlockUserData::MatchType TextBlockUserData::checkOpenParenthesis(QTextCursor *cursor, QChar c)
|
|
|
|
|
{
|
|
|
|
|
QTextBlock block = cursor->block();
|
2010-04-26 14:06:29 +02:00
|
|
|
if (!BaseTextDocumentLayout::hasParentheses(block) || BaseTextDocumentLayout::ifdefedOut(block))
|
2010-04-26 14:02:09 +02:00
|
|
|
return NoMatch;
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
Parentheses parenList = BaseTextDocumentLayout::parentheses(block);
|
2010-04-26 14:02:09 +02:00
|
|
|
Parenthesis openParen, closedParen;
|
|
|
|
|
QTextBlock closedParenParag = block;
|
|
|
|
|
|
|
|
|
|
const int cursorPos = cursor->position() - closedParenParag.position();
|
|
|
|
|
int i = 0;
|
|
|
|
|
int ignore = 0;
|
|
|
|
|
bool foundOpen = false;
|
|
|
|
|
for (;;) {
|
|
|
|
|
if (!foundOpen) {
|
|
|
|
|
if (i >= parenList.count())
|
|
|
|
|
return NoMatch;
|
|
|
|
|
openParen = parenList.at(i);
|
|
|
|
|
if (openParen.pos != cursorPos) {
|
|
|
|
|
++i;
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
foundOpen = true;
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i >= parenList.count()) {
|
|
|
|
|
for (;;) {
|
|
|
|
|
closedParenParag = closedParenParag.next();
|
|
|
|
|
if (!closedParenParag.isValid())
|
|
|
|
|
return NoMatch;
|
2010-04-26 14:06:29 +02:00
|
|
|
if (BaseTextDocumentLayout::hasParentheses(closedParenParag)
|
|
|
|
|
&& !BaseTextDocumentLayout::ifdefedOut(closedParenParag)) {
|
|
|
|
|
parenList = BaseTextDocumentLayout::parentheses(closedParenParag);
|
2010-04-26 14:02:09 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
i = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closedParen = parenList.at(i);
|
|
|
|
|
if (closedParen.type == Parenthesis::Opened) {
|
|
|
|
|
ignore++;
|
|
|
|
|
++i;
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
if (ignore > 0) {
|
|
|
|
|
ignore--;
|
|
|
|
|
++i;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cursor->clearSelection();
|
|
|
|
|
cursor->setPosition(closedParenParag.position() + closedParen.pos + 1, QTextCursor::KeepAnchor);
|
|
|
|
|
|
|
|
|
|
if ((c == QLatin1Char('{') && closedParen.chr != QLatin1Char('}'))
|
|
|
|
|
|| (c == QLatin1Char('(') && closedParen.chr != QLatin1Char(')'))
|
|
|
|
|
|| (c == QLatin1Char('[') && closedParen.chr != QLatin1Char(']'))
|
|
|
|
|
|| (c == QLatin1Char('+') && closedParen.chr != QLatin1Char('-'))
|
|
|
|
|
)
|
|
|
|
|
return Mismatch;
|
|
|
|
|
|
|
|
|
|
return Match;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextBlockUserData::MatchType TextBlockUserData::checkClosedParenthesis(QTextCursor *cursor, QChar c)
|
|
|
|
|
{
|
|
|
|
|
QTextBlock block = cursor->block();
|
2010-04-26 14:06:29 +02:00
|
|
|
if (!BaseTextDocumentLayout::hasParentheses(block) || BaseTextDocumentLayout::ifdefedOut(block))
|
2010-04-26 14:02:09 +02:00
|
|
|
return NoMatch;
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
Parentheses parenList = BaseTextDocumentLayout::parentheses(block);
|
2010-04-26 14:02:09 +02:00
|
|
|
Parenthesis openParen, closedParen;
|
|
|
|
|
QTextBlock openParenParag = block;
|
|
|
|
|
|
|
|
|
|
const int cursorPos = cursor->position() - openParenParag.position();
|
|
|
|
|
int i = parenList.count() - 1;
|
|
|
|
|
int ignore = 0;
|
|
|
|
|
bool foundClosed = false;
|
|
|
|
|
for (;;) {
|
|
|
|
|
if (!foundClosed) {
|
|
|
|
|
if (i < 0)
|
|
|
|
|
return NoMatch;
|
|
|
|
|
closedParen = parenList.at(i);
|
|
|
|
|
if (closedParen.pos != cursorPos - 1) {
|
|
|
|
|
--i;
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
foundClosed = true;
|
|
|
|
|
--i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i < 0) {
|
|
|
|
|
for (;;) {
|
|
|
|
|
openParenParag = openParenParag.previous();
|
|
|
|
|
if (!openParenParag.isValid())
|
|
|
|
|
return NoMatch;
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
if (BaseTextDocumentLayout::hasParentheses(openParenParag)
|
|
|
|
|
&& !BaseTextDocumentLayout::ifdefedOut(openParenParag)) {
|
|
|
|
|
parenList = BaseTextDocumentLayout::parentheses(openParenParag);
|
2010-04-26 14:02:09 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
i = parenList.count() - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
openParen = parenList.at(i);
|
|
|
|
|
if (openParen.type == Parenthesis::Closed) {
|
|
|
|
|
ignore++;
|
|
|
|
|
--i;
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
if (ignore > 0) {
|
|
|
|
|
ignore--;
|
|
|
|
|
--i;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cursor->clearSelection();
|
|
|
|
|
cursor->setPosition(openParenParag.position() + openParen.pos, QTextCursor::KeepAnchor);
|
|
|
|
|
|
2012-01-05 11:05:28 +01:00
|
|
|
if ((c == QLatin1Char('}') && openParen.chr != QLatin1Char('{')) ||
|
|
|
|
|
(c == QLatin1Char(')') && openParen.chr != QLatin1Char('(')) ||
|
|
|
|
|
(c == QLatin1Char(']') && openParen.chr != QLatin1Char('[')) ||
|
|
|
|
|
(c == QLatin1Char('-') && openParen.chr != QLatin1Char('+')))
|
2010-04-26 14:02:09 +02:00
|
|
|
return Mismatch;
|
|
|
|
|
|
|
|
|
|
return Match;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-29 13:09:33 +08:00
|
|
|
bool TextBlockUserData::findPreviousOpenParenthesis(QTextCursor *cursor, bool select, bool onlyInCurrentBlock)
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
QTextBlock block = cursor->block();
|
|
|
|
|
int position = cursor->position();
|
|
|
|
|
int ignore = 0;
|
|
|
|
|
while (block.isValid()) {
|
2010-04-26 14:06:29 +02:00
|
|
|
Parentheses parenList = BaseTextDocumentLayout::parentheses(block);
|
|
|
|
|
if (!parenList.isEmpty() && !BaseTextDocumentLayout::ifdefedOut(block)) {
|
2010-04-26 14:02:09 +02:00
|
|
|
for (int i = parenList.count()-1; i >= 0; --i) {
|
|
|
|
|
Parenthesis paren = parenList.at(i);
|
|
|
|
|
if (block == cursor->block() &&
|
|
|
|
|
(position - block.position() <= paren.pos + (paren.type == Parenthesis::Closed ? 1 : 0)))
|
|
|
|
|
continue;
|
|
|
|
|
if (paren.type == Parenthesis::Closed) {
|
|
|
|
|
++ignore;
|
|
|
|
|
} else if (ignore > 0) {
|
|
|
|
|
--ignore;
|
|
|
|
|
} else {
|
|
|
|
|
cursor->setPosition(block.position() + paren.pos, select ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-10-29 13:09:33 +08:00
|
|
|
if (onlyInCurrentBlock)
|
|
|
|
|
return false;
|
2010-04-26 14:02:09 +02:00
|
|
|
block = block.previous();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TextBlockUserData::findPreviousBlockOpenParenthesis(QTextCursor *cursor, bool checkStartPosition)
|
|
|
|
|
{
|
|
|
|
|
QTextBlock block = cursor->block();
|
|
|
|
|
int position = cursor->position();
|
|
|
|
|
int ignore = 0;
|
|
|
|
|
while (block.isValid()) {
|
2010-04-26 14:06:29 +02:00
|
|
|
Parentheses parenList = BaseTextDocumentLayout::parentheses(block);
|
|
|
|
|
if (!parenList.isEmpty() && !BaseTextDocumentLayout::ifdefedOut(block)) {
|
2010-04-26 14:02:09 +02:00
|
|
|
for (int i = parenList.count()-1; i >= 0; --i) {
|
|
|
|
|
Parenthesis paren = parenList.at(i);
|
|
|
|
|
if (paren.chr != QLatin1Char('{') && paren.chr != QLatin1Char('}')
|
2012-10-14 10:42:49 +02:00
|
|
|
&& paren.chr != QLatin1Char('+') && paren.chr != QLatin1Char('-'))
|
2010-04-26 14:02:09 +02:00
|
|
|
continue;
|
|
|
|
|
if (block == cursor->block()) {
|
|
|
|
|
if (position - block.position() <= paren.pos + (paren.type == Parenthesis::Closed ? 1 : 0))
|
|
|
|
|
continue;
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
if (checkStartPosition && paren.type == Parenthesis::Opened && paren.pos== cursor->position())
|
2010-04-26 14:02:09 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (paren.type == Parenthesis::Closed) {
|
|
|
|
|
++ignore;
|
|
|
|
|
} else if (ignore > 0) {
|
|
|
|
|
--ignore;
|
|
|
|
|
} else {
|
|
|
|
|
cursor->setPosition(block.position() + paren.pos);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
block = block.previous();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TextBlockUserData::findNextClosingParenthesis(QTextCursor *cursor, bool select)
|
|
|
|
|
{
|
|
|
|
|
QTextBlock block = cursor->block();
|
|
|
|
|
int position = cursor->position();
|
|
|
|
|
int ignore = 0;
|
|
|
|
|
while (block.isValid()) {
|
2010-04-26 14:06:29 +02:00
|
|
|
Parentheses parenList = BaseTextDocumentLayout::parentheses(block);
|
|
|
|
|
if (!parenList.isEmpty() && !BaseTextDocumentLayout::ifdefedOut(block)) {
|
2010-04-26 14:02:09 +02:00
|
|
|
for (int i = 0; i < parenList.count(); ++i) {
|
|
|
|
|
Parenthesis paren = parenList.at(i);
|
|
|
|
|
if (block == cursor->block() &&
|
|
|
|
|
(position - block.position() > paren.pos - (paren.type == Parenthesis::Opened ? 1 : 0)))
|
|
|
|
|
continue;
|
|
|
|
|
if (paren.type == Parenthesis::Opened) {
|
|
|
|
|
++ignore;
|
|
|
|
|
} else if (ignore > 0) {
|
|
|
|
|
--ignore;
|
|
|
|
|
} else {
|
|
|
|
|
cursor->setPosition(block.position() + paren.pos+1, select ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
block = block.next();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TextBlockUserData::findNextBlockClosingParenthesis(QTextCursor *cursor)
|
|
|
|
|
{
|
|
|
|
|
QTextBlock block = cursor->block();
|
|
|
|
|
int position = cursor->position();
|
|
|
|
|
int ignore = 0;
|
|
|
|
|
while (block.isValid()) {
|
2010-04-26 14:06:29 +02:00
|
|
|
Parentheses parenList = BaseTextDocumentLayout::parentheses(block);
|
|
|
|
|
if (!parenList.isEmpty() && !BaseTextDocumentLayout::ifdefedOut(block)) {
|
2010-04-26 14:02:09 +02:00
|
|
|
for (int i = 0; i < parenList.count(); ++i) {
|
|
|
|
|
Parenthesis paren = parenList.at(i);
|
|
|
|
|
if (paren.chr != QLatin1Char('{') && paren.chr != QLatin1Char('}')
|
2012-10-14 10:42:49 +02:00
|
|
|
&& paren.chr != QLatin1Char('+') && paren.chr != QLatin1Char('-'))
|
2010-04-26 14:02:09 +02:00
|
|
|
continue;
|
|
|
|
|
if (block == cursor->block() &&
|
|
|
|
|
(position - block.position() > paren.pos - (paren.type == Parenthesis::Opened ? 1 : 0)))
|
|
|
|
|
continue;
|
|
|
|
|
if (paren.type == Parenthesis::Opened) {
|
|
|
|
|
++ignore;
|
|
|
|
|
} else if (ignore > 0) {
|
|
|
|
|
--ignore;
|
|
|
|
|
} else {
|
|
|
|
|
cursor->setPosition(block.position() + paren.pos+1);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
block = block.next();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextBlockUserData::MatchType TextBlockUserData::matchCursorBackward(QTextCursor *cursor)
|
|
|
|
|
{
|
|
|
|
|
cursor->clearSelection();
|
|
|
|
|
const QTextBlock block = cursor->block();
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
if (!BaseTextDocumentLayout::hasParentheses(block) || BaseTextDocumentLayout::ifdefedOut(block))
|
2010-04-26 14:02:09 +02:00
|
|
|
return NoMatch;
|
|
|
|
|
|
|
|
|
|
const int relPos = cursor->position() - block.position();
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
Parentheses parentheses = BaseTextDocumentLayout::parentheses(block);
|
2010-04-26 14:02:09 +02:00
|
|
|
const Parentheses::const_iterator cend = parentheses.constEnd();
|
|
|
|
|
for (Parentheses::const_iterator it = parentheses.constBegin();it != cend; ++it) {
|
|
|
|
|
const Parenthesis &paren = *it;
|
|
|
|
|
if (paren.pos == relPos - 1
|
|
|
|
|
&& paren.type == Parenthesis::Closed) {
|
|
|
|
|
return checkClosedParenthesis(cursor, paren.chr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NoMatch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextBlockUserData::MatchType TextBlockUserData::matchCursorForward(QTextCursor *cursor)
|
|
|
|
|
{
|
|
|
|
|
cursor->clearSelection();
|
|
|
|
|
const QTextBlock block = cursor->block();
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
if (!BaseTextDocumentLayout::hasParentheses(block) || BaseTextDocumentLayout::ifdefedOut(block))
|
2010-04-26 14:02:09 +02:00
|
|
|
return NoMatch;
|
|
|
|
|
|
|
|
|
|
const int relPos = cursor->position() - block.position();
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
Parentheses parentheses = BaseTextDocumentLayout::parentheses(block);
|
2010-04-26 14:02:09 +02:00
|
|
|
const Parentheses::const_iterator cend = parentheses.constEnd();
|
|
|
|
|
for (Parentheses::const_iterator it = parentheses.constBegin();it != cend; ++it) {
|
|
|
|
|
const Parenthesis &paren = *it;
|
|
|
|
|
if (paren.pos == relPos
|
|
|
|
|
&& paren.type == Parenthesis::Opened) {
|
|
|
|
|
return checkOpenParenthesis(cursor, paren.chr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NoMatch;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-02 15:43:34 +02:00
|
|
|
void TextBlockUserData::setCodeFormatterData(CodeFormatterData *data)
|
|
|
|
|
{
|
|
|
|
|
if (m_codeFormatterData)
|
|
|
|
|
delete m_codeFormatterData;
|
|
|
|
|
|
|
|
|
|
m_codeFormatterData = data;
|
|
|
|
|
}
|
2010-04-26 14:02:09 +02:00
|
|
|
|
2011-03-02 18:43:26 +01:00
|
|
|
void TextBlockUserData::addMark(ITextMark *mark)
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
for ( ; i < m_marks.size(); ++i) {
|
|
|
|
|
if (mark->priority() < m_marks.at(i)->priority())
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
m_marks.insert(i, mark);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
BaseTextDocumentLayout::BaseTextDocumentLayout(QTextDocument *doc)
|
2012-02-15 13:06:16 +01:00
|
|
|
: QPlainTextDocumentLayout(doc),
|
|
|
|
|
lastSaveRevision(0),
|
|
|
|
|
hasMarks(false),
|
|
|
|
|
maxMarkWidthFactor(1.0),
|
|
|
|
|
m_requiredWidth(0),
|
|
|
|
|
m_documentMarker(new Internal::DocumentMarker(doc))
|
|
|
|
|
{
|
|
|
|
|
|
2010-04-26 14:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
BaseTextDocumentLayout::~BaseTextDocumentLayout()
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
2012-02-15 13:39:10 +01:00
|
|
|
documentClosing();
|
2010-04-26 14:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
void BaseTextDocumentLayout::setParentheses(const QTextBlock &block, const Parentheses &parentheses)
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
if (parentheses.isEmpty()) {
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
userData->clearParentheses();
|
|
|
|
|
} else {
|
|
|
|
|
userData(block)->setParentheses(parentheses);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
Parentheses BaseTextDocumentLayout::parentheses(const QTextBlock &block)
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
return userData->parentheses();
|
|
|
|
|
return Parentheses();
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
bool BaseTextDocumentLayout::hasParentheses(const QTextBlock &block)
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
return userData->hasParentheses();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
bool BaseTextDocumentLayout::setIfdefedOut(const QTextBlock &block)
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
return userData(block)->setIfdefedOut();
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
bool BaseTextDocumentLayout::clearIfdefedOut(const QTextBlock &block)
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
return userData->clearIfdefedOut();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
bool BaseTextDocumentLayout::ifdefedOut(const QTextBlock &block)
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
return userData->ifdefedOut();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
int BaseTextDocumentLayout::braceDepthDelta(const QTextBlock &block)
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
return userData->braceDepthDelta();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
int BaseTextDocumentLayout::braceDepth(const QTextBlock &block)
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
int state = block.userState();
|
|
|
|
|
if (state == -1)
|
|
|
|
|
return 0;
|
|
|
|
|
return state >> 8;
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
void BaseTextDocumentLayout::setBraceDepth(QTextBlock &block, int depth)
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
int state = block.userState();
|
|
|
|
|
if (state == -1)
|
|
|
|
|
state = 0;
|
|
|
|
|
state = state & 0xff;
|
|
|
|
|
block.setUserState((depth << 8) | state);
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-26 14:06:29 +02:00
|
|
|
void BaseTextDocumentLayout::changeBraceDepth(QTextBlock &block, int delta)
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
if (delta)
|
|
|
|
|
setBraceDepth(block, braceDepth(block) + delta);
|
|
|
|
|
}
|
2010-05-20 15:10:26 +02:00
|
|
|
|
2010-07-12 11:16:10 +02:00
|
|
|
void BaseTextDocumentLayout::setLexerState(const QTextBlock &block, int state)
|
|
|
|
|
{
|
|
|
|
|
if (state == 0) {
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
userData->setLexerState(0);
|
|
|
|
|
} else {
|
|
|
|
|
userData(block)->setLexerState(qMax(0,state));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int BaseTextDocumentLayout::lexerState(const QTextBlock &block)
|
|
|
|
|
{
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
return userData->lexerState();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-20 15:10:26 +02:00
|
|
|
void BaseTextDocumentLayout::setFoldingIndent(const QTextBlock &block, int indent)
|
|
|
|
|
{
|
|
|
|
|
if (indent == 0) {
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
userData->setFoldingIndent(0);
|
|
|
|
|
} else {
|
|
|
|
|
userData(block)->setFoldingIndent(qMax(0,indent));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int BaseTextDocumentLayout::foldingIndent(const QTextBlock &block)
|
|
|
|
|
{
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
return userData->foldingIndent();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseTextDocumentLayout::changeFoldingIndent(QTextBlock &block, int delta)
|
|
|
|
|
{
|
|
|
|
|
if (delta)
|
|
|
|
|
setFoldingIndent(block, foldingIndent(block) + delta);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BaseTextDocumentLayout::canFold(const QTextBlock &block)
|
|
|
|
|
{
|
|
|
|
|
return (block.next().isValid() && foldingIndent(block.next()) > foldingIndent(block));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BaseTextDocumentLayout::isFolded(const QTextBlock &block)
|
|
|
|
|
{
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
return userData->folded();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseTextDocumentLayout::setFolded(const QTextBlock &block, bool folded)
|
|
|
|
|
{
|
|
|
|
|
if (folded)
|
|
|
|
|
userData(block)->setFolded(true);
|
|
|
|
|
else {
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
return userData->setFolded(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-17 14:59:14 +01:00
|
|
|
void BaseTextDocumentLayout::requestExtraAreaUpdate()
|
|
|
|
|
{
|
|
|
|
|
emit updateExtraArea();
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-15 13:06:16 +01:00
|
|
|
ITextMarkable *BaseTextDocumentLayout::markableInterface()
|
|
|
|
|
{
|
|
|
|
|
return m_documentMarker;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-20 15:10:26 +02:00
|
|
|
void BaseTextDocumentLayout::doFoldOrUnfold(const QTextBlock& block, bool unfold)
|
|
|
|
|
{
|
|
|
|
|
if (!canFold(block))
|
|
|
|
|
return;
|
|
|
|
|
QTextBlock b = block.next();
|
|
|
|
|
|
|
|
|
|
int indent = foldingIndent(block);
|
2010-09-07 15:55:06 +02:00
|
|
|
while (b.isValid() && foldingIndent(b) > indent && (unfold || b.next().isValid())) {
|
2010-05-20 15:10:26 +02:00
|
|
|
b.setVisible(unfold);
|
|
|
|
|
b.setLineCount(unfold? qMax(1, b.layout()->lineCount()) : 0);
|
|
|
|
|
if (unfold) { // do not unfold folded sub-blocks
|
|
|
|
|
if (isFolded(b) && b.next().isValid()) {
|
|
|
|
|
int jndent = foldingIndent(b);
|
|
|
|
|
b = b.next();
|
|
|
|
|
while (b.isValid() && foldingIndent(b) > jndent)
|
|
|
|
|
b = b.next();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
b = b.next();
|
|
|
|
|
}
|
|
|
|
|
setFolded(block, !unfold);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-02 13:47:14 +02:00
|
|
|
void BaseTextDocumentLayout::setRequiredWidth(int width)
|
|
|
|
|
{
|
|
|
|
|
int oldw = m_requiredWidth;
|
|
|
|
|
m_requiredWidth = width;
|
|
|
|
|
int dw = QPlainTextDocumentLayout::documentSize().width();
|
|
|
|
|
if (oldw > dw || width > dw)
|
|
|
|
|
emitDocumentSizeChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QSizeF BaseTextDocumentLayout::documentSize() const
|
|
|
|
|
{
|
|
|
|
|
QSizeF size = QPlainTextDocumentLayout::documentSize();
|
|
|
|
|
size.setWidth(qMax((qreal)m_requiredWidth, size.width()));
|
|
|
|
|
return size;
|
|
|
|
|
}
|
2011-08-12 12:13:23 +02:00
|
|
|
|
2012-03-30 11:40:31 +02:00
|
|
|
TextMarks BaseTextDocumentLayout::documentClosing()
|
2012-02-15 13:39:10 +01:00
|
|
|
{
|
2012-03-30 11:40:31 +02:00
|
|
|
TextMarks marks;
|
2012-02-15 13:39:10 +01:00
|
|
|
QTextBlock block = document()->begin();
|
|
|
|
|
while (block.isValid()) {
|
|
|
|
|
if (TextBlockUserData *data = static_cast<TextBlockUserData *>(block.userData()))
|
2012-03-30 11:40:31 +02:00
|
|
|
marks.append(data->documentClosing());
|
2012-02-15 13:39:10 +01:00
|
|
|
block = block.next();
|
|
|
|
|
}
|
2012-03-30 11:40:31 +02:00
|
|
|
return marks;
|
2012-02-15 13:39:10 +01:00
|
|
|
}
|
|
|
|
|
|
2012-03-30 11:40:31 +02:00
|
|
|
void BaseTextDocumentLayout::documentReloaded(TextMarks marks)
|
|
|
|
|
{
|
|
|
|
|
foreach (ITextMark *mark, marks) {
|
|
|
|
|
int blockNumber = mark->lineNumber() - 1;
|
|
|
|
|
QTextBlock block = document()->findBlockByNumber(blockNumber);
|
|
|
|
|
if (block.isValid()) {
|
|
|
|
|
TextBlockUserData *userData = BaseTextDocumentLayout::userData(block);
|
|
|
|
|
userData->addMark(mark);
|
|
|
|
|
mark->setMarkableInterface(m_documentMarker);
|
|
|
|
|
mark->updateBlock(block);
|
|
|
|
|
} else {
|
|
|
|
|
TextEditor::Internal::DocumentMarker *documentMarker
|
|
|
|
|
= static_cast<TextEditor::Internal::DocumentMarker *>(m_documentMarker);
|
|
|
|
|
documentMarker->removeMarkFromMarksCache(mark);
|
|
|
|
|
mark->removedFromEditor();
|
2012-02-17 14:59:14 +01:00
|
|
|
mark->setMarkableInterface(0);
|
2012-03-30 11:40:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
requestUpdate();
|
|
|
|
|
}
|
2012-02-15 13:39:10 +01:00
|
|
|
|
2012-02-14 19:27:15 +01:00
|
|
|
void BaseTextDocumentLayout::updateMarksLineNumber()
|
|
|
|
|
{
|
2012-03-13 16:05:16 +01:00
|
|
|
// Note: the breakpointmanger deletes breakpoint marks and readds them
|
|
|
|
|
// if it doesn't agree with our updating
|
2012-02-14 19:27:15 +01:00
|
|
|
QTextBlock block = document()->begin();
|
|
|
|
|
int blockNumber = 0;
|
|
|
|
|
while (block.isValid()) {
|
|
|
|
|
if (const TextBlockUserData *userData = testUserData(block))
|
2012-03-13 16:05:16 +01:00
|
|
|
foreach (ITextMark *mrk, userData->marks())
|
2012-02-14 19:27:15 +01:00
|
|
|
mrk->updateLineNumber(blockNumber + 1);
|
|
|
|
|
block = block.next();
|
|
|
|
|
++blockNumber;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseTextDocumentLayout::updateMarksBlock(const QTextBlock &block)
|
|
|
|
|
{
|
|
|
|
|
if (const TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
foreach (ITextMark *mrk, userData->marks())
|
|
|
|
|
mrk->updateBlock(block);
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-12 12:13:23 +02:00
|
|
|
BaseTextDocumentLayout::FoldValidator::FoldValidator()
|
|
|
|
|
: m_layout(0)
|
|
|
|
|
, m_requestDocUpdate(false)
|
|
|
|
|
, m_insideFold(0)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void BaseTextDocumentLayout::FoldValidator::setup(BaseTextDocumentLayout *layout)
|
|
|
|
|
{
|
|
|
|
|
m_layout = layout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseTextDocumentLayout::FoldValidator::reset()
|
|
|
|
|
{
|
|
|
|
|
m_insideFold = 0;
|
|
|
|
|
m_requestDocUpdate = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseTextDocumentLayout::FoldValidator::process(QTextBlock block)
|
|
|
|
|
{
|
|
|
|
|
if (!m_layout)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const QTextBlock &previous = block.previous();
|
|
|
|
|
if (!previous.isValid())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if ((BaseTextDocumentLayout::isFolded(previous)
|
|
|
|
|
&& !BaseTextDocumentLayout::canFold(previous))
|
|
|
|
|
|| (!BaseTextDocumentLayout::isFolded(previous)
|
|
|
|
|
&& BaseTextDocumentLayout::canFold(previous)
|
|
|
|
|
&& !block.isVisible())) {
|
|
|
|
|
BaseTextDocumentLayout::setFolded(previous, !BaseTextDocumentLayout::isFolded(previous));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (BaseTextDocumentLayout::isFolded(previous) && !m_insideFold)
|
|
|
|
|
m_insideFold = BaseTextDocumentLayout::foldingIndent(block);
|
|
|
|
|
|
|
|
|
|
bool toggleVisibility = false;
|
|
|
|
|
if (m_insideFold) {
|
|
|
|
|
if (BaseTextDocumentLayout::foldingIndent(block) >= m_insideFold) {
|
|
|
|
|
if (block.isVisible())
|
|
|
|
|
toggleVisibility = true;
|
|
|
|
|
} else {
|
|
|
|
|
m_insideFold = 0;
|
|
|
|
|
if (!block.isVisible())
|
|
|
|
|
toggleVisibility = true;
|
|
|
|
|
}
|
|
|
|
|
} else if (!block.isVisible()) {
|
|
|
|
|
toggleVisibility = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (toggleVisibility) {
|
|
|
|
|
block.setVisible(!block.isVisible());
|
|
|
|
|
block.setLineCount(block.isVisible() ? qMax(1, block.layout()->lineCount()) : 0);
|
|
|
|
|
m_requestDocUpdate = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseTextDocumentLayout::FoldValidator::finalize()
|
|
|
|
|
{
|
|
|
|
|
if (m_requestDocUpdate && m_layout) {
|
|
|
|
|
m_layout->requestUpdate();
|
|
|
|
|
m_layout->emitDocumentSizeChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-02-15 13:06:16 +01:00
|
|
|
|
|
|
|
|
#include "basetextdocumentlayout.moc"
|