2015-08-24 08:43:18 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 Jan Dalheimer <jan@dalheimer.de>
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2015-08-24 08:43:18 +02:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
2015-08-24 08:43:18 +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.
|
2015-08-24 08:43:18 +02:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "cmakeindenter.h"
|
|
|
|
|
|
|
|
|
|
namespace CMakeProjectManager {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2019-01-16 09:37:54 +01:00
|
|
|
CMakeIndenter::CMakeIndenter(QTextDocument *doc)
|
|
|
|
|
: TextEditor::TextIndenter(doc)
|
|
|
|
|
{}
|
|
|
|
|
|
2015-08-24 08:43:18 +02:00
|
|
|
bool CMakeIndenter::isElectricCharacter(const QChar &ch) const
|
|
|
|
|
{
|
|
|
|
|
return ch == QLatin1Char('(') || ch == QLatin1Char(')');
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 14:59:39 +01:00
|
|
|
static int startsWithChar(const QString &line, char character)
|
|
|
|
|
{
|
|
|
|
|
int occurrences = 0;
|
|
|
|
|
for (int i = 0; i < line.size(); ++i) {
|
|
|
|
|
if (line.at(i) == character) {
|
|
|
|
|
occurrences++;
|
|
|
|
|
} else if (!line.at(i).isSpace()) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return occurrences;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-24 08:43:18 +02:00
|
|
|
static bool lineContainsFunction(const QString &line, const QString &function)
|
|
|
|
|
{
|
|
|
|
|
const int indexOfFunction = line.indexOf(function);
|
|
|
|
|
if (indexOfFunction == -1)
|
|
|
|
|
return false;
|
|
|
|
|
for (int i = 0; i < indexOfFunction; ++i) {
|
|
|
|
|
if (!line.at(i).isSpace())
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
for (int i = indexOfFunction + function.size(); i < line.size(); ++i) {
|
|
|
|
|
if (line.at(i) == QLatin1Char('('))
|
|
|
|
|
return true;
|
|
|
|
|
else if (!line.at(i).isSpace())
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
static bool lineStartsBlock(const QString &line)
|
|
|
|
|
{
|
|
|
|
|
return lineContainsFunction(line, QStringLiteral("function")) ||
|
|
|
|
|
lineContainsFunction(line, QStringLiteral("macro")) ||
|
|
|
|
|
lineContainsFunction(line, QStringLiteral("foreach")) ||
|
|
|
|
|
lineContainsFunction(line, QStringLiteral("while")) ||
|
|
|
|
|
lineContainsFunction(line, QStringLiteral("if")) ||
|
|
|
|
|
lineContainsFunction(line, QStringLiteral("elseif")) ||
|
|
|
|
|
lineContainsFunction(line, QStringLiteral("else"));
|
|
|
|
|
}
|
|
|
|
|
static bool lineEndsBlock(const QString &line)
|
|
|
|
|
{
|
|
|
|
|
return lineContainsFunction(line, QStringLiteral("endfunction")) ||
|
|
|
|
|
lineContainsFunction(line, QStringLiteral("endmacro")) ||
|
|
|
|
|
lineContainsFunction(line, QStringLiteral("endforeach")) ||
|
|
|
|
|
lineContainsFunction(line, QStringLiteral("endwhile")) ||
|
|
|
|
|
lineContainsFunction(line, QStringLiteral("endif")) ||
|
|
|
|
|
lineContainsFunction(line, QStringLiteral("elseif")) ||
|
|
|
|
|
lineContainsFunction(line, QStringLiteral("else"));
|
|
|
|
|
}
|
|
|
|
|
static bool lineIsEmpty(const QString &line)
|
|
|
|
|
{
|
|
|
|
|
for (const QChar &c : line) {
|
|
|
|
|
if (!c.isSpace())
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int paranthesesLevel(const QString &line)
|
|
|
|
|
{
|
2020-09-18 13:15:18 +02:00
|
|
|
const QString beforeComment = line.mid(0, line.indexOf(QLatin1Char('#')));
|
2015-08-24 08:43:18 +02:00
|
|
|
const int opening = beforeComment.count(QLatin1Char('('));
|
|
|
|
|
const int closing = beforeComment.count(QLatin1Char(')'));
|
2021-12-08 14:59:39 +01:00
|
|
|
|
|
|
|
|
return opening - closing;
|
2015-08-24 08:43:18 +02:00
|
|
|
}
|
|
|
|
|
|
2019-01-16 09:37:54 +01:00
|
|
|
int CMakeIndenter::indentFor(const QTextBlock &block,
|
2019-01-28 08:11:20 +01:00
|
|
|
const TextEditor::TabSettings &tabSettings,
|
|
|
|
|
int /*cursorPositionInEditor*/)
|
2015-08-24 08:43:18 +02:00
|
|
|
{
|
|
|
|
|
QTextBlock previousBlock = block.previous();
|
|
|
|
|
// find the next previous block that is non-empty (contains non-whitespace characters)
|
|
|
|
|
while (previousBlock.isValid() && lineIsEmpty(previousBlock.text()))
|
|
|
|
|
previousBlock = previousBlock.previous();
|
2016-01-13 14:32:23 +01:00
|
|
|
if (!previousBlock.isValid())
|
|
|
|
|
return 0;
|
2015-08-24 08:43:18 +02:00
|
|
|
|
2016-01-13 14:32:23 +01:00
|
|
|
const QString previousLine = previousBlock.text();
|
|
|
|
|
const QString currentLine = block.text();
|
|
|
|
|
int indentation = tabSettings.indentationColumn(previousLine);
|
2015-08-24 08:43:18 +02:00
|
|
|
|
2016-01-13 14:32:23 +01:00
|
|
|
if (lineStartsBlock(previousLine))
|
|
|
|
|
indentation += tabSettings.m_indentSize;
|
|
|
|
|
if (lineEndsBlock(currentLine))
|
2021-12-08 14:59:39 +01:00
|
|
|
indentation -= tabSettings.m_indentSize;
|
|
|
|
|
|
|
|
|
|
// de-dent lines that start with closing parantheses immediately
|
|
|
|
|
indentation -= tabSettings.m_indentSize * startsWithChar(currentLine, ')');
|
2015-08-24 08:43:18 +02:00
|
|
|
|
2021-12-08 14:59:39 +01:00
|
|
|
if (int paranthesesCount = paranthesesLevel(previousLine) - startsWithChar(previousLine, ')'))
|
|
|
|
|
indentation += tabSettings.m_indentSize * (paranthesesCount > 0 ? 1 : -1);
|
|
|
|
|
return qMax(0, indentation);
|
2015-08-24 08:43:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace CMakeProjectManager
|
2016-01-13 14:32:23 +01:00
|
|
|
|
|
|
|
|
|