@@ -19,9 +19,10 @@ use lsp_types::{
1919 TextDocumentSyncKind , TextDocumentSyncOptions , TextDocumentSyncSaveOptions , Url ,
2020 WorkspaceClientCapabilities , WorkspaceFoldersServerCapabilities , WorkspaceServerCapabilities ,
2121} ;
22- use playground_server:: { FrontendMessage , LangServerToWasmMessage , PreLangServerToWasmMessage } ;
22+ use playground_server:: { FrontendMessage , WebviewNotification , WebviewRouterMessage } ;
2323use schedule:: Task ;
2424use serde:: { Deserialize , Serialize } ;
25+ use serde_json:: json;
2526use tokio:: sync:: { broadcast, RwLock } ;
2627
2728use self :: {
@@ -48,9 +49,9 @@ pub type Result<T> = std::result::Result<T, api::Error>;
4849
4950pub ( crate ) struct ServerArgs {
5051 pub tokio_runtime : tokio:: runtime:: Runtime ,
51- pub broadcast_tx : broadcast:: Sender < LangServerToWasmMessage > ,
52- pub playground_rx : broadcast:: Receiver < PreLangServerToWasmMessage > ,
53- pub playground_tx : broadcast:: Sender < PreLangServerToWasmMessage > ,
52+ pub webview_router_to_websocket_tx : broadcast:: Sender < WebviewNotification > ,
53+ pub to_webview_router_rx : broadcast:: Receiver < WebviewRouterMessage > ,
54+ pub to_webview_router_tx : broadcast:: Sender < WebviewRouterMessage > ,
5455 pub playground_port : u16 ,
5556 pub proxy_port : u16 ,
5657}
@@ -175,7 +176,7 @@ impl Server {
175176 global_settings,
176177 & workspaces,
177178 args. playground_port ,
178- args. playground_tx . clone ( ) ,
179+ args. to_webview_router_tx . clone ( ) ,
179180 client_version,
180181 ) ?;
181182
@@ -194,7 +195,6 @@ impl Server {
194195
195196 {
196197 let lsp_sender = server. connection . make_sender ( ) ;
197- let playground_tx = server. session . playground_tx . clone ( ) ;
198198 server. args . tokio_runtime . spawn ( async move {
199199 lsp_sender
200200 . send ( Message :: Notification ( lsp_server:: Notification :: new (
@@ -208,15 +208,32 @@ impl Server {
208208 } ) ;
209209 }
210210 {
211- let mut playground_rx = server. args . playground_rx . resubscribe ( ) ;
212- let broadcast_tx = server. args . broadcast_tx . clone ( ) ;
211+ // Start the webview router loop
212+ //
213+ // This is the communication bridge between the webview and IDE in non-VSCode environments
214+ // and allows the webview to send messages to Jetbrains and allows Jetbrains to send messages
215+ // to the webview.
216+ //
217+ // webview->IDE is generally backed by the webview POSTing to /webview/SEND_LSP_NOTIFICATION_TO_IDE,
218+ // and the language server will then forward that to the IDE
219+ // IDE->webview is generally backed by the IDE calling POST /webview/SEND_LSP_NOTIFICATION_TO_WEBVIEW,
220+ // and the language server will then forward that to the webview
221+ //
222+ // (Note that although the language-server pretends to offer a request-response API, it does not
223+ // block on either the IDE or webview responding before responding to its caller.)
224+ //
225+ // Incoming messages are received via to_webview_router_tx, which the router will then decide to
226+ // dispatch to either the webview (via its websocket) or the IDE (via its LSP connection).
227+ let lsp_sender = server. connection . make_sender ( ) ;
228+ let mut to_webview_router_rx = server. args . to_webview_router_rx . resubscribe ( ) ;
229+ let webview_router_to_websocket_tx = server. args . webview_router_to_websocket_tx . clone ( ) ;
213230 let session = server. session . clone ( ) ;
214231 server. args . tokio_runtime . spawn ( async move {
215232 tracing:: info!( "Starting playground rx loop" ) ;
216- while let Ok ( msg) = playground_rx . recv ( ) . await {
233+ while let Ok ( msg) = to_webview_router_rx . recv ( ) . await {
217234 tracing:: info!( "playground rx loop: {:?}" , msg) ;
218235 match msg {
219- PreLangServerToWasmMessage :: WasmIsInitialized => {
236+ WebviewRouterMessage :: WasmIsInitialized => {
220237 tracing:: info!( "Received playground INITIALIZED request" ) ;
221238 let projects = session. baml_src_projects . lock ( ) ;
222239 for ( _, project) in projects. iter ( ) {
@@ -237,8 +254,8 @@ impl Server {
237254 ( key, contents)
238255 } )
239256 . collect ( ) ;
240- broadcast_tx
241- . send ( LangServerToWasmMessage :: PlaygroundMessage (
257+ let _ = webview_router_to_websocket_tx
258+ . send ( WebviewNotification :: PlaygroundMessage (
242259 FrontendMessage :: add_project {
243260 root_path : project
244261 . root_path ( )
@@ -247,13 +264,38 @@ impl Server {
247264 files : files_map,
248265 } ,
249266 ) )
250- . unwrap ( ) ;
267+ . inspect_err ( |e| {
268+ tracing:: error!( "Failed to forward add_project to playground: {e}" ) ;
269+ } ) ;
251270 }
252271 }
253- PreLangServerToWasmMessage :: FrontendMessage ( msg) => {
254- broadcast_tx
255- . send ( LangServerToWasmMessage :: PlaygroundMessage ( msg) )
256- . unwrap ( ) ;
272+ WebviewRouterMessage :: SendLspNotificationToIde ( notification) => {
273+ tracing:: info!( "Received playground SEND_LSP_NOTIFICATION request: {:?}" , notification) ;
274+ let _ = lsp_sender
275+ . send ( Message :: Notification ( notification) )
276+ . inspect_err ( |e| {
277+ tracing:: error!( "Failed to forward SEND_LSP_NOTIFICATION message to language-server: {e}" ) ;
278+ } ) ;
279+ }
280+ WebviewRouterMessage :: SendLspNotificationToWebview ( notification) => {
281+ tracing:: info!( "Received playground SEND_LSP_NOTIFICATION_TO_WEBVIEW request: {:?}" , notification) ;
282+ let _ = webview_router_to_websocket_tx
283+ . send ( WebviewNotification :: PlaygroundMessage (
284+ FrontendMessage :: lsp_message {
285+ method : notification. method ,
286+ params : notification. params ,
287+ } ,
288+ ) )
289+ . inspect_err ( |e| {
290+ tracing:: error!( "Failed to forward SEND_LSP_NOTIFICATION_TO_WEBVIEW message to webview: {e}" ) ;
291+ } ) ;
292+ }
293+ WebviewRouterMessage :: CustomNotificationToWebview ( msg) => {
294+ let _ = webview_router_to_websocket_tx
295+ . send ( WebviewNotification :: PlaygroundMessage ( msg) )
296+ . inspect_err ( |e| {
297+ tracing:: error!( "Failed to forward FrontendMessage to playground: {e}" ) ;
298+ } ) ;
257299 }
258300 }
259301 }
@@ -339,7 +381,7 @@ impl Server {
339381 & self . client_capabilities ,
340382 self . session ,
341383 self . worker_threads ,
342- self . args . broadcast_tx ,
384+ self . args . webview_router_to_websocket_tx ,
343385 ) ?;
344386 self . connection . close ( ) ?;
345387 Ok ( ( ) )
@@ -355,7 +397,7 @@ impl Server {
355397 _client_capabilities : & ClientCapabilities ,
356398 mut session : Session ,
357399 worker_threads : NonZeroUsize ,
358- broadcast_tx : broadcast:: Sender < LangServerToWasmMessage > ,
400+ webview_router_to_websocket_tx : broadcast:: Sender < WebviewNotification > ,
359401 ) -> anyhow:: Result < ( ) > {
360402 // Ensure we have a notifier for reload operations
361403 let client = client:: Client :: new ( connection. make_sender ( ) ) ;
@@ -371,7 +413,7 @@ impl Server {
371413 if connection. handle_shutdown ( & msg) ? {
372414 break ;
373415 }
374- // broadcast_tx .send(LangServerToWasmMessage::LspMessage(msg.clone()))?;
416+ // webview_router_to_websocket_tx .send(LangServerToWasmMessage::LspMessage(msg.clone()))?;
375417 let tasks = match msg {
376418 Message :: Request ( req) => vec ! [ api:: request( req) ] ,
377419 Message :: Notification ( notification) => api:: notification ( notification) ,
0 commit comments