blob: 036834f8e5cc1b94d51875f7a59ddd0b9a1e2fbe (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "javaindenter.h"
#include <texteditor/tabsettings.h>
#include <QTextDocument>
using namespace Android;
using namespace Android::Internal;
JavaIndenter::JavaIndenter(QTextDocument *doc)
: TextEditor::TextIndenter(doc)
{}
JavaIndenter::~JavaIndenter() = default;
bool JavaIndenter::isElectricCharacter(const QChar &ch) const
{
if (ch == QLatin1Char('{')
|| ch == QLatin1Char('}')) {
return true;
}
return false;
}
void JavaIndenter::indentBlock(const QTextBlock &block,
const QChar &typedChar,
const TextEditor::TabSettings &tabSettings,
int /*cursorPositionInEditor*/)
{
int indent = indentFor(block, tabSettings);
if (typedChar == QLatin1Char('}'))
indent -= tabSettings.m_indentSize;
tabSettings.indentLine(block, qMax(0, indent));
}
int JavaIndenter::indentFor(const QTextBlock &block,
const TextEditor::TabSettings &tabSettings,
int /*cursorPositionInEditor*/)
{
QTextBlock previous = block.previous();
if (!previous.isValid())
return 0;
QString previousText = previous.text();
while (previousText.trimmed().isEmpty()) {
previous = previous.previous();
if (!previous.isValid())
return 0;
previousText = previous.text();
}
int indent = tabSettings.indentationColumn(previousText);
int adjust = previousText.count(QLatin1Char('{')) - previousText.count(QLatin1Char('}'));
adjust *= tabSettings.m_indentSize;
return qMax(0, indent + adjust);
}
|