@@ -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) => {
0 commit comments