2010-11-26 10:59:33 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2012-01-26 18:33:46 +01:00
|
|
|
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
2010-11-26 10:59:33 +01:00
|
|
|
**
|
2012-07-19 12:26:56 +02:00
|
|
|
** Contact: http://www.qt-project.org/
|
2010-11-26 10:59:33 +01:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2010-11-26 10:59:33 +01:00
|
|
|
**
|
2010-12-17 17:14:20 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 17:14:20 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
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
|
|
|
|
|
|
|
|
using namespace GLSLEditor;
|
|
|
|
|
using namespace Internal;
|
|
|
|
|
|
|
|
|
|
GLSLIndenter::GLSLIndenter()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
GLSLIndenter::~GLSLIndenter()
|
|
|
|
|
{}
|
|
|
|
|
|
2010-11-30 14:14:33 +01:00
|
|
|
bool GLSLIndenter::isElectricCharacter(const QChar &ch) const
|
2010-11-26 10:59:33 +01:00
|
|
|
{
|
|
|
|
|
if (ch == QLatin1Char('{') ||
|
|
|
|
|
ch == QLatin1Char('}') ||
|
|
|
|
|
ch == QLatin1Char(':') ||
|
|
|
|
|
ch == QLatin1Char('#')) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-30 14:14:33 +01:00
|
|
|
void GLSLIndenter::indentBlock(QTextDocument *doc,
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// only reindent the current line when typing electric characters if the
|
|
|
|
|
// indent is the same it would be if the line were empty
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2010-11-30 14:14:33 +01:00
|
|
|
void GLSLIndenter::indent(QTextDocument *doc,
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|