This repository was archived by the owner on Jul 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathlex_utils.cc
417 lines (367 loc) · 14 KB
/
lex_utils.cc
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "lex_utils.h"
#include <doctest/doctest.h>
#include <algorithm>
#include <iostream>
// VSCode (UTF-16) disagrees with Emacs lsp-mode (UTF-8) on how to represent
// text documents.
// We use a UTF-8 iterator to approximate UTF-16 in the specification (weird).
// This is good enough and fails only for UTF-16 surrogate pairs.
int GetOffsetForPosition(lsPosition position, std::string_view content) {
size_t i = 0;
// Iterate lines until we have found the correct line.
while (position.line > 0 && i < content.size()) {
if (content[i] == '\n')
position.line--;
i++;
}
// Iterate characters on the target line.
while (position.character > 0 && i < content.size()) {
if (uint8_t(content[i++]) >= 128) {
// Skip 0b10xxxxxx
while (i < content.size() && uint8_t(content[i]) >= 128 &&
uint8_t(content[i]) < 192)
i++;
}
position.character--;
}
return int(i);
}
lsPosition GetPositionForOffset(int offset, std::string_view content) {
lsPosition result;
for (int i = 0; i < offset && i < content.length(); ++i) {
if (content[i] == '\n') {
result.line++;
result.character = 0;
} else {
result.character++;
}
}
return result;
}
lsPosition CharPos(std::string_view search,
char character,
int character_offset) {
lsPosition result;
size_t index = 0;
while (index < search.size()) {
char c = search[index];
if (c == character)
break;
if (c == '\n') {
result.line += 1;
result.character = 0;
} else {
result.character += 1;
}
++index;
}
assert(index < search.size());
result.character += character_offset;
return result;
}
// TODO: eliminate |line_number| param.
optional<lsRange> ExtractQuotedRange(int line_number, const std::string& line) {
// Find starting and ending quote.
int start = 0;
while (start < (int)line.size()) {
char c = line[start];
++start;
if (c == '"' || c == '<')
break;
}
if (start == (int)line.size())
return nullopt;
int end = (int)line.size();
while (end > 0) {
char c = line[end];
if (c == '"' || c == '>')
break;
--end;
}
if (start >= end)
return nullopt;
return lsRange(lsPosition(line_number, start), lsPosition(line_number, end));
}
void LexFunctionDeclaration(const std::string& buffer_content,
lsPosition declaration_spelling,
optional<std::string> type_name,
std::string* insert_text,
int* newlines_after_name) {
int name_start = GetOffsetForPosition(declaration_spelling, buffer_content);
bool parse_return_type = true;
// We need to check if we have a return type (ctors and dtors do not).
if (type_name) {
int name_end = name_start;
while (name_end < buffer_content.size()) {
char c = buffer_content[name_end];
if (isspace(c) || c == '(')
break;
++name_end;
}
std::string func_name =
buffer_content.substr(name_start, name_end - name_start);
if (func_name == *type_name || func_name == ("~" + *type_name))
parse_return_type = false;
}
// We need to fetch the return type. This can get complex, ie,
//
// std::vector <int> foo();
//
int return_start = name_start;
if (parse_return_type) {
int paren_balance = 0;
int angle_balance = 0;
bool expect_token = true;
while (return_start > 0) {
char c = buffer_content[return_start - 1];
if (paren_balance == 0 && angle_balance == 0) {
if (isspace(c) && !expect_token) {
break;
}
if (!isspace(c))
expect_token = false;
}
if (c == ')')
++paren_balance;
if (c == '(') {
--paren_balance;
expect_token = true;
}
if (c == '>')
++angle_balance;
if (c == '<') {
--angle_balance;
expect_token = true;
}
return_start -= 1;
}
}
// We need to fetch the arguments. Just scan for the next ';'.
*newlines_after_name = 0;
int end = name_start;
while (end < buffer_content.size()) {
char c = buffer_content[end];
if (c == ';')
break;
if (c == '\n')
*newlines_after_name += 1;
++end;
}
std::string result;
result += buffer_content.substr(return_start, name_start - return_start);
if (type_name && !type_name->empty())
result += *type_name + "::";
result += buffer_content.substr(name_start, end - name_start);
TrimEndInPlace(result);
result += " {\n}";
*insert_text = result;
}
std::string_view LexIdentifierAroundPos(lsPosition position,
std::string_view content) {
int start = GetOffsetForPosition(position, content);
int end = start + 1;
char c;
// We search for :: before the cursor but not after to get the qualifier.
for (; start > 0; start--) {
c = content[start - 1];
if (isalnum(c) || c == '_')
;
else if (c == ':' && start > 1 && content[start - 2] == ':')
start--;
else
break;
}
for (; end < (int)content.size(); end++)
if (c = content[end], !(isalnum(c) || c == '_'))
break;
return content.substr(start, end - start);
}
// Find discontinous |search| in |content|.
// Return |found| and the count of skipped chars before found.
std::pair<bool, int> CaseFoldingSubsequenceMatch(std::string_view search,
std::string_view content) {
bool hasUppercaseLetter = std::any_of(search.begin(), search.end(), isupper);
int skip = 0;
size_t j = 0;
for (char c : search) {
while (j < content.size() &&
(hasUppercaseLetter ? content[j] != c
: tolower(content[j]) != tolower(c)))
++j, ++skip;
if (j == content.size())
return {false, skip};
++j;
}
return {true, skip};
}
TEST_SUITE("Offset") {
TEST_CASE("past end") {
std::string content = "foo";
int offset = GetOffsetForPosition(lsPosition(10, 10), content);
REQUIRE(offset <= content.size());
}
TEST_CASE("in middle of content") {
std::string content = "abcdefghijk";
for (int i = 0; i < content.size(); ++i) {
int offset = GetOffsetForPosition(lsPosition(0, i), content);
REQUIRE(i == offset);
}
}
TEST_CASE("at end of content") {
REQUIRE(GetOffsetForPosition(lsPosition(0, 0), "") == 0);
REQUIRE(GetOffsetForPosition(lsPosition(0, 1), "a") == 1);
}
}
TEST_SUITE("offset") {
TEST_CASE("some") {
REQUIRE(GetPositionForOffset(0, "012345\n012345") == lsPosition(0, 0));
REQUIRE(GetPositionForOffset(1, "012345\n012345") == lsPosition(0, 1));
REQUIRE(GetPositionForOffset(2, "012345\n012345") == lsPosition(0, 2));
REQUIRE(GetPositionForOffset(3, "012345\n012345") == lsPosition(0, 3));
REQUIRE(GetPositionForOffset(4, "012345\n012345") == lsPosition(0, 4));
REQUIRE(GetPositionForOffset(5, "012345\n012345") == lsPosition(0, 5));
REQUIRE(GetPositionForOffset(6, "012345\n012345") == lsPosition(0, 6));
REQUIRE(GetPositionForOffset(7, "012345\n012345") == lsPosition(1, 0));
REQUIRE(GetPositionForOffset(8, "012345\n012345") == lsPosition(1, 1));
REQUIRE(GetPositionForOffset(9, "012345\n012345") == lsPosition(1, 2));
REQUIRE(GetPositionForOffset(10, "012345\n012345") == lsPosition(1, 3));
REQUIRE(GetPositionForOffset(11, "012345\n012345") == lsPosition(1, 4));
REQUIRE(GetPositionForOffset(12, "012345\n012345") == lsPosition(1, 5));
// Overflow
REQUIRE(GetPositionForOffset(13, "012345\n012345") == lsPosition(1, 6));
REQUIRE(GetPositionForOffset(100, "012345\n012345") == lsPosition(1, 6));
}
TEST_CASE("overflow") {
REQUIRE(GetOffsetForPosition(lsPosition(0, 0), "a") == 0);
REQUIRE(GetOffsetForPosition(lsPosition(0, 1), "a") == 1);
REQUIRE(GetPositionForOffset(0, "0") == lsPosition(0, 0));
REQUIRE(GetPositionForOffset(1, "0") == lsPosition(0, 1));
REQUIRE(GetPositionForOffset(5, "0") == lsPosition(0, 1));
}
}
TEST_SUITE("Substring") {
TEST_CASE("skip") {
REQUIRE(CaseFoldingSubsequenceMatch("a", "a") == std::make_pair(true, 0));
REQUIRE(CaseFoldingSubsequenceMatch("b", "a") == std::make_pair(false, 1));
REQUIRE(CaseFoldingSubsequenceMatch("", "") == std::make_pair(true, 0));
REQUIRE(CaseFoldingSubsequenceMatch("a", "ba") == std::make_pair(true, 1));
REQUIRE(CaseFoldingSubsequenceMatch("aa", "aba") ==
std::make_pair(true, 1));
REQUIRE(CaseFoldingSubsequenceMatch("aa", "baa") ==
std::make_pair(true, 1));
REQUIRE(CaseFoldingSubsequenceMatch("aA", "aA") == std::make_pair(true, 0));
REQUIRE(CaseFoldingSubsequenceMatch("aA", "aa") ==
std::make_pair(false, 1));
REQUIRE(CaseFoldingSubsequenceMatch("incstdioh", "include <stdio.h>") ==
std::make_pair(true, 7));
}
}
TEST_SUITE("LexFunctionDeclaration") {
TEST_CASE("simple") {
std::string buffer_content = " void Foo(); ";
lsPosition declaration = CharPos(buffer_content, 'F');
std::string insert_text;
int newlines_after_name = 0;
LexFunctionDeclaration(buffer_content, declaration, nullopt, &insert_text,
&newlines_after_name);
REQUIRE(insert_text == "void Foo() {\n}");
REQUIRE(newlines_after_name == 0);
LexFunctionDeclaration(buffer_content, declaration, std::string("Type"),
&insert_text, &newlines_after_name);
REQUIRE(insert_text == "void Type::Foo() {\n}");
REQUIRE(newlines_after_name == 0);
}
TEST_CASE("ctor") {
std::string buffer_content = " Foo(); ";
lsPosition declaration = CharPos(buffer_content, 'F');
std::string insert_text;
int newlines_after_name = 0;
LexFunctionDeclaration(buffer_content, declaration, std::string("Foo"),
&insert_text, &newlines_after_name);
REQUIRE(insert_text == "Foo::Foo() {\n}");
REQUIRE(newlines_after_name == 0);
}
TEST_CASE("dtor") {
std::string buffer_content = " ~Foo(); ";
lsPosition declaration = CharPos(buffer_content, '~');
std::string insert_text;
int newlines_after_name = 0;
LexFunctionDeclaration(buffer_content, declaration, std::string("Foo"),
&insert_text, &newlines_after_name);
REQUIRE(insert_text == "Foo::~Foo() {\n}");
REQUIRE(newlines_after_name == 0);
}
TEST_CASE("complex return type") {
std::string buffer_content = " std::vector<int> Foo(); ";
lsPosition declaration = CharPos(buffer_content, 'F');
std::string insert_text;
int newlines_after_name = 0;
LexFunctionDeclaration(buffer_content, declaration, nullopt, &insert_text,
&newlines_after_name);
REQUIRE(insert_text == "std::vector<int> Foo() {\n}");
REQUIRE(newlines_after_name == 0);
LexFunctionDeclaration(buffer_content, declaration, std::string("Type"),
&insert_text, &newlines_after_name);
REQUIRE(insert_text == "std::vector<int> Type::Foo() {\n}");
REQUIRE(newlines_after_name == 0);
}
TEST_CASE("extra complex return type") {
std::string buffer_content = " std::function < int() > \n Foo(); ";
lsPosition declaration = CharPos(buffer_content, 'F');
std::string insert_text;
int newlines_after_name = 0;
LexFunctionDeclaration(buffer_content, declaration, nullopt, &insert_text,
&newlines_after_name);
REQUIRE(insert_text == "std::function < int() > \n Foo() {\n}");
REQUIRE(newlines_after_name == 0);
LexFunctionDeclaration(buffer_content, declaration, std::string("Type"),
&insert_text, &newlines_after_name);
REQUIRE(insert_text == "std::function < int() > \n Type::Foo() {\n}");
REQUIRE(newlines_after_name == 0);
}
TEST_CASE("parameters") {
std::string buffer_content = "void Foo(int a,\n\n int b); ";
lsPosition declaration = CharPos(buffer_content, 'F');
std::string insert_text;
int newlines_after_name = 0;
LexFunctionDeclaration(buffer_content, declaration, nullopt, &insert_text,
&newlines_after_name);
REQUIRE(insert_text == "void Foo(int a,\n\n int b) {\n}");
REQUIRE(newlines_after_name == 2);
LexFunctionDeclaration(buffer_content, declaration, std::string("Type"),
&insert_text, &newlines_after_name);
REQUIRE(insert_text == "void Type::Foo(int a,\n\n int b) {\n}");
REQUIRE(newlines_after_name == 2);
}
}
TEST_SUITE("LexWordAroundPos") {
TEST_CASE("edges") {
std::string content = "Foobar";
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'F'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'o'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'b'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'a'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'r'), content) == "Foobar");
}
TEST_CASE("simple") {
std::string content = " Foobar ";
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'F'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'o'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'b'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'a'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'r'), content) == "Foobar");
}
TEST_CASE("underscores, numbers and ::") {
std::string content = " file:ns::_my_t5ype7 ";
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'f'), content) == "file");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 's'), content) == "ns");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'y'), content) ==
"ns::_my_t5ype7");
}
TEST_CASE("dot, dash, colon are skipped") {
std::string content = "1. 2- 3:";
REQUIRE(LexIdentifierAroundPos(CharPos(content, '1'), content) == "1");
REQUIRE(LexIdentifierAroundPos(CharPos(content, '2'), content) == "2");
REQUIRE(LexIdentifierAroundPos(CharPos(content, '3'), content) == "3");
}
}