Skip to content

Commit fa32a99

Browse files
committed
Add: use a Rust-based send_result that doesn't use JSON encoding/decoding.
1 parent baab9d7 commit fa32a99

File tree

2 files changed

+83
-37
lines changed

2 files changed

+83
-37
lines changed

extensions/VSCode/src/extension.ts

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,10 @@ export const activate = (context: vscode.ExtensionContext) => {
335335
value as UpdateMessageContents;
336336
const doc = get_document(current_update.file_path);
337337
if (doc === undefined) {
338-
send_result(id, {
339-
Err: `No open document for ${current_update.file_path}`,
340-
});
338+
sendResult(
339+
id,
340+
`No open document for ${current_update.file_path}`,
341+
);
341342
break;
342343
}
343344
if (current_update.contents !== undefined) {
@@ -421,7 +422,7 @@ export const activate = (context: vscode.ExtensionContext) => {
421422
);
422423
}
423424
}
424-
send_result(id);
425+
sendResult(id);
425426
break;
426427
}
427428

@@ -438,12 +439,13 @@ export const activate = (context: vscode.ExtensionContext) => {
438439
document,
439440
current_editor?.viewColumn,
440441
);
441-
send_result(id);
442+
sendResult(id);
442443
},
443444
(reason) =>
444-
send_result(id, {
445-
Err: `Error: unable to open file ${current_file}: ${reason}`,
446-
}),
445+
sendResult(
446+
id,
447+
`Error: unable to open file ${current_file}: ${reason}`,
448+
),
447449
);
448450
} else {
449451
// TODO: open using a custom document editor.
@@ -466,14 +468,15 @@ export const activate = (context: vscode.ExtensionContext) => {
466468
},
467469
)
468470
.then(
469-
() => send_result(id),
471+
() => sendResult(id),
470472
(reason) =>
471-
send_result(id, {
472-
Err: `Error: unable to open file ${current_file}: ${reason}`,
473-
}),
473+
sendResult(
474+
id,
475+
`Error: unable to open file ${current_file}: ${reason}`,
476+
),
474477
);
475478
}
476-
send_result(id);
479+
sendResult(id);
477480
}
478481
break;
479482
}
@@ -509,19 +512,18 @@ export const activate = (context: vscode.ExtensionContext) => {
509512
const doc = get_document(load_file);
510513
const load_file_result =
511514
doc === undefined ? null : doc.getText();
512-
send_result(id, {
513-
Ok: {
514-
LoadFile: load_file_result,
515-
},
516-
});
515+
codeChatEditorServer.sendResultLoadfile(
516+
id,
517+
load_file_result,
518+
);
517519
break;
518520
}
519521

520522
case "ClientHtml": {
521523
const client_html = value as string;
522524
assert(webview_panel !== undefined);
523525
webview_panel.webview.html = client_html;
524-
send_result(id);
526+
sendResult(id);
525527
// Now that the Client is loaded, send the editor's
526528
// current file to the server.
527529
send_update(false);
@@ -599,22 +601,12 @@ const report_server_timeout = (message_id: number) => {
599601
};
600602

601603
// Send a result (a response to a message from the server) back to the server.
602-
const send_result = (id: number, result: MessageResult = { Ok: "Void" }) => {
603-
// We can't simply call `send_message` because that function expects a
604-
// result message back from the server.
605-
const jm: EditorMessage = {
606-
id,
607-
message: {
608-
Result: result,
609-
},
610-
};
604+
const sendResult = (id: number, result: string | null = null) => {
611605
assert(codeChatEditorServer);
612606
console_log(
613-
`CodeChat Editor extension: sending result ${JSON.stringify(
614-
jm,
615-
).substring(0, MAX_MESSAGE_LENGTH)}.`,
607+
`CodeChat Editor extension: sending result ${format_struct(result)}.`,
616608
);
617-
codeChatEditorServer.sendMessage(JSON.stringify(jm));
609+
codeChatEditorServer.sendResult(id, result);
618610
};
619611

620612
// This is called after an event such as an edit, when the CodeChat panel

extensions/VSCode/src/lib.rs

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use tokio::sync::{
4343
use code_chat_editor::{
4444
ide::vscode::{connection_id_raw_to_str, vscode_ide_core},
4545
translation::{CreatedTranslationQueues, create_translation_queues},
46-
webserver::{self, EditorMessage, WebAppState, setup_server},
46+
webserver::{self, EditorMessage, ResultOkTypes, WebAppState, setup_server},
4747
};
4848

4949
// Code
@@ -152,6 +152,15 @@ impl CodeChatEditorServer {
152152
}
153153
}
154154

155+
// TODO: wait for a response; produce an error if no response after a timeout.
156+
// Automatically generate an ID for each message.
157+
async fn send_editor_message(&self, editor_message: EditorMessage) -> std::io::Result<()> {
158+
self.from_ide_tx
159+
.send(editor_message)
160+
.await
161+
.map_err(|e| std::io::Error::other(e.to_string()))
162+
}
163+
155164
// Given a JSON-encoded message, send it to the Client. This returns an
156165
// error if string's contents can't be JSON decoded to an `EditorMessage`.
157166
#[napi]
@@ -161,10 +170,55 @@ impl CodeChatEditorServer {
161170
message: String,
162171
) -> std::io::Result<()> {
163172
let editor_message = serde_json::from_str::<EditorMessage>(&message)?;
164-
self.from_ide_tx
165-
.send(editor_message)
166-
.await
167-
.map_err(|e| std::io::Error::other(e.to_string()))
173+
self.send_editor_message(editor_message).await
174+
}
175+
176+
// TODO: not used yet; need to integrate in auto-result tracking, auto ID
177+
// generation.
178+
#[napi]
179+
pub async fn send_message_opened(&self, id: f64, hosted_in_ide: bool) -> std::io::Result<()> {
180+
let editor_message = EditorMessage {
181+
id,
182+
message: webserver::EditorMessageContents::Opened(webserver::IdeType::VSCode(
183+
hosted_in_ide,
184+
)),
185+
};
186+
self.send_editor_message(editor_message).await
187+
}
188+
189+
// Send either an Ok(Void) or an Error result to the Client.
190+
#[napi]
191+
pub async fn send_result(
192+
&self,
193+
id: f64,
194+
message_result: Option<String>,
195+
) -> std::io::Result<()> {
196+
let editor_message = EditorMessage {
197+
id,
198+
message: webserver::EditorMessageContents::Result(
199+
if let Some(message_result) = message_result {
200+
Err(message_result)
201+
} else {
202+
Ok(ResultOkTypes::Void)
203+
},
204+
),
205+
};
206+
self.send_editor_message(editor_message).await
207+
}
208+
209+
#[napi]
210+
pub async fn send_result_loadfile(
211+
&self,
212+
id: f64,
213+
load_file: Option<String>,
214+
) -> std::io::Result<()> {
215+
let editor_message = EditorMessage {
216+
id,
217+
message: webserver::EditorMessageContents::Result(Ok(ResultOkTypes::LoadFile(
218+
load_file,
219+
))),
220+
};
221+
self.send_editor_message(editor_message).await
168222
}
169223

170224
// This returns after the server shuts down.

0 commit comments

Comments
 (0)