Skip to content

Commit 6ac9668

Browse files
committed
Generalize rustfmt config
1 parent 67351a0 commit 6ac9668

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

crates/rust-analyzer/src/main_loop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::{
3838
subscriptions::Subscriptions,
3939
},
4040
req,
41-
world::{Config, WorldSnapshot, WorldState},
41+
world::{Config, RustfmtConfig, WorldSnapshot, WorldState},
4242
Result, ServerConfig,
4343
};
4444
use req::ConfigurationParams;
@@ -110,7 +110,7 @@ fn get_config(
110110
} else {
111111
None
112112
},
113-
rustfmt_args: config.rustfmt_args.clone(),
113+
rustfmt: RustfmtConfig::Rustfmt { extra_args: config.rustfmt_args.clone() },
114114
vscode_lldb: config.vscode_lldb,
115115
proc_macro_srv: None, // FIXME: get this from config
116116
}

crates/rust-analyzer/src/main_loop/handlers.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::{
3939
from_json,
4040
req::{self, Decoration, InlayHint, InlayHintsParams},
4141
semantic_tokens::SemanticTokensBuilder,
42-
world::WorldSnapshot,
42+
world::{RustfmtConfig, WorldSnapshot},
4343
LspError, Result,
4444
};
4545

@@ -610,13 +610,24 @@ pub fn handle_formatting(
610610
let file_line_index = world.analysis().file_line_index(file_id)?;
611611
let end_position = TextUnit::of_str(&file).conv_with(&file_line_index);
612612

613-
let mut rustfmt = process::Command::new("rustfmt");
614-
rustfmt.args(&world.config.rustfmt_args);
615-
if let Some(&crate_id) = crate_ids.first() {
616-
// Assume all crates are in the same edition
617-
let edition = world.analysis().crate_edition(crate_id)?;
618-
rustfmt.args(&["--edition", &edition.to_string()]);
619-
}
613+
let mut rustfmt = match &world.config.rustfmt {
614+
RustfmtConfig::Rustfmt { extra_args } => {
615+
let mut cmd = process::Command::new("rustfmt");
616+
cmd.args(extra_args);
617+
if let Some(&crate_id) = crate_ids.first() {
618+
// Assume all crates are in the same edition
619+
let edition = world.analysis().crate_edition(crate_id)?;
620+
cmd.arg("--edition");
621+
cmd.arg(edition.to_string());
622+
}
623+
cmd
624+
}
625+
RustfmtConfig::CustomCommand { command, args } => {
626+
let mut cmd = process::Command::new(command);
627+
cmd.args(args);
628+
cmd
629+
}
630+
};
620631

621632
if let Ok(path) = params.text_document.uri.to_file_path() {
622633
if let Some(parent) = path.parent() {

crates/rust-analyzer/src/world.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,30 @@ pub struct Config {
5757
pub supports_location_link: bool,
5858
pub line_folding_only: bool,
5959
pub inlay_hints: InlayHintsConfig,
60-
pub rustfmt_args: Vec<String>,
60+
pub rustfmt: RustfmtConfig,
6161
pub check: Option<FlycheckConfig>,
6262
pub vscode_lldb: bool,
6363
pub proc_macro_srv: Option<String>,
6464
}
6565

66+
#[derive(Debug, Clone)]
67+
pub enum RustfmtConfig {
68+
Rustfmt {
69+
extra_args: Vec<String>,
70+
},
71+
#[allow(unused)]
72+
CustomCommand {
73+
command: String,
74+
args: Vec<String>,
75+
},
76+
}
77+
78+
impl Default for RustfmtConfig {
79+
fn default() -> Self {
80+
RustfmtConfig::Rustfmt { extra_args: Vec::new() }
81+
}
82+
}
83+
6684
/// `WorldState` is the primary mutable state of the language server
6785
///
6886
/// The most interesting components are `vfs`, which stores a consistent

0 commit comments

Comments
 (0)