2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-11-05 14:27:16 +01: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-11-05 14:27:16 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-11-05 14:27:16 +01: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-11-05 14:27:16 +01: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 17:14:20 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-11-05 14:27:16 +01:00
|
|
|
|
|
|
|
|
#include "cppqtstyleindenter.h"
|
|
|
|
|
|
2011-02-03 15:48:14 +01:00
|
|
|
#include "cppcodeformatter.h"
|
|
|
|
|
#include "cpptoolssettings.h"
|
|
|
|
|
#include "cppcodestylepreferences.h"
|
|
|
|
|
#include "cpptoolsconstants.h"
|
2010-11-05 14:27:16 +01:00
|
|
|
#include <texteditor/tabsettings.h>
|
2011-02-03 15:48:14 +01:00
|
|
|
#include <texteditor/texteditorsettings.h>
|
2010-11-05 14:27:16 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QChar>
|
|
|
|
|
#include <QTextDocument>
|
|
|
|
|
#include <QTextBlock>
|
|
|
|
|
#include <QTextCursor>
|
2010-11-05 14:27:16 +01:00
|
|
|
|
2011-02-03 15:48:14 +01:00
|
|
|
using namespace CppTools;
|
2010-11-05 14:27:16 +01:00
|
|
|
|
|
|
|
|
CppQtStyleIndenter::CppQtStyleIndenter()
|
2011-02-03 15:48:14 +01:00
|
|
|
: m_cppCodeStylePreferences(0)
|
|
|
|
|
{
|
2011-07-12 08:52:32 +02:00
|
|
|
// Just for safety. setCodeStylePreferences should be called when the editor the
|
|
|
|
|
// indenter belongs to gets initialized.
|
2011-08-16 10:45:23 +02:00
|
|
|
m_cppCodeStylePreferences = CppToolsSettings::instance()->cppCodeStyle();
|
2011-02-03 15:48:14 +01:00
|
|
|
}
|
2010-11-05 14:27:16 +01:00
|
|
|
|
|
|
|
|
CppQtStyleIndenter::~CppQtStyleIndenter()
|
|
|
|
|
{}
|
|
|
|
|
|
2010-11-30 14:14:33 +01:00
|
|
|
bool CppQtStyleIndenter::isElectricCharacter(const QChar &ch) const
|
2010-11-05 14:27:16 +01:00
|
|
|
{
|
|
|
|
|
if (ch == QLatin1Char('{') ||
|
|
|
|
|
ch == QLatin1Char('}') ||
|
|
|
|
|
ch == QLatin1Char(':') ||
|
|
|
|
|
ch == QLatin1Char('#')) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-08 13:50:24 +01:00
|
|
|
static bool colonIsElectric(const QString &text)
|
|
|
|
|
{
|
|
|
|
|
// switch cases and access declarations should be reindented
|
|
|
|
|
if (text.contains(QLatin1String("case"))
|
|
|
|
|
|| text.contains(QLatin1String("default"))
|
|
|
|
|
|| text.contains(QLatin1String("public"))
|
|
|
|
|
|| text.contains(QLatin1String("private"))
|
|
|
|
|
|| text.contains(QLatin1String("protected"))
|
2011-05-25 12:05:52 +02:00
|
|
|
|| text.contains(QLatin1String("signals"))
|
|
|
|
|
|| text.contains(QLatin1String("Q_SIGNALS"))) {
|
2010-12-08 13:50:24 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// lines that start with : might have a constructor initializer list
|
|
|
|
|
const QString trimmedtext = text.trimmed();
|
|
|
|
|
if (!trimmedtext.isEmpty() && trimmedtext.at(0) == QLatin1Char(':'))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-30 14:14:33 +01:00
|
|
|
void CppQtStyleIndenter::indentBlock(QTextDocument *doc,
|
|
|
|
|
const QTextBlock &block,
|
|
|
|
|
const QChar &typedChar,
|
2011-08-04 11:19:25 +02:00
|
|
|
const TextEditor::TabSettings &tabSettings)
|
2010-11-05 14:27:16 +01:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(doc)
|
|
|
|
|
|
2011-08-04 11:19:25 +02:00
|
|
|
CppTools::QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings());
|
2010-11-05 14:27:16 +01:00
|
|
|
|
|
|
|
|
codeFormatter.updateStateUntil(block);
|
|
|
|
|
int indent;
|
|
|
|
|
int padding;
|
|
|
|
|
codeFormatter.indentFor(block, &indent, &padding);
|
|
|
|
|
|
|
|
|
|
if (isElectricCharacter(typedChar)) {
|
2010-12-08 13:50:24 +01:00
|
|
|
// : should not be electric for labels
|
|
|
|
|
if (typedChar == QLatin1Char(':') && !colonIsElectric(block.text()))
|
2010-12-07 14:30:17 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// only reindent the current line when typing electric characters if the
|
|
|
|
|
// indent is the same it would be if the line were empty
|
2010-11-05 14:27:16 +01:00
|
|
|
int newlineIndent;
|
|
|
|
|
int newlinePadding;
|
|
|
|
|
codeFormatter.indentForNewLineAfter(block.previous(), &newlineIndent, &newlinePadding);
|
2011-08-04 11:19:25 +02:00
|
|
|
if (tabSettings.indentationColumn(block.text()) != newlineIndent + newlinePadding)
|
2010-11-05 14:27:16 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-04 11:19:25 +02:00
|
|
|
tabSettings.indentLine(block, indent + padding, padding);
|
2010-11-05 14:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
2010-11-30 14:14:33 +01:00
|
|
|
void CppQtStyleIndenter::indent(QTextDocument *doc,
|
|
|
|
|
const QTextCursor &cursor,
|
|
|
|
|
const QChar &typedChar,
|
2011-08-04 11:19:25 +02:00
|
|
|
const TextEditor::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();
|
|
|
|
|
|
2011-08-04 11:19:25 +02:00
|
|
|
CppTools::QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings());
|
2010-11-05 14:27:16 +01:00
|
|
|
codeFormatter.updateStateUntil(block);
|
|
|
|
|
|
2011-08-04 11:19:25 +02:00
|
|
|
QTextCursor tc = cursor;
|
2010-11-05 14:27:16 +01:00
|
|
|
tc.beginEditBlock();
|
|
|
|
|
do {
|
|
|
|
|
int indent;
|
|
|
|
|
int padding;
|
|
|
|
|
codeFormatter.indentFor(block, &indent, &padding);
|
2011-08-04 11:19:25 +02:00
|
|
|
tabSettings.indentLine(block, indent + padding, padding);
|
2010-11-05 14:27:16 +01:00
|
|
|
codeFormatter.updateLineStateChange(block);
|
|
|
|
|
block = block.next();
|
|
|
|
|
} while (block.isValid() && block != end);
|
|
|
|
|
tc.endEditBlock();
|
|
|
|
|
} else {
|
2011-08-04 11:19:25 +02:00
|
|
|
indentBlock(doc, cursor.block(), typedChar, tabSettings);
|
2010-11-05 14:27:16 +01:00
|
|
|
}
|
|
|
|
|
}
|
2011-02-03 15:48:14 +01:00
|
|
|
|
2011-08-16 10:45:23 +02:00
|
|
|
void CppQtStyleIndenter::setCodeStylePreferences(TextEditor::ICodeStylePreferences *preferences)
|
2011-02-03 15:48:14 +01:00
|
|
|
{
|
|
|
|
|
CppTools::CppCodeStylePreferences *cppCodeStylePreferences
|
|
|
|
|
= qobject_cast<CppTools::CppCodeStylePreferences *>(preferences);
|
|
|
|
|
if (cppCodeStylePreferences)
|
|
|
|
|
m_cppCodeStylePreferences = cppCodeStylePreferences;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-16 10:45:23 +02:00
|
|
|
void CppQtStyleIndenter::invalidateCache(QTextDocument *doc)
|
|
|
|
|
{
|
|
|
|
|
CppTools::QtStyleCodeFormatter formatter;
|
|
|
|
|
formatter.invalidateCache(doc);
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-03 15:48:14 +01:00
|
|
|
CppCodeStyleSettings CppQtStyleIndenter::codeStyleSettings() const
|
|
|
|
|
{
|
|
|
|
|
if (m_cppCodeStylePreferences)
|
2011-08-16 10:45:23 +02:00
|
|
|
return m_cppCodeStylePreferences->currentCodeStyleSettings();
|
2011-02-03 15:48:14 +01:00
|
|
|
return CppCodeStyleSettings();
|
|
|
|
|
}
|