mirror of
https://github.com/RGBCube/serenity
synced 2025-05-21 15:25:07 +00:00
LanguageServers/Cpp: Use FileDB and AutoCompleteEngine base class
This commit is contained in:
parent
bed06b13f3
commit
02038a0ede
7 changed files with 126 additions and 97 deletions
|
@ -0,0 +1,36 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
|
||||||
|
* 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()
|
||||||
|
{
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
|
||||||
|
* 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 <DevTools/HackStudio/AutoCompleteResponse.h>
|
||||||
|
#include <LibGUI/TextPosition.h>
|
||||||
|
|
||||||
|
class AutoCompleteEngine {
|
||||||
|
public:
|
||||||
|
AutoCompleteEngine(const FileDB& filedb);
|
||||||
|
virtual ~AutoCompleteEngine();
|
||||||
|
|
||||||
|
virtual Vector<GUI::AutocompleteProvider::Entry> 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;
|
||||||
|
};
|
|
@ -1,8 +1,10 @@
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
|
AutoCompleteEngine.cpp
|
||||||
ClientConnection.cpp
|
ClientConnection.cpp
|
||||||
main.cpp
|
FileDB.cpp
|
||||||
LexerAutoComplete.cpp
|
LexerAutoComplete.cpp
|
||||||
ParserAutoComplete.cpp
|
ParserAutoComplete.cpp
|
||||||
|
main.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
set(GENERATED_SOURCES
|
set(GENERATED_SOURCES
|
||||||
|
|
|
@ -40,6 +40,7 @@ ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int
|
||||||
: IPC::ClientConnection<LanguageClientEndpoint, LanguageServerEndpoint>(*this, move(socket), client_id)
|
: IPC::ClientConnection<LanguageClientEndpoint, LanguageServerEndpoint>(*this, move(socket), client_id)
|
||||||
{
|
{
|
||||||
s_connections.set(client_id, *this);
|
s_connections.set(client_id, *this);
|
||||||
|
m_autocomplete_engine = make<LexerAutoComplete>(m_filedb);
|
||||||
}
|
}
|
||||||
|
|
||||||
ClientConnection::~ClientConnection()
|
ClientConnection::~ClientConnection()
|
||||||
|
@ -66,38 +67,13 @@ OwnPtr<Messages::LanguageServer::GreetResponse> ClientConnection::handle(const M
|
||||||
return make<Messages::LanguageServer::GreetResponse>();
|
return make<Messages::LanguageServer::GreetResponse>();
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
void ClientConnection::handle(const Messages::LanguageServer::FileOpened& message)
|
||||||
{
|
{
|
||||||
auto file = Core::File::construct(this);
|
if (m_filedb.is_open(message.file_name())) {
|
||||||
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");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto content = file->read_all();
|
m_filedb.add(message.file_name(), message.file().take_fd());
|
||||||
StringView content_view(content);
|
m_autocomplete_engine->file_opened(message.file_name());
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientConnection::handle(const Messages::LanguageServer::FileEditInsertText& message)
|
void ClientConnection::handle(const Messages::LanguageServer::FileEditInsertText& message)
|
||||||
|
@ -107,16 +83,8 @@ void ClientConnection::handle(const Messages::LanguageServer::FileEditInsertText
|
||||||
dbgln("Text: {}", message.text());
|
dbgln("Text: {}", message.text());
|
||||||
dbgln("[{}:{}]", message.start_line(), message.start_column());
|
dbgln("[{}:{}]", message.start_line(), message.start_column());
|
||||||
#endif
|
#endif
|
||||||
auto document = document_for(message.file_name());
|
m_filedb.on_file_edit_insert_text(message.file_name(), message.text(), message.start_line(), message.start_column());
|
||||||
if (!document) {
|
m_autocomplete_engine->on_edit(message.file_name());
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientConnection::handle(const Messages::LanguageServer::FileEditRemoveText& message)
|
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("RemoveText for file: {}", message.file_name());
|
||||||
dbgln("[{}:{} - {}:{}]", message.start_line(), message.start_column(), message.end_line(), message.end_column());
|
dbgln("[{}:{} - {}:{}]", message.start_line(), message.start_column(), message.end_line(), message.end_column());
|
||||||
#endif
|
#endif
|
||||||
auto document = document_for(message.file_name());
|
m_filedb.on_file_edit_remove_text(message.file_name(), message.start_line(), message.start_column(), message.end_line(), message.end_column());
|
||||||
if (!document) {
|
m_autocomplete_engine->on_edit(message.file_name());
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientConnection::handle(const Messages::LanguageServer::AutoCompleteSuggestions& message)
|
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());
|
dbgln("AutoCompleteSuggestions for: {} {}:{}", message.file_name(), message.cursor_line(), message.cursor_column());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
auto document = document_for(message.file_name());
|
auto document = m_filedb.get(message.file_name());
|
||||||
if (!document) {
|
if (!document) {
|
||||||
dbgln("file {} has not been opened", message.file_name());
|
dbgln("file {} has not been opened", message.file_name());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<GUI::AutocompleteProvider::Entry> suggestions;
|
GUI::TextPosition autocomplete_position = { (size_t)message.cursor_line(), (size_t)max(message.cursor_column(), message.cursor_column() - 1) };
|
||||||
switch (m_auto_complete_mode) {
|
Vector<GUI::AutocompleteProvider::Entry> suggestions = m_autocomplete_engine->get_suggestions(message.file_name(), autocomplete_position);
|
||||||
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) });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
post_message(Messages::LanguageClient::AutoCompleteSuggestions(move(suggestions)));
|
post_message(Messages::LanguageClient::AutoCompleteSuggestions(move(suggestions)));
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<GUI::TextDocument> 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)
|
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) {
|
if (!document) {
|
||||||
dbgln("file {} has not been opened", message.file_name());
|
dbgln("file {} has not been opened", message.file_name());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto content = message.content();
|
auto content = message.content();
|
||||||
document->set_text(content.view());
|
document->set_text(content.view());
|
||||||
|
m_autocomplete_engine->on_edit(message.file_name());
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientConnection::handle(const Messages::LanguageServer::SetAutoCompleteMode& message)
|
void ClientConnection::handle(const Messages::LanguageServer::SetAutoCompleteMode& message)
|
||||||
|
@ -195,9 +132,9 @@ void ClientConnection::handle(const Messages::LanguageServer::SetAutoCompleteMod
|
||||||
dbgln("SetAutoCompleteMode: {}", message.mode());
|
dbgln("SetAutoCompleteMode: {}", message.mode());
|
||||||
#endif
|
#endif
|
||||||
if (message.mode() == "Parser")
|
if (message.mode() == "Parser")
|
||||||
m_auto_complete_mode = AutoCompleteMode::Parser;
|
m_autocomplete_engine = make<ParserAutoComplete>(m_filedb);
|
||||||
else
|
else
|
||||||
m_auto_complete_mode = AutoCompleteMode::Lexer;
|
m_autocomplete_engine = make<LexerAutoComplete>(m_filedb);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,10 +26,11 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "AutoCompleteEngine.h"
|
||||||
|
#include "FileDB.h"
|
||||||
#include <AK/HashMap.h>
|
#include <AK/HashMap.h>
|
||||||
#include <AK/LexicalPath.h>
|
#include <AK/LexicalPath.h>
|
||||||
#include <DevTools/HackStudio/AutoCompleteResponse.h>
|
#include <DevTools/HackStudio/AutoCompleteResponse.h>
|
||||||
#include <LibGUI/TextDocument.h>
|
|
||||||
#include <LibIPC/ClientConnection.h>
|
#include <LibIPC/ClientConnection.h>
|
||||||
|
|
||||||
#include <DevTools/HackStudio/LanguageServers/LanguageClientEndpoint.h>
|
#include <DevTools/HackStudio/LanguageServers/LanguageClientEndpoint.h>
|
||||||
|
@ -57,16 +58,8 @@ private:
|
||||||
virtual void handle(const Messages::LanguageServer::AutoCompleteSuggestions&) override;
|
virtual void handle(const Messages::LanguageServer::AutoCompleteSuggestions&) override;
|
||||||
virtual void handle(const Messages::LanguageServer::SetAutoCompleteMode&) override;
|
virtual void handle(const Messages::LanguageServer::SetAutoCompleteMode&) override;
|
||||||
|
|
||||||
RefPtr<GUI::TextDocument> document_for(const String& file_name);
|
FileDB m_filedb;
|
||||||
|
OwnPtr<AutoCompleteEngine> m_autocomplete_engine;
|
||||||
HashMap<String, NonnullRefPtr<GUI::TextDocument>> m_open_files;
|
|
||||||
|
|
||||||
enum class AutoCompleteMode {
|
|
||||||
Lexer,
|
|
||||||
Parser
|
|
||||||
};
|
|
||||||
|
|
||||||
AutoCompleteMode m_auto_complete_mode { AutoCompleteMode::Lexer };
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,8 +31,19 @@
|
||||||
|
|
||||||
namespace LanguageServers::Cpp {
|
namespace LanguageServers::Cpp {
|
||||||
|
|
||||||
Vector<GUI::AutocompleteProvider::Entry> LexerAutoComplete::get_suggestions(const String& code, const GUI::TextPosition& autocomplete_position)
|
LexerAutoComplete::LexerAutoComplete(const FileDB& filedb)
|
||||||
|
: AutoCompleteEngine(filedb)
|
||||||
{
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector<GUI::AutocompleteProvider::Entry> 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);
|
auto lines = code.split('\n', true);
|
||||||
Cpp::Lexer lexer(code);
|
Cpp::Lexer lexer(code);
|
||||||
auto tokens = lexer.lex();
|
auto tokens = lexer.lex();
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "AutoCompleteEngine.h"
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <DevTools/HackStudio/AutoCompleteResponse.h>
|
#include <DevTools/HackStudio/AutoCompleteResponse.h>
|
||||||
|
@ -36,16 +37,16 @@ namespace LanguageServers::Cpp {
|
||||||
|
|
||||||
using namespace ::Cpp;
|
using namespace ::Cpp;
|
||||||
|
|
||||||
class LexerAutoComplete {
|
class LexerAutoComplete : public AutoCompleteEngine {
|
||||||
public:
|
public:
|
||||||
LexerAutoComplete() = delete;
|
LexerAutoComplete(const FileDB& filedb);
|
||||||
|
|
||||||
static Vector<GUI::AutocompleteProvider::Entry> get_suggestions(const String& code, const GUI::TextPosition& autocomplete_position);
|
virtual Vector<GUI::AutocompleteProvider::Entry> get_suggestions(const String& file, const GUI::TextPosition& autocomplete_position) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static Optional<size_t> token_in_position(const Vector<Cpp::Token>&, const GUI::TextPosition&);
|
Optional<size_t> token_in_position(const Vector<Cpp::Token>&, const GUI::TextPosition&);
|
||||||
static StringView text_of_token(const Vector<String>& lines, const Cpp::Token&);
|
StringView text_of_token(const Vector<String>& lines, const Cpp::Token&);
|
||||||
static Vector<GUI::AutocompleteProvider::Entry> identifier_prefixes(const Vector<String>& lines, const Vector<Cpp::Token>&, size_t target_token_index);
|
Vector<GUI::AutocompleteProvider::Entry> identifier_prefixes(const Vector<String>& lines, const Vector<Cpp::Token>&, size_t target_token_index);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue