]> git.lizzy.rs Git - rust.git/commitdiff
Run rustfmt with respect to Cargo.toml edition
authorVincent Rouillé <vincent@speedy37.fr>
Wed, 4 Dec 2019 22:05:01 +0000 (23:05 +0100)
committerVincent Rouillé <vincent@speedy37.fr>
Wed, 4 Dec 2019 22:05:01 +0000 (23:05 +0100)
crates/ra_db/src/input.rs
crates/ra_ide/src/lib.rs
crates/ra_lsp_server/src/main_loop/handlers.rs
crates/ra_lsp_server/tests/heavy_tests/main.rs

index b6d851776129c2f1ade35a03fee43af02c689022..2a7ed20d1ce91b89a198cc15a7e1b15ed7af39f5 100644 (file)
@@ -235,6 +235,15 @@ fn from_str(s: &str) -> Result<Self, Self::Err> {
     }
 }
 
+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
index d1bff4a761eb8be29bdc95c526cda036578b14b9..779a81b2c0aa0d9ee9a47a02081d05311fa6da8d 100644 (file)
@@ -422,6 +422,11 @@ pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
         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<Edition> {
+        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<FileId> {
         self.with_db(|db| db.crate_graph().crate_root(crate_id))
index ca47ff5e16cab2c598134735db9236994c55382a..4095836344003051b17b6f073586100fff792276 100644 (file)
@@ -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() {
index 29224cbe8e7d769cda9825df0a811bb6006bd0c7..fec50bd25c8de1bcbefab0b6e5f67b17fbcbd29a 100644 (file)
@@ -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 @@ fn main() {}
     );
 }
 
+#[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::<Formatting>(
+        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(