Skip to content

Commit 89a90b6

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

File tree

1 file changed

+71
-48
lines changed

1 file changed

+71
-48
lines changed

server/src/translation.rs

Lines changed: 71 additions & 48 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,13 +236,13 @@ 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.
244245
pub static ref EOL_FINDER: Regex = Regex::new("[^\r\n]*(\r?\n)").unwrap();
245-
246246
}
247247

248248
// Data structures
@@ -404,7 +404,8 @@ pub async fn translation_task(
404404
HashMap::new();
405405
debug!("VSCode processing task started.");
406406

407-
// Create a queue for HTTP requests fo communicate with this task.
407+
// Create a queue for HTTP requests fo communicate with this
408+
// task.
408409
let (from_http_tx, mut from_http_rx) = mpsc::channel(10);
409410
app_state_task
410411
.processing_task_queue_tx
@@ -416,41 +417,41 @@ pub async fn translation_task(
416417
let mut id: f64 = INITIAL_MESSAGE_ID + MESSAGE_ID_INCREMENT;
417418
let mut source_code = String::new();
418419
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.
420+
// The initial state will be overwritten by the first `Update`
421+
// or `LoadFile`, so this value doesn't matter.
421422
let mut eol = EolType::Lf;
422423
// Some means this contains valid HTML; None means don't use it
423424
// (since it would have contained Markdown).
424425
let mut code_mirror_doc_blocks = Some(Vec::new());
425426
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:
427+
// To send a diff from Server to Client or vice versa, we need
428+
// to ensure they are in sync:
428429
//
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.
430+
// 1. IDE update -> Server -> Client or Client update -> Server
431+
// -> IDE: the Server and Client sync is pending. Client
432+
// response -> Server -> IDE or IDE response -> Server ->
433+
// Client: the Server and Client are synced.
434+
// 2. IDE current file -> Server -> Client or Client current
435+
// file -> Server -> IDE: Out of sync.
435436
//
436437
// 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.
438+
// achieved. So, we need to track the ID of the most recent IDE
439+
// -> Client update or Client -> IDE update, if one is in
440+
// flight. When complete, mark the connection as synchronized.
441+
// Since all IDs are unique, we can use a single variable to
442+
// store the ID.
441443
//
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.
444+
// Currently, when the Client sends an update, mark the
445+
// connection as out of sync, since the update contains not HTML
446+
// in the doc blocks, but Markdown. When Turndown is moved from
447+
// JavaScript to Rust, this can be changed, since both sides
448+
// will have HTML in the doc blocks.
447449
let mut sync_state = SyncState::OutOfSync;
448450
loop {
449451
select! {
450452
// Look for messages from the IDE.
451453
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())]);
454+
debug!("Received IDE message id = {}, message = {}", ide_message.id, debug_shorten(&ide_message.message));
454455
match ide_message.message {
455456
// Handle messages that the IDE must not send.
456457
EditorMessageContents::Opened(_) |
@@ -462,7 +463,8 @@ pub async fn translation_task(
462463
send_response(&to_ide_tx, ide_message.id, Err(msg.to_string())).await;
463464
},
464465

465-
// Handle messages that are simply passed through.
466+
// Handle messages that are simply passed
467+
// through.
466468
EditorMessageContents::Closed |
467469
EditorMessageContents::RequestClose => {
468470
debug!("Forwarding it to the Client.");
@@ -481,9 +483,10 @@ pub async fn translation_task(
481483
ResultOkTypes::LoadFile(_) => true,
482484
}
483485
};
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).
486+
// Pass the message to the client if this
487+
// isn't a `LoadFile` result (the only type
488+
// of result which the Server should
489+
// handle).
487490
if !is_loadfile {
488491
debug!("Forwarding it to the Client.");
489492
// If this was confirmation from the IDE
@@ -502,8 +505,9 @@ pub async fn translation_task(
502505
break 'task;
503506
};
504507

505-
// Take ownership of the result after sending it
506-
// above (which requires ownership).
508+
// Take ownership of the result after
509+
// sending it above (which requires
510+
// ownership).
507511
let EditorMessageContents::Result(result) = ide_message.message else {
508512
error!("{}", "Not a result.");
509513
break;
@@ -522,9 +526,9 @@ pub async fn translation_task(
522526
};
523527

524528
// 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").
529+
// doesn't have a PDF viewer, determine if
530+
// this is a PDF file. (TODO: look at the
531+
// magic number also -- "%PDF").
528532
let use_pdf_js = http_request.file_path.extension() == Some(OsStr::new("pdf"));
529533
let (simple_http_response, option_update, file_contents) = match file_contents_option {
530534
Some(file_contents) => {
@@ -578,8 +582,8 @@ pub async fn translation_task(
578582
error!("Not plain!");
579583
break;
580584
};
581-
// We must clone here, since the original is
582-
// placed in the TX queue.
585+
// We must clone here, since the original
586+
// is placed in the TX queue.
583587
source_code = file_contents.unwrap();
584588
code_mirror_doc = plain.doc.clone();
585589
code_mirror_doc_blocks = Some(plain.doc_blocks.clone());
@@ -731,8 +735,8 @@ pub async fn translation_task(
731735
}
732736
}
733737

734-
// Update the current file; translate it to a URL
735-
// then pass it to the Client.
738+
// Update the current file; translate it to a
739+
// URL then pass it to the Client.
736740
EditorMessageContents::CurrentFile(file_path, _is_text) => {
737741
debug!("Translating and forwarding it to the Client.");
738742
match try_canonicalize(&file_path) {
@@ -768,18 +772,19 @@ pub async fn translation_task(
768772
id,
769773
message: EditorMessageContents::LoadFile(http_request.file_path.clone())
770774
}));
771-
// Store the ID and request, which are needed to send a
772-
// response when the `LoadFile` result is received.
775+
// Store the ID and request, which are needed to
776+
// send a response when the `LoadFile` result is
777+
// received.
773778
load_file_requests.insert(id.to_bits(), http_request);
774779
id += MESSAGE_ID_INCREMENT;
775780
}
776781

777782
// Handle messages from the client.
778783
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())]);
784+
debug!("Received Client message id = {}, message = {}", client_message.id, debug_shorten(&client_message.message));
781785
match client_message.message {
782-
// Handle messages that the client must not send.
786+
// Handle messages that the client must not
787+
// send.
783788
EditorMessageContents::Opened(_) |
784789
EditorMessageContents::LoadFile(_) |
785790
EditorMessageContents::RequestClose |
@@ -789,7 +794,8 @@ pub async fn translation_task(
789794
send_response(&to_client_tx, client_message.id, Err(msg.to_string())).await;
790795
},
791796

792-
// Handle messages that are simply passed through.
797+
// Handle messages that are simply passed
798+
// through.
793799
EditorMessageContents::Closed |
794800
EditorMessageContents::Result(_) => {
795801
debug!("Forwarding it to the IDE.");
@@ -804,8 +810,9 @@ pub async fn translation_task(
804810

805811
// Open a web browser when requested.
806812
EditorMessageContents::OpenUrl(url) => {
807-
// This doesn't work in Codespaces. TODO: send
808-
// this back to the VSCode window, then call
813+
// This doesn't work in Codespaces. TODO:
814+
// send this back to the VSCode window, then
815+
// call
809816
// `vscode.env.openExternal(vscode.Uri.parse(url))`.
810817
if let Err(err) = webbrowser::open(&url) {
811818
let msg = format!("Unable to open web browser to URL {url}: {err}");
@@ -898,8 +905,8 @@ pub async fn translation_task(
898905
}
899906
},
900907

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

0 commit comments

Comments
 (0)