AutoCompleteResponse.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/String.h>
  28. #include <AK/Types.h>
  29. #include <LibGUI/AutocompleteProvider.h>
  30. #include <LibIPC/Decoder.h>
  31. #include <LibIPC/Encoder.h>
  32. namespace IPC {
  33. template<>
  34. inline bool encode(IPC::Encoder& encoder, const GUI::AutocompleteProvider::Entry& response)
  35. {
  36. encoder << response.completion;
  37. encoder << (u64)response.partial_input_length;
  38. encoder << (u32)response.kind;
  39. encoder << (u32)response.language;
  40. return true;
  41. }
  42. template<>
  43. inline bool decode(IPC::Decoder& decoder, GUI::AutocompleteProvider::Entry& response)
  44. {
  45. u32 kind = 0;
  46. u32 language = 0;
  47. u64 partial_input_length = 0;
  48. bool ok = decoder.decode(response.completion)
  49. && decoder.decode(partial_input_length)
  50. && decoder.decode(kind)
  51. && decoder.decode(language);
  52. if (ok) {
  53. response.kind = static_cast<GUI::AutocompleteProvider::CompletionKind>(kind);
  54. response.language = static_cast<GUI::AutocompleteProvider::Language>(language);
  55. response.partial_input_length = partial_input_length;
  56. }
  57. return ok;
  58. }
  59. template<>
  60. inline bool encode(Encoder& encoder, const GUI::AutocompleteProvider::ProjectLocation& location)
  61. {
  62. encoder << location.file;
  63. encoder << (u64)location.line;
  64. encoder << (u64)location.column;
  65. return true;
  66. }
  67. template<>
  68. inline bool decode(Decoder& decoder, GUI::AutocompleteProvider::ProjectLocation& location)
  69. {
  70. u64 line = 0;
  71. u64 column = 0;
  72. if (!decoder.decode(location.file))
  73. return false;
  74. if (!decoder.decode(line))
  75. return false;
  76. if (!decoder.decode(column))
  77. return false;
  78. location.line = line;
  79. location.column = column;
  80. return true;
  81. }
  82. template<>
  83. inline bool encode(Encoder& encoder, const GUI::AutocompleteProvider::Declaration& declaration)
  84. {
  85. encoder << declaration.name;
  86. if (!encode(encoder, declaration.position))
  87. return false;
  88. encoder << (u32)declaration.type;
  89. return true;
  90. }
  91. template<>
  92. inline bool decode(Decoder& decoder, GUI::AutocompleteProvider::Declaration& declaration)
  93. {
  94. if (!decoder.decode(declaration.name))
  95. return false;
  96. if (!decode(decoder, declaration.position))
  97. return false;
  98. u32 type;
  99. if (!decoder.decode(type))
  100. return false;
  101. declaration.type = static_cast<GUI::AutocompleteProvider::DeclarationType>(type);
  102. return true;
  103. }
  104. }