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

Support formatting config via rustfmt.toml #331

Merged
merged 3 commits into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 15 additions & 8 deletions src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use url::Url;
use vfs::{Vfs, Change, FileContents};
use racer;
use rustfmt::{Input as FmtInput, format_input};
use rustfmt::config::{self, WriteMode};
use config::FmtConfig;
use serde_json;
use span;
use Span;
Expand All @@ -43,6 +43,7 @@ pub struct ActionHandler {
build_queue: Arc<BuildQueue>,
current_project: Mutex<Option<PathBuf>>,
previous_build_results: Mutex<BuildResults>,
fmt_config: Mutex<FmtConfig>,
}

impl ActionHandler {
Expand All @@ -55,6 +56,7 @@ impl ActionHandler {
build_queue: build_queue,
current_project: Mutex::new(None),
previous_build_results: Mutex::new(HashMap::new()),
fmt_config: Mutex::new(FmtConfig::default()),
}
}

Expand All @@ -65,7 +67,16 @@ impl ActionHandler {
}
{
let mut current_project = self.current_project.lock().unwrap();
*current_project = Some(root_path.clone());
if current_project
.as_ref()
.map_or(true, |existing| *existing != root_path) {
let new_path = root_path.clone();
{
let mut config = self.fmt_config.lock().unwrap();
*config = FmtConfig::from(&new_path);
}
*current_project = Some(new_path);
}
}
self.build(&root_path, BuildPriority::Immediate, out);
}
Expand Down Expand Up @@ -436,13 +447,9 @@ impl ActionHandler {
return;
}
};

let mut config = config::Config::default();
config.set().skip_children(true);
config.set().write_mode(WriteMode::Plain);

let config = self.fmt_config.lock().unwrap();
let mut buf = Vec::<u8>::new();
match format_input(input, &config, Some(&mut buf)) {
match format_input(input, config.get_rustfmt_config(), Some(&mut buf)) {
Ok((summary, ..)) => {
// format_input returns Ok even if there are any errors, i.e., parsing errors.
if summary.has_no_errors() {
Expand Down
45 changes: 44 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use toml;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use rustfmt::config::Config as RustfmtConfig;
use rustfmt::config::WriteMode;

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

// Just like the Config struct but with each property wrapped
// as Option<T>. This is used to parse a rustfmt.toml that doesn't
// as Option<T>. This is used to parse a rls.toml that doesn't
// specity all properties of `Config`.
// We first parse into `ParsedConfig`, then create a default `Config`
// and overwrite the properties with corresponding values from `ParsedConfig`
Expand Down Expand Up @@ -155,3 +157,44 @@ create_config! {
cfg_test: bool, true, false, "build cfg(test) code";
unstable_features: bool, false, false, "enable unstable features";
}

/// A rustfmt config (typically specified via rustfmt.toml)
/// The FmtConfig is not an exact translation of the config
/// rustfmt generates from the user's toml file, since when
/// using rustfmt with rls certain configuration options are
/// always used. See `FmtConfig::set_rls_options`
pub struct FmtConfig(RustfmtConfig);

impl FmtConfig {
/// Look for `.rustmt.toml` or `rustfmt.toml` in `path`, falling back
/// to the default config if neither exist
pub fn from(path: &Path) -> FmtConfig {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use the code in Rustfmt for this? Even if it means changing the API in Rustfmt a bit, I'd prefer that, rather than duplicating code, if we don't have to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a crack at this: rust-lang/rustfmt#1613

if let Ok((config, _)) = RustfmtConfig::from_resolved_toml_path(path) {
let mut config = FmtConfig(config);
config.set_rls_options();
return config;
}
FmtConfig::default()
}

/// Return an immutable borrow of the config, will always
/// have any relevant rls specific options set
pub fn get_rustfmt_config(&self) -> &RustfmtConfig {
&self.0
}

// options that are always used when formatting with rls
fn set_rls_options(&mut self) {
self.0.set().skip_children(true);
self.0.set().write_mode(WriteMode::Plain);
}
}

impl Default for FmtConfig {
fn default() -> FmtConfig {
let config = RustfmtConfig::default();
let mut config = FmtConfig(config);
config.set_rls_options();
config
}
}