@@ -149,6 +149,22 @@ void lsp_overlay_configuration_filesystem::close_document(
149
149
QLJS_ASSERT (erased > 0 );
150
150
}
151
151
152
+ byte_buffer& outgoing_lsp_message_queue::new_message () {
153
+ return this ->messages_ .emplace_back ();
154
+ }
155
+
156
+ void outgoing_lsp_message_queue::send (lsp_endpoint_remote& remote) {
157
+ for (byte_buffer& notification_json : this ->messages_ ) {
158
+ if (notification_json.empty ()) {
159
+ // TODO(strager): Fix our tests so they don't make empty
160
+ // byte_buffer-s.
161
+ continue ;
162
+ }
163
+ remote.send_message (std::move (notification_json));
164
+ }
165
+ this ->messages_ .clear ();
166
+ }
167
+
152
168
linting_lsp_server_handler::linting_lsp_server_handler (
153
169
configuration_filesystem* fs, lsp_linter* linter)
154
170
: config_fs_(fs), config_loader_(&this ->config_fs_), linter_(*linter) {
@@ -253,7 +269,7 @@ void linting_lsp_server_handler::filesystem_changed() {
253
269
void linting_lsp_server_handler::add_watch_io_errors (
254
270
const std::vector<watch_io_error>& errors) {
255
271
if (!errors.empty () && !this ->did_report_watch_io_error_ ) {
256
- byte_buffer& out_json = this ->pending_notification_jsons_ . emplace_back ();
272
+ byte_buffer& out_json = this ->outgoing_messages_ . new_message ();
257
273
// clang-format off
258
274
out_json.append_copy (u8R"--( {)--"
259
275
u8R"--( "jsonrpc":"2.0",)--"
@@ -309,7 +325,7 @@ void linting_lsp_server_handler::handle_workspace_configuration_response(
309
325
}
310
326
311
327
void linting_lsp_server_handler::handle_initialized_notification () {
312
- byte_buffer& request_json = this ->pending_notification_jsons_ . emplace_back ();
328
+ byte_buffer& request_json = this ->outgoing_messages_ . new_message ();
313
329
this ->workspace_configuration_ .build_request (initial_configuration_request_id,
314
330
request_json);
315
331
}
@@ -366,8 +382,7 @@ void linting_lsp_server_handler::config_document::on_text_changed(
366
382
367
383
void linting_lsp_server_handler::lintable_document::on_text_changed (
368
384
linting_lsp_server_handler& handler, string8_view document_uri_json) {
369
- byte_buffer& notification_json =
370
- handler.pending_notification_jsons_ .emplace_back ();
385
+ byte_buffer& notification_json = handler.outgoing_messages_ .new_message ();
371
386
handler.linter_ .lint_and_get_diagnostics_notification (
372
387
*this , document_uri_json, notification_json);
373
388
}
@@ -455,8 +470,7 @@ void linting_lsp_server_handler::handle_text_document_did_open_notification(
455
470
if (*config_file) {
456
471
doc->config = &(*config_file)->config ;
457
472
if (!(*config_file)->errors .empty ()) {
458
- byte_buffer& message_json =
459
- this ->pending_notification_jsons_ .emplace_back ();
473
+ byte_buffer& message_json = this ->outgoing_messages_ .new_message ();
460
474
this ->write_configuration_errors_notification (
461
475
document_path, *config_file, message_json);
462
476
}
@@ -465,13 +479,11 @@ void linting_lsp_server_handler::handle_text_document_did_open_notification(
465
479
}
466
480
} else {
467
481
doc->config = &this ->default_config_ ;
468
- byte_buffer& message_json =
469
- this ->pending_notification_jsons_ .emplace_back ();
482
+ byte_buffer& message_json = this ->outgoing_messages_ .new_message ();
470
483
this ->write_configuration_loader_error_notification (
471
484
document_path, config_file.error_to_string (), message_json);
472
485
}
473
- byte_buffer& notification_json =
474
- this ->pending_notification_jsons_ .emplace_back ();
486
+ byte_buffer& notification_json = this ->outgoing_messages_ .new_message ();
475
487
this ->linter_ .lint_and_get_diagnostics_notification (*doc, uri->json ,
476
488
notification_json);
477
489
@@ -485,7 +497,7 @@ void linting_lsp_server_handler::handle_text_document_did_open_notification(
485
497
/* token=*/ doc.get ());
486
498
QLJS_ASSERT (config_file.ok ());
487
499
byte_buffer& config_diagnostics_json =
488
- this ->pending_notification_jsons_ . emplace_back ();
500
+ this ->outgoing_messages_ . new_message ();
489
501
this ->get_config_file_diagnostics_notification (
490
502
*config_file, uri->json , doc->version_json , config_diagnostics_json);
491
503
@@ -546,7 +558,7 @@ void linting_lsp_server_handler::config_document::on_config_file_changed(
546
558
QLJS_ASSERT (change.config_file );
547
559
if (change.config_file ) {
548
560
byte_buffer& config_diagnostics_json =
549
- handler.pending_notification_jsons_ . emplace_back ();
561
+ handler.outgoing_messages_ . new_message ();
550
562
handler.get_config_file_diagnostics_notification (
551
563
change.config_file , to_json_escaped_string_with_quotes (document_uri),
552
564
this ->version_json , config_diagnostics_json);
@@ -562,16 +574,14 @@ void linting_lsp_server_handler::lintable_document::on_config_file_changed(
562
574
QLJS_UNIMPLEMENTED ();
563
575
}
564
576
if (change.error ) {
565
- byte_buffer& message_json =
566
- handler.pending_notification_jsons_ .emplace_back ();
577
+ byte_buffer& message_json = handler.outgoing_messages_ .new_message ();
567
578
handler.write_configuration_loader_error_notification (
568
579
document_path, change.error ->error_to_string (), message_json);
569
580
}
570
581
configuration* config = change.config_file ? &change.config_file ->config
571
582
: &handler.default_config_ ;
572
583
this ->config = config;
573
- byte_buffer& notification_json =
574
- handler.pending_notification_jsons_ .emplace_back ();
584
+ byte_buffer& notification_json = handler.outgoing_messages_ .new_message ();
575
585
// TODO(strager): Don't copy document_uri if it contains only non-special
576
586
// characters.
577
587
// TODO(strager): Cache the result of to_json_escaped_string?
0 commit comments