2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 Jan Dalheimer <jan@dalheimer.de>
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2015-08-24 08:43:18 +02:00
|
|
|
|
|
|
|
|
#include "cmakeindenter.h"
|
|
|
|
|
|
2022-09-29 15:26:31 +02:00
|
|
|
namespace CMakeProjectManager::Internal {
|
2015-08-24 08:43:18 +02:00
|
|
|
|
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")) ||
|
2023-05-25 12:46:32 +02:00
|
|
|
lineContainsFunction(line, QStringLiteral("else")) ||
|
|
|
|
|
lineContainsFunction(line, QStringLiteral("block"));
|
2015-08-24 08:43:18 +02:00
|
|
|
}
|
|
|
|
|
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")) ||
|
2023-05-25 12:46:32 +02:00
|
|
|
lineContainsFunction(line, QStringLiteral("else")) ||
|
|
|
|
|
lineContainsFunction(line, QStringLiteral("endblock"));
|
2015-08-24 08:43:18 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2022-09-29 15:26:31 +02:00
|
|
|
} // CMakeProjectManager::Internal
|