Skip to content

Commit 46d37a8

Browse files
bors[bot]Speedy37
andauthored
Merge #2477
2477: Run rustfmt with respect to Cargo.toml edition r=matklad a=Speedy37 Fixes #2146 Fixes #1959 Co-authored-by: Vincent Rouillé <[email protected]>
2 parents 1fe0b8c + b437dca commit 46d37a8

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

crates/ra_db/src/input.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,15 @@ impl FromStr for Edition {
235235
}
236236
}
237237

238+
impl 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+
238247
impl Dependency {
239248
pub fn crate_id(&self) -> CrateId {
240249
self.crate_id

crates/ra_ide/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@ impl Analysis {
422422
self.with_db(|db| parent_module::crate_for(db, file_id))
423423
}
424424

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+
425430
/// Returns the root file of the given crate.
426431
pub fn crate_root(&self, crate_id: CrateId) -> Cancelable<FileId> {
427432
self.with_db(|db| db.crate_graph().crate_root(crate_id))

crates/ra_lsp_server/src/main_loop/handlers.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,12 +555,18 @@ pub fn handle_formatting(
555555
let _p = profile("handle_formatting");
556556
let file_id = params.text_document.try_conv_with(&world)?;
557557
let file = world.analysis().file_text(file_id)?;
558+
let crate_ids = world.analysis().crate_for(file_id)?;
558559

559560
let file_line_index = world.analysis().file_line_index(file_id)?;
560561
let end_position = TextUnit::of_str(&file).conv_with(&file_line_index);
561562

562563
use std::process;
563564
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+
}
564570
rustfmt.stdin(process::Stdio::piped()).stdout(process::Stdio::piped());
565571

566572
if let Ok(path) = params.text_document.uri.to_file_path() {

crates/ra_lsp_server/tests/heavy_tests/main.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ fn main() {}
172172
fn test_format_document() {
173173
let server = project(
174174
r#"
175+
//- Cargo.toml
175176
[package]
176177
name = "foo"
177178
version = "0.0.0"
@@ -219,6 +220,63 @@ pub use std::collections::HashMap;
219220
);
220221
}
221222

223+
#[test]
224+
fn test_format_document_2018() {
225+
let server = project(
226+
r#"
227+
//- Cargo.toml
228+
[package]
229+
name = "foo"
230+
version = "0.0.0"
231+
edition = "2018"
232+
233+
//- src/lib.rs
234+
mod bar;
235+
236+
async fn test() {
237+
}
238+
239+
fn main() {
240+
}
241+
242+
pub 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+
260+
async fn test() {}
261+
262+
fn main() {}
263+
264+
pub 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+
}
222280
#[test]
223281
fn test_missing_module_code_action() {
224282
let server = project(

0 commit comments

Comments
 (0)