diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index b6d8517761..2a7ed20d1c 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -235,6 +235,15 @@ impl FromStr for Edition { } } +impl fmt::Display for Edition { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(match self { + Edition::Edition2015 => "2015", + Edition::Edition2018 => "2018", + }) + } +} + impl Dependency { pub fn crate_id(&self) -> CrateId { self.crate_id diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index d1bff4a761..779a81b2c0 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -422,6 +422,11 @@ impl Analysis { self.with_db(|db| parent_module::crate_for(db, file_id)) } + /// Returns the edition of the given crate. + pub fn crate_edition(&self, crate_id: CrateId) -> Cancelable { + self.with_db(|db| db.crate_graph().edition(crate_id)) + } + /// Returns the root file of the given crate. pub fn crate_root(&self, crate_id: CrateId) -> Cancelable { self.with_db(|db| db.crate_graph().crate_root(crate_id)) diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index ca47ff5e16..4095836344 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -555,12 +555,18 @@ pub fn handle_formatting( let _p = profile("handle_formatting"); let file_id = params.text_document.try_conv_with(&world)?; let file = world.analysis().file_text(file_id)?; + let crate_ids = world.analysis().crate_for(file_id)?; let file_line_index = world.analysis().file_line_index(file_id)?; let end_position = TextUnit::of_str(&file).conv_with(&file_line_index); use std::process; let mut rustfmt = process::Command::new("rustfmt"); + if let Some(&crate_id) = crate_ids.first() { + // Assume all crates are in the same edition + let edition = world.analysis().crate_edition(crate_id)?; + rustfmt.args(&["--edition", &edition.to_string()]); + } rustfmt.stdin(process::Stdio::piped()).stdout(process::Stdio::piped()); if let Ok(path) = params.text_document.uri.to_file_path() { diff --git a/crates/ra_lsp_server/tests/heavy_tests/main.rs b/crates/ra_lsp_server/tests/heavy_tests/main.rs index 29224cbe8e..fec50bd25c 100644 --- a/crates/ra_lsp_server/tests/heavy_tests/main.rs +++ b/crates/ra_lsp_server/tests/heavy_tests/main.rs @@ -172,6 +172,7 @@ fn main() {} fn test_format_document() { let server = project( r#" +//- Cargo.toml [package] name = "foo" version = "0.0.0" @@ -219,6 +220,63 @@ pub use std::collections::HashMap; ); } +#[test] +fn test_format_document_2018() { + let server = project( + r#" +//- Cargo.toml +[package] +name = "foo" +version = "0.0.0" +edition = "2018" + +//- src/lib.rs +mod bar; + +async fn test() { +} + +fn main() { +} + +pub use std::collections::HashMap; +"#, + ); + server.wait_until_workspace_is_loaded(); + + server.request::( + DocumentFormattingParams { + text_document: server.doc_id("src/lib.rs"), + options: FormattingOptions { + tab_size: 4, + insert_spaces: false, + properties: HashMap::new(), + }, + }, + json!([ + { + "newText": r#"mod bar; + +async fn test() {} + +fn main() {} + +pub use std::collections::HashMap; +"#, + "range": { + "end": { + "character": 0, + "line": 10 + }, + "start": { + "character": 0, + "line": 0 + } + } + } + ]), + ); +} #[test] fn test_missing_module_code_action() { let server = project(