Skip to content

Commit

Permalink
run compiler in LSP server
Browse files Browse the repository at this point in the history
Reviewed By: kassens

Differential Revision: D21307691

fbshipit-source-id: 862c71ab1c15365c5284f3ee499e9ff14a7575f6
  • Loading branch information
Brandon Dail authored and facebook-github-bot committed Apr 30, 2020
1 parent f544651 commit ace95ba
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
3 changes: 3 additions & 0 deletions compiler/crates/relay-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
5 changes: 3 additions & 2 deletions compiler/crates/relay-lsp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ mod server;
use lsp_server::Connection;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
let (connection, io_handles) = Connection::stdio();
let params = server::initialize(&connection)?;
server::run(&connection, params)?;
server::run(&connection, params).await?;
io_handles.join()?;
Ok(())
}
Expand Down
48 changes: 44 additions & 4 deletions compiler/crates/relay-lsp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<dyn Error + Sync + Send>> {
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(())
Expand Down

0 comments on commit ace95ba

Please sign in to comment.