Debian Codegraph @

LspCpp/src/lsp/lsp.cpp

raw
1
2
3 #include "LibLsp/lsp/lru_cache.h"
4
5 #include <rapidjson/writer.h>
6
7 #include <stdio.h>
8 #include <iostream>
9 #include "LibLsp/lsp/location_type.h"
10 #include "LibLsp/lsp/out_list.h"
11 #include "LibLsp/lsp/lsTextDocumentIdentifier.h"
12 #include "LibLsp/lsp/lsVersionedTextDocumentIdentifier.h"
13 #include "LibLsp/lsp/lsResponseError.h"
14 #include "LibLsp/lsp/lsPosition.h"
15 #include "LibLsp/lsp/lsTextEdit.h"
16 #include "LibLsp/lsp/lsMarkedString.h"
17 #include "LibLsp/lsp/lsWorkspaceEdit.h"
18 #include "LibLsp/lsp/textDocument/code_action.h"
19 #include "LibLsp/lsp/textDocument/document_symbol.h"
20 #include "LibLsp/lsp/extention/jdtls/codeActionResult.h"
21
22 #include "LibLsp/lsp/textDocument/selectionRange.h"
23 #include "LibLsp/lsp/AbsolutePath.h"
24
25 #ifdef _WIN32
26 #ifndef NOMINMAX
27 #define NOMINMAX
28 #endif
29 #include <Windows.h>
30 #else
31 #include <climits>
32 #include <cstdlib>
33 #endif
34
35 #include "LibLsp/lsp/Directory.h"
36 #include "LibLsp/lsp/lsFormattingOptions.h"
37 #include "LibLsp/JsonRpc/json.h"
38 #include "LibLsp/lsp/language/language.h"
39
40 #include <network/uri/uri_builder.hpp>
41
42 #include "LibLsp/lsp/lsp_completion.h"
43 #include "LibLsp/lsp/utils.h"
44 #include "LibLsp/lsp/client/registerCapability.h"
45
46 #include <array>
47 #include <cstdint>
48 #include <iomanip>
49 #include <iostream>
50 #include <random>
51 #include <sstream>
52 // namespace
53
54 lsTextDocumentIdentifier lsVersionedTextDocumentIdentifier::AsTextDocumentIdentifier() const
55 {
56 lsTextDocumentIdentifier result;
57 result.uri = uri;
58 return result;
59 }
60
61 lsPosition::lsPosition()
62 {
63 }
64 lsPosition::lsPosition(int line, int character) : line(line), character(character)
65 {
66 }
67
68 bool lsPosition::operator==(lsPosition const& other) const
69 {
70 return line == other.line && character == other.character;
71 }
72
73 bool lsPosition::operator<(lsPosition const& other) const
74 {
75 return line != other.line ? line < other.line : character < other.character;
76 }
77
78 std::string lsPosition::ToString() const
79 {
80 return std::to_string(line) + ":" + std::to_string(character);
81 }
82 lsPosition const lsPosition::kZeroPosition = lsPosition();
83
84 lsRange::lsRange()
85 {
86 }
87 lsRange::lsRange(lsPosition start, lsPosition end) : start(start), end(end)
88 {
89 }
90
91 bool lsRange::operator==(lsRange const& o) const
92 {
93 return start == o.start && end == o.end;
94 }
95
96 bool lsRange::operator<(lsRange const& o) const
97 {
98 return !(start == o.start) ? start < o.start : end < o.end;
99 }
100
101 std::string lsRange::ToString() const
102 {
103 std::stringstream ss;
104 ss << "start:" << start.ToString() << std::endl;
105 ss << "end" << end.ToString() << std::endl;
106 return ss.str();
107 }
108
109 lsLocation::lsLocation()
110 {
111 }
112 lsLocation::lsLocation(lsDocumentUri uri, lsRange range) : uri(uri), range(range)
113 {
114 }
115
116 bool lsLocation::operator==(lsLocation const& o) const
117 {
118 return uri == o.uri && range == o.range;
119 }
120
121 bool lsLocation::operator<(lsLocation const& o) const
122 {
123 return std::make_tuple(uri.raw_uri_, range) < std::make_tuple(o.uri.raw_uri_, o.range);
124 }
125
126 bool lsTextEdit::operator==(lsTextEdit const& that)
127 {
128 return range == that.range && newText == that.newText;
129 }
130
131 std::string lsTextEdit::ToString() const
132 {
133 std::stringstream ss;
134 ss << "Range:" << range.ToString() << std::endl;
135 ss << "newText:" << newText << std::endl;
136 return ss.str();
137 }
138
139 void Reflect(Writer& visitor, lsMarkedString& value)
140 {
141 // If there is a language, emit a `{language:string, value:string}` object. If
142 // not, emit a string.
143 if (value.language)
144 {
145 REFLECT_MEMBER_START();
146 REFLECT_MEMBER(language);
147 REFLECT_MEMBER(value);
148 REFLECT_MEMBER_END();
149 }
150 else
151 {
152 Reflect(visitor, value.value);
153 }
154 }
155
156 void Reflect(Reader& visitor, lsMarkedString& value)
157 {
158 REFLECT_MEMBER_START();
159 REFLECT_MEMBER(language);
160 REFLECT_MEMBER(value);
161 REFLECT_MEMBER_END();
162 }
163
164 void Reflect(Reader& visitor, LocationListEither::Either& value)
165 {
166 if (!visitor.IsArray())
167 {
168 throw std::invalid_argument("Rsp_LocationListEither::Either& value is not array");
169 }
170 auto data = ((JsonReader&)visitor).m_->GetArray();
171 if (data.Size() && data[0].HasMember("originSelectionRange"))
172 {
173 Reflect(visitor, value.second);
174 }
175 else
176 {
177 Reflect(visitor, value.first);
178 }
179 }
180
181 void Reflect(Writer& visitor, LocationListEither::Either& value)
182 {
183 if (value.first)
184 {
185 Reflect(visitor, value.first.value());
186 }
187 else if (value.second)
188 {
189 Reflect(visitor, value.second.value());
190 }
191 }
192
193 void Reflect(Reader& visitor, TextDocumentCodeAction::Either& value)
194 {
195
196 if (visitor.HasMember("command"))
197 {
198 if (visitor["command"]->IsString())
199 {
200 Reflect(visitor, value.first);
201 }
202 else
203 {
204 Reflect(visitor, value.second);
205 }
206 }
207 else
208 {
209 if (visitor.HasMember("diagnostics") || visitor.HasMember("edit"))
210 {
211 Reflect(visitor, value.second);
212 }
213 else
214 {
215 Reflect(visitor, value.first);
216 }
217 }
218 }
219
220 void Reflect(Reader& visitor, lsWorkspaceEdit::Either& value)
221 {
222
223 if (visitor.HasMember("textDocument"))
224 {
225 Reflect(visitor, value.first);
226 }
227 else
228 {
229 Reflect(visitor, value.second);
230 }
231 }
232 ResourceOperation* GetResourceOperation(lsp::Any& lspAny)
233 {
234 rapidjson::Document document;
235 auto& data = lspAny.Data();
236 document.Parse(data.c_str(), data.length());
237 if (document.HasParseError())
238 {
239 // ��ʾ
240 return nullptr;
241 }
242 auto find = document.FindMember("kind");
243
244 JsonReader visitor {&document};
245 try
246 {
247 if (find->value == "create")
248 {
249 auto ptr = std::make_unique<lsCreateFile>();
250 auto temp = ptr.get();
251 Reflect(visitor, *temp);
252 return ptr.release();
253 }
254 else if (find->value == "rename")
255 {
256 auto ptr = std::make_unique<lsRenameFile>();
257 auto temp = ptr.get();
258 Reflect(visitor, *temp);
259 return ptr.release();
260 }
261 else if (find->value == "delete")
262 {
263
264 auto ptr = std::make_unique<lsDeleteFile>();
265 auto temp = ptr.get();
266 Reflect(visitor, *temp);
267 return ptr.release();
268 }
269 }
270 catch (std::exception&)
271 {
272 }
273 return nullptr;
274 }
275
276 void Reflect(Writer& visitor, ResourceOperation* value)
277 {
278
279 if (!value)
280 {
281 throw std::invalid_argument("ResourceOperation value is nullptr");
282 }
283 if (value->kind == "create")
284 {
285 auto temp = (lsCreateFile*)value;
286 Reflect(visitor, *temp);
287 }
288 else if (value->kind == "rename")
289 {
290 auto temp = (lsRenameFile*)value;
291 Reflect(visitor, *temp);
292 }
293 else if (value->kind == "delete")
294 {
295
296 auto temp = (lsDeleteFile*)value;
297 Reflect(visitor, *temp);
298 }
299 }
300
301 int lsp::Any::GuessType()
302 {
303 if (!data.empty())
304 {
305 if (data == "null")
306 {
307 jsonType = rapidjson::kNullType;
308 }
309 else if (data == "true")
310 {
311 jsonType = rapidjson::kTrueType;
312 }
313 else if (data == "false")
314 {
315 jsonType = rapidjson::kFalseType;
316 }
317 else if (data[0] == '{')
318 {
319 jsonType = rapidjson::kObjectType;
320 }
321 else if (data[0] == '[')
322 {
323 if (data.size() >= 2 && data[1] == '{')
324 {
325 jsonType = rapidjson::kStringType;
326 }
327 else
328 {
329 jsonType = rapidjson::kArrayType;
330 }
331 }
332 else if (data[0] == '"')
333 {
334 jsonType = rapidjson::kStringType;
335 }
336 else
337 {
338 jsonType = rapidjson::kNumberType;
339 }
340 }
341 else
342 {
343 if (jsonType != kUnKnown)
344 {
345 return jsonType;
346 }
347 jsonType = rapidjson::kNullType;
348 }
349 return jsonType;
350 }
351
352 int lsp::Any::GetType()
353 {
354 if (jsonType == Type::kUnKnown)
355 {
356 if (data.empty())
357 {
358 jsonType = rapidjson::kNullType;
359 return jsonType;
360 }
361 rapidjson::Document document;
362 document.Parse(data.c_str(), data.length());
363 if (document.HasParseError())
364 {
365 // ��ʾ
366 return jsonType;
367 }
368 jsonType = document.GetType();
369 }
370 return jsonType;
371 }
372
373 void lsp::Any::Set(std::unique_ptr<LspMessage> value)
374 {
375 if (value)
376 {
377 jsonType = rapidjson::Type::kObjectType;
378 data = value->ToJson();
379 }
380 else
381 {
382 assert(false);
383 }
384 }
385
386 void lsp::Any::SetJsonString(std::string&& _data, Type _type)
387 {
388 jsonType = _type;
389 data.swap(_data);
390 GetType();
391 }
392
393 void lsp::Any::SetJsonString(std::string const& _data, Type _type)
394 {
395 jsonType = _type;
396 data = (_data);
397 GetType();
398 }
399
400 void lsp::Any::swap(Any& arg) noexcept
401 {
402 data.swap(arg.data);
403 int const temp = jsonType;
404 jsonType = arg.jsonType;
405 arg.jsonType = temp;
406 }
407
408 class JsonReaderForAny : public JsonReader
409 {
410 public:
411 JsonReaderForAny() : JsonReader(&document)
412 {
413 }
414 rapidjson::Document document;
415 };
416
417 bool lsp::Any::GetForMapHelper(std::string& value)
418 {
419 return Get(value);
420 }
421
422 bool lsp::Any::GetForMapHelper(optional<std::string>& value)
423 {
424 return Get(value);
425 }
426
427 std::unique_ptr<Reader> lsp::Any::GetReader()
428 {
429 auto reader = new JsonReaderForAny();
430 std::unique_ptr<Reader> ret(reader);
431 reader->document.Parse(data.c_str(), data.length());
432 if (reader->document.HasParseError())
433 {
434 return {};
435 }
436 if (jsonType == kUnKnown)
437 {
438 jsonType = reader->document.GetType();
439 }
440 return (ret);
441 }
442
443 class JsonWriterForAny : public JsonWriter
444 {
445 public:
446 rapidjson::StringBuffer output;
447 rapidjson::Writer<rapidjson::StringBuffer> writer;
448 JsonWriterForAny() : JsonWriter(&writer), writer(output)
449 {
450 }
451 };
452
453 std::unique_ptr<Writer> lsp::Any::GetWriter() const
454 {
455 return std::make_unique<JsonWriterForAny>();
456 }
457
458 void lsp::Any::SetData(std::unique_ptr<Writer>& writer)
459 {
460 auto _temp = static_cast<JsonWriterForAny*>(writer.get());
461 data = _temp->output.GetString();
462 GuessType();
463 }
464
465 namespace
466 {
467 #if 0
468 rapidjson::Type convert(lsp::Any::Type type)
469 {
470 switch (type)
471 {
472 case lsp::Any::Type::kNullType:
473 return rapidjson::Type::kNullType;
474 case lsp::Any::Type::kFalseType:
475 return rapidjson::Type::kFalseType;
476 case lsp::Any::Type::kTrueType:
477 return rapidjson::Type::kTrueType;
478 case lsp::Any::Type::kObjectType:
479 return rapidjson::Type::kObjectType;
480 case lsp::Any::Type::kArrayType:
481 return rapidjson::Type::kArrayType;
482 case lsp::Any::Type::kStringType:
483 return rapidjson::Type::kStringType;
484 case lsp::Any::Type::kNumberType:
485 return rapidjson::Type::kNumberType;
486 default:
487 return rapidjson::Type::kNullType;
488 }
489 }
490 #endif
491 lsp::Any::Type convert(rapidjson::Type type)
492 {
493 switch (type)
494 {
495 case rapidjson::Type::kNullType:
496 return lsp::Any::Type::kNullType;
497 case rapidjson::Type::kFalseType:
498 return lsp::Any::Type::kFalseType;
499 case rapidjson::Type::kTrueType:
500 return lsp::Any::Type::kTrueType;
501 case rapidjson::Type::kObjectType:
502 return lsp::Any::Type::kObjectType;
503 case rapidjson::Type::kArrayType:
504 return lsp::Any::Type::kArrayType;
505 case rapidjson::Type::kStringType:
506 return lsp::Any::Type::kStringType;
507 case rapidjson::Type::kNumberType:
508 return lsp::Any::Type::kNumberType;
509 default:
510 return lsp::Any::Type::kNullType;
511 }
512 }
513 } // namespace
514
515 void Reflect(Reader& visitor, lsp::Any& value)
516 {
517
518 //if (visitor.IsNull()) {
519 // visitor.GetNull();
520 // value.SetJsonString("", rapidjson::Type::kNullType);
521 // return;
522 //}else
523 //{
524 //
525 //}
526 JsonReader& json_reader = reinterpret_cast<JsonReader&>(visitor);
527 value.SetJsonString(visitor.ToString(), convert(json_reader.m_->GetType()));
528 }
529 void Reflect(Writer& visitor, lsp::Any& value)
530 {
531 JsonWriter& json_writer = reinterpret_cast<JsonWriter&>(visitor);
532 json_writer.m_->RawValue(value.Data().data(), value.Data().size(), static_cast<rapidjson::Type>(value.GetType()));
533 }
534 void Reflect(Reader& visitor, lsFormattingOptions::KeyData& value)
535 {
536 if (visitor.IsBool())
537 {
538 Reflect(visitor, value._boolean);
539 }
540 else if (visitor.IsInt() || visitor.IsInt64() || visitor.IsUint64())
541 {
542 Reflect(visitor, value._integer);
543 }
544 else if (visitor.IsString())
545 {
546 Reflect(visitor, value._string);
547 }
548 }
549 void Reflect(Writer& visitor, lsFormattingOptions::KeyData& value)
550 {
551 if (value._boolean.has_value())
552 {
553 Reflect(visitor, value._boolean);
554 }
555 else if (value._integer.has_value())
556 {
557 Reflect(visitor, value._integer);
558 }
559 else if (value._string.has_value())
560 {
561 Reflect(visitor, value._string);
562 }
563 }
564
565 lsCreateFile::lsCreateFile()
566 {
567 kind = "create";
568 }
569
570 lsDeleteFile::lsDeleteFile()
571 {
572 kind = "delete";
573 }
574
575 lsRenameFile::lsRenameFile()
576 {
577 kind = "rename";
578 }
579
580 void Reflect(Reader& visitor, optional<SelectionRange*>& value)
581 {
582 if (visitor.IsNull())
583 {
584 visitor.GetNull();
585 return;
586 }
587
588 SelectionRange* entry_value = nullptr;
589
590 std::unique_ptr<SelectionRange> ptr = std::make_unique<SelectionRange>();
591 SelectionRange* temp = ptr.get();
592 Reflect(visitor, *temp);
593
594 entry_value = ptr.release();
595 value = (entry_value);
596 }
597 void Reflect(Writer& visitor, SelectionRange* value)
598 {
599
600 if (!value)
601 {
602 throw std::invalid_argument("ResourceOperation value is nullptr");
603 }
604
605 Reflect(visitor, *value);
606 }
607
608 std::string make_file_scheme_uri(std::string const& absolute_path)
609 {
610 network::uri_builder builder;
611 builder.scheme("file");
612 builder.host("");
613 builder.path(absolute_path);
614 return builder.uri().string();
615 //// lsDocumentUri uri;
616 //// uri.SetPath(absolute_path);
617 /// return uri.raw_uri_;
618 }
619
620 // static
621 AbsolutePath AbsolutePath::BuildDoNotUse(std::string const& path)
622 {
623 AbsolutePath p;
624 p.path = std::string(path);
625 return p;
626 }
627
628 AbsolutePath::AbsolutePath()
629 {
630 }
631
632 AbsolutePath::operator std::string() const
633 {
634 return path;
635 }
636
637 bool AbsolutePath::operator==(AbsolutePath const& rhs) const
638 {
639 return path == rhs.path;
640 }
641
642 bool AbsolutePath::operator!=(AbsolutePath const& rhs) const
643 {
644 return path != rhs.path;
645 }
646
647 bool AbsolutePath::operator<(AbsolutePath const& rhs) const
648 {
649 return path < rhs.path;
650 }
651
652 bool AbsolutePath::operator>(AbsolutePath const& rhs) const
653 {
654 return path > rhs.path;
655 }
656
657 void Reflect(Reader& visitor, AbsolutePath& value)
658 {
659 value.path = visitor.GetString();
660 }
661 void Reflect(Writer& visitor, AbsolutePath& value)
662 {
663 visitor.String(value.path.c_str(), value.path.length());
664 }
665
666 std::ostream& operator<<(std::ostream& out, AbsolutePath const& path)
667 {
668 out << path.path;
669 return out;
670 }
671
672 lsDocumentUri lsDocumentUri::FromPath(AbsolutePath const& path)
673 {
674 lsDocumentUri result;
675 result.SetPath(path);
676 return result;
677 }
678 //void lsDocumentUri::SetPath(const AbsolutePath& path)
679 //{
680 // raw_uri_ = make_file_scheme_uri(path.path);
681 //}
682 //
683 void lsDocumentUri::SetPath(AbsolutePath const& path)
684 {
685 // file:///c%3A/Users/jacob/Desktop/superindex/indexer/full_tests
686 raw_uri_ = path;
687
688 size_t index = raw_uri_.find(":");
689 if (index == 1)
690 { // widows drive letters must always be 1 char
691 raw_uri_.replace(raw_uri_.begin() + index, raw_uri_.begin() + index + 1, "%3A");
692 }
693
694 // subset of reserved characters from the URI standard
695 // http://www.ecma-international.org/ecma-262/6.0/#sec-uri-syntax-and-semantics
696 std::string t;
697 t.reserve(8 + raw_uri_.size());
698
699 // TODO: proper fix
700 #if defined(_WIN32)
701 t += "file:///";
702 #else
703 t += "file://";
704 #endif
705
706 // clang-format off
707 for (char c : raw_uri_)
708 switch (c) {
709 case ' ': t += "%20"; break;
710 case '#': t += "%23"; break;
711 case '$': t += "%24"; break;
712 case '&': t += "%26"; break;
713 case '(': t += "%28"; break;
714 case ')': t += "%29"; break;
715 case '+': t += "%2B"; break;
716 case ',': t += "%2C"; break;
717 case ';': t += "%3B"; break;
718 case '?': t += "%3F"; break;
719 case '@': t += "%40"; break;
720 default: t += c; break;
721 }
722 // clang-format on
723 raw_uri_ = std::move(t);
724 }
725
726 std::string lsDocumentUri::GetRawPath() const
727 {
728
729 if (raw_uri_.compare(0, 8, "file:///"))
730 {
731 return raw_uri_;
732 }
733
734 std::string ret;
735 #if defined(_WIN32)
736 size_t i = 8;
737 #else
738 size_t i = 7;
739 #endif
740 auto from_hex = [](unsigned char const& c) -> unsigned int
741 {
742 unsigned char c_from_zero_char = c - '0';
743 return c_from_zero_char < 10 ? c_from_zero_char : (c | 32) - 'a' + 10;
744 };
745 for (; i < raw_uri_.size(); i++)
746 {
747 if (i + 3 <= raw_uri_.size() && raw_uri_[i] == '%')
748 {
749 ret.push_back(static_cast<char>(from_hex(raw_uri_[i + 1]) * 16 + from_hex(raw_uri_[i + 2])));
750 i += 2;
751 }
752 else
753 {
754 ret.push_back(raw_uri_[i] == '\\' ? '/' : raw_uri_[i]);
755 }
756 }
757 return ret;
758 }
759
760 lsDocumentUri::lsDocumentUri()
761 {
762 }
763
764 lsDocumentUri::lsDocumentUri(AbsolutePath const& path)
765 {
766 SetPath(path);
767 }
768
769 lsDocumentUri::lsDocumentUri(lsDocumentUri const& other) : raw_uri_(other.raw_uri_)
770 {
771 }
772
773 bool lsDocumentUri::operator==(lsDocumentUri const& other) const
774 {
775 return raw_uri_ == other.raw_uri_;
776 }
777
778 bool lsDocumentUri::operator==(std::string const& other) const
779 {
780 return raw_uri_ == other;
781 }
782
783 AbsolutePath lsDocumentUri::GetAbsolutePath() const
784 {
785
786 if (raw_uri_.find("file://") != std::string::npos)
787 {
788 try
789 {
790 return lsp::NormalizePath(GetRawPath(), false /*ensure_exists*/, false);
791 }
792 catch (std::exception&)
793 {
794 return AbsolutePath("", false);
795 }
796 }
797
798 return AbsolutePath(raw_uri_, false);
799 }
800
801 AbsolutePath::AbsolutePath(std::string const& path, bool validate) : path(path)
802 {
803 // TODO: enable validation after fixing tests.
804 if (validate && !lsp::IsAbsolutePath(path))
805 {
806 qualify = false;
807 auto temp = lsp::NormalizePath(path, false);
808 if (!temp.path.empty())
809 {
810 this->path = temp.path;
811 }
812 }
813 }
814
815 void Reflect(Writer& visitor, lsDocumentUri& value)
816 {
817 Reflect(visitor, value.raw_uri_);
818 }
819 void Reflect(Reader& visitor, lsDocumentUri& value)
820 {
821 Reflect(visitor, value.raw_uri_);
822 // Only record the path when we deserialize a URI, since it most likely came
823 // from the client.
824 }
825
826 std::string ProgressReport::ToString() const
827 {
828 std::string info;
829 info += "id:" + id + "\n";
830 info += "task:" + task + "\n";
831 info += "subTask:" + subTask + "\n";
832 info += "status:" + status + "\n";
833 {
834 std::stringstream ss;
835 ss << "totalWork:" << totalWork << std::endl;
836 info += ss.str();
837 }
838 {
839 std::stringstream ss;
840 ss << "workDone:" << workDone << std::endl;
841 info += ss.str();
842 }
843
844 {
845 std::stringstream ss;
846 ss << "complete:" << complete << std::endl;
847 info += ss.str();
848 }
849
850 return info;
851 }
852
853 std::string EventNotification::ToString() const
854 {
855 std::string info;
856 if (ClasspathUpdated == eventType)
857 {
858 info += "eventType:ClasspathUpdated\n";
859 }
860 else if (ProjectsImported == eventType)
861 {
862 info += "eventType:ProjectsImported\n";
863 }
864 else
865 {
866 std::ostringstream oss;
867 oss << std::hex << eventType << std::endl;
868
869 info += "eventType:";
870 info += oss.str();
871 }
872 info += "data:" + data.Data() + "\n";
873 return info;
874 }
875
876 std::string lsp::ToString(lsCompletionItemKind _kind)
877 {
878 switch (_kind)
879 {
880 case lsCompletionItemKind::Text:
881 return "Text";
882 case lsCompletionItemKind::Method:
883 return "Method";
884 case lsCompletionItemKind::Function:
885 return "";
886 case lsCompletionItemKind::Constructor:
887 return "Function";
888 case lsCompletionItemKind::Field:
889 return "Field";
890 case lsCompletionItemKind::Variable:
891 return "";
892 case lsCompletionItemKind::Class:
893 return "Variable";
894 case lsCompletionItemKind::Interface:
895 return "Interface";
896 case lsCompletionItemKind::Module:
897 return "Module";
898 case lsCompletionItemKind::Property:
899 return "Property";
900 case lsCompletionItemKind::Unit:
901 return "Unit";
902 case lsCompletionItemKind::Value:
903 return "Value";
904 case lsCompletionItemKind::Enum:
905 return "Enum";
906 case lsCompletionItemKind::Keyword:
907 return "Keyword";
908 case lsCompletionItemKind::Snippet:
909 return "Snippet";
910 case lsCompletionItemKind::Color:
911 return "Color";
912 case lsCompletionItemKind::File:
913 return "File";
914 case lsCompletionItemKind::Reference:
915 return "Reference";
916 case lsCompletionItemKind::Folder:
917 return "Folder";
918 case lsCompletionItemKind::EnumMember:
919 return "EnumMember";
920 case lsCompletionItemKind::Constant:
921 return "Constant";
922 case lsCompletionItemKind::Struct:
923 return "Struct";
924 case lsCompletionItemKind::Event:
925 return "Event";
926 case lsCompletionItemKind::Operator:
927 return "Operator";
928 case lsCompletionItemKind::TypeParameter:
929 return "TypeParameter";
930 default:
931 return "Unknown";
932 }
933 }
934
935 std::string lsp::ToString(lsInsertTextFormat _kind)
936 {
937 if (_kind == lsInsertTextFormat::PlainText)
938 {
939 return "PlainText";
940 }
941 else if (_kind == lsInsertTextFormat::Snippet)
942 {
943 return "Snippet";
944 }
945 else
946 {
947 return "Unknown";
948 }
949 }
950
951 std::string const& lsCompletionItem::InsertedContent() const
952 {
953 if (textEdit)
954 {
955 return textEdit->newText;
956 }
957 if (insertText.has_value() && !insertText->empty())
958 {
959 return insertText.value();
960 }
961 return label;
962 }
963
964 std::string lsCompletionItem::DisplayText()
965 {
966
967 if (detail)
968 {
969
970 return label + " in " + detail.value();
971 }
972 return label;
973 }
974
975 std::string lsCompletionItem::ToString()
976 {
977 std::stringstream info;
978 info << "label : " << label << std::endl;
979 if (kind)
980 {
981 info << "kind : " << lsp::ToString(kind.value()) << std::endl;
982 }
983 else
984 {
985 info << "kind : no exist." << std::endl;
986 }
987
988 if (detail)
989 {
990 info << "detail : " << detail.value() << std::endl;
991 }
992 else
993 {
994 info << "detail : no exist." << std::endl;
995 }
996
997 if (documentation)
998 {
999 info << "documentation : " << std::endl;
1000 if (documentation.value().first)
1001 {
1002 info << documentation.value().first.value();
1003 }
1004 else if (documentation.value().second)
1005 {
1006 info << documentation.value().second.value().value;
1007 }
1008 }
1009 else
1010 {
1011 info << "documentation : no exist." << std::endl;
1012 }
1013
1014 if (deprecated)
1015 {
1016 info << "deprecated : " << deprecated.value() << std::endl;
1017 }
1018 else
1019 {
1020 info << "deprecated : no exist." << std::endl;
1021 }
1022
1023 if (preselect)
1024 {
1025 info << "preselect : " << preselect.value() << std::endl;
1026 }
1027 else
1028 {
1029 info << "preselect : no exist." << std::endl;
1030 }
1031
1032 if (sortText)
1033 {
1034 info << "sortText : " << sortText.value() << std::endl;
1035 }
1036 else
1037 {
1038 info << "sortText : no exist." << std::endl;
1039 }
1040
1041 if (filterText)
1042 {
1043 info << "filterText : " << filterText.value() << std::endl;
1044 }
1045 else
1046 {
1047 info << "filterText : no exist." << std::endl;
1048 }
1049
1050 if (insertText)
1051 {
1052 info << "insertText : " << insertText.value() << std::endl;
1053 }
1054 else
1055 {
1056 info << "insertText : no exist." << std::endl;
1057 }
1058
1059 if (insertTextFormat)
1060 {
1061 info << "insertText : " << lsp::ToString(insertTextFormat.value()) << std::endl;
1062 }
1063 else
1064 {
1065 info << "insertTextFormat : no exist." << std::endl;
1066 }
1067
1068 if (textEdit)
1069 {
1070 info << "textEdit : " << textEdit.value().ToString() << std::endl;
1071 }
1072 else
1073 {
1074 info << "textEdit : no exist." << std::endl;
1075 }
1076
1077 return info.str();
1078 }
1079 namespace JDT
1080 {
1081 namespace CodeActionKind
1082 {
1083
1084 /**
1085 * Base kind for quickfix actions: 'quickfix'
1086 */
1087 char const* QuickFix = "quickfix";
1088
1089 /**
1090 * Base kind for refactoring actions: 'refactor'
1091 */
1092 char const* Refactor = "refactor";
1093
1094 /**
1095 * Base kind for refactoring extraction actions: 'refactor.extract'
1096 *
1097 * Example extract actions:
1098 *
1099 * - Extract method - Extract function - Extract variable - Extract interface
1100 * from class - ...
1101 */
1102 char const* RefactorExtract = "refactor.extract";
1103
1104 /**
1105 * Base kind for refactoring inline actions: 'refactor.inline'
1106 *
1107 * Example inline actions:
1108 *
1109 * - Inline function - Inline variable - Inline constant - ...
1110 */
1111 char const* RefactorInline = "refactor.inline";
1112
1113 /**
1114 * Base kind for refactoring rewrite actions: 'refactor.rewrite'
1115 *
1116 * Example rewrite actions:
1117 *
1118 * - Convert JavaScript function to class - Add or remove parameter -
1119 * Encapsulate field - Make method static - Move method to base class - ...
1120 */
1121 char const* RefactorRewrite = "refactor.rewrite";
1122
1123 /**
1124 * Base kind for source actions: `source`
1125 *
1126 * Source code actions apply to the entire file.
1127 */
1128 char const* Source = "source";
1129
1130 /**
1131 * Base kind for an organize imports source action: `source.organizeImports`
1132 */
1133 char const* SourceOrganizeImports = "source.organizeImports";
1134
1135 char const* COMMAND_ID_APPLY_EDIT = "java.apply.workspaceEdit";
1136
1137 }; // namespace CodeActionKind
1138
1139 } // namespace JDT
1140 Directory::Directory(AbsolutePath const& path) : path(path.path)
1141 {
1142 lsp::EnsureEndsInSlash(this->path);
1143 }
1144
1145 bool Directory::operator==(Directory const& rhs) const
1146 {
1147 return path == rhs.path;
1148 }
1149
1150 bool Directory::operator!=(Directory const& rhs) const
1151 {
1152 return path != rhs.path;
1153 }
1154
1155 // Generates a random UUID (version 4) as a string without using any static variables
1156 std::string generate_uuid_v4() {
1157 // Create a PRNG on each call (no static variables)
1158 std::random_device rd;
1159 std::mt19937_64 gen(rd());
1160 std::uniform_int_distribution<uint64_t> dist;
1161
1162 // Generate 16 bytes of randomness in two 64-bit chunks
1163 uint64_t part1 = dist(gen);
1164 uint64_t part2 = dist(gen);
1165 std::array<uint8_t, 16> bytes;
1166 for (int i = 0; i < 8; ++i) {
1167 bytes[i] = static_cast<uint8_t>(part1 >> (8 * (7 - i)));
1168 bytes[8 + i] = static_cast<uint8_t>(part2 >> (8 * (7 - i)));
1169 }
1170
1171 // Set version (4) and variant (RFC 4122)
1172 bytes[6] = (bytes[6] & 0x0F) | 0x40; // version: 0100xxxx
1173 bytes[8] = (bytes[8] & 0x3F) | 0x80; // variant: 10xxxxxx
1174
1175 // Convert to hex string with hyphens (8-4-4-4-12)
1176 std::ostringstream ss;
1177 ss << std::hex << std::setfill('0');
1178 int groups[] = {4, 2, 2, 2, 6};
1179 int idx = 0;
1180 for (int g = 0; g < 5; ++g) {
1181 for (int i = 0; i < groups[g]; ++i) {
1182 ss << std::setw(2) << static_cast<int>(bytes[idx++]);
1183 }
1184 if (g < 4) ss << '-';
1185 }
1186 return ss.str();
1187 }
1188
1189 Registration Registration::Create(std::string const& method)
1190 {
1191 Registration reg;
1192 reg.method = method;
1193 reg.id = generate_uuid_v4();
1194 return reg;
1195 }