Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 6e1c338

Browse files
authored
Merge pull request #331 from khadiwala/master
Support formatting config via rustfmt.toml
2 parents eb953a3 + c546202 commit 6e1c338

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/actions/mod.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use url::Url;
1616
use vfs::{Vfs, Change, FileContents};
1717
use racer;
1818
use rustfmt::{Input as FmtInput, format_input};
19-
use rustfmt::config::{self, WriteMode};
19+
use config::FmtConfig;
2020
use serde_json;
2121
use span;
2222
use Span;
@@ -43,6 +43,7 @@ pub struct ActionHandler {
4343
build_queue: Arc<BuildQueue>,
4444
current_project: Mutex<Option<PathBuf>>,
4545
previous_build_results: Mutex<BuildResults>,
46+
fmt_config: Mutex<FmtConfig>,
4647
}
4748

4849
impl ActionHandler {
@@ -55,6 +56,7 @@ impl ActionHandler {
5556
build_queue: build_queue,
5657
current_project: Mutex::new(None),
5758
previous_build_results: Mutex::new(HashMap::new()),
59+
fmt_config: Mutex::new(FmtConfig::default()),
5860
}
5961
}
6062

@@ -65,7 +67,16 @@ impl ActionHandler {
6567
}
6668
{
6769
let mut current_project = self.current_project.lock().unwrap();
68-
*current_project = Some(root_path.clone());
70+
if current_project
71+
.as_ref()
72+
.map_or(true, |existing| *existing != root_path) {
73+
let new_path = root_path.clone();
74+
{
75+
let mut config = self.fmt_config.lock().unwrap();
76+
*config = FmtConfig::from(&new_path);
77+
}
78+
*current_project = Some(new_path);
79+
}
6980
}
7081
self.build(&root_path, BuildPriority::Immediate, out);
7182
}
@@ -436,13 +447,9 @@ impl ActionHandler {
436447
return;
437448
}
438449
};
439-
440-
let mut config = config::Config::default();
441-
config.set().skip_children(true);
442-
config.set().write_mode(WriteMode::Plain);
443-
450+
let config = self.fmt_config.lock().unwrap();
444451
let mut buf = Vec::<u8>::new();
445-
match format_input(input, &config, Some(&mut buf)) {
452+
match format_input(input, config.get_rustfmt_config(), Some(&mut buf)) {
446453
Ok((summary, ..)) => {
447454
// format_input returns Ok even if there are any errors, i.e., parsing errors.
448455
if summary.has_no_errors() {

src/config.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use toml;
1313
use std::fs::File;
1414
use std::io::Read;
1515
use std::path::Path;
16+
use rustfmt::config::Config as RustfmtConfig;
17+
use rustfmt::config::WriteMode;
1618

1719
// This trait and the following impl blocks are there so that we an use
1820
// UCFS inside the get_docs() function on types for configs.
@@ -46,7 +48,7 @@ macro_rules! create_config {
4648
}
4749

4850
// Just like the Config struct but with each property wrapped
49-
// as Option<T>. This is used to parse a rustfmt.toml that doesn't
51+
// as Option<T>. This is used to parse a rls.toml that doesn't
5052
// specity all properties of `Config`.
5153
// We first parse into `ParsedConfig`, then create a default `Config`
5254
// and overwrite the properties with corresponding values from `ParsedConfig`
@@ -155,3 +157,44 @@ create_config! {
155157
cfg_test: bool, true, false, "build cfg(test) code";
156158
unstable_features: bool, false, false, "enable unstable features";
157159
}
160+
161+
/// A rustfmt config (typically specified via rustfmt.toml)
162+
/// The FmtConfig is not an exact translation of the config
163+
/// rustfmt generates from the user's toml file, since when
164+
/// using rustfmt with rls certain configuration options are
165+
/// always used. See `FmtConfig::set_rls_options`
166+
pub struct FmtConfig(RustfmtConfig);
167+
168+
impl FmtConfig {
169+
/// Look for `.rustmt.toml` or `rustfmt.toml` in `path`, falling back
170+
/// to the default config if neither exist
171+
pub fn from(path: &Path) -> FmtConfig {
172+
if let Ok((config, _)) = RustfmtConfig::from_resolved_toml_path(path) {
173+
let mut config = FmtConfig(config);
174+
config.set_rls_options();
175+
return config;
176+
}
177+
FmtConfig::default()
178+
}
179+
180+
/// Return an immutable borrow of the config, will always
181+
/// have any relevant rls specific options set
182+
pub fn get_rustfmt_config(&self) -> &RustfmtConfig {
183+
&self.0
184+
}
185+
186+
// options that are always used when formatting with rls
187+
fn set_rls_options(&mut self) {
188+
self.0.set().skip_children(true);
189+
self.0.set().write_mode(WriteMode::Plain);
190+
}
191+
}
192+
193+
impl Default for FmtConfig {
194+
fn default() -> FmtConfig {
195+
let config = RustfmtConfig::default();
196+
let mut config = FmtConfig(config);
197+
config.set_rls_options();
198+
config
199+
}
200+
}

0 commit comments

Comments
 (0)