LanguageClient.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #include "LanguageClient.h"
  27. #include <AK/String.h>
  28. #include <AK/Vector.h>
  29. #include <DevTools/HackStudio/LanguageServers/LanguageServerEndpoint.h>
  30. namespace HackStudio {
  31. void ServerConnection::handle(const Messages::LanguageClient::AutoCompleteSuggestions& message)
  32. {
  33. if (!m_language_client) {
  34. dbgln("Language Server connection has no attached language client");
  35. return;
  36. }
  37. m_language_client->provide_autocomplete_suggestions(message.suggestions());
  38. }
  39. void LanguageClient::open_file(const String& path, int fd)
  40. {
  41. m_connection.post_message(Messages::LanguageServer::FileOpened(path, fd));
  42. }
  43. void LanguageClient::set_file_content(const String& path, const String& content)
  44. {
  45. m_connection.post_message(Messages::LanguageServer::SetFileContent(path, content));
  46. }
  47. void LanguageClient::insert_text(const String& path, const String& text, size_t line, size_t column)
  48. {
  49. m_connection.post_message(Messages::LanguageServer::FileEditInsertText(path, text, line, column));
  50. }
  51. void LanguageClient::remove_text(const String& path, size_t from_line, size_t from_column, size_t to_line, size_t to_column)
  52. {
  53. m_connection.post_message(Messages::LanguageServer::FileEditRemoveText(path, from_line, from_column, to_line, to_column));
  54. }
  55. void LanguageClient::request_autocomplete(const String& path, size_t cursor_line, size_t cursor_column)
  56. {
  57. set_active_client();
  58. m_connection.post_message(Messages::LanguageServer::AutoCompleteSuggestions(path, cursor_line, cursor_column));
  59. }
  60. void LanguageClient::provide_autocomplete_suggestions(const Vector<GUI::AutocompleteProvider::Entry>& suggestions)
  61. {
  62. if (on_autocomplete_suggestions)
  63. on_autocomplete_suggestions(suggestions);
  64. // Otherwise, drop it on the floor :shrug:
  65. }
  66. void LanguageClient::set_autocomplete_mode(const String& mode)
  67. {
  68. m_connection.post_message(Messages::LanguageServer::SetAutoCompleteMode(mode));
  69. }
  70. void LanguageClient::set_active_client()
  71. {
  72. m_connection.attach(*this);
  73. }
  74. }