forked from rust-lang/rls
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.rs
More file actions
411 lines (364 loc) · 14.6 KB
/
config.rs
File metadata and controls
411 lines (364 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Configuration for the workspace that RLS is operating within and options for
//! tweaking the RLS's behavior itself.
use std::marker::PhantomData;
use std::str::FromStr;
use std::fmt;
use std::env;
use std::fmt::Debug;
use std::io::sink;
use std::path::{Path, PathBuf};
use cargo::CargoResult;
use cargo::util::{homedir, important_paths, Config as CargoConfig};
use cargo::core::{Shell, Workspace};
use failure;
use serde;
use serde::de::{Deserialize, Deserializer, Visitor};
use rustfmt::config::Config as RustfmtConfig;
use rustfmt::config::WriteMode;
const DEFAULT_WAIT_TO_BUILD: u64 = 1500;
/// Some values in the config can be inferred without an explicit value set by
/// the user. There are no guarantees which values will or will not be passed
/// to the server, so we treat deserialized values effectively as `Option<T>`
/// and use `None` to mark the values as unspecified, otherwise we always use
/// `Specified` variant for the deserialized values. For user-provided `None`
/// values, they must be `Inferred` prior to usage (and can be further
/// `Specified` by the user).
#[derive(Clone, Debug, Serialize)]
pub enum Inferrable<T> {
/// Explicitly specified value by the user. Retrieved by deserializing a
/// non-`null` value. Can replace every other variant.
Specified(T),
/// Value that's inferred by the server. Can't replace a `Specified` variant.
Inferred(T),
/// Marker value that's retrieved when deserializing a user-specified `null`
/// value. Can't be used alone and has to be replaced by server-`Inferred`
/// or user-`Specified` value.
None,
}
// Deserialize as if it's `Option<T>` and use `None` variant if it's `None`,
// otherwise use `Specified` variant for deserialized value.
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Inferrable<T> {
fn deserialize<D>(deserializer: D) -> Result<Inferrable<T>, D::Error>
where
D: Deserializer<'de>,
{
let value = Option::<T>::deserialize(deserializer)?;
Ok(match value {
None => Inferrable::None,
Some(value) => Inferrable::Specified(value),
})
}
}
impl<T> Inferrable<T> {
pub fn is_none(&self) -> bool {
match *self {
Inferrable::None => true,
_ => false,
}
}
}
impl<T: Clone + Debug> Inferrable<T> {
/// Combine these inferrable values, preferring our own specified values
/// when possible, and falling back the given default value.
pub fn combine_with_default(&self, new: &Self, default: T) -> Self {
match (self, new) {
// Don't allow to update a Specified value with an Inferred one
(&Inferrable::Specified(_), &Inferrable::Inferred(_)) => self.clone(),
// When trying to update with a `None`, use Inferred variant with
// a specified default value, as `None` value can't be used directly
(_, &Inferrable::None) => Inferrable::Inferred(default),
_ => new.clone(),
}
}
/// Infer the given value if we don't already have an explicitly specified
/// value.
pub fn infer(&mut self, value: T) {
if let Inferrable::Specified(_) = *self {
trace!("Trying to infer {:?} on a {:?}", value, self);
return;
}
*self = Inferrable::Inferred(value);
}
}
impl<T> AsRef<T> for Inferrable<T> {
fn as_ref(&self) -> &T {
match *self {
Inferrable::Inferred(ref value) | Inferrable::Specified(ref value) => value,
// Default values should always be initialized as `Inferred` even
// before actual inference takes place, `None` variant is only used
// when deserializing and should not be read directly (via `as_ref`)
Inferrable::None => unreachable!(),
}
}
}
/// RLS configuration options.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[allow(missing_docs)]
#[serde(default)]
pub struct Config {
pub sysroot: Option<String>,
pub target: Option<String>,
pub rustflags: Option<String>,
pub build_lib: Inferrable<bool>,
pub build_bin: Inferrable<Option<String>>,
pub cfg_test: bool,
pub unstable_features: bool,
pub wait_to_build: u64,
pub show_warnings: bool,
pub goto_def_racer_fallback: bool,
pub workspace_mode: bool,
/// Clear the RUST_LOG env variable before calling rustc/cargo? Default: true
pub clear_env_rust_log: bool,
/// Build the project only when a file got saved and not on file change. Default: false
pub build_on_save: bool,
pub use_crate_blacklist: bool,
/// Cargo target dir. If set overrides the default one.
pub target_dir: Inferrable<Option<PathBuf>>,
pub features: Vec<String>,
pub all_features: bool,
pub no_default_features: bool,
pub jobs: Option<u32>,
pub all_targets: bool,
/// Enable use of racer for `textDocument/completion` requests
pub racer_completion: bool,
#[serde(deserialize_with = "deserialize_clippy_preference")]
pub clippy_preference: ClippyPreference,
}
impl Default for Config {
fn default() -> Config {
let mut result = Config {
sysroot: None,
target: None,
rustflags: None,
build_lib: Inferrable::Inferred(false),
build_bin: Inferrable::Inferred(None),
cfg_test: false,
unstable_features: false,
wait_to_build: DEFAULT_WAIT_TO_BUILD,
show_warnings: true,
goto_def_racer_fallback: false,
workspace_mode: true,
clear_env_rust_log: true,
build_on_save: false,
use_crate_blacklist: true,
target_dir: Inferrable::Inferred(None),
features: vec![],
all_features: false,
no_default_features: false,
jobs: None,
all_targets: false,
racer_completion: true,
clippy_preference: ClippyPreference::OptIn,
};
result.normalise();
result
}
}
impl Config {
/// Join this configuration with the new config.
pub fn update(&mut self, mut new: Config) {
new.target_dir = self.target_dir.combine_with_default(&new.target_dir, None);
new.build_lib = self.build_lib.combine_with_default(&new.build_lib, false);
new.build_bin = self.build_bin.combine_with_default(&new.build_bin, None);
// Ignore requests to disable workspace mode.
self.workspace_mode = true;
*self = new;
}
/// Ensures that unstable options are only allowed if `unstable_features` is
/// true and that is not allowed on stable release channels.
pub fn normalise(&mut self) {
let allow_unstable = option_env!("CFG_RELEASE_CHANNEL")
.map(|c| c == "nightly")
.unwrap_or(true);
if !allow_unstable {
if self.unstable_features {
eprintln!("`unstable_features` setting can only be used on nightly channel");
}
self.unstable_features = false;
}
if !self.unstable_features {
// Force-set any unstable features here.
}
}
/// Is this config incomplete, and needs additional values to be inferred?
pub fn needs_inference(&self) -> bool {
self.build_bin.is_none() ||
self.build_lib.is_none() ||
self.target_dir.is_none()
}
/// Tries to auto-detect certain option values if they were unspecified.
/// Specifically, this:
/// - tries to infer `build_bin` and `build_lib` under `workspace_mode: false`
/// - detects correct `target/` build directory used by Cargo, if not specified.
pub fn infer_defaults(&mut self, project_dir: &Path) -> CargoResult<()> {
// Note that this may not be equal build_dir when inside a workspace member
let manifest_path = important_paths::find_root_manifest_for_wd(project_dir)?;
trace!("root manifest_path: {:?}", &manifest_path);
let shell = Shell::from_write(Box::new(sink()));
let cwd = env::current_dir().expect("failed to get cwd");
let config = CargoConfig::new(
shell,
cwd.to_path_buf(),
homedir(project_dir).unwrap(),
);
let ws = Workspace::new(&manifest_path, &config)?;
// Constructing a `Workspace` also probes the filesystem and detects where to place the
// build artifacts. We need to rely on Cargo's behaviour directly not to possibly place our
// own artifacts somewhere else (e.g. when analyzing only a single crate in a workspace)
match self.target_dir {
// We require an absolute path, so adjust a relative one if it's passed.
Inferrable::Specified(Some(ref mut path)) if path.is_relative() => {
*path = project_dir.join(&path);
}
_ => {},
}
if self.target_dir.as_ref().is_none() {
let target_dir = ws.target_dir().clone().into_path_unlocked();
let target_dir = target_dir.join("rls");
self.target_dir.infer(Some(target_dir));
trace!(
"For project path {:?} Cargo told us to use this target/ dir: {:?}",
project_dir,
self.target_dir.as_ref().as_ref().unwrap(),
);
}
// Finish if we're in workspace_mode, inferring `build_bin` and
// `build_lib` only matters if we're in single package mode.
if self.workspace_mode {
return Ok(());
}
let package = ws.current()?;
trace!(
"infer_config_defaults: Auto-detected `{}` package",
package.name()
);
let targets = package.targets();
let (lib, bin) = if targets.iter().any(|x| x.is_lib()) {
(true, None)
} else {
let mut bins = targets.iter().filter(|x| x.is_bin());
// No `lib` detected, but also can't find any `bin` target - there's
// no sensible target here, so just Err out
let first = bins.nth(0)
.ok_or_else(|| failure::err_msg("No `bin` or `lib` targets in the package"))?;
let mut bins = targets.iter().filter(|x| x.is_bin());
let target = match bins.find(|x| x.src_path().ends_with("main.rs")) {
Some(main_bin) => main_bin,
None => first,
};
(false, Some(target.name().to_owned()))
};
trace!(
"infer_config_defaults: build_lib: {:?}, build_bin: {:?}",
lib,
bin
);
// Unless crate target is explicitly specified, mark the values as
// inferred, so they're not simply ovewritten on config change without
// any specified value
let (lib, bin) = match (&self.build_lib, &self.build_bin) {
(&Inferrable::Specified(true), _) => (lib, None),
(_, &Inferrable::Specified(Some(_))) => (false, bin),
_ => (lib, bin),
};
self.build_lib.infer(lib);
self.build_bin.infer(bin);
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum ClippyPreference {
/// Disable clippy
Off,
/// Enable clippy, but "allow" clippy lints (ie require "warn" override)
OptIn,
/// Enable clippy
On,
}
/// Permissive deserialization for `ClippyPreference`
/// "opt-in", "Optin" -> `ClippyPreference::OptIn`
impl FromStr for ClippyPreference {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"off" => Ok(ClippyPreference::Off),
"optin" | "opt-in" => Ok(ClippyPreference::OptIn),
"on" => Ok(ClippyPreference::On),
_ => Err(()),
}
}
}
/// Permissive custom deserialization for `ClippyPreference` using `FromStr`
fn deserialize_clippy_preference<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: Deserialize<'de> + FromStr<Err = ()>,
D: Deserializer<'de>,
{
struct ClippyPrefDeserializer<T>(PhantomData<fn() -> T>);
impl<'de, T> Visitor<'de> for ClippyPrefDeserializer<T>
where
T: Deserialize<'de> + FromStr<Err = ()>,
{
type Value = T;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(r#"on", "opt-in" or "off""#)
}
fn visit_str<E: serde::de::Error>(self, value: &str) -> Result<T, E> {
FromStr::from_str(value)
.map_err(|_| serde::de::Error::unknown_variant(value, &["on", "opt-in", "off"]))
}
}
deserializer.deserialize_any(ClippyPrefDeserializer(PhantomData))
}
/// 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 {
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
}
}
#[test]
fn clippy_preference_from_str() {
assert_eq!(ClippyPreference::from_str("Optin"), Ok(ClippyPreference::OptIn));
assert_eq!(ClippyPreference::from_str("OFF"), Ok(ClippyPreference::Off));
assert_eq!(ClippyPreference::from_str("opt-in"), Ok(ClippyPreference::OptIn));
assert_eq!(ClippyPreference::from_str("on"), Ok(ClippyPreference::On));
}