From ace95ba9948bf70ca381c0292c1d419d6e6a65fd Mon Sep 17 00:00:00 2001 From: Brandon Dail Date: Wed, 29 Apr 2020 21:31:22 -0700 Subject: [PATCH] run compiler in LSP server Reviewed By: kassens Differential Revision: D21307691 fbshipit-source-id: 862c71ab1c15365c5284f3ee499e9ff14a7575f6 --- compiler/crates/relay-lsp/Cargo.toml | 3 ++ compiler/crates/relay-lsp/src/main.rs | 5 +-- compiler/crates/relay-lsp/src/server.rs | 48 ++++++++++++++++++++++--- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/compiler/crates/relay-lsp/Cargo.toml b/compiler/crates/relay-lsp/Cargo.toml index a8ef9409a980b..d1c93133c82fb 100644 --- a/compiler/crates/relay-lsp/Cargo.toml +++ b/compiler/crates/relay-lsp/Cargo.toml @@ -9,6 +9,9 @@ include = ["src/**/*.rs"] [dependencies] relay-compiler = { path = "../relay-compiler" } +env_logger = "0.7" +log = "0.4.8" lsp-server = "0.3.1" lsp-types = "0.73.0" serde_json = "1.0" +tokio = { version = "=0.2.13", features = ["full"] } diff --git a/compiler/crates/relay-lsp/src/main.rs b/compiler/crates/relay-lsp/src/main.rs index cbb8f26bf5e52..bd157f55db04b 100644 --- a/compiler/crates/relay-lsp/src/main.rs +++ b/compiler/crates/relay-lsp/src/main.rs @@ -12,10 +12,11 @@ mod server; use lsp_server::Connection; use std::error::Error; -fn main() -> Result<(), Box> { +#[tokio::main] +async fn main() -> Result<(), Box> { let (connection, io_handles) = Connection::stdio(); let params = server::initialize(&connection)?; - server::run(&connection, params)?; + server::run(&connection, params).await?; io_handles.join()?; Ok(()) } diff --git a/compiler/crates/relay-lsp/src/server.rs b/compiler/crates/relay-lsp/src/server.rs index 16a6168603fb6..21f7de4aa26bc 100644 --- a/compiler/crates/relay-lsp/src/server.rs +++ b/compiler/crates/relay-lsp/src/server.rs @@ -14,6 +14,13 @@ use lsp_types::{ use lsp_server::{Connection, Message, Notification as ServerNotification}; +use relay_compiler::compiler::Compiler; +use relay_compiler::config::Config; + +use std::path::PathBuf; + +use log::info; + /// Initializes an LSP connection, handling the `initize` message and `initialized` notification /// handshake. pub fn initialize( @@ -26,16 +33,49 @@ pub fn initialize( } /// Run the main server loop -pub fn run( +pub async fn run( connection: &Connection, _params: InitializeParams, ) -> Result<(), Box> { show_info_message("Relay Language Server Started", connection)?; + + // TODO(brandondail) don't hardcode the test project config here + let home = std::env::var("HOME").unwrap(); + let config_path = PathBuf::from(format!( + "{}/fbsource/fbcode/relay/config/config.test.json", + home + )); + + let root_dir = PathBuf::from(format!("{}/fbsource", home)); + let mut config = Config::load(root_dir, config_path).unwrap(); + + // Don't write artifacts by default + config.write_artifacts = false; + + info!("Compiler config: {:#?}", config); + + let compiler = Compiler::new(config); + + match compiler.compile(None).await { + Ok(_) => { + info!("Compiled project successfully"); + } + Err(_) => { + info!("Failed to compile project"); + } + } + for msg in &connection.receiver { match msg { - Message::Request(_req) => {} - Message::Response(_resp) => {} - Message::Notification(_not) => {} + Message::Request(req) => { + info!("Request: {:?}", req); + } + Message::Response(resp) => { + info!("Request: {:?}", resp); + } + Message::Notification(notif) => { + info!("Notification: {:?}", notif); + } } } Ok(())