-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell-highlight.cpp
More file actions
240 lines (215 loc) · 9.12 KB
/
Copy pathshell-highlight.cpp
File metadata and controls
240 lines (215 loc) · 9.12 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// SPDX-FileCopyrightText: 2026 Paolo Anzani
// SPDX-License-Identifier: Apache-2.0
#include "shell-highlight.h"
#include <algorithm>
#include <cctype>
#include <string_view>
namespace {
constexpr uintattr_t command_style = 14 | TB_BOLD;
constexpr uintattr_t keyword_style = 13 | TB_BOLD;
constexpr uintattr_t option_style = 12;
constexpr uintattr_t string_style = 10;
constexpr uintattr_t variable_style = 14;
constexpr uintattr_t operator_style = 13;
constexpr uintattr_t comment_style = 245 | TB_DIM;
bool isShellOperator(const char character) {
return std::string_view("|&;()<> ").find(character) != std::string_view::npos;
}
bool isKeyword(const std::string_view word) {
constexpr std::string_view keywords[] = {
"case", "do", "done", "elif", "else", "esac", "fi", "for",
"function", "if", "in", "select", "then", "time", "until",
"while",
};
return std::ranges::find(keywords, word) != std::end(keywords);
}
bool keywordStartsCommand(const std::string_view word) {
constexpr std::string_view command_keywords[] = {
"do", "elif", "else", "if", "then", "time", "until", "while",
};
return std::ranges::find(command_keywords, word) != std::end(command_keywords);
}
bool isAssignment(const std::string_view word) {
const std::size_t equals = word.find('=');
if (equals == std::string_view::npos || equals == 0) {
return false;
}
const auto valid_first = [](const unsigned char character) {
return std::isalpha(character) != 0 || character == '_';
};
const auto valid_rest = [](const unsigned char character) {
return std::isalnum(character) != 0 || character == '_';
};
if (!valid_first(static_cast<unsigned char>(word.front()))) {
return false;
}
return std::all_of(word.begin() + 1, word.begin() + static_cast<std::ptrdiff_t>(equals),
[&](const char character) {
return valid_rest(static_cast<unsigned char>(character));
});
}
std::size_t quotedEnd(const std::string_view source, const std::size_t start) {
const char quote = source[start];
std::size_t position = start + 1;
while (position < source.size()) {
if (quote == '"' && source[position] == '\\' && position + 1 < source.size()) {
position += 2;
} else if (source[position++] == quote) {
break;
}
}
return position;
}
std::size_t variableEnd(const std::string_view source, const std::size_t start) {
std::size_t position = start + 1;
if (position >= source.size()) {
return position;
}
if (source[position] == '{') {
const std::size_t closing = source.find('}', position + 1);
return closing == std::string_view::npos ? source.size() : closing + 1;
}
if (source[position] == '(') {
// Command substitutions can contain full shell programs. Keeping
// their balanced contents together is clearer than pretending to
// parse nested shell grammar here.
int depth = 1;
position += 1;
while (position < source.size() && depth != 0) {
if (source[position] == '(') {
++depth;
} else if (source[position] == ')') {
--depth;
}
++position;
}
return position;
}
if (std::isalnum(static_cast<unsigned char>(source[position])) != 0 ||
source[position] == '_') {
while (position < source.size() &&
(std::isalnum(static_cast<unsigned char>(source[position])) != 0 ||
source[position] == '_')) {
++position;
}
} else {
++position;
}
return position;
}
std::size_t operatorEnd(const std::string_view source, const std::size_t start) {
constexpr std::string_view pairs[] = {
"&&", "||", ">>", "<<", ";;", "|&", ">&", "<&",
};
if (start + 2 <= source.size()) {
const std::string_view pair = source.substr(start, 2);
if (std::ranges::find(pairs, pair) != std::end(pairs)) {
return start + 2;
}
}
return start + 1;
}
bool operatorStartsCommand(const std::string_view value) {
return value == ";" || value == ";;" || value == "|" || value == "||" ||
value == "|&" || value == "&" || value == "&&" || value == "(";
}
} // namespace
namespace microcodex::ui {
std::vector<StyledSpan> highlightShell(const std::string_view source) {
StyledLine line;
bool expect_command = true;
bool at_word_start = true;
for (std::size_t position = 0; position < source.size();) {
const char character = source[position];
if (std::isspace(static_cast<unsigned char>(character)) != 0) {
if (character == '\n') {
expect_command = true;
}
const std::size_t start = position++;
while (position < source.size() &&
std::isspace(static_cast<unsigned char>(source[position])) != 0) {
if (source[position] == '\n') {
expect_command = true;
}
++position;
}
appendSpan(line, source.substr(start, position - start));
at_word_start = true;
continue;
}
if (character == '#' && at_word_start) {
const std::size_t end = source.find('\n', position);
const std::size_t length = end == std::string_view::npos
? source.size() - position
: end - position;
appendSpan(line, source.substr(position, length), comment_style);
position += length;
continue;
}
if (character == '\'' || character == '"') {
const std::size_t end = quotedEnd(source, position);
appendSpan(line, source.substr(position, end - position), string_style);
if (at_word_start && expect_command) {
expect_command = false;
}
position = end;
at_word_start = false;
continue;
}
if (character == '$') {
const std::size_t end = variableEnd(source, position);
appendSpan(line, source.substr(position, end - position), variable_style);
if (at_word_start && expect_command) {
expect_command = false;
}
position = end;
at_word_start = false;
continue;
}
if (character == '\\' && position + 1 < source.size()) {
appendSpan(line, source.substr(position, 2), string_style);
if (at_word_start && expect_command) {
expect_command = false;
}
position += 2;
at_word_start = false;
continue;
}
if (isShellOperator(character) && character != ' ') {
const std::size_t end = operatorEnd(source, position);
const std::string_view value = source.substr(position, end - position);
appendSpan(line, value, operator_style);
if (operatorStartsCommand(value)) {
expect_command = true;
}
position = end;
at_word_start = true;
continue;
}
const std::size_t start = position++;
while (position < source.size() &&
std::isspace(static_cast<unsigned char>(source[position])) == 0 &&
!isShellOperator(source[position]) && source[position] != '\'' &&
source[position] != '"' && source[position] != '$' &&
source[position] != '\\') {
++position;
}
const std::string_view word = source.substr(start, position - start);
if (isKeyword(word)) {
appendSpan(line, word, keyword_style);
expect_command = keywordStartsCommand(word);
} else if (expect_command && isAssignment(word)) {
appendSpan(line, word, variable_style);
} else if (expect_command) {
appendSpan(line, word, command_style);
expect_command = false;
} else if (word.starts_with('-')) {
appendSpan(line, word, option_style);
} else {
appendSpan(line, word);
}
at_word_start = false;
}
return std::move(line.spans);
}
} // namespace microcodex::ui