rust/crates/rust-analyzer/src/config.rs

222 lines
7.2 KiB
Rust
Raw Normal View History

2019-10-25 01:00:30 -05:00
//! Config used by the language server.
//!
//! We currently get this config from `initialize` LSP request, which is not the
//! best way to do it, but was the simplest thing we could implement.
//!
//! Of particular interest is the `feature_flags` hash map: while other fields
//! configure the server itself, feature flags are passed into analysis, and
//! tweak things like automatic insertion of `()` in completions.
2019-08-22 06:44:16 -05:00
use rustc_hash::FxHashMap;
2020-04-01 10:00:37 -05:00
use crate::feature_flags::FeatureFlags;
2020-04-01 07:32:04 -05:00
use lsp_types::TextDocumentClientCapabilities;
use ra_flycheck::FlycheckConfig;
2020-04-01 10:00:37 -05:00
use ra_ide::{CompletionConfig, InlayHintsConfig};
2019-12-13 04:16:34 -06:00
use ra_project_model::CargoFeatures;
use serde::{Deserialize, Deserializer};
2020-04-01 07:32:04 -05:00
#[derive(Debug, Clone)]
pub struct Config {
pub publish_decorations: bool,
2020-04-01 10:00:37 -05:00
pub publish_diagnostics: bool,
pub notifications: NotificationsConfig,
2020-04-01 07:32:04 -05:00
pub supports_location_link: bool,
pub line_folding_only: bool,
pub inlay_hints: InlayHintsConfig,
2020-04-01 10:00:37 -05:00
pub completion: CompletionConfig,
pub call_info_full: bool,
2020-04-01 07:32:04 -05:00
pub rustfmt: RustfmtConfig,
pub check: Option<FlycheckConfig>,
pub vscode_lldb: bool,
pub proc_macro_srv: Option<String>,
}
2020-04-01 10:00:37 -05:00
#[derive(Debug, Clone)]
pub struct NotificationsConfig {
pub workspace_loaded: bool,
pub cargo_toml_not_found: bool,
}
2020-04-01 07:32:04 -05:00
#[derive(Debug, Clone)]
pub enum RustfmtConfig {
Rustfmt {
extra_args: Vec<String>,
},
#[allow(unused)]
CustomCommand {
command: String,
args: Vec<String>,
},
}
impl Default for RustfmtConfig {
fn default() -> Self {
RustfmtConfig::Rustfmt { extra_args: Vec::new() }
}
}
pub(crate) fn get_config(
config: &ServerConfig,
text_document_caps: Option<&TextDocumentClientCapabilities>,
) -> Config {
2020-04-01 10:00:37 -05:00
let feature_flags = get_feature_flags(config);
2020-04-01 07:32:04 -05:00
Config {
publish_decorations: config.publish_decorations,
2020-04-01 10:00:37 -05:00
publish_diagnostics: feature_flags.get("lsp.diagnostics"),
notifications: NotificationsConfig {
workspace_loaded: feature_flags.get("notifications.workspace-loaded"),
cargo_toml_not_found: feature_flags.get("notifications.cargo-toml-not-found"),
},
2020-04-01 07:32:04 -05:00
supports_location_link: text_document_caps
.and_then(|it| it.definition)
.and_then(|it| it.link_support)
.unwrap_or(false),
line_folding_only: text_document_caps
.and_then(|it| it.folding_range.as_ref())
.and_then(|it| it.line_folding_only)
.unwrap_or(false),
inlay_hints: InlayHintsConfig {
type_hints: config.inlay_hints_type,
parameter_hints: config.inlay_hints_parameter,
chaining_hints: config.inlay_hints_chaining,
max_length: config.inlay_hints_max_length,
},
2020-04-01 10:00:37 -05:00
completion: CompletionConfig {
enable_postfix_completions: feature_flags.get("completion.enable-postfix"),
add_call_parenthesis: feature_flags.get("completion.insertion.add-call-parenthesis"),
add_call_argument_snippets: feature_flags
.get("completion.insertion.add-argument-snippets"),
},
call_info_full: feature_flags.get("call-info.full"),
2020-04-01 07:32:04 -05:00
check: if config.cargo_watch_enable {
Some(FlycheckConfig::CargoCommand {
command: config.cargo_watch_command.clone(),
all_targets: config.cargo_watch_all_targets,
extra_args: config.cargo_watch_args.clone(),
})
} else {
None
},
rustfmt: RustfmtConfig::Rustfmt { extra_args: config.rustfmt_args.clone() },
vscode_lldb: config.vscode_lldb,
proc_macro_srv: None, // FIXME: get this from config
}
}
2020-04-01 10:00:37 -05:00
fn get_feature_flags(config: &ServerConfig) -> FeatureFlags {
let mut ff = FeatureFlags::default();
for (flag, &value) in &config.feature_flags {
if ff.set(flag.as_str(), value).is_err() {
log::error!("unknown feature flag: {:?}", flag);
}
}
log::info!("feature_flags: {:#?}", ff);
ff
}
/// Client provided initialization options
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase", default)]
2019-08-06 06:12:58 -05:00
pub struct ServerConfig {
/// Whether the client supports our custom highlighting publishing decorations.
/// This is different to the highlightingOn setting, which is whether the user
/// wants our custom highlighting to be used.
///
/// Defaults to `false`
#[serde(deserialize_with = "nullable_bool_false")]
pub publish_decorations: bool,
pub exclude_globs: Vec<String>,
2019-09-06 08:25:24 -05:00
#[serde(deserialize_with = "nullable_bool_false")]
pub use_client_watching: bool,
2019-06-07 12:49:29 -05:00
pub lru_capacity: Option<usize>,
2019-08-19 07:41:18 -05:00
2020-03-12 12:01:36 -05:00
#[serde(deserialize_with = "nullable_bool_true")]
pub inlay_hints_type: bool,
#[serde(deserialize_with = "nullable_bool_true")]
pub inlay_hints_parameter: bool,
#[serde(deserialize_with = "nullable_bool_true")]
pub inlay_hints_chaining: bool,
2020-03-12 12:01:36 -05:00
pub inlay_hints_max_length: Option<usize>,
pub cargo_watch_enable: bool,
pub cargo_watch_args: Vec<String>,
pub cargo_watch_command: String,
pub cargo_watch_all_targets: bool,
2019-08-19 07:41:18 -05:00
/// For internal usage to make integrated tests faster.
#[serde(deserialize_with = "nullable_bool_true")]
pub with_sysroot: bool,
2019-08-22 06:44:16 -05:00
/// Fine grained feature flags to disable specific features.
pub feature_flags: FxHashMap<String, bool>,
2019-12-13 04:16:34 -06:00
2020-02-17 02:44:58 -06:00
pub rustfmt_args: Vec<String>,
2019-12-13 04:16:34 -06:00
/// Cargo feature configurations.
pub cargo_features: CargoFeatures,
/// Enabled if the vscode_lldb extension is available.
pub vscode_lldb: bool,
}
2019-08-06 06:12:58 -05:00
impl Default for ServerConfig {
fn default() -> ServerConfig {
ServerConfig {
publish_decorations: false,
exclude_globs: Vec::new(),
2019-09-06 08:25:24 -05:00
use_client_watching: false,
lru_capacity: None,
2020-03-12 12:01:36 -05:00
inlay_hints_type: true,
inlay_hints_parameter: true,
inlay_hints_chaining: true,
2020-03-12 12:01:36 -05:00
inlay_hints_max_length: None,
cargo_watch_enable: true,
cargo_watch_args: Vec::new(),
cargo_watch_command: "check".to_string(),
cargo_watch_all_targets: true,
2019-08-19 07:41:18 -05:00
with_sysroot: true,
2019-08-22 06:44:16 -05:00
feature_flags: FxHashMap::default(),
2019-12-13 04:16:34 -06:00
cargo_features: Default::default(),
2020-02-17 02:44:58 -06:00
rustfmt_args: Vec::new(),
vscode_lldb: false,
}
}
}
/// Deserializes a null value to a bool false by default
fn nullable_bool_false<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
D: Deserializer<'de>,
{
let opt = Option::deserialize(deserializer)?;
Ok(opt.unwrap_or(false))
}
/// Deserializes a null value to a bool true by default
fn nullable_bool_true<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
D: Deserializer<'de>,
{
let opt = Option::deserialize(deserializer)?;
Ok(opt.unwrap_or(true))
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn deserialize_init_options_defaults() {
// check that null == default for both fields
2019-08-06 06:12:58 -05:00
let default = ServerConfig::default();
assert_eq!(default, serde_json::from_str(r#"{}"#).unwrap());
assert_eq!(
default,
2019-10-25 01:00:30 -05:00
serde_json::from_str(r#"{"publishDecorations":null, "lruCapacity":null}"#).unwrap()
);
}
}