Skip to content

Commit 0ac7ff1

Browse files
committed
wip - Fix: update Client with changes produced during translation.
1 parent 2c346cc commit 0ac7ff1

File tree

3 files changed

+102
-58
lines changed

3 files changed

+102
-58
lines changed

server/src/translation.rs

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -865,45 +865,19 @@ impl TranslationTask {
865865
// Send the new translated contents.
866866
debug!("Sending translated contents to Client.");
867867
let CodeMirrorDiffable::Plain(
868-
ref ccfw_source_plain,
868+
ref code_mirror_translated,
869869
) = ccfw.source
870870
else {
871871
panic!("Unexpected diff value.");
872872
};
873873
// Send a diff if possible.
874-
let client_contents = if let Some(ref cmdb) =
875-
self.code_mirror_doc_blocks
876-
&& self.sent_full
877-
{
878-
let doc_diff = diff_str(
879-
&self.code_mirror_doc,
880-
&ccfw_source_plain.doc,
881-
);
882-
let code_mirror_diff =
883-
diff_code_mirror_doc_blocks(
884-
cmdb,
885-
&ccfw_source_plain.doc_blocks,
886-
);
887-
CodeChatForWeb {
888-
// Clone needed here, so we can copy it
889-
// later.
890-
metadata: ccfw.metadata.clone(),
891-
source: CodeMirrorDiffable::Diff(
892-
CodeMirrorDiff {
893-
doc: doc_diff,
894-
doc_blocks: code_mirror_diff,
895-
// The diff was made between the current version (`version`) and the new version (`contents.version`).
896-
version: self.version,
897-
},
898-
),
899-
version: ccfw.version,
900-
}
874+
let client_contents = if self.sent_full {
875+
self.wrap_translation(
876+
&ccfw,
877+
code_mirror_translated,
878+
)
901879
} else {
902880
self.sent_full = true;
903-
// We must make a clone to put in the TX
904-
// queue; this allows us to keep the
905-
// original below to use with the next
906-
// diff.
907881
ccfw.clone()
908882
};
909883
queue_send_func!(self.to_client_tx.send(EditorMessage {
@@ -918,15 +892,16 @@ impl TranslationTask {
918892
// Update to the latest code after
919893
// computing diffs. To avoid ownership
920894
// problems, re-define `ccfw_source_plain`.
921-
let CodeMirrorDiffable::Plain(ccfw_source_plain) =
922-
ccfw.source
895+
let CodeMirrorDiffable::Plain(
896+
code_mirror_translated,
897+
) = ccfw.source
923898
else {
924899
panic!("{}", "Unexpected diff value.");
925900
};
926901
self.source_code = code_mirror.doc;
927-
self.code_mirror_doc = ccfw_source_plain.doc;
902+
self.code_mirror_doc = code_mirror_translated.doc;
928903
self.code_mirror_doc_blocks =
929-
Some(ccfw_source_plain.doc_blocks);
904+
Some(code_mirror_translated.doc_blocks);
930905
// Update to the version of the file just sent.
931906
self.version = contents.version;
932907
Ok(ResultOkTypes::Void)
@@ -983,6 +958,32 @@ impl TranslationTask {
983958
true
984959
}
985960

961+
/// Given contents translated from `ccfw` to `code_mirror_translated`, return a `CodeChatForWeb` with these translated contents, using a diff.
962+
fn wrap_translation(
963+
&self,
964+
ccfw: &CodeChatForWeb,
965+
code_mirror_translated: &CodeMirror,
966+
) -> CodeChatForWeb {
967+
assert!(self.sent_full);
968+
let doc_diff = diff_str(&self.code_mirror_doc, &code_mirror_translated.doc);
969+
let Some(ref cmdb) = self.code_mirror_doc_blocks else {
970+
panic!("Should have diff of doc blocks!");
971+
};
972+
let doc_blocks_diff = diff_code_mirror_doc_blocks(cmdb, &code_mirror_translated.doc_blocks);
973+
CodeChatForWeb {
974+
// Clone needed here, so we can copy it
975+
// later.
976+
metadata: ccfw.metadata.clone(),
977+
source: CodeMirrorDiffable::Diff(CodeMirrorDiff {
978+
doc: doc_diff,
979+
doc_blocks: doc_blocks_diff,
980+
// The diff was made between the current version (`version`) and the new version (`contents.version`).
981+
version: self.version,
982+
}),
983+
version: ccfw.version,
984+
}
985+
}
986+
986987
async fn client_update(&mut self, client_message: EditorMessage) -> bool {
987988
let EditorMessageContents::Update(update_message_contents) = client_message.message else {
988989
panic!("Expected update message.");
@@ -1000,6 +1001,34 @@ impl TranslationTask {
10001001
None => None,
10011002
Some(cfw) => match codechat_for_web_to_source(&cfw) {
10021003
Ok(new_source_code) => {
1004+
// Translate back to the Client to see if there are any changes after this conversion.
1005+
if let Ok(ccfws) = source_to_codechat_for_web_string(
1006+
&new_source_code,
1007+
&clean_file_path,
1008+
cfw.version,
1009+
false,
1010+
) && let TranslationResultsString::CodeChat(ref ccfw) = ccfws.0
1011+
&& let CodeMirrorDiffable::Plain(ref code_mirror_translated) =
1012+
ccfw.source
1013+
&& self.sent_full
1014+
{
1015+
// Compute the diff.
1016+
let ccfw_client_diff =
1017+
self.wrap_translation(ccfw, code_mirror_translated);
1018+
let CodeMirrorDiffable::Diff(client_contents) =
1019+
ccfw_client_diff.source
1020+
else {
1021+
panic!("Expected diff.");
1022+
};
1023+
if !client_contents.doc.is_empty()
1024+
|| !client_contents.doc_blocks.is_empty()
1025+
{
1026+
// Translating back to the client produced a non-empty diff. Send this to the client.
1027+
println!(
1028+
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
1029+
);
1030+
}
1031+
};
10031032
// Correct EOL endings for use with the
10041033
// IDE.
10051034
let new_source_code_eol = eol_convert(new_source_code, &self.eol);
@@ -1035,10 +1064,7 @@ impl TranslationTask {
10351064
panic!("Diff not supported.");
10361065
};
10371066
self.code_mirror_doc = cmd.doc;
1038-
// TODO: instead of `cmd.doc_blocks`, use
1039-
// `None` to indicate that the doc blocks
1040-
// contain Markdown instead of HTML.
1041-
self.code_mirror_doc_blocks = None;
1067+
self.code_mirror_doc_blocks = Some(cmd.doc_blocks);
10421068
ccfw
10431069
}
10441070
Err(message) => {
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# Test updates in the client that modify the client after appending to a line.
2-
def foo():
3-
A comment
4-
print()
1+
# The contents of this file don't matter -- tests will supply the content,
2+
# instead of loading it from disk. However, it does need to exist for
3+
# `canonicalize` to find the correct path to this file.

server/tests/overall_core/mod.rs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ use std::{
6060
use assert_fs::TempDir;
6161
use dunce::canonicalize;
6262
use futures::FutureExt;
63+
use indoc::indoc;
6364
use pretty_assertions::assert_eq;
6465
use thirtyfour::{
6566
By, ChromiumLikeCapabilities, DesiredCapabilities, Key, WebDriver, error::WebDriverError,
@@ -1035,7 +1036,6 @@ async fn test_client_core(
10351036
Ok(())
10361037
}
10371038

1038-
/* TODO: fails until self-updates work.
10391039
mod test3 {
10401040
use super::*;
10411041
use pretty_assertions::assert_eq;
@@ -1084,8 +1084,23 @@ async fn test_client_updates_core(
10841084
.await;
10851085

10861086
// Respond to the load request.
1087+
let ide_version = 0.0;
10871088
codechat_server
1088-
.send_result_loadfile(server_id, None)
1089+
.send_result_loadfile(
1090+
server_id,
1091+
Some((
1092+
indoc!(
1093+
"
1094+
# Test updates in the client that modify the client after appending to a line.
1095+
def foo():
1096+
A comment
1097+
print()
1098+
"
1099+
)
1100+
.to_string(),
1101+
ide_version,
1102+
)),
1103+
)
10891104
.await
10901105
.unwrap();
10911106

@@ -1132,9 +1147,11 @@ async fn test_client_updates_core(
11321147
.unwrap();
11331148

11341149
// Verify the updated text.
1150+
let msg = codechat_server.get_message_timeout(TIMEOUT).await.unwrap();
1151+
let client_version = get_version(&msg);
11351152
let mut client_id = INITIAL_CLIENT_MESSAGE_ID;
11361153
assert_eq!(
1137-
codechat_server.get_message_timeout(TIMEOUT).await.unwrap(),
1154+
msg,
11381155
EditorMessage {
11391156
id: client_id,
11401157
message: EditorMessageContents::Update(UpdateMessageContents {
@@ -1149,19 +1166,18 @@ async fn test_client_updates_core(
11491166
to: None,
11501167
insert: "# testing\n".to_string()
11511168
}],
1152-
doc_blocks: vec![]
1153-
})
1169+
doc_blocks: vec![],
1170+
version: ide_version,
1171+
}),
1172+
version: client_version,
11541173
}),
11551174
cursor_position: Some(1),
1156-
scroll_position: Some(0.0)
1175+
scroll_position: Some(1.0)
11571176
})
11581177
}
11591178
);
11601179
codechat_server.send_result(client_id, None).await.unwrap();
11611180

1162-
// Move the cursor to code, then check that the position is correct. TODO:
1163-
// need access to codemirror in test mode.
1164-
11651181
// Insert a character to check the insertion point.
11661182
let code_line_css = ".CodeChat-CodeMirror .cm-line";
11671183
let code_line = driver_ref.find(By::Css(code_line_css)).await.unwrap();
@@ -1181,9 +1197,11 @@ async fn test_client_updates_core(
11811197
code_line.send_keys(Key::Home + "# ").await.unwrap();
11821198
// This should edit the (new) third line of the file after word wrap: `def
11831199
// foo():`.
1200+
let msg = codechat_server.get_message_timeout(TIMEOUT).await.unwrap();
1201+
let new_client_version = get_version(&msg);
11841202
client_id += MESSAGE_ID_INCREMENT;
11851203
assert_eq!(
1186-
codechat_server.get_message_timeout(TIMEOUT).await.unwrap(),
1204+
msg,
11871205
EditorMessage {
11881206
id: client_id,
11891207
message: EditorMessageContents::Update(UpdateMessageContents {
@@ -1198,16 +1216,17 @@ async fn test_client_updates_core(
11981216
to: Some(131),
11991217
insert: " # A comment\n".to_string()
12001218
}],
1201-
doc_blocks: vec![]
1202-
})
1219+
doc_blocks: vec![],
1220+
version: client_version,
1221+
}),
1222+
version: new_client_version,
12031223
}),
12041224
cursor_position: Some(4),
1205-
scroll_position: Some(0.0)
1225+
scroll_position: Some(1.0)
12061226
})
12071227
}
12081228
);
12091229
codechat_server.send_result(client_id, None).await.unwrap();
12101230

12111231
Ok(())
12121232
}
1213-
*/

0 commit comments

Comments
 (0)