2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-04-26 14:02:09 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://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
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2010-04-26 14:02:09 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-04-26 14:02:09 +02:00
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
#include "textdocumentlayout.h"
|
2015-02-26 13:22:35 +01:00
|
|
|
#include "textdocument.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-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()
|
|
|
|
|
{
|
2014-07-19 11:27:28 +02:00
|
|
|
foreach (TextMark *mrk, m_marks) {
|
2014-07-18 15:29:04 +02:00
|
|
|
mrk->baseTextDocument()->removeMarkFromMarksCache(mrk);
|
|
|
|
|
mrk->setBaseTextDocument(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();
|
2014-09-26 09:14:03 +02:00
|
|
|
if (!TextDocumentLayout::hasParentheses(block) || TextDocumentLayout::ifdefedOut(block))
|
2010-04-26 14:02:09 +02:00
|
|
|
return NoMatch;
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
Parentheses parenList = TextDocumentLayout::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;
|
2014-09-26 09:14:03 +02:00
|
|
|
if (TextDocumentLayout::hasParentheses(closedParenParag)
|
|
|
|
|
&& !TextDocumentLayout::ifdefedOut(closedParenParag)) {
|
|
|
|
|
parenList = TextDocumentLayout::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();
|
2014-09-26 09:14:03 +02:00
|
|
|
if (!TextDocumentLayout::hasParentheses(block) || TextDocumentLayout::ifdefedOut(block))
|
2010-04-26 14:02:09 +02:00
|
|
|
return NoMatch;
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
Parentheses parenList = TextDocumentLayout::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;
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
if (TextDocumentLayout::hasParentheses(openParenParag)
|
|
|
|
|
&& !TextDocumentLayout::ifdefedOut(openParenParag)) {
|
|
|
|
|
parenList = TextDocumentLayout::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()) {
|
2014-09-26 09:14:03 +02:00
|
|
|
Parentheses parenList = TextDocumentLayout::parentheses(block);
|
|
|
|
|
if (!parenList.isEmpty() && !TextDocumentLayout::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()) {
|
2014-09-26 09:14:03 +02:00
|
|
|
Parentheses parenList = TextDocumentLayout::parentheses(block);
|
|
|
|
|
if (!parenList.isEmpty() && !TextDocumentLayout::ifdefedOut(block)) {
|
2010-04-26 14:02:09 +02:00
|
|
|
for (int i = parenList.count()-1; i >= 0; --i) {
|
|
|
|
|
Parenthesis paren = parenList.at(i);
|
2017-05-08 09:47:52 +02:00
|
|
|
if (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()) {
|
2014-09-26 09:14:03 +02:00
|
|
|
Parentheses parenList = TextDocumentLayout::parentheses(block);
|
|
|
|
|
if (!parenList.isEmpty() && !TextDocumentLayout::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()) {
|
2014-09-26 09:14:03 +02:00
|
|
|
Parentheses parenList = TextDocumentLayout::parentheses(block);
|
|
|
|
|
if (!parenList.isEmpty() && !TextDocumentLayout::ifdefedOut(block)) {
|
2010-04-26 14:02:09 +02:00
|
|
|
for (int i = 0; i < parenList.count(); ++i) {
|
|
|
|
|
Parenthesis paren = parenList.at(i);
|
2017-05-08 09:47:52 +02:00
|
|
|
if (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();
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
if (!TextDocumentLayout::hasParentheses(block) || TextDocumentLayout::ifdefedOut(block))
|
2010-04-26 14:02:09 +02:00
|
|
|
return NoMatch;
|
|
|
|
|
|
|
|
|
|
const int relPos = cursor->position() - block.position();
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
Parentheses parentheses = TextDocumentLayout::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();
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
if (!TextDocumentLayout::hasParentheses(block) || TextDocumentLayout::ifdefedOut(block))
|
2010-04-26 14:02:09 +02:00
|
|
|
return NoMatch;
|
|
|
|
|
|
|
|
|
|
const int relPos = cursor->position() - block.position();
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
Parentheses parentheses = TextDocumentLayout::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
|
|
|
|
2014-07-19 11:27:28 +02:00
|
|
|
void TextBlockUserData::addMark(TextMark *mark)
|
2011-03-02 18:43:26 +01:00
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
for ( ; i < m_marks.size(); ++i) {
|
|
|
|
|
if (mark->priority() < m_marks.at(i)->priority())
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
m_marks.insert(i, mark);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
TextDocumentLayout::TextDocumentLayout(QTextDocument *doc)
|
2012-02-15 13:06:16 +01:00
|
|
|
: QPlainTextDocumentLayout(doc),
|
|
|
|
|
lastSaveRevision(0),
|
|
|
|
|
hasMarks(false),
|
|
|
|
|
maxMarkWidthFactor(1.0),
|
2014-07-18 15:29:04 +02:00
|
|
|
m_requiredWidth(0)
|
|
|
|
|
{}
|
2010-04-26 14:02:09 +02:00
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
TextDocumentLayout::~TextDocumentLayout()
|
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
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
void TextDocumentLayout::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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
Parentheses TextDocumentLayout::parentheses(const QTextBlock &block)
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
return userData->parentheses();
|
|
|
|
|
return Parentheses();
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
bool TextDocumentLayout::hasParentheses(const QTextBlock &block)
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
return userData->hasParentheses();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
bool TextDocumentLayout::setIfdefedOut(const QTextBlock &block)
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
return userData(block)->setIfdefedOut();
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
bool TextDocumentLayout::clearIfdefedOut(const QTextBlock &block)
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
return userData->clearIfdefedOut();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
bool TextDocumentLayout::ifdefedOut(const QTextBlock &block)
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
return userData->ifdefedOut();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
int TextDocumentLayout::braceDepthDelta(const QTextBlock &block)
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
return userData->braceDepthDelta();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
int TextDocumentLayout::braceDepth(const QTextBlock &block)
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
int state = block.userState();
|
|
|
|
|
if (state == -1)
|
|
|
|
|
return 0;
|
|
|
|
|
return state >> 8;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
void TextDocumentLayout::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);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
void TextDocumentLayout::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
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
void TextDocumentLayout::setLexerState(const QTextBlock &block, int state)
|
2010-07-12 11:16:10 +02:00
|
|
|
{
|
|
|
|
|
if (state == 0) {
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
userData->setLexerState(0);
|
|
|
|
|
} else {
|
|
|
|
|
userData(block)->setLexerState(qMax(0,state));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
int TextDocumentLayout::lexerState(const QTextBlock &block)
|
2010-07-12 11:16:10 +02:00
|
|
|
{
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
return userData->lexerState();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
void TextDocumentLayout::setFoldingIndent(const QTextBlock &block, int indent)
|
2010-05-20 15:10:26 +02:00
|
|
|
{
|
|
|
|
|
if (indent == 0) {
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
userData->setFoldingIndent(0);
|
|
|
|
|
} else {
|
2015-05-20 08:38:52 +02:00
|
|
|
userData(block)->setFoldingIndent(indent);
|
2010-05-20 15:10:26 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
int TextDocumentLayout::foldingIndent(const QTextBlock &block)
|
2010-05-20 15:10:26 +02:00
|
|
|
{
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
return userData->foldingIndent();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
void TextDocumentLayout::changeFoldingIndent(QTextBlock &block, int delta)
|
2010-05-20 15:10:26 +02:00
|
|
|
{
|
|
|
|
|
if (delta)
|
|
|
|
|
setFoldingIndent(block, foldingIndent(block) + delta);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
bool TextDocumentLayout::canFold(const QTextBlock &block)
|
2010-05-20 15:10:26 +02:00
|
|
|
{
|
|
|
|
|
return (block.next().isValid() && foldingIndent(block.next()) > foldingIndent(block));
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
bool TextDocumentLayout::isFolded(const QTextBlock &block)
|
2010-05-20 15:10:26 +02:00
|
|
|
{
|
|
|
|
|
if (TextBlockUserData *userData = testUserData(block))
|
|
|
|
|
return userData->folded();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
void TextDocumentLayout::setFolded(const QTextBlock &block, bool folded)
|
2010-05-20 15:10:26 +02:00
|
|
|
{
|
|
|
|
|
if (folded)
|
|
|
|
|
userData(block)->setFolded(true);
|
2013-07-17 00:01:45 +03:00
|
|
|
else if (TextBlockUserData *userData = testUserData(block))
|
2017-04-14 12:06:24 +02:00
|
|
|
userData->setFolded(false);
|
2017-10-11 14:14:10 +02:00
|
|
|
else
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
TextDocumentLayout *layout = qobject_cast<TextDocumentLayout *>(
|
|
|
|
|
block.document()->documentLayout());
|
|
|
|
|
if (layout)
|
|
|
|
|
emit layout->foldChanged(block.blockNumber(), folded);
|
2010-05-20 15:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
void TextDocumentLayout::requestExtraAreaUpdate()
|
2012-02-17 14:59:14 +01:00
|
|
|
{
|
|
|
|
|
emit updateExtraArea();
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
void TextDocumentLayout::doFoldOrUnfold(const QTextBlock& block, bool unfold)
|
2010-05-20 15:10:26 +02:00
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
void TextDocumentLayout::setRequiredWidth(int width)
|
2010-07-02 13:47:14 +02:00
|
|
|
{
|
|
|
|
|
int oldw = m_requiredWidth;
|
|
|
|
|
m_requiredWidth = width;
|
|
|
|
|
int dw = QPlainTextDocumentLayout::documentSize().width();
|
|
|
|
|
if (oldw > dw || width > dw)
|
|
|
|
|
emitDocumentSizeChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
QSizeF TextDocumentLayout::documentSize() const
|
2010-07-02 13:47:14 +02:00
|
|
|
{
|
|
|
|
|
QSizeF size = QPlainTextDocumentLayout::documentSize();
|
|
|
|
|
size.setWidth(qMax((qreal)m_requiredWidth, size.width()));
|
|
|
|
|
return size;
|
|
|
|
|
}
|
2011-08-12 12:13:23 +02:00
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
TextMarks TextDocumentLayout::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
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
void TextDocumentLayout::documentReloaded(TextMarks marks, TextDocument *baseTextDocument)
|
2012-03-30 11:40:31 +02:00
|
|
|
{
|
2014-07-19 11:27:28 +02:00
|
|
|
foreach (TextMark *mark, marks) {
|
2012-03-30 11:40:31 +02:00
|
|
|
int blockNumber = mark->lineNumber() - 1;
|
|
|
|
|
QTextBlock block = document()->findBlockByNumber(blockNumber);
|
|
|
|
|
if (block.isValid()) {
|
2014-09-26 09:14:03 +02:00
|
|
|
TextBlockUserData *userData = TextDocumentLayout::userData(block);
|
2012-03-30 11:40:31 +02:00
|
|
|
userData->addMark(mark);
|
2014-07-18 15:29:04 +02:00
|
|
|
mark->setBaseTextDocument(baseTextDocument);
|
2012-03-30 11:40:31 +02:00
|
|
|
mark->updateBlock(block);
|
|
|
|
|
} else {
|
2014-07-18 15:29:04 +02:00
|
|
|
baseTextDocument->removeMarkFromMarksCache(mark);
|
|
|
|
|
mark->setBaseTextDocument(0);
|
2014-10-07 07:44:57 +02:00
|
|
|
mark->removedFromEditor();
|
2012-03-30 11:40:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
requestUpdate();
|
|
|
|
|
}
|
2012-02-15 13:39:10 +01:00
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
void TextDocumentLayout::updateMarksLineNumber()
|
2012-02-14 19:27:15 +01:00
|
|
|
{
|
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))
|
2014-07-19 11:27:28 +02:00
|
|
|
foreach (TextMark *mrk, userData->marks())
|
2012-02-14 19:27:15 +01:00
|
|
|
mrk->updateLineNumber(blockNumber + 1);
|
|
|
|
|
block = block.next();
|
|
|
|
|
++blockNumber;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
void TextDocumentLayout::updateMarksBlock(const QTextBlock &block)
|
2012-02-14 19:27:15 +01:00
|
|
|
{
|
|
|
|
|
if (const TextBlockUserData *userData = testUserData(block))
|
2014-07-19 11:27:28 +02:00
|
|
|
foreach (TextMark *mrk, userData->marks())
|
2012-02-14 19:27:15 +01:00
|
|
|
mrk->updateBlock(block);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
TextDocumentLayout::FoldValidator::FoldValidator()
|
2011-08-12 12:13:23 +02:00
|
|
|
: m_layout(0)
|
|
|
|
|
, m_requestDocUpdate(false)
|
|
|
|
|
, m_insideFold(0)
|
|
|
|
|
{}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
void TextDocumentLayout::FoldValidator::setup(TextDocumentLayout *layout)
|
2011-08-12 12:13:23 +02:00
|
|
|
{
|
|
|
|
|
m_layout = layout;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
void TextDocumentLayout::FoldValidator::reset()
|
2011-08-12 12:13:23 +02:00
|
|
|
{
|
|
|
|
|
m_insideFold = 0;
|
|
|
|
|
m_requestDocUpdate = false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
void TextDocumentLayout::FoldValidator::process(QTextBlock block)
|
2011-08-12 12:13:23 +02:00
|
|
|
{
|
|
|
|
|
if (!m_layout)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const QTextBlock &previous = block.previous();
|
|
|
|
|
if (!previous.isValid())
|
|
|
|
|
return;
|
|
|
|
|
|
2016-05-18 09:20:08 +02:00
|
|
|
const bool preIsFolded = isFolded(previous);
|
|
|
|
|
const bool preCanFold = canFold(previous);
|
|
|
|
|
const bool isVisible = block.isVisible();
|
|
|
|
|
|
|
|
|
|
if (preIsFolded && !preCanFold)
|
|
|
|
|
setFolded(previous, false);
|
|
|
|
|
else if (!preIsFolded && preCanFold && previous.isVisible() && !isVisible)
|
|
|
|
|
setFolded(previous, true);
|
|
|
|
|
|
|
|
|
|
if (isFolded(previous) && !m_insideFold)
|
|
|
|
|
m_insideFold = foldingIndent(block);
|
|
|
|
|
|
|
|
|
|
bool shouldBeVisible = m_insideFold == 0;
|
|
|
|
|
if (!shouldBeVisible) {
|
|
|
|
|
shouldBeVisible = foldingIndent(block) < m_insideFold;
|
|
|
|
|
if (shouldBeVisible)
|
2011-08-12 12:13:23 +02:00
|
|
|
m_insideFold = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-18 09:20:08 +02:00
|
|
|
if (shouldBeVisible != isVisible) {
|
|
|
|
|
block.setVisible(shouldBeVisible);
|
2011-08-12 12:13:23 +02:00
|
|
|
block.setLineCount(block.isVisible() ? qMax(1, block.layout()->lineCount()) : 0);
|
|
|
|
|
m_requestDocUpdate = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
void TextDocumentLayout::FoldValidator::finalize()
|
2011-08-12 12:13:23 +02:00
|
|
|
{
|
|
|
|
|
if (m_requestDocUpdate && m_layout) {
|
|
|
|
|
m_layout->requestUpdate();
|
|
|
|
|
m_layout->emitDocumentSizeChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|