aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_db/src/input.rs9
-rw-r--r--crates/ra_ide/src/lib.rs5
-rw-r--r--crates/ra_lsp_server/src/main_loop/handlers.rs6
-rw-r--r--crates/ra_lsp_server/tests/heavy_tests/main.rs58
4 files changed, 78 insertions, 0 deletions
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs
index b6d851776..2a7ed20d1 100644
--- a/crates/ra_db/src/input.rs
+++ b/crates/ra_db/src/input.rs
@@ -235,6 +235,15 @@ impl FromStr for Edition {
235 } 235 }
236} 236}
237 237
238impl fmt::Display for Edition {
239 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
240 f.write_str(match self {
241 Edition::Edition2015 => "2015",
242 Edition::Edition2018 => "2018",
243 })
244 }
245}
246
238impl Dependency { 247impl Dependency {
239 pub fn crate_id(&self) -> CrateId { 248 pub fn crate_id(&self) -> CrateId {
240 self.crate_id 249 self.crate_id
diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs
index d1bff4a76..779a81b2c 100644
--- a/crates/ra_ide/src/lib.rs
+++ b/crates/ra_ide/src/lib.rs
@@ -422,6 +422,11 @@ impl Analysis {
422 self.with_db(|db| parent_module::crate_for(db, file_id)) 422 self.with_db(|db| parent_module::crate_for(db, file_id))
423 } 423 }
424 424
425 /// Returns the edition of the given crate.
426 pub fn crate_edition(&self, crate_id: CrateId) -> Cancelable<Edition> {
427 self.with_db(|db| db.crate_graph().edition(crate_id))
428 }
429
425 /// Returns the root file of the given crate. 430 /// Returns the root file of the given crate.
426 pub fn crate_root(&self, crate_id: CrateId) -> Cancelable<FileId> { 431 pub fn crate_root(&self, crate_id: CrateId) -> Cancelable<FileId> {
427 self.with_db(|db| db.crate_graph().crate_root(crate_id)) 432 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 ca47ff5e1..409583634 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(
555 let _p = profile("handle_formatting"); 555 let _p = profile("handle_formatting");
556 let file_id = params.text_document.try_conv_with(&world)?; 556 let file_id = params.text_document.try_conv_with(&world)?;
557 let file = world.analysis().file_text(file_id)?; 557 let file = world.analysis().file_text(file_id)?;
558 let crate_ids = world.analysis().crate_for(file_id)?;
558 559
559 let file_line_index = world.analysis().file_line_index(file_id)?; 560 let file_line_index = world.analysis().file_line_index(file_id)?;
560 let end_position = TextUnit::of_str(&file).conv_with(&file_line_index); 561 let end_position = TextUnit::of_str(&file).conv_with(&file_line_index);
561 562
562 use std::process; 563 use std::process;
563 let mut rustfmt = process::Command::new("rustfmt"); 564 let mut rustfmt = process::Command::new("rustfmt");
565 if let Some(&crate_id) = crate_ids.first() {
566 // Assume all crates are in the same edition
567 let edition = world.analysis().crate_edition(crate_id)?;
568 rustfmt.args(&["--edition", &edition.to_string()]);
569 }
564 rustfmt.stdin(process::Stdio::piped()).stdout(process::Stdio::piped()); 570 rustfmt.stdin(process::Stdio::piped()).stdout(process::Stdio::piped());
565 571
566 if let Ok(path) = params.text_document.uri.to_file_path() { 572 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 29224cbe8..fec50bd25 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() {}
172fn test_format_document() { 172fn test_format_document() {
173 let server = project( 173 let server = project(
174 r#" 174 r#"
175//- Cargo.toml
175[package] 176[package]
176name = "foo" 177name = "foo"
177version = "0.0.0" 178version = "0.0.0"
@@ -220,6 +221,63 @@ pub use std::collections::HashMap;
220} 221}
221 222
222#[test] 223#[test]
224fn test_format_document_2018() {
225 let server = project(
226 r#"
227//- Cargo.toml
228[package]
229name = "foo"
230version = "0.0.0"
231edition = "2018"
232
233//- src/lib.rs
234mod bar;
235
236async fn test() {
237}
238
239fn main() {
240}
241
242pub use std::collections::HashMap;
243"#,
244 );
245 server.wait_until_workspace_is_loaded();
246
247 server.request::<Formatting>(
248 DocumentFormattingParams {
249 text_document: server.doc_id("src/lib.rs"),
250 options: FormattingOptions {
251 tab_size: 4,
252 insert_spaces: false,
253 properties: HashMap::new(),
254 },
255 },
256 json!([
257 {
258 "newText": r#"mod bar;
259
260async fn test() {}
261
262fn main() {}
263
264pub use std::collections::HashMap;
265"#,
266 "range": {
267 "end": {
268 "character": 0,
269 "line": 10
270 },
271 "start": {
272 "character": 0,
273 "line": 0
274 }
275 }
276 }
277 ]),
278 );
279}
280#[test]
223fn test_missing_module_code_action() { 281fn test_missing_module_code_action() {
224 let server = project( 282 let server = project(
225 r#" 283 r#"