This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvcreatorindenter.cpp
79 lines (63 loc) · 2.19 KB
/
vcreatorindenter.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "vcreatorindenter.h"
#include "vcreatorlexer.h"
#include <texteditor/icodestylepreferences.h>
#include <texteditor/tabsettings.h>
#include <texteditor/simplecodestylepreferences.h>
#include <texteditor/tabsettings.h>
namespace VCreator {
namespace Internal {
VlangIndenter::VlangIndenter(QTextDocument *doc) : TextEditor::TextIndenter(doc)
{
}
bool VlangIndenter::isElectricCharacter(const QChar &ch) const
{
return ch == '{';
}
void VlangIndenter::indentBlock(const QTextBlock &block, const QChar &typedChar, const TextEditor::TabSettings &settings, int cursorPositionInEditor)
{
const QString currentLine = block.text();
const QTextBlock previousBlock = block.previous();
const QString previousLine = previousBlock.text();
const int previousState = previousBlock.userState();
if (!previousBlock.isValid()) {
settings.indentLine(block, 0);
return;
}
// Calculate indentation
int indentation = 0;
if (rightTrimmed(currentLine).isEmpty()) {
// Current line is empty so we calculate indentation based on previous line
const int indentationDiff = calculateIndentationDiff(previousLine, previousState, settings.m_indentSize);
indentation = settings.indentationColumn(previousLine) + indentationDiff;
}
else {
// We don't change indentation if the line is already indented.
// This is safer but sub optimal
indentation = settings.indentationColumn(block.text());
}
// Sets indentation
settings.indentLine(block, std::max(0, indentation));
}
bool VlangIndenter::startsBlock(const QString &line, int state) const
{
return false;
}
bool VlangIndenter::endsBlock(const QString &line, int state) const
{
return false;
}
int VlangIndenter::calculateIndentationDiff(const QString &previousLine, int previousState, int indentSize) const
{
return 0;
}
QString VlangIndenter::rightTrimmed(const QString &str)
{
int n = str.size() - 1;
for (; n >= 0; --n) {
if (!str.at(n).isSpace())
return str.left(n + 1);
}
return QString();
}
} // namespace Internal
} // namespace Vcreator