ClientConnection.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  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 "ClientConnection.h"
  27. #include "LexerAutoComplete.h"
  28. #include "ParserAutoComplete.h"
  29. #include <AK/Debug.h>
  30. #include <AK/HashMap.h>
  31. #include <LibCore/File.h>
  32. #include <LibGUI/TextDocument.h>
  33. namespace LanguageServers::Cpp {
  34. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  35. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
  36. : IPC::ClientConnection<LanguageClientEndpoint, LanguageServerEndpoint>(*this, move(socket), client_id)
  37. {
  38. s_connections.set(client_id, *this);
  39. m_autocomplete_engine = make<LexerAutoComplete>(m_filedb);
  40. }
  41. ClientConnection::~ClientConnection()
  42. {
  43. }
  44. void ClientConnection::die()
  45. {
  46. s_connections.remove(client_id());
  47. exit(0);
  48. }
  49. OwnPtr<Messages::LanguageServer::GreetResponse> ClientConnection::handle(const Messages::LanguageServer::Greet& message)
  50. {
  51. m_filedb.set_project_root(message.project_root());
  52. if (unveil(message.project_root().characters(), "r") < 0) {
  53. perror("unveil");
  54. exit(1);
  55. }
  56. if (unveil(nullptr, nullptr) < 0) {
  57. perror("unveil");
  58. exit(1);
  59. }
  60. return make<Messages::LanguageServer::GreetResponse>();
  61. }
  62. void ClientConnection::handle(const Messages::LanguageServer::FileOpened& message)
  63. {
  64. if (m_filedb.is_open(message.file_name())) {
  65. return;
  66. }
  67. m_filedb.add(message.file_name(), message.file().take_fd());
  68. m_autocomplete_engine->file_opened(message.file_name());
  69. }
  70. void ClientConnection::handle(const Messages::LanguageServer::FileEditInsertText& message)
  71. {
  72. #if CPP_LANGUAGE_SERVER_DEBUG
  73. dbgln("InsertText for file: {}", message.file_name());
  74. dbgln("Text: {}", message.text());
  75. dbgln("[{}:{}]", message.start_line(), message.start_column());
  76. #endif
  77. m_filedb.on_file_edit_insert_text(message.file_name(), message.text(), message.start_line(), message.start_column());
  78. m_autocomplete_engine->on_edit(message.file_name());
  79. }
  80. void ClientConnection::handle(const Messages::LanguageServer::FileEditRemoveText& message)
  81. {
  82. #if CPP_LANGUAGE_SERVER_DEBUG
  83. dbgln("RemoveText for file: {}", message.file_name());
  84. dbgln("[{}:{} - {}:{}]", message.start_line(), message.start_column(), message.end_line(), message.end_column());
  85. #endif
  86. m_filedb.on_file_edit_remove_text(message.file_name(), message.start_line(), message.start_column(), message.end_line(), message.end_column());
  87. m_autocomplete_engine->on_edit(message.file_name());
  88. }
  89. void ClientConnection::handle(const Messages::LanguageServer::AutoCompleteSuggestions& message)
  90. {
  91. #if CPP_LANGUAGE_SERVER_DEBUG
  92. dbgln("AutoCompleteSuggestions for: {} {}:{}", message.file_name(), message.cursor_line(), message.cursor_column());
  93. #endif
  94. auto document = m_filedb.get(message.file_name());
  95. if (!document) {
  96. dbgln("file {} has not been opened", message.file_name());
  97. return;
  98. }
  99. GUI::TextPosition autocomplete_position = { (size_t)message.cursor_line(), (size_t)max(message.cursor_column(), message.cursor_column() - 1) };
  100. Vector<GUI::AutocompleteProvider::Entry> suggestions = m_autocomplete_engine->get_suggestions(message.file_name(), autocomplete_position);
  101. post_message(Messages::LanguageClient::AutoCompleteSuggestions(move(suggestions)));
  102. }
  103. void ClientConnection::handle(const Messages::LanguageServer::SetFileContent& message)
  104. {
  105. auto document = m_filedb.get(message.file_name());
  106. if (!document) {
  107. dbgln("file {} has not been opened", message.file_name());
  108. return;
  109. }
  110. auto content = message.content();
  111. document->set_text(content.view());
  112. m_autocomplete_engine->on_edit(message.file_name());
  113. }
  114. void ClientConnection::handle(const Messages::LanguageServer::SetAutoCompleteMode& message)
  115. {
  116. #ifdef DEBUG_CPP_LANGUAGE_SERVER
  117. dbgln("SetAutoCompleteMode: {}", message.mode());
  118. #endif
  119. if (message.mode() == "Parser")
  120. m_autocomplete_engine = make<ParserAutoComplete>(m_filedb);
  121. else
  122. m_autocomplete_engine = make<LexerAutoComplete>(m_filedb);
  123. }
  124. }