mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
Run rustfmt with respect to Cargo.toml edition
This commit is contained in:
parent
1fe0b8c03f
commit
b437dca4bd
4 changed files with 78 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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<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))
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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::<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(
|
||||
|
|
Loading…
Reference in a new issue