2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-11-26 10:59:33 +01:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2010-11-26 10:59:33 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-11-26 10:59:33 +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
|
2014-10-01 13:21:18 +02:00
|
|
|
** conditions see http://www.qt.io/licensing. For further information
|
|
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2010-11-26 10:59:33 +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
|
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.
|
2010-11-26 10:59:33 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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-26 10:59:33 +01:00
|
|
|
|
|
|
|
|
#include "glslindenter.h"
|
|
|
|
|
|
|
|
|
|
#include <cpptools/cppcodeformatter.h>
|
2011-02-03 15:48:14 +01:00
|
|
|
#include <cpptools/cpptoolssettings.h>
|
|
|
|
|
#include <cpptools/cppcodestylepreferences.h>
|
2010-11-26 10:59:33 +01:00
|
|
|
#include <texteditor/tabsettings.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QChar>
|
|
|
|
|
#include <QTextDocument>
|
|
|
|
|
#include <QTextBlock>
|
|
|
|
|
#include <QTextCursor>
|
2010-11-26 10:59:33 +01:00
|
|
|
|
2014-08-21 20:11:15 +02:00
|
|
|
namespace GlslEditor {
|
2014-08-21 08:29:55 +02:00
|
|
|
namespace Internal {
|
2010-11-26 10:59:33 +01:00
|
|
|
|
2014-08-21 08:29:55 +02:00
|
|
|
GlslIndenter::GlslIndenter()
|
2010-11-26 10:59:33 +01:00
|
|
|
{}
|
|
|
|
|
|
2014-08-21 08:29:55 +02:00
|
|
|
GlslIndenter::~GlslIndenter()
|
2010-11-26 10:59:33 +01:00
|
|
|
{}
|
|
|
|
|
|
2014-08-21 08:29:55 +02:00
|
|
|
bool GlslIndenter::isElectricCharacter(const QChar &ch) const
|
2010-11-26 10:59:33 +01:00
|
|
|
{
|
2014-08-21 08:29:55 +02:00
|
|
|
return ch == QLatin1Char('{')
|
|
|
|
|
|| ch == QLatin1Char('}')
|
|
|
|
|
|| ch == QLatin1Char(':')
|
|
|
|
|
|| ch == QLatin1Char('#');
|
2010-11-26 10:59:33 +01:00
|
|
|
}
|
|
|
|
|
|
2014-08-21 08:29:55 +02:00
|
|
|
void GlslIndenter::indentBlock(QTextDocument *doc,
|
2010-11-30 14:14:33 +01:00
|
|
|
const QTextBlock &block,
|
|
|
|
|
const QChar &typedChar,
|
2011-08-04 11:19:25 +02:00
|
|
|
const TextEditor::TabSettings &tabSettings)
|
2010-11-26 10:59:33 +01:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(doc)
|
|
|
|
|
|
2011-02-03 15:48:14 +01:00
|
|
|
// TODO: do something with it
|
2011-08-04 11:19:25 +02:00
|
|
|
CppTools::QtStyleCodeFormatter codeFormatter(tabSettings,
|
2011-08-16 10:45:23 +02:00
|
|
|
CppTools::CppToolsSettings::instance()->cppCodeStyle()->codeStyleSettings());
|
2010-11-26 10:59:33 +01:00
|
|
|
|
|
|
|
|
codeFormatter.updateStateUntil(block);
|
|
|
|
|
int indent;
|
|
|
|
|
int padding;
|
|
|
|
|
codeFormatter.indentFor(block, &indent, &padding);
|
|
|
|
|
|
2014-08-21 08:29:55 +02:00
|
|
|
// 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-26 10:59:33 +01:00
|
|
|
if (isElectricCharacter(typedChar)) {
|
|
|
|
|
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-26 10:59:33 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-04 11:19:25 +02:00
|
|
|
tabSettings.indentLine(block, indent + padding, padding);
|
2010-11-26 10:59:33 +01:00
|
|
|
}
|
|
|
|
|
|
2014-08-21 08:29:55 +02:00
|
|
|
void GlslIndenter::indent(QTextDocument *doc,
|
2010-11-30 14:14:33 +01:00
|
|
|
const QTextCursor &cursor,
|
|
|
|
|
const QChar &typedChar,
|
2011-08-04 11:19:25 +02:00
|
|
|
const TextEditor::TabSettings &tabSettings)
|
2010-11-26 10:59:33 +01:00
|
|
|
{
|
|
|
|
|
if (cursor.hasSelection()) {
|
|
|
|
|
QTextBlock block = doc->findBlock(cursor.selectionStart());
|
|
|
|
|
const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
|
|
|
|
|
|
2011-02-03 15:48:14 +01:00
|
|
|
// TODO: do something with it
|
2011-08-04 11:19:25 +02:00
|
|
|
CppTools::QtStyleCodeFormatter codeFormatter(tabSettings,
|
2011-08-16 10:45:23 +02:00
|
|
|
CppTools::CppToolsSettings::instance()->cppCodeStyle()->codeStyleSettings());
|
2010-11-26 10:59:33 +01:00
|
|
|
codeFormatter.updateStateUntil(block);
|
|
|
|
|
|
2011-08-04 11:19:25 +02:00
|
|
|
QTextCursor tc = cursor;
|
2010-11-26 10:59:33 +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-26 10:59:33 +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-26 10:59:33 +01:00
|
|
|
}
|
|
|
|
|
}
|
2014-08-21 08:29:55 +02:00
|
|
|
|
|
|
|
|
} // namespace Internal
|
2014-08-21 20:11:15 +02:00
|
|
|
} // namespace GlslEditor
|