Skip to content

Commit

Permalink
refactor(lsp): std::vector -> Span
Browse files Browse the repository at this point in the history
add_watch_io_errors and handle_config_file_changes don't *need* a
vector, they just need a list. Have them accept a Span instead to make
this more obvious.
  • Loading branch information
strager committed Oct 27, 2023
1 parent e627145 commit 497c58b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/quick-lint-js/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,8 @@ void run_lsp_server() {
}

void report_pending_watch_io_errors() {
this->handler_.add_watch_io_errors(this->fs_.take_watch_errors());
this->handler_.add_watch_io_errors(
Span<const Watch_IO_Error>(this->fs_.take_watch_errors()));
this->handler_.flush_pending_notifications(this->writer_);
#if QLJS_EVENT_LOOP2_PIPE_WRITE
this->enable_or_disable_writer_events_as_needed();
Expand Down
14 changes: 9 additions & 5 deletions src/quick-lint-js/lsp/lsp-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <simdjson.h>
#include <string>
#include <utility>
#include <vector>

using namespace std::literals::string_view_literals;

Expand Down Expand Up @@ -222,12 +223,13 @@ void Linting_LSP_Server_Handler::filesystem_changed() {
this->config_loader_.refresh();
{
Lock_Ptr<LSP_Documents> documents = this->documents_.lock();
this->handle_config_file_changes(documents, config_changes);
this->handle_config_file_changes(
documents, Span<const Configuration_Change>(config_changes));
}
}

void Linting_LSP_Server_Handler::add_watch_io_errors(
const std::vector<Watch_IO_Error>& errors) {
Span<const Watch_IO_Error> errors) {
if (!errors.empty() && !this->did_report_watch_io_error_) {
Byte_Buffer& out_json = this->outgoing_messages_.new_message();
// clang-format off
Expand Down Expand Up @@ -366,7 +368,8 @@ void Linting_LSP_Server_Handler::handle_text_document_did_change_notification(
case LSP_Documents::Document_Type::config: {
std::vector<Configuration_Change> config_changes =
this->config_loader_.refresh();
this->handle_config_file_changes(documents, config_changes);
this->handle_config_file_changes(
documents, Span<const Configuration_Change>(config_changes));
break;
}

Expand Down Expand Up @@ -517,7 +520,8 @@ void Linting_LSP_Server_Handler::handle_text_document_did_open_notification(
this->config_loader_.refresh();
{
Lock_Ptr<LSP_Documents> documents = this->documents_.lock();
this->handle_config_file_changes(documents, config_changes);
this->handle_config_file_changes(
documents, Span<const Configuration_Change>(config_changes));
}

doc_ptr = std::move(doc);
Expand Down Expand Up @@ -552,7 +556,7 @@ void Linting_LSP_Server_Handler::

void Linting_LSP_Server_Handler::handle_config_file_changes(
Lock_Ptr<LSP_Documents>& documents,
const std::vector<Configuration_Change>& config_changes) {
Span<const Configuration_Change> config_changes) {
for (auto& entry : documents->documents) {
const String8& document_uri = entry.first;
LSP_Documents::Document_Base& doc = *entry.second;
Expand Down
5 changes: 2 additions & 3 deletions src/quick-lint-js/lsp/lsp-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <quick-lint-js/util/narrow-cast.h>
#include <quick-lint-js/util/synchronized.h>
#include <string>
#include <vector>

namespace quick_lint_js {
class Byte_Buffer;
Expand Down Expand Up @@ -150,7 +149,7 @@ class Linting_LSP_Server_Handler final : public JSON_RPC_Message_Handler {
this->outgoing_messages_.send(remote);
}

void add_watch_io_errors(const std::vector<Watch_IO_Error>&);
void add_watch_io_errors(Span<const Watch_IO_Error>);

private:
void handle_initialize_request(::simdjson::ondemand::object& request,
Expand Down Expand Up @@ -197,7 +196,7 @@ class Linting_LSP_Server_Handler final : public JSON_RPC_Message_Handler {

void handle_config_file_changes(
Lock_Ptr<LSP_Documents>& documents,
const std::vector<Configuration_Change>& config_changes);
Span<const Configuration_Change> config_changes);

void get_config_file_diagnostics_notification(Loaded_Config_File*,
String8_View uri_json,
Expand Down
13 changes: 7 additions & 6 deletions test/test-lsp-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <quick-lint-js/lsp/lsp-server.h>
#include <quick-lint-js/lsp/outgoing-json-rpc-message-queue.h>
#include <quick-lint-js/port/char8.h>
#include <quick-lint-js/port/span.h>
#include <quick-lint-js/port/warning.h>
#include <quick-lint-js/spy-lsp-endpoint-remote.h>
#include <quick-lint-js/version.h>
Expand Down Expand Up @@ -2339,7 +2340,7 @@ TEST_F(Test_Linting_LSP_Server,
}

TEST_F(Test_Linting_LSP_Server, showing_io_errors_shows_only_first) {
this->handler->add_watch_io_errors(std::vector<Watch_IO_Error>{
this->handler->add_watch_io_errors(Span<const Watch_IO_Error>({
Watch_IO_Error{
.path = "/banana",
.io_error = generic_file_io_error,
Expand All @@ -2348,7 +2349,7 @@ TEST_F(Test_Linting_LSP_Server, showing_io_errors_shows_only_first) {
.path = "/orange",
.io_error = generic_file_io_error,
},
});
}));
this->handler->flush_pending_notifications(*this->client);

std::vector<TJSON_Value> notifications = this->client->notifications();
Expand All @@ -2366,20 +2367,20 @@ TEST_F(Test_Linting_LSP_Server, showing_io_errors_shows_only_first) {
}

TEST_F(Test_Linting_LSP_Server, showing_io_errors_shows_only_first_ever) {
this->handler->add_watch_io_errors(std::vector<Watch_IO_Error>{
this->handler->add_watch_io_errors(Span<const Watch_IO_Error>({
Watch_IO_Error{
.path = "/banana",
.io_error = generic_file_io_error,
},
});
}));
this->handler->flush_pending_notifications(*this->client);
// Separate call to add_watch_io_errors:
this->handler->add_watch_io_errors(std::vector<Watch_IO_Error>{
this->handler->add_watch_io_errors(Span<const Watch_IO_Error>({
Watch_IO_Error{
.path = "/orange",
.io_error = generic_file_io_error,
},
});
}));
this->handler->flush_pending_notifications(*this->client);

std::vector<TJSON_Value> notifications = this->client->notifications();
Expand Down

0 comments on commit 497c58b

Please sign in to comment.