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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
// Copyright (C) Filippo Cucchetto <filippocucchetto@gmail.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "nimindenter.h"
#include "../tools/nimlexer.h"
#include <texteditor/icodestylepreferences.h>
#include <texteditor/tabsettings.h>
#include <texteditor/simplecodestylepreferences.h>
#include <texteditor/tabsettings.h>
#include <QSet>
#include <QDebug>
namespace Nim {
class NimIndenter final : public TextEditor::TextIndenter
{
public:
explicit NimIndenter(QTextDocument *doc)
: TextEditor::TextIndenter(doc)
{}
bool isElectricCharacter(const QChar &ch) const final
{
return ch == QLatin1Char(':') || ch == QLatin1Char('=');
}
void indentBlock(const QTextBlock &block,
const QChar &typedChar,
const TextEditor::TabSettings &settings,
int cursorPositionInEditor = -1) final;
private:
bool startsBlock(const QString &line, int state) const;
bool endsBlock(const QString &line, int state) const;
int calculateIndentationDiff(const QString &previousLine,
int previousState,
int indentSize) const;
};
static QString 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();
}
void NimIndenter::indentBlock(const QTextBlock &block,
const QChar &typedChar,
const TextEditor::TabSettings &settings,
int cursorPositionInEditor)
{
Q_UNUSED(typedChar)
Q_UNUSED(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 NimIndenter::startsBlock(const QString &line, int state) const
{
NimLexer lexer(line.constData(), line.size(), static_cast<NimLexer::State>(state));
// Read until end of line and save the last token
NimLexer::Token previous;
NimLexer::Token current = lexer.next();
while (current.type != NimLexer::TokenType::EndOfText) {
switch (current.type) {
case NimLexer::TokenType::Comment:
case NimLexer::TokenType::Documentation:
break;
default:
previous = current;
break;
}
current = lexer.next();
}
// electric characters start a new block, and are operators
if (previous.type == NimLexer::TokenType::Operator) {
QStringView ref = QStringView(line).mid(previous.begin, previous.length);
return ref.isEmpty() ? false : isElectricCharacter(ref.at(0));
}
// some keywords starts a new block
if (previous.type == NimLexer::TokenType::Keyword) {
QStringView ref = QStringView(line).mid(previous.begin, previous.length);
return ref == QLatin1String("type")
|| ref == QLatin1String("var")
|| ref == QLatin1String("let")
|| ref == QLatin1String("enum")
|| ref == QLatin1String("object");
}
return false;
}
bool NimIndenter::endsBlock(const QString &line, int state) const
{
NimLexer lexer(line.constData(), line.size(), static_cast<NimLexer::State>(state));
// Read until end of line and save the last tokens
NimLexer::Token previous;
NimLexer::Token current = lexer.next();
while (current.type != NimLexer::TokenType::EndOfText) {
previous = current;
current = lexer.next();
}
// Some keywords end a block
if (previous.type == NimLexer::TokenType::Keyword) {
QStringView ref = QStringView(line).mid(previous.begin, previous.length);
return ref == QLatin1String("return")
|| ref == QLatin1String("break")
|| ref == QLatin1String("continue");
}
return false;
}
int NimIndenter::calculateIndentationDiff(const QString &previousLine, int previousState, int indentSize) const
{
if (previousLine.isEmpty())
return 0;
if (startsBlock(previousLine, previousState))
return indentSize;
if (endsBlock(previousLine, previousState))
return -indentSize;
return 0;
}
TextEditor::Indenter *createNimIndenter(QTextDocument *doc)
{
return new NimIndenter(doc);
}
} // Nim
|