2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-05-10 12:53:13 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-05-10 12:53:13 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-05-10 12:53:13 +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-05-10 12:53:13 +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-05-10 12:53:13 +02:00
|
|
|
|
|
|
|
|
#include "indenter.h"
|
|
|
|
|
#include "tabsettings.h"
|
2016-01-13 14:32:23 +01:00
|
|
|
#include "textdocumentlayout.h"
|
2010-05-10 12:53:13 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTextDocument>
|
|
|
|
|
#include <QTextCursor>
|
2011-08-04 11:19:25 +02:00
|
|
|
|
2010-05-10 12:53:13 +02:00
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
|
|
|
|
Indenter::Indenter()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
Indenter::~Indenter()
|
|
|
|
|
{}
|
|
|
|
|
|
2010-11-30 14:14:33 +01:00
|
|
|
bool Indenter::isElectricCharacter(const QChar &) const
|
2010-11-05 14:27:16 +01:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-30 14:14:33 +01:00
|
|
|
void Indenter::indentBlock(QTextDocument *doc,
|
2010-11-09 10:36:02 +01:00
|
|
|
const QTextBlock &block,
|
|
|
|
|
const QChar &typedChar,
|
2015-02-03 23:46:35 +02:00
|
|
|
const TabSettings &tabSettings)
|
2010-11-09 10:36:02 +01:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(doc);
|
|
|
|
|
Q_UNUSED(typedChar);
|
2016-01-13 14:32:23 +01:00
|
|
|
const int indent = indentFor(block, tabSettings);
|
|
|
|
|
if (indent < 0)
|
|
|
|
|
return;
|
|
|
|
|
tabSettings.indentLine(block, indent);
|
2010-11-09 10:36:02 +01:00
|
|
|
}
|
|
|
|
|
|
2010-11-30 14:14:33 +01:00
|
|
|
void Indenter::indent(QTextDocument *doc,
|
|
|
|
|
const QTextCursor &cursor,
|
|
|
|
|
const QChar &typedChar,
|
2018-08-29 15:58:13 +02:00
|
|
|
const TabSettings &tabSettings,
|
|
|
|
|
bool /*autoTriggered*/)
|
2010-05-10 12:53:13 +02:00
|
|
|
{
|
2010-11-05 14:27:16 +01:00
|
|
|
if (cursor.hasSelection()) {
|
|
|
|
|
QTextBlock block = doc->findBlock(cursor.selectionStart());
|
|
|
|
|
const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
|
|
|
|
|
do {
|
2011-08-04 11:19:25 +02:00
|
|
|
indentBlock(doc, block, typedChar, tabSettings);
|
2010-11-05 14:27:16 +01:00
|
|
|
block = block.next();
|
|
|
|
|
} while (block.isValid() && block != end);
|
|
|
|
|
} else {
|
2011-08-04 11:19:25 +02:00
|
|
|
indentBlock(doc, cursor.block(), typedChar, tabSettings);
|
2010-11-05 14:27:16 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-03 23:46:35 +02:00
|
|
|
void Indenter::reindent(QTextDocument *doc, const QTextCursor &cursor, const TabSettings &tabSettings)
|
2010-11-05 14:27:16 +01:00
|
|
|
{
|
|
|
|
|
if (cursor.hasSelection()) {
|
|
|
|
|
QTextBlock block = doc->findBlock(cursor.selectionStart());
|
|
|
|
|
const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
|
|
|
|
|
|
|
|
|
|
// skip empty blocks
|
|
|
|
|
while (block.isValid() && block != end) {
|
|
|
|
|
QString bt = block.text();
|
2011-08-04 11:19:25 +02:00
|
|
|
if (tabSettings.firstNonSpace(bt) < bt.size())
|
2010-11-05 14:27:16 +01:00
|
|
|
break;
|
2011-08-04 11:19:25 +02:00
|
|
|
indentBlock(doc, block, QChar::Null, tabSettings);
|
2010-11-05 14:27:16 +01:00
|
|
|
block = block.next();
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-04 11:19:25 +02:00
|
|
|
int previousIndentation = tabSettings.indentationColumn(block.text());
|
|
|
|
|
indentBlock(doc, block, QChar::Null, tabSettings);
|
|
|
|
|
int currentIndentation = tabSettings.indentationColumn(block.text());
|
2010-11-05 14:27:16 +01:00
|
|
|
int delta = currentIndentation - previousIndentation;
|
|
|
|
|
|
|
|
|
|
block = block.next();
|
|
|
|
|
while (block.isValid() && block != end) {
|
2011-08-04 11:19:25 +02:00
|
|
|
tabSettings.reindentLine(block, delta);
|
2010-11-05 14:27:16 +01:00
|
|
|
block = block.next();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2011-08-04 11:19:25 +02:00
|
|
|
indentBlock(doc, cursor.block(), QChar::Null, tabSettings);
|
2010-11-05 14:27:16 +01:00
|
|
|
}
|
2010-05-10 12:53:13 +02:00
|
|
|
}
|
2011-02-03 15:48:14 +01:00
|
|
|
|
2011-08-16 10:45:23 +02:00
|
|
|
void Indenter::setCodeStylePreferences(ICodeStylePreferences *)
|
2011-02-03 15:48:14 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
void Indenter::invalidateCache(QTextDocument *)
|
|
|
|
|
{
|
|
|
|
|
}
|
2016-01-13 14:32:23 +01:00
|
|
|
|
|
|
|
|
int Indenter::indentFor(const QTextBlock &block, const TabSettings &tabSettings)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(block)
|
|
|
|
|
Q_UNUSED(tabSettings)
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2016-08-25 13:25:45 +02:00
|
|
|
|
|
|
|
|
IndentationForBlock Indenter::indentationForBlocks(const QVector<QTextBlock> &blocks,
|
|
|
|
|
const TabSettings &tabSettings)
|
|
|
|
|
{
|
|
|
|
|
IndentationForBlock ret;
|
|
|
|
|
foreach (QTextBlock block, blocks)
|
|
|
|
|
ret.insert(block.blockNumber(), indentFor(block, tabSettings));
|
|
|
|
|
return ret;
|
|
|
|
|
}
|