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

Make rustfmt an optional dependency #617

Merged
merged 1 commit into from
Dec 7, 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
19 changes: 16 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@ os:
- osx
rust:
- nightly
env:
- RUSTFMT=true
matrix:
include:
rust: nightly
env: RUSTFMT=false
os: linux
install:
# Required for Racer autoconfiguration
rustup component add rust-src
script:
script: |
#!/bin/bash
# compile #[cfg(not(test))] code
- cargo build --verbose
- cargo test --verbose
if [ "$RUSTFMT" == "false" ] ; then
cargo build --verbose --no-default-features
cargo test --verbose --no-default-features
else
cargo build --verbose
cargo test --verbose
fi
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ rls-data = { version = "0.13", features = ["serialize-serde"] }
rls-rustc = "0.1"
rls-span = { version = "0.4", features = ["serialize-serde"] }
rls-vfs = { version = "0.4", features = ["racer-impls"] }
rustfmt-nightly = "0.2.17"
rustfmt-nightly = { version = "0.2.17", optional = true }
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
url = "1.1.0"
rayon = "0.9"

[features]
default = ["rustfmt"]
rustfmt = ["rustfmt-nightly"]
5 changes: 4 additions & 1 deletion src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

use analysis::AnalysisHost;
use vfs::Vfs;
use config::{Config, FmtConfig};
#[cfg(feature = "rustfmt")]
use config::FmtConfig;
use config::Config;
use serde_json;
use url::Url;
use span;
Expand Down Expand Up @@ -158,6 +160,7 @@ impl InitActionContext {
}
}

#[cfg(feature = "rustfmt")]
fn fmt_config(&self) -> FmtConfig {
FmtConfig::from(&self.current_project)
}
Expand Down
29 changes: 29 additions & 0 deletions src/actions/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
use actions::{ActionContext, InitActionContext};
use data;
use url::Url;
#[cfg(feature = "rustfmt")]
use vfs::FileContents;
use racer;
#[cfg(feature = "rustfmt")]
use rustfmt::{format_input, Input as FmtInput};
#[cfg(feature = "rustfmt")]
use rustfmt::file_lines::{FileLines, Range as RustfmtRange};
use serde_json;
use span;
Expand Down Expand Up @@ -778,13 +781,26 @@ impl RequestAction for Formatting {
))
}

#[cfg(feature = "rustfmt")]
fn handle(
&mut self,
ctx: InitActionContext,
params: Self::Params,
) -> Result<Self::Response, ResponseError> {
reformat(params.text_document, None, &params.options, ctx)
}

#[cfg(not(feature = "rustfmt"))]
fn handle(
&mut self,
_: InitActionContext,
_: Self::Params,
) -> Result<Self::Response, ResponseError> {
Err(ResponseError::Message(
ErrorCode::InternalError,
"rustfmt was not distributed with this rls release".into(),
))
}
}

/// Pretty print the source within the given location range.
Expand All @@ -809,6 +825,7 @@ impl RequestAction for RangeFormatting {
))
}

#[cfg(feature = "rustfmt")]
fn handle(
&mut self,
ctx: InitActionContext,
Expand All @@ -821,8 +838,20 @@ impl RequestAction for RangeFormatting {
ctx,
)
}
#[cfg(not(feature = "rustfmt"))]
fn handle(
&mut self,
_: InitActionContext,
_: Self::Params,
) -> Result<Self::Response, ResponseError> {
Err(ResponseError::Message(
ErrorCode::InternalError,
"rustfmt was not distributed with this rls release".into(),
))
}
}

#[cfg(feature = "rustfmt")]
fn reformat(
doc: TextDocumentIdentifier,
selection: Option<Range>,
Expand Down
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ use cargo::core::{Shell, Workspace};

use serde::de::{Deserialize, Deserializer};

#[cfg(feature = "rustfmt")]
use rustfmt::config::Config as RustfmtConfig;
#[cfg(feature = "rustfmt")]
use rustfmt::config::WriteMode;

const DEFAULT_WAIT_TO_BUILD: u64 = 500;
Expand Down Expand Up @@ -282,8 +284,10 @@ impl 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`
#[cfg(feature = "rustfmt")]
pub struct FmtConfig(RustfmtConfig);

#[cfg(feature = "rustfmt")]
impl FmtConfig {
/// Look for `.rustmt.toml` or `rustfmt.toml` in `path`, falling back
/// to the default config if neither exist
Expand All @@ -309,6 +313,7 @@ impl FmtConfig {
}
}

#[cfg(feature = "rustfmt")]
impl Default for FmtConfig {
fn default() -> FmtConfig {
let config = RustfmtConfig::default();
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ extern crate rls_data as data;
extern crate rls_rustc as rustc_shim;
extern crate rls_span as span;
extern crate rls_vfs as vfs;
#[cfg(feature = "rustfmt")]
extern crate rustfmt_nightly as rustfmt;
extern crate serde;
#[macro_use]
Expand Down