2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2014-01-15 16:58:52 +01:00
|
|
|
|
|
|
|
|
#include "javaindenter.h"
|
|
|
|
|
#include <texteditor/tabsettings.h>
|
|
|
|
|
|
|
|
|
|
#include <QTextDocument>
|
|
|
|
|
|
|
|
|
|
using namespace Android;
|
|
|
|
|
using namespace Android::Internal;
|
|
|
|
|
|
2019-01-16 09:37:54 +01:00
|
|
|
JavaIndenter::JavaIndenter(QTextDocument *doc)
|
|
|
|
|
: TextEditor::TextIndenter(doc)
|
|
|
|
|
{}
|
2014-01-15 16:58:52 +01:00
|
|
|
|
2018-07-25 12:19:15 +02:00
|
|
|
JavaIndenter::~JavaIndenter() = default;
|
2014-01-15 16:58:52 +01:00
|
|
|
|
|
|
|
|
bool JavaIndenter::isElectricCharacter(const QChar &ch) const
|
|
|
|
|
{
|
|
|
|
|
if (ch == QLatin1Char('{')
|
|
|
|
|
|| ch == QLatin1Char('}')) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-16 09:37:54 +01:00
|
|
|
void JavaIndenter::indentBlock(const QTextBlock &block,
|
2016-01-13 14:32:23 +01:00
|
|
|
const QChar &typedChar,
|
2019-01-28 08:11:20 +01:00
|
|
|
const TextEditor::TabSettings &tabSettings,
|
|
|
|
|
int /*cursorPositionInEditor*/)
|
2014-01-15 16:58:52 +01:00
|
|
|
{
|
2016-01-13 14:32:23 +01:00
|
|
|
int indent = indentFor(block, tabSettings);
|
|
|
|
|
if (typedChar == QLatin1Char('}'))
|
|
|
|
|
indent -= tabSettings.m_indentSize;
|
|
|
|
|
tabSettings.indentLine(block, qMax(0, indent));
|
|
|
|
|
}
|
2014-01-15 16:58:52 +01:00
|
|
|
|
2019-01-28 08:11:20 +01:00
|
|
|
int JavaIndenter::indentFor(const QTextBlock &block,
|
|
|
|
|
const TextEditor::TabSettings &tabSettings,
|
|
|
|
|
int /*cursorPositionInEditor*/)
|
2016-01-13 14:32:23 +01:00
|
|
|
{
|
2014-01-15 16:58:52 +01:00
|
|
|
QTextBlock previous = block.previous();
|
2016-01-13 14:32:23 +01:00
|
|
|
if (!previous.isValid())
|
|
|
|
|
return 0;
|
|
|
|
|
|
2014-01-15 16:58:52 +01:00
|
|
|
QString previousText = previous.text();
|
|
|
|
|
while (previousText.trimmed().isEmpty()) {
|
|
|
|
|
previous = previous.previous();
|
2016-01-13 14:32:23 +01:00
|
|
|
if (!previous.isValid())
|
|
|
|
|
return 0;
|
2014-01-15 16:58:52 +01:00
|
|
|
previousText = previous.text();
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-13 14:32:23 +01:00
|
|
|
int indent = tabSettings.indentationColumn(previousText);
|
2014-01-15 16:58:52 +01:00
|
|
|
|
2016-01-13 14:32:23 +01:00
|
|
|
int adjust = previousText.count(QLatin1Char('{')) - previousText.count(QLatin1Char('}'));
|
|
|
|
|
adjust *= tabSettings.m_indentSize;
|
2014-01-15 16:58:52 +01:00
|
|
|
|
2016-01-13 14:32:23 +01:00
|
|
|
return qMax(0, indent + adjust);
|
2014-01-15 16:58:52 +01:00
|
|
|
}
|