Debian Codegraph @

LspCpp/src/lsp/utils.cpp

raw
1 #include "LibLsp/lsp/utils.h"
2
3 #include <algorithm>
4 #include <cassert>
5 #include <cctype>
6 #include <cstring>
7 #include <fstream>
8 #include <functional>
9
10 #include <queue>
11 #include <sstream>
12 #include <string>
13 #include <unordered_map>
14 #include <sys/stat.h>
15
16 #include "LibLsp/lsp/lsPosition.h"
17 #include "utf8.h"
18 #ifdef _WIN32
19 #include <Windows.h>
20 #else
21 #include <unistd.h>
22 #endif
23
24 // DEFAULT_RESOURCE_DIRECTORY is passed with quotes for non-MSVC compilers, ie,
25 // foo vs "foo".
26 #if defined(_MSC_VER)
27 #define _STRINGIFY(x) #x
28 #define ENSURE_STRING_MACRO_ARGUMENT(x) _STRINGIFY(x)
29 #else
30 #define ENSURE_STRING_MACRO_ARGUMENT(x) x
31 #endif
32 #include "LibLsp/JsonRpc/filesystemVersion.h"
33 //#include <boost/utility.hpp>
34 //#include <boost/filesystem.hpp>
35 namespace lsp
36 {
37
38 // See http://stackoverflow.com/a/2072890
39 bool EndsWith(std::string value, std::string ending)
40 {
41 if (ending.size() > value.size())
42 {
43 return false;
44 }
45 return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
46 }
47
48 bool StartsWith(std::string value, std::string start)
49 {
50 if (start.size() > value.size())
51 {
52 return false;
53 }
54 return std::equal(start.begin(), start.end(), value.begin());
55 }
56
57 bool AnyStartsWith(std::vector<std::string> const& values, std::string const& start)
58 {
59 return std::any_of(
60 std::begin(values), std::end(values), [&start](std::string const& value) { return StartsWith(value, start); }
61 );
62 }
63
64 bool StartsWithAny(std::string const& value, std::vector<std::string> const& startings)
65 {
66 return std::any_of(
67 std::begin(startings), std::end(startings),
68 [&value](std::string const& starting) { return StartsWith(value, starting); }
69 );
70 }
71
72 bool EndsWithAny(std::string const& value, std::vector<std::string> const& endings)
73 {
74 return std::any_of(
75 std::begin(endings), std::end(endings), [&value](std::string const& ending) { return EndsWith(value, ending); }
76 );
77 }
78
79 bool FindAnyPartial(std::string const& value, std::vector<std::string> const& values)
80 {
81 return std::any_of(
82 std::begin(values), std::end(values),
83 [&value](std::string const& v) { return value.find(v) != std::string::npos; }
84 );
85 }
86
87 std::string GetDirName(std::string path)
88 {
89
90 ReplaceAll(path, "\\", "/");
91 if (path.size() && path.back() == '/')
92 {
93 path.pop_back();
94 }
95 size_t last_slash = path.find_last_of('/');
96 if (last_slash == std::string::npos)
97 {
98 return "./";
99 }
100 return path.substr(0, last_slash + 1);
101 }
102
103 std::string GetBaseName(std::string const& path)
104 {
105 size_t last_slash = path.find_last_of('/');
106 if (last_slash != std::string::npos && (last_slash + 1) < path.size())
107 {
108 return path.substr(last_slash + 1);
109 }
110 return path;
111 }
112
113 std::string StripFileType(std::string const& path)
114 {
115 size_t last_period = path.find_last_of('.');
116 if (last_period != std::string::npos)
117 {
118 return path.substr(0, last_period);
119 }
120 return path;
121 }
122
123 // See http://stackoverflow.com/a/29752943
124 std::string ReplaceAll(std::string const& source, std::string const& from, std::string const& to)
125 {
126 std::string result;
127 result.reserve(source.length()); // avoids a few memory allocations
128
129 std::string::size_type last_pos = 0;
130 std::string::size_type find_pos;
131
132 while (std::string::npos != (find_pos = source.find(from, last_pos)))
133 {
134 result.append(source, last_pos, find_pos - last_pos);
135 result += to;
136 last_pos = find_pos + from.length();
137 }
138
139 // Care for the rest after last occurrence
140 result += source.substr(last_pos);
141
142 return result;
143 }
144
145 std::vector<std::string> SplitString(std::string const& str, std::string const& delimiter)
146 {
147 // http://stackoverflow.com/a/13172514
148 std::vector<std::string> strings;
149
150 std::string::size_type pos = 0;
151 std::string::size_type prev = 0;
152 while ((pos = str.find(delimiter, prev)) != std::string::npos)
153 {
154 strings.emplace_back(str.substr(prev, pos - prev));
155 prev = pos + 1;
156 }
157
158 // To get the last substring (or only, if delimiter is not found)
159 strings.emplace_back(str.substr(prev));
160
161 return strings;
162 }
163
164 void EnsureEndsInSlash(std::string& path)
165 {
166 if (path.empty() || path[path.size() - 1] != '/')
167 {
168 path += '/';
169 }
170 }
171
172 std::string EscapeFileName(std::string path)
173 {
174 if (path.size() && path.back() == '/')
175 {
176 path.pop_back();
177 }
178 std::replace(path.begin(), path.end(), '\\', '@');
179 std::replace(path.begin(), path.end(), '/', '@');
180 std::replace(path.begin(), path.end(), ':', '@');
181 return path;
182 }
183
184 // http://stackoverflow.com/a/6089413
185 std::istream& SafeGetline(std::istream& is, std::string& t)
186 {
187 t.clear();
188
189 // The characters in the stream are read one-by-one using a std::streambuf.
190 // That is faster than reading them one-by-one using the std::istream. Code
191 // that uses streambuf this way must be guarded by a sentry object. The sentry
192 // object performs various tasks, such as thread synchronization and updating
193 // the stream state.
194
195 std::istream::sentry se(is, true);
196 std::streambuf* sb = is.rdbuf();
197
198 for (;;)
199 {
200 int c = sb->sbumpc();
201 if (c == EOF)
202 {
203 // Also handle the case when the last line has no line ending
204 if (t.empty())
205 {
206 is.setstate(std::ios::eofbit);
207 }
208 return is;
209 }
210
211 t += (char)c;
212
213 if (c == '\n')
214 {
215 return is;
216 }
217 }
218 }
219
220 bool FileExists(std::string const& filename)
221 {
222 std::ifstream cache(filename);
223 return cache.is_open();
224 }
225
226 optional<std::string> ReadContent(AbsolutePath const& filename)
227 {
228
229 std::ifstream cache;
230 cache.open(filename.path);
231
232 try
233 {
234 return std::string(std::istreambuf_iterator<char>(cache), std::istreambuf_iterator<char>());
235 }
236 catch (std::ios_base::failure&)
237 {
238 return {};
239 }
240 }
241
242 std::vector<std::string> ReadLinesWithEnding(AbsolutePath const& filename)
243 {
244 std::vector<std::string> result;
245
246 std::ifstream input(filename.path);
247 for (std::string line; SafeGetline(input, line);)
248 {
249 result.emplace_back(line);
250 }
251
252 return result;
253 }
254
255 bool WriteToFile(std::string const& filename, std::string const& content)
256 {
257 std::ofstream file(filename, std::ios::out | std::ios::trunc | std::ios::binary);
258 if (!file.good())
259 {
260
261 return false;
262 }
263
264 file << content;
265 return true;
266 }
267
268 std::string FormatMicroseconds(long long microseconds)
269 {
270 long long milliseconds = microseconds / 1000;
271 long long remaining = microseconds - milliseconds;
272
273 // Only show two digits after the dot.
274 while (remaining >= 100)
275 {
276 remaining /= 10;
277 }
278
279 return std::to_string(milliseconds) + "." + std::to_string(remaining) + "ms";
280 }
281
282 std::string UpdateToRnNewlines(std::string output)
283 {
284 size_t idx = 0;
285 while (true)
286 {
287 idx = output.find('\n', idx);
288
289 // No more matches.
290 if (idx == std::string::npos)
291 {
292 break;
293 }
294
295 // Skip an existing "\r\n" match.
296 if (idx > 0 && output[idx - 1] == '\r')
297 {
298 ++idx;
299 continue;
300 }
301
302 // Replace "\n" with "\r|n".
303 output.replace(output.begin() + idx, output.begin() + idx + 1, "\r\n");
304 }
305
306 return output;
307 }
308
309 bool IsAbsolutePath(std::string const& path)
310 {
311 return IsUnixAbsolutePath(path) || IsWindowsAbsolutePath(path);
312 }
313
314 bool IsUnixAbsolutePath(std::string const& path)
315 {
316 return !path.empty() && path[0] == '/';
317 }
318
319 bool IsWindowsAbsolutePath(std::string const& path)
320 {
321 auto is_drive_letter = [](char c) { return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); };
322
323 return path.size() > 3 && path[1] == ':' && (path[2] == '/' || path[2] == '\\') && is_drive_letter(path[0]);
324 }
325
326 bool IsDirectory(std::string const& path)
327 {
328 struct stat path_stat;
329
330 if (stat(path.c_str(), &path_stat) != 0)
331 {
332 perror("cannot access path");
333 return false;
334 }
335
336 return path_stat.st_mode & S_IFDIR;
337 }
338
339 std::string ws2s(std::wstring const& wstr)
340 {
341 // BOOST_IF_CONSTEXPR (we'll just compile it, compiler probably will optimize it away)
342 if (sizeof(wchar_t) == 2)
343 {
344 std::string narrow;
345 utf8::utf16to8(wstr.begin(), wstr.end(), std::back_inserter(narrow));
346 return narrow;
347 }
348 else
349 {
350 std::string narrow;
351 utf8::utf32to8(wstr.begin(), wstr.end(), std::back_inserter(narrow));
352 return narrow;
353 }
354 }
355 std::wstring s2ws(std::string const& str)
356 {
357 std::wstring wide;
358 // BOOST_IF_CONSTEXPR (we'll just compile it, compiler probably will optimize it away)
359 if (sizeof(wchar_t) == 2)
360 {
361 utf8::utf8to16(str.begin(), str.end(), std::back_inserter(wide));
362 return wide;
363 }
364 else
365 {
366 utf8::utf8to32(str.begin(), str.end(), std::back_inserter(wide));
367 return wide;
368 }
369 }
370
371 #ifndef _WIN32
372 // Returns the canonicalized absolute pathname, without expanding symbolic
373 // links. This is a variant of realpath(2), C++ rewrite of
374 // https://github.com/freebsd/freebsd/blob/master/lib/libc/stdlib/realpath.c
375 AbsolutePath RealPathNotExpandSymlink(std::string path, bool ensure_exists)
376 {
377 if (path.empty())
378 {
379 errno = EINVAL;
380 return {};
381 }
382 if (path[0] == '\0')
383 {
384 errno = ENOENT;
385 return {};
386 }
387
388 // Do not use PATH_MAX because it is tricky on Linux.
389 // See https://eklitzke.org/path-max-is-tricky
390 char tmp[1024];
391 std::string resolved;
392 size_t i = 0;
393 struct stat sb;
394 if (path[0] == '/')
395 {
396 resolved = "/";
397 i = 1;
398 }
399 else
400 {
401 if (!getcwd(tmp, sizeof tmp) && ensure_exists)
402 {
403 return {};
404 }
405 resolved = tmp;
406 }
407
408 while (i < path.size())
409 {
410 auto j = path.find('/', i);
411 if (j == std::string::npos)
412 {
413 j = path.size();
414 }
415 auto next_token = path.substr(i, j - i);
416 i = j + 1;
417 if (resolved.back() != '/')
418 {
419 resolved += '/';
420 }
421 if (next_token.empty() || next_token == ".")
422 {
423 // Handle consequential slashes and "."
424 continue;
425 }
426 else if (next_token == "..")
427 {
428 // Strip the last path component except when it is single "/"
429 if (resolved.size() > 1)
430 {
431 resolved.resize(resolved.rfind('/', resolved.size() - 2) + 1);
432 }
433 continue;
434 }
435 // Append the next path component.
436 // Here we differ from realpath(3), we use stat(2) instead of
437 // lstat(2) because we do not want to resolve symlinks.
438 resolved += next_token;
439 if (stat(resolved.c_str(), &sb) != 0 && ensure_exists)
440 {
441 return {};
442 }
443 if (!S_ISDIR(sb.st_mode) && j < path.size() && ensure_exists)
444 {
445 errno = ENOTDIR;
446 return {};
447 }
448 }
449
450 // Remove trailing slash except when a single "/".
451 if (resolved.size() > 1 && resolved.back() == '/')
452 {
453 resolved.pop_back();
454 }
455 return AbsolutePath(resolved, true /*validate*/);
456 }
457 #endif
458
459 AbsolutePath NormalizePath(std::string const& path0, bool ensure_exists, bool force_lower_on_windows)
460 {
461 #ifdef _WIN32
462
463 std::wstring path = lsp::s2ws(path0);
464
465 wchar_t buffer[MAX_PATH] = (L"");
466
467 // Normalize the path name, ie, resolve `..`.
468 unsigned long len = GetFullPathNameW(path.c_str(), MAX_PATH, buffer, nullptr);
469 if (!len)
470 {
471 return {};
472 }
473 path = std::wstring(buffer, len);
474
475 // Get the actual casing of the path, ie, if the file on disk is `C:\FooBar`
476 // and this function is called with `c:\fooBar` this will return `c:\FooBar`.
477 // (drive casing is lowercase).
478 if (ensure_exists)
479 {
480 len = GetLongPathNameW(path.c_str(), buffer, MAX_PATH);
481 if (!len)
482 {
483 return {};
484 }
485 path = std::wstring(buffer, len);
486 }
487
488 // Empty paths have no meaning.
489 if (path.empty())
490 {
491 return {};
492 }
493
494 // We may need to normalize the drive name to upper-case; at the moment
495 // vscode sends lower-case path names.
496 /*
497 path[0] = toupper(path[0]);
498 */
499 // Make the path all lower-case, since windows is case-insensitive.
500 if (force_lower_on_windows)
501 {
502 for (size_t i = 0; i < path.size(); ++i)
503 {
504 path[i] = (wchar_t)tolower(path[i]);
505 }
506 }
507
508 // cquery assumes forward-slashes.
509 std::replace(path.begin(), path.end(), '\\', '/');
510
511 return AbsolutePath(lsp::ws2s(path), false /*validate*/);
512 #else
513
514 return RealPathNotExpandSymlink(path0, ensure_exists);
515
516 #endif
517 }
518
519 // VSCode (UTF-16) disagrees with Emacs lsp-mode (UTF-8) on how to represent
520 // text documents.
521 // We use a UTF-8 iterator to approximate UTF-16 in the specification (weird).
522 // This is good enough and fails only for UTF-16 surrogate pairs.
523 int GetOffsetForPosition(lsPosition position, std::string const& content)
524 {
525 size_t i = 0;
526 // Iterate lines until we have found the correct line.
527 while (position.line > 0 && i < content.size())
528 {
529 if (content[i] == '\n')
530 {
531 position.line--;
532 }
533 i++;
534 }
535 // Iterate characters on the target line.
536 while (position.character > 0 && i < content.size())
537 {
538 if (uint8_t(content[i++]) >= 128)
539 {
540 // Skip 0b10xxxxxx
541 while (i < content.size() && uint8_t(content[i]) >= 128 && uint8_t(content[i]) < 192)
542 {
543 i++;
544 }
545 }
546 position.character--;
547 }
548 return int(i);
549 }
550
551 lsPosition GetPositionForOffset(size_t offset, std::string const& content)
552 {
553 lsPosition result;
554 for (size_t i = 0; i < offset && i < content.length(); ++i)
555 {
556 if (content[i] == '\n')
557 {
558 result.line++;
559 result.character = 0;
560 }
561 else
562 {
563 result.character++;
564 }
565 }
566 return result;
567 }
568
569 lsPosition CharPos(std::string const& search, char character, int character_offset)
570 {
571 lsPosition result;
572 size_t index = 0;
573 while (index < search.size())
574 {
575 char c = search[index];
576 if (c == character)
577 {
578 break;
579 }
580 if (c == '\n')
581 {
582 result.line += 1;
583 result.character = 0;
584 }
585 else
586 {
587 result.character += 1;
588 }
589 ++index;
590 }
591 assert(index < search.size());
592 result.character += character_offset;
593 return result;
594 }
595
596 void scanDirsUseRecursive(std::wstring const& rootPath, std::vector<std::wstring>& ret)
597 {
598 namespace fs = filesystem;
599 fs::path fullpath(rootPath);
600 if (!fs::exists(fullpath))
601 {
602 return;
603 }
604 fs::recursive_directory_iterator end_iter;
605 for (fs::recursive_directory_iterator iter(fullpath); iter != end_iter; iter++)
606 {
607 try
608 {
609 if (fs::is_directory(*iter))
610 {
611 ret.push_back(iter->path().wstring());
612 }
613 }
614 catch (std::exception const&)
615 {
616 continue;
617 }
618 }
619 }
620
621 void scanDirsNoRecursive(std::wstring const& rootPath, std::vector<std::wstring>& ret)
622 {
623 namespace fs = filesystem;
624 fs::path myPath(rootPath);
625 if (!fs::exists(rootPath))
626 {
627 return;
628 }
629 fs::directory_iterator endIter;
630 for (fs::directory_iterator iter(myPath); iter != endIter; iter++)
631 {
632 if (fs::is_directory(*iter))
633 {
634 ret.push_back(iter->path().wstring());
635 }
636 }
637 }
638
639 void to_lower(std::wstring& input, std::locale const& loc = std::locale())
640 {
641 std::ctype<wchar_t> const& facet = std::use_facet<std::ctype<wchar_t>>(loc);
642 for (std::size_t i = 0; i < input.size(); ++i)
643 {
644 input[i] = facet.tolower(input[i]);
645 }
646 }
647
648 void scanFilesUseRecursive(std::wstring const& rootPath, std::vector<std::wstring>& ret, std::wstring suf)
649 {
650 namespace fs = filesystem;
651 to_lower(suf);
652
653 fs::path fullpath(rootPath);
654 if (!fs::exists(fullpath))
655 {
656 return;
657 }
658 fs::recursive_directory_iterator end_iter;
659 for (fs::recursive_directory_iterator iter(fullpath); iter != end_iter; iter++)
660 {
661 try
662 {
663 if (!fs::is_directory(*iter) && fs::is_regular_file(*iter))
664 {
665 auto temp_path = iter->path().wstring();
666 auto size = suf.size();
667 if (!size)
668 {
669 ret.push_back(std::move(temp_path));
670 }
671 else
672 {
673
674 if (temp_path.size() < size)
675 {
676 continue;
677 }
678 auto suf_temp = temp_path.substr(temp_path.size() - size);
679 to_lower(suf_temp);
680 if (suf_temp == suf)
681 {
682 ret.push_back(std::move(temp_path));
683 }
684 }
685 }
686 }
687 catch (std::exception const&)
688 {
689 continue;
690 }
691 }
692 }
693
694 void scanFileNamesUseRecursive(std::wstring const& rootPath, std::vector<std::wstring>& ret, std::wstring strSuf)
695 {
696 scanFilesUseRecursive(rootPath, ret, strSuf);
697 std::vector<std::wstring> names;
698 for (auto& it : ret)
699 {
700 if (it.size() >= rootPath.size())
701 {
702 names.push_back(it.substr(rootPath.size()));
703 }
704 }
705 ret.swap(names);
706 }
707
708 void scanFileNamesUseRecursive(std::string const& rootPath, std::vector<std::string>& ret, std::string strSuf)
709 {
710 std::vector<std::wstring> out;
711 scanFileNamesUseRecursive(s2ws(rootPath), out, s2ws(strSuf));
712 for (auto& it : out)
713 {
714 ret.push_back(ws2s(it));
715 }
716 }
717
718 void scanFilesUseRecursive(std::string const& rootPath, std::vector<std::string>& ret, std::string strSuf)
719 {
720 std::vector<std::wstring> out;
721 scanFilesUseRecursive(s2ws(rootPath), out, s2ws(strSuf));
722 for (auto& it : out)
723 {
724 ret.push_back(ws2s(it));
725 }
726 }
727
728 } // namespace lsp