}
}
+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
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))
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() {
fn test_format_document() {
let server = project(
r#"
+//- Cargo.toml
[package]
name = "foo"
version = "0.0.0"
);
}
+#[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(