From 02038a0edee0f01f76e90c8b4fbdf208592ea262 Mon Sep 17 00:00:00 2001 From: Itamar Date: Sat, 6 Feb 2021 16:25:15 +0200 Subject: [PATCH] LanguageServers/Cpp: Use FileDB and AutoCompleteEngine base class --- .../Cpp/AutoCompleteEngine.cpp | 36 +++++++ .../LanguageServers/Cpp/AutoCompleteEngine.h | 49 ++++++++++ .../LanguageServers/Cpp/CMakeLists.txt | 4 +- .../LanguageServers/Cpp/ClientConnection.cpp | 93 +++---------------- .../LanguageServers/Cpp/ClientConnection.h | 15 +-- .../LanguageServers/Cpp/LexerAutoComplete.cpp | 13 ++- .../LanguageServers/Cpp/LexerAutoComplete.h | 13 +-- 7 files changed, 126 insertions(+), 97 deletions(-) create mode 100644 Userland/DevTools/HackStudio/LanguageServers/Cpp/AutoCompleteEngine.cpp create mode 100644 Userland/DevTools/HackStudio/LanguageServers/Cpp/AutoCompleteEngine.h diff --git a/Userland/DevTools/HackStudio/LanguageServers/Cpp/AutoCompleteEngine.cpp b/Userland/DevTools/HackStudio/LanguageServers/Cpp/AutoCompleteEngine.cpp new file mode 100644 index 0000000000..6ee40241aa --- /dev/null +++ b/Userland/DevTools/HackStudio/LanguageServers/Cpp/AutoCompleteEngine.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021, Itamar S. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "AutoCompleteEngine.h" + +AutoCompleteEngine::AutoCompleteEngine(const FileDB& filedb) + : m_filedb(filedb) +{ +} + +AutoCompleteEngine::~AutoCompleteEngine() +{ +} diff --git a/Userland/DevTools/HackStudio/LanguageServers/Cpp/AutoCompleteEngine.h b/Userland/DevTools/HackStudio/LanguageServers/Cpp/AutoCompleteEngine.h new file mode 100644 index 0000000000..2e61eb9101 --- /dev/null +++ b/Userland/DevTools/HackStudio/LanguageServers/Cpp/AutoCompleteEngine.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2021, Itamar S. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "FileDB.h" +#include +#include + +class AutoCompleteEngine { +public: + AutoCompleteEngine(const FileDB& filedb); + virtual ~AutoCompleteEngine(); + + virtual Vector get_suggestions(const String& file, const GUI::TextPosition& autocomplete_position) = 0; + + // TODO: In the future we can pass the range that was edited and only re-parse what we have to. + virtual void on_edit([[maybe_unused]] const String& file) {}; + virtual void file_opened([[maybe_unused]] const String& file) {}; + +protected: + const FileDB& filedb() const { return m_filedb; } + +private: + const FileDB& m_filedb; +}; diff --git a/Userland/DevTools/HackStudio/LanguageServers/Cpp/CMakeLists.txt b/Userland/DevTools/HackStudio/LanguageServers/Cpp/CMakeLists.txt index 7d34577447..cfe5cd180a 100644 --- a/Userland/DevTools/HackStudio/LanguageServers/Cpp/CMakeLists.txt +++ b/Userland/DevTools/HackStudio/LanguageServers/Cpp/CMakeLists.txt @@ -1,8 +1,10 @@ set(SOURCES + AutoCompleteEngine.cpp ClientConnection.cpp - main.cpp + FileDB.cpp LexerAutoComplete.cpp ParserAutoComplete.cpp + main.cpp ) set(GENERATED_SOURCES diff --git a/Userland/DevTools/HackStudio/LanguageServers/Cpp/ClientConnection.cpp b/Userland/DevTools/HackStudio/LanguageServers/Cpp/ClientConnection.cpp index c6dd71cf48..f964be7b1d 100644 --- a/Userland/DevTools/HackStudio/LanguageServers/Cpp/ClientConnection.cpp +++ b/Userland/DevTools/HackStudio/LanguageServers/Cpp/ClientConnection.cpp @@ -40,6 +40,7 @@ ClientConnection::ClientConnection(NonnullRefPtr socket, int : IPC::ClientConnection(*this, move(socket), client_id) { s_connections.set(client_id, *this); + m_autocomplete_engine = make(m_filedb); } ClientConnection::~ClientConnection() @@ -66,38 +67,13 @@ OwnPtr ClientConnection::handle(const M return make(); } -class DefaultDocumentClient final : public GUI::TextDocument::Client { -public: - virtual ~DefaultDocumentClient() override = default; - virtual void document_did_append_line() override {}; - virtual void document_did_insert_line(size_t) override {}; - virtual void document_did_remove_line(size_t) override {}; - virtual void document_did_remove_all_lines() override {}; - virtual void document_did_change() override {}; - virtual void document_did_set_text() override {}; - virtual void document_did_set_cursor(const GUI::TextPosition&) override {}; - - virtual bool is_automatic_indentation_enabled() const override { return false; } - virtual int soft_tab_width() const override { return 4; } -}; - -static DefaultDocumentClient s_default_document_client; - void ClientConnection::handle(const Messages::LanguageServer::FileOpened& message) { - auto file = Core::File::construct(this); - if (!file->open(message.file().take_fd(), Core::IODevice::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes)) { - errno = file->error(); - perror("open"); - dbgln("Failed to open project file"); + if (m_filedb.is_open(message.file_name())) { return; } - auto content = file->read_all(); - StringView content_view(content); - auto document = GUI::TextDocument::create(&s_default_document_client); - document->set_text(content_view); - m_open_files.set(message.file_name(), document); - dbgln_if(FILE_CONTENT_DEBUG, "{}", document->text()); + m_filedb.add(message.file_name(), message.file().take_fd()); + m_autocomplete_engine->file_opened(message.file_name()); } void ClientConnection::handle(const Messages::LanguageServer::FileEditInsertText& message) @@ -107,16 +83,8 @@ void ClientConnection::handle(const Messages::LanguageServer::FileEditInsertText dbgln("Text: {}", message.text()); dbgln("[{}:{}]", message.start_line(), message.start_column()); #endif - auto document = document_for(message.file_name()); - if (!document) { - dbgln("file {} has not been opened", message.file_name()); - return; - } - GUI::TextPosition start_position { (size_t)message.start_line(), (size_t)message.start_column() }; - document->insert_at(start_position, message.text(), &s_default_document_client); -#if FILE_CONTENT_DEBUG - dbgln("{}", document->text()); -#endif + m_filedb.on_file_edit_insert_text(message.file_name(), message.text(), message.start_line(), message.start_column()); + m_autocomplete_engine->on_edit(message.file_name()); } void ClientConnection::handle(const Messages::LanguageServer::FileEditRemoveText& message) @@ -125,23 +93,8 @@ void ClientConnection::handle(const Messages::LanguageServer::FileEditRemoveText dbgln("RemoveText for file: {}", message.file_name()); dbgln("[{}:{} - {}:{}]", message.start_line(), message.start_column(), message.end_line(), message.end_column()); #endif - auto document = document_for(message.file_name()); - if (!document) { - dbgln("file {} has not been opened", message.file_name()); - return; - } - GUI::TextPosition start_position { (size_t)message.start_line(), (size_t)message.start_column() }; - GUI::TextRange range { - GUI::TextPosition { (size_t)message.start_line(), - (size_t)message.start_column() }, - GUI::TextPosition { (size_t)message.end_line(), - (size_t)message.end_column() } - }; - - document->remove(range); -#if FILE_CONTENT_DEBUG - dbgln("{}", document->text()); -#endif + m_filedb.on_file_edit_remove_text(message.file_name(), message.start_line(), message.start_column(), message.end_line(), message.end_column()); + m_autocomplete_engine->on_edit(message.file_name()); } void ClientConnection::handle(const Messages::LanguageServer::AutoCompleteSuggestions& message) @@ -150,43 +103,27 @@ void ClientConnection::handle(const Messages::LanguageServer::AutoCompleteSugges dbgln("AutoCompleteSuggestions for: {} {}:{}", message.file_name(), message.cursor_line(), message.cursor_column()); #endif - auto document = document_for(message.file_name()); + auto document = m_filedb.get(message.file_name()); if (!document) { dbgln("file {} has not been opened", message.file_name()); return; } - Vector suggestions; - switch (m_auto_complete_mode) { - case AutoCompleteMode::Lexer: - suggestions = LexerAutoComplete::get_suggestions(document->text(), { (size_t)message.cursor_line(), (size_t)max(message.cursor_column(), message.cursor_column() - 1) }); - break; - case AutoCompleteMode::Parser: { - auto engine = ParserAutoComplete(document->text()); - suggestions = engine.get_suggestions({ (size_t)message.cursor_line(), (size_t)max(message.cursor_column(), message.cursor_column() - 1) }); - } - } + GUI::TextPosition autocomplete_position = { (size_t)message.cursor_line(), (size_t)max(message.cursor_column(), message.cursor_column() - 1) }; + Vector suggestions = m_autocomplete_engine->get_suggestions(message.file_name(), autocomplete_position); post_message(Messages::LanguageClient::AutoCompleteSuggestions(move(suggestions))); } -RefPtr ClientConnection::document_for(const String& file_name) -{ - auto document_optional = m_open_files.get(file_name); - if (!document_optional.has_value()) - return nullptr; - - return document_optional.value(); -} - void ClientConnection::handle(const Messages::LanguageServer::SetFileContent& message) { - auto document = document_for(message.file_name()); + auto document = m_filedb.get(message.file_name()); if (!document) { dbgln("file {} has not been opened", message.file_name()); return; } auto content = message.content(); document->set_text(content.view()); + m_autocomplete_engine->on_edit(message.file_name()); } void ClientConnection::handle(const Messages::LanguageServer::SetAutoCompleteMode& message) @@ -195,9 +132,9 @@ void ClientConnection::handle(const Messages::LanguageServer::SetAutoCompleteMod dbgln("SetAutoCompleteMode: {}", message.mode()); #endif if (message.mode() == "Parser") - m_auto_complete_mode = AutoCompleteMode::Parser; + m_autocomplete_engine = make(m_filedb); else - m_auto_complete_mode = AutoCompleteMode::Lexer; + m_autocomplete_engine = make(m_filedb); } } diff --git a/Userland/DevTools/HackStudio/LanguageServers/Cpp/ClientConnection.h b/Userland/DevTools/HackStudio/LanguageServers/Cpp/ClientConnection.h index 3389ba8231..8118cbf675 100644 --- a/Userland/DevTools/HackStudio/LanguageServers/Cpp/ClientConnection.h +++ b/Userland/DevTools/HackStudio/LanguageServers/Cpp/ClientConnection.h @@ -26,10 +26,11 @@ #pragma once +#include "AutoCompleteEngine.h" +#include "FileDB.h" #include #include #include -#include #include #include @@ -57,16 +58,8 @@ private: virtual void handle(const Messages::LanguageServer::AutoCompleteSuggestions&) override; virtual void handle(const Messages::LanguageServer::SetAutoCompleteMode&) override; - RefPtr document_for(const String& file_name); - - HashMap> m_open_files; - - enum class AutoCompleteMode { - Lexer, - Parser - }; - - AutoCompleteMode m_auto_complete_mode { AutoCompleteMode::Lexer }; + FileDB m_filedb; + OwnPtr m_autocomplete_engine; }; } diff --git a/Userland/DevTools/HackStudio/LanguageServers/Cpp/LexerAutoComplete.cpp b/Userland/DevTools/HackStudio/LanguageServers/Cpp/LexerAutoComplete.cpp index 9a31679728..721b6de953 100644 --- a/Userland/DevTools/HackStudio/LanguageServers/Cpp/LexerAutoComplete.cpp +++ b/Userland/DevTools/HackStudio/LanguageServers/Cpp/LexerAutoComplete.cpp @@ -31,8 +31,19 @@ namespace LanguageServers::Cpp { -Vector LexerAutoComplete::get_suggestions(const String& code, const GUI::TextPosition& autocomplete_position) +LexerAutoComplete::LexerAutoComplete(const FileDB& filedb) + : AutoCompleteEngine(filedb) { +} + +Vector LexerAutoComplete::get_suggestions(const String& file, const GUI::TextPosition& autocomplete_position) +{ + auto document = filedb().get(file); + if (!document) { + dbgln("didn't find document for {}", file); + return {}; + } + auto code = document->text(); auto lines = code.split('\n', true); Cpp::Lexer lexer(code); auto tokens = lexer.lex(); diff --git a/Userland/DevTools/HackStudio/LanguageServers/Cpp/LexerAutoComplete.h b/Userland/DevTools/HackStudio/LanguageServers/Cpp/LexerAutoComplete.h index e482818cfc..58a836cce2 100644 --- a/Userland/DevTools/HackStudio/LanguageServers/Cpp/LexerAutoComplete.h +++ b/Userland/DevTools/HackStudio/LanguageServers/Cpp/LexerAutoComplete.h @@ -26,6 +26,7 @@ #pragma once +#include "AutoCompleteEngine.h" #include #include #include @@ -36,16 +37,16 @@ namespace LanguageServers::Cpp { using namespace ::Cpp; -class LexerAutoComplete { +class LexerAutoComplete : public AutoCompleteEngine { public: - LexerAutoComplete() = delete; + LexerAutoComplete(const FileDB& filedb); - static Vector get_suggestions(const String& code, const GUI::TextPosition& autocomplete_position); + virtual Vector get_suggestions(const String& file, const GUI::TextPosition& autocomplete_position) override; private: - static Optional token_in_position(const Vector&, const GUI::TextPosition&); - static StringView text_of_token(const Vector& lines, const Cpp::Token&); - static Vector identifier_prefixes(const Vector& lines, const Vector&, size_t target_token_index); + Optional token_in_position(const Vector&, const GUI::TextPosition&); + StringView text_of_token(const Vector& lines, const Cpp::Token&); + Vector identifier_prefixes(const Vector& lines, const Vector&, size_t target_token_index); }; }