2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-11-26 10:59:33 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
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
|
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-11-26 10:59:33 +01: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 17:14:20 +01:00
|
|
|
**
|
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
|
|
|
|
2019-01-16 09:37:54 +01:00
|
|
|
GlslIndenter::GlslIndenter(QTextDocument *doc)
|
|
|
|
|
: TextEditor::TextIndenter(doc)
|
|
|
|
|
{}
|
2018-11-11 00:24:20 +01:00
|
|
|
GlslIndenter::~GlslIndenter() = default;
|
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
|
|
|
{
|
2019-01-16 09:37:54 +01:00
|
|
|
return ch == QLatin1Char('{') || ch == QLatin1Char('}') || ch == QLatin1Char(':')
|
|
|
|
|
|| ch == QLatin1Char('#');
|
2010-11-26 10:59:33 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-16 09:37:54 +01:00
|
|
|
void GlslIndenter::indentBlock(const QTextBlock &block,
|
2010-11-30 14:14:33 +01:00
|
|
|
const QChar &typedChar,
|
2011-08-04 11:19:25 +02:00
|
|
|
const TextEditor::TabSettings &tabSettings)
|
2010-11-26 10:59:33 +01:00
|
|
|
{
|
2011-02-03 15:48:14 +01:00
|
|
|
// TODO: do something with it
|
2019-01-16 09:37:54 +01:00
|
|
|
CppTools::QtStyleCodeFormatter
|
|
|
|
|
codeFormatter(tabSettings,
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2019-01-16 09:37:54 +01:00
|
|
|
void GlslIndenter::indent(const QTextCursor &cursor,
|
2010-11-30 14:14:33 +01:00
|
|
|
const QChar &typedChar,
|
2019-01-16 09:37:54 +01:00
|
|
|
const TextEditor::TabSettings &tabSettings)
|
2010-11-26 10:59:33 +01:00
|
|
|
{
|
|
|
|
|
if (cursor.hasSelection()) {
|
2019-01-16 09:37:54 +01:00
|
|
|
QTextBlock block = m_doc->findBlock(cursor.selectionStart());
|
|
|
|
|
const QTextBlock end = m_doc->findBlock(cursor.selectionEnd()).next();
|
2010-11-26 10:59:33 +01:00
|
|
|
|
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,
|
2019-01-16 09:37:54 +01: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 {
|
2019-01-16 09:37:54 +01:00
|
|
|
indentBlock(cursor.block(), typedChar, tabSettings);
|
2010-11-26 10:59:33 +01:00
|
|
|
}
|
|
|
|
|
}
|
2014-08-21 08:29:55 +02:00
|
|
|
|
2016-01-13 14:32:23 +01:00
|
|
|
int GlslIndenter::indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings)
|
|
|
|
|
{
|
2019-01-16 09:37:54 +01:00
|
|
|
CppTools::QtStyleCodeFormatter
|
|
|
|
|
codeFormatter(tabSettings,
|
|
|
|
|
CppTools::CppToolsSettings::instance()->cppCodeStyle()->codeStyleSettings());
|
2016-01-13 14:32:23 +01:00
|
|
|
|
|
|
|
|
codeFormatter.updateStateUntil(block);
|
|
|
|
|
int indent;
|
|
|
|
|
int padding;
|
|
|
|
|
codeFormatter.indentFor(block, &indent, &padding);
|
|
|
|
|
|
|
|
|
|
return indent;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-16 09:37:54 +01:00
|
|
|
TextEditor::IndentationForBlock GlslIndenter::indentationForBlocks(
|
|
|
|
|
const QVector<QTextBlock> &blocks, const TextEditor::TabSettings &tabSettings)
|
2016-08-25 13:25:45 +02:00
|
|
|
{
|
2019-01-16 09:37:54 +01:00
|
|
|
CppTools::QtStyleCodeFormatter
|
|
|
|
|
codeFormatter(tabSettings,
|
|
|
|
|
CppTools::CppToolsSettings::instance()->cppCodeStyle()->codeStyleSettings());
|
2016-08-25 13:25:45 +02:00
|
|
|
|
|
|
|
|
codeFormatter.updateStateUntil(blocks.last());
|
|
|
|
|
|
|
|
|
|
TextEditor::IndentationForBlock ret;
|
|
|
|
|
foreach (QTextBlock block, blocks) {
|
|
|
|
|
int indent;
|
|
|
|
|
int padding;
|
|
|
|
|
codeFormatter.indentFor(block, &indent, &padding);
|
|
|
|
|
ret.insert(block.blockNumber(), indent);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-21 08:29:55 +02:00
|
|
|
} // namespace Internal
|
2014-08-21 20:11:15 +02:00
|
|
|
} // namespace GlslEditor
|