From cebaa9840b2af1a57755891b95ebc8a023add51e Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Wed, 6 Dec 2017 17:12:46 +0100 Subject: [PATCH] Make rustfmt an optional dependency --- .travis.yml | 19 ++++++++++++++++--- Cargo.toml | 6 +++++- src/actions/mod.rs | 5 ++++- src/actions/requests.rs | 29 +++++++++++++++++++++++++++++ src/config.rs | 5 +++++ src/main.rs | 1 + 6 files changed, 60 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5073b00db5e..3d32685914b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/Cargo.toml b/Cargo.toml index 4fac1a6345b..022d3bfd231 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] diff --git a/src/actions/mod.rs b/src/actions/mod.rs index fc253ea6d94..3ae92103ba2 100644 --- a/src/actions/mod.rs +++ b/src/actions/mod.rs @@ -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; @@ -158,6 +160,7 @@ impl InitActionContext { } } + #[cfg(feature = "rustfmt")] fn fmt_config(&self) -> FmtConfig { FmtConfig::from(&self.current_project) } diff --git a/src/actions/requests.rs b/src/actions/requests.rs index 6ae8aa25996..2b949de2987 100644 --- a/src/actions/requests.rs +++ b/src/actions/requests.rs @@ -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; @@ -778,6 +781,7 @@ impl RequestAction for Formatting { )) } + #[cfg(feature = "rustfmt")] fn handle( &mut self, ctx: InitActionContext, @@ -785,6 +789,18 @@ impl RequestAction for Formatting { ) -> Result { reformat(params.text_document, None, ¶ms.options, ctx) } + + #[cfg(not(feature = "rustfmt"))] + fn handle( + &mut self, + _: InitActionContext, + _: Self::Params, + ) -> Result { + Err(ResponseError::Message( + ErrorCode::InternalError, + "rustfmt was not distributed with this rls release".into(), + )) + } } /// Pretty print the source within the given location range. @@ -809,6 +825,7 @@ impl RequestAction for RangeFormatting { )) } + #[cfg(feature = "rustfmt")] fn handle( &mut self, ctx: InitActionContext, @@ -821,8 +838,20 @@ impl RequestAction for RangeFormatting { ctx, ) } + #[cfg(not(feature = "rustfmt"))] + fn handle( + &mut self, + _: InitActionContext, + _: Self::Params, + ) -> Result { + Err(ResponseError::Message( + ErrorCode::InternalError, + "rustfmt was not distributed with this rls release".into(), + )) + } } +#[cfg(feature = "rustfmt")] fn reformat( doc: TextDocumentIdentifier, selection: Option, diff --git a/src/config.rs b/src/config.rs index 7a988a55673..eda9309a1b8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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; @@ -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 @@ -309,6 +313,7 @@ impl FmtConfig { } } +#[cfg(feature = "rustfmt")] impl Default for FmtConfig { fn default() -> FmtConfig { let config = RustfmtConfig::default(); diff --git a/src/main.rs b/src/main.rs index 4611ed80668..2442a77a636 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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]