Skip to content

Commit dcde0e4

Browse files
committed
Fix: correct debug prints.
1 parent d4e034a commit dcde0e4

File tree

1 file changed

+68
-47
lines changed

1 file changed

+68
-47
lines changed

server/src/translation.rs

Lines changed: 68 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@
205205
// -------
206206
//
207207
// ### Standard library
208-
use std::{cmp::min, collections::HashMap, ffi::OsStr, path::PathBuf};
208+
use std::{collections::HashMap, ffi::OsStr, fmt::Debug, path::PathBuf};
209209

210210
// ### Third-party
211211
use actix_web::web;
@@ -236,8 +236,9 @@ use crate::{
236236

237237
// Globals
238238
// -------
239+
//
239240
// The max length of a message to show in the console.
240-
const MAX_MESSAGE_LENGTH: usize = 300;
241+
const MAX_MESSAGE_LENGTH: usize = 30000;
241242

242243
lazy_static! {
243244
/// A regex to determine the type of the first EOL. See 'PROCESSINGS1.
@@ -404,7 +405,8 @@ pub async fn translation_task(
404405
HashMap::new();
405406
debug!("VSCode processing task started.");
406407

407-
// Create a queue for HTTP requests fo communicate with this task.
408+
// Create a queue for HTTP requests fo communicate with this
409+
// task.
408410
let (from_http_tx, mut from_http_rx) = mpsc::channel(10);
409411
app_state_task
410412
.processing_task_queue_tx
@@ -416,41 +418,41 @@ pub async fn translation_task(
416418
let mut id: f64 = INITIAL_MESSAGE_ID + MESSAGE_ID_INCREMENT;
417419
let mut source_code = String::new();
418420
let mut code_mirror_doc = String::new();
419-
// The initial state will be overwritten by the first `Update` or
420-
// `LoadFile`, so this value doesn't matter.
421+
// The initial state will be overwritten by the first `Update`
422+
// or `LoadFile`, so this value doesn't matter.
421423
let mut eol = EolType::Lf;
422424
// Some means this contains valid HTML; None means don't use it
423425
// (since it would have contained Markdown).
424426
let mut code_mirror_doc_blocks = Some(Vec::new());
425427
let prefix_str = "/".to_string() + &prefix.join("/");
426-
// To send a diff from Server to Client or vice versa, we need to
427-
// ensure they are in sync:
428+
// To send a diff from Server to Client or vice versa, we need
429+
// to ensure they are in sync:
428430
//
429-
// 1. IDE update -> Server -> Client or Client update -> Server ->
430-
// IDE: the Server and Client sync is pending. Client response
431-
// -> Server -> IDE or IDE response -> Server -> Client: the
432-
// Server and Client are synced.
433-
// 2. IDE current file -> Server -> Client or Client current file
434-
// -> Server -> IDE: Out of sync.
431+
// 1. IDE update -> Server -> Client or Client update -> Server
432+
// -> IDE: the Server and Client sync is pending. Client
433+
// response -> Server -> IDE or IDE response -> Server ->
434+
// Client: the Server and Client are synced.
435+
// 2. IDE current file -> Server -> Client or Client current
436+
// file -> Server -> IDE: Out of sync.
435437
//
436438
// It's only safe to send a diff when the most recent sync is
437-
// achieved. So, we need to track the ID of the most recent IDE ->
438-
// Client update or Client -> IDE update, if one is in flight. When
439-
// complete, mark the connection as synchronized. Since all IDs are
440-
// unique, we can use a single variable to store the ID.
439+
// achieved. So, we need to track the ID of the most recent IDE
440+
// -> Client update or Client -> IDE update, if one is in
441+
// flight. When complete, mark the connection as synchronized.
442+
// Since all IDs are unique, we can use a single variable to
443+
// store the ID.
441444
//
442-
// Currently, when the Client sends an update, mark the connection
443-
// as out of sync, since the update contains not HTML in the doc
444-
// blocks, but Markdown. When Turndown is moved from JavaScript to
445-
// Rust, this can be changed, since both sides will have HTML in the
446-
// doc blocks.
445+
// Currently, when the Client sends an update, mark the
446+
// connection as out of sync, since the update contains not HTML
447+
// in the doc blocks, but Markdown. When Turndown is moved from
448+
// JavaScript to Rust, this can be changed, since both sides
449+
// will have HTML in the doc blocks.
447450
let mut sync_state = SyncState::OutOfSync;
448451
loop {
449452
select! {
450453
// Look for messages from the IDE.
451454
Some(ide_message) = from_ide_rx.recv() => {
452-
let msg = format!("{:?}", ide_message.message);
453-
debug!("Received IDE message id = {}, message = {}", ide_message.id, &msg[..min(MAX_MESSAGE_LENGTH, msg.len())]);
455+
debug!("Received IDE message id = {}, message = {}", ide_message.id, debug_shorten(&ide_message.message));
454456
match ide_message.message {
455457
// Handle messages that the IDE must not send.
456458
EditorMessageContents::Opened(_) |
@@ -462,7 +464,8 @@ pub async fn translation_task(
462464
send_response(&to_ide_tx, ide_message.id, Err(msg.to_string())).await;
463465
},
464466

465-
// Handle messages that are simply passed through.
467+
// Handle messages that are simply passed
468+
// through.
466469
EditorMessageContents::Closed |
467470
EditorMessageContents::RequestClose => {
468471
debug!("Forwarding it to the Client.");
@@ -481,9 +484,10 @@ pub async fn translation_task(
481484
ResultOkTypes::LoadFile(_) => true,
482485
}
483486
};
484-
// Pass the message to the client if this isn't
485-
// a `LoadFile` result (the only type of result
486-
// which the Server should handle).
487+
// Pass the message to the client if this
488+
// isn't a `LoadFile` result (the only type
489+
// of result which the Server should
490+
// handle).
487491
if !is_loadfile {
488492
debug!("Forwarding it to the Client.");
489493
// If this was confirmation from the IDE
@@ -502,8 +506,9 @@ pub async fn translation_task(
502506
break 'task;
503507
};
504508

505-
// Take ownership of the result after sending it
506-
// above (which requires ownership).
509+
// Take ownership of the result after
510+
// sending it above (which requires
511+
// ownership).
507512
let EditorMessageContents::Result(result) = ide_message.message else {
508513
error!("{}", "Not a result.");
509514
break;
@@ -522,9 +527,9 @@ pub async fn translation_task(
522527
};
523528

524529
// Process the file contents. Since VSCode
525-
// doesn't have a PDF viewer, determine if this
526-
// is a PDF file. (TODO: look at the magic
527-
// number also -- "%PDF").
530+
// doesn't have a PDF viewer, determine if
531+
// this is a PDF file. (TODO: look at the
532+
// magic number also -- "%PDF").
528533
let use_pdf_js = http_request.file_path.extension() == Some(OsStr::new("pdf"));
529534
let (simple_http_response, option_update, file_contents) = match file_contents_option {
530535
Some(file_contents) => {
@@ -578,8 +583,8 @@ pub async fn translation_task(
578583
error!("Not plain!");
579584
break;
580585
};
581-
// We must clone here, since the original is
582-
// placed in the TX queue.
586+
// We must clone here, since the original
587+
// is placed in the TX queue.
583588
source_code = file_contents.unwrap();
584589
code_mirror_doc = plain.doc.clone();
585590
code_mirror_doc_blocks = Some(plain.doc_blocks.clone());
@@ -731,8 +736,8 @@ pub async fn translation_task(
731736
}
732737
}
733738

734-
// Update the current file; translate it to a URL
735-
// then pass it to the Client.
739+
// Update the current file; translate it to a
740+
// URL then pass it to the Client.
736741
EditorMessageContents::CurrentFile(file_path, _is_text) => {
737742
debug!("Translating and forwarding it to the Client.");
738743
match try_canonicalize(&file_path) {
@@ -768,18 +773,19 @@ pub async fn translation_task(
768773
id,
769774
message: EditorMessageContents::LoadFile(http_request.file_path.clone())
770775
}));
771-
// Store the ID and request, which are needed to send a
772-
// response when the `LoadFile` result is received.
776+
// Store the ID and request, which are needed to
777+
// send a response when the `LoadFile` result is
778+
// received.
773779
load_file_requests.insert(id.to_bits(), http_request);
774780
id += MESSAGE_ID_INCREMENT;
775781
}
776782

777783
// Handle messages from the client.
778784
Some(client_message) = from_client_rx.recv() => {
779-
let msg = format!("{:?}", client_message.message);
780-
debug!("Received Client message id = {}, message = {}", client_message.id, &msg[..min(MAX_MESSAGE_LENGTH, msg.len())]);
785+
debug!("Received Client message id = {}, message = {}", client_message.id, debug_shorten(&client_message.message));
781786
match client_message.message {
782-
// Handle messages that the client must not send.
787+
// Handle messages that the client must not
788+
// send.
783789
EditorMessageContents::Opened(_) |
784790
EditorMessageContents::LoadFile(_) |
785791
EditorMessageContents::RequestClose |
@@ -789,7 +795,8 @@ pub async fn translation_task(
789795
send_response(&to_client_tx, client_message.id, Err(msg.to_string())).await;
790796
},
791797

792-
// Handle messages that are simply passed through.
798+
// Handle messages that are simply passed
799+
// through.
793800
EditorMessageContents::Closed |
794801
EditorMessageContents::Result(_) => {
795802
debug!("Forwarding it to the IDE.");
@@ -804,8 +811,9 @@ pub async fn translation_task(
804811

805812
// Open a web browser when requested.
806813
EditorMessageContents::OpenUrl(url) => {
807-
// This doesn't work in Codespaces. TODO: send
808-
// this back to the VSCode window, then call
814+
// This doesn't work in Codespaces. TODO:
815+
// send this back to the VSCode window, then
816+
// call
809817
// `vscode.env.openExternal(vscode.Uri.parse(url))`.
810818
if let Err(err) = webbrowser::open(&url) {
811819
let msg = format!("Unable to open web browser to URL {url}: {err}");
@@ -898,8 +906,8 @@ pub async fn translation_task(
898906
}
899907
},
900908

901-
// Update the current file; translate it to a URL
902-
// then pass it to the IDE.
909+
// Update the current file; translate it to a
910+
// URL then pass it to the IDE.
903911
EditorMessageContents::CurrentFile(url_string, _is_text) => {
904912
debug!("Forwarding translated path to IDE.");
905913
let result = match url_to_path(&url_string, prefix) {
@@ -997,3 +1005,16 @@ fn eol_convert(s: String, eol_type: &EolType) -> String {
9971005
s
9981006
}
9991007
}
1008+
1009+
// Provide a simple debug function that prints only the first
1010+
// `MAX_MESSAGE_LENGTH` characters of the provided value.
1011+
fn debug_shorten<T: Debug>(val: T) -> String {
1012+
#[cfg(debug_assertions)]
1013+
let msg = format!("{:?}", val);
1014+
let max_index = msg
1015+
.char_indices()
1016+
.nth(MAX_MESSAGE_LENGTH)
1017+
.unwrap_or((msg.len(), 'x'))
1018+
.0;
1019+
msg[..max_index].to_string()
1020+
}

0 commit comments

Comments
 (0)