Skip to content

Commit 710c574

Browse files
committed
refactor(lsp): split long function into parsing func + logic func
1 parent fa815ea commit 710c574

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

src/quick-lint-js/lsp/lsp-server.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ namespace {
4040
constexpr lsp_endpoint_handler::request_id_type
4141
initial_configuration_request_id = 1;
4242

43-
struct string_json_token {
44-
string8_view data;
45-
string8_view json;
46-
};
47-
4843
// Returns std::nullopt on failure (e.g. missing key or not a string).
4944
std::optional<string_json_token> maybe_get_string_token(
5045
::simdjson::ondemand::value& string);
@@ -437,7 +432,18 @@ void linting_lsp_server_handler::handle_text_document_did_open_notification(
437432
return;
438433
}
439434

440-
std::string document_path = parse_file_from_lsp_uri(uri->data);
435+
this->handle_text_document_did_open_notification(
436+
lsp_text_document_did_open_notification{
437+
.language_id = language_id,
438+
.uri = *uri,
439+
.version_json = get_raw_json(version),
440+
.text = text,
441+
});
442+
}
443+
444+
void linting_lsp_server_handler::handle_text_document_did_open_notification(
445+
const lsp_text_document_did_open_notification& notification) {
446+
std::string document_path = parse_file_from_lsp_uri(notification.uri.data);
441447
if (document_path.empty()) {
442448
// TODO(strager): Report a warning and use a default configuration.
443449
QLJS_UNIMPLEMENTED();
@@ -446,12 +452,12 @@ void linting_lsp_server_handler::handle_text_document_did_open_notification(
446452
auto init_document = [&](document_base& doc) {
447453
this->config_fs_.open_document(document_path, &doc.doc);
448454

449-
doc.doc.set_text(text);
450-
doc.version_json = get_raw_json(version);
455+
doc.doc.set_text(notification.text);
456+
doc.version_json = notification.version_json;
451457
};
452458

453459
std::unique_ptr<document_base> doc_ptr;
454-
if (const lsp_language* lang = lsp_language::find(language_id)) {
460+
if (const lsp_language* lang = lsp_language::find(notification.language_id)) {
455461
auto doc = std::make_unique<lintable_document>();
456462
init_document(*doc);
457463
doc->lint_options = lang->lint_options;
@@ -476,7 +482,7 @@ void linting_lsp_server_handler::handle_text_document_did_open_notification(
476482
this->write_configuration_loader_error_notification(
477483
document_path, config_file.error_to_string(), message_json);
478484
}
479-
this->linter_.lint(*doc, uri->json, this->outgoing_messages_);
485+
this->linter_.lint(*doc, notification.uri.json, this->outgoing_messages_);
480486

481487
doc_ptr = std::move(doc);
482488
} else if (this->config_loader_.is_config_file_path(document_path)) {
@@ -490,7 +496,8 @@ void linting_lsp_server_handler::handle_text_document_did_open_notification(
490496
byte_buffer& config_diagnostics_json =
491497
this->outgoing_messages_.new_message();
492498
this->get_config_file_diagnostics_notification(
493-
*config_file, uri->json, doc->version_json, config_diagnostics_json);
499+
*config_file, notification.uri.json, doc->version_json,
500+
config_diagnostics_json);
494501

495502
std::vector<configuration_change> config_changes =
496503
this->config_loader_.refresh();
@@ -504,7 +511,7 @@ void linting_lsp_server_handler::handle_text_document_did_open_notification(
504511

505512
// If the document already exists, deallocate that document_base and use ours.
506513
// TODO(strager): Should we report a warning if a document already existed?
507-
this->documents_[string8(uri->data)] = std::move(doc_ptr);
514+
this->documents_[string8(notification.uri.data)] = std::move(doc_ptr);
508515
}
509516

510517
void linting_lsp_server_handler::

src/quick-lint-js/lsp/lsp-server.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <quick-lint-js/lsp/lsp-workspace-configuration.h>
2525
#include <quick-lint-js/port/char8.h>
2626
#include <quick-lint-js/simdjson-fwd.h>
27+
#include <quick-lint-js/simdjson.h>
2728
#include <quick-lint-js/util/narrow-cast.h>
2829
#include <string>
2930
#include <vector>
@@ -161,8 +162,18 @@ class linting_lsp_server_handler final : public lsp_endpoint_handler {
161162
::simdjson::ondemand::object& request);
162163
void handle_text_document_did_close_notification(
163164
::simdjson::ondemand::object& request);
165+
166+
struct lsp_text_document_did_open_notification {
167+
std::string_view language_id;
168+
string_json_token uri;
169+
string8_view version_json;
170+
string8_view text;
171+
};
164172
void handle_text_document_did_open_notification(
165173
::simdjson::ondemand::object& request);
174+
void handle_text_document_did_open_notification(
175+
const lsp_text_document_did_open_notification&);
176+
166177
void handle_workspace_did_change_configuration_notification(
167178
::simdjson::ondemand::object& request);
168179

src/quick-lint-js/simdjson.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
#include <string_view>
1010

1111
namespace quick_lint_js {
12+
struct string_json_token {
13+
// The string data.
14+
string8_view data;
15+
// The string data in JSON format.
16+
string8_view json;
17+
};
18+
1219
string8_view get_raw_json(::simdjson::ondemand::value&);
1320

1421
// Returns true on success.

0 commit comments

Comments
 (0)