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

171 lines
6.3 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.
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};
2020-04-01 11:51:16 -05:00
use ra_project_model::CargoConfig;
2020-04-01 11:41:43 -05:00
use serde::Deserialize;
2020-04-01 07:32:04 -05:00
#[derive(Debug, Clone)]
pub struct Config {
2020-04-01 11:46:26 -05:00
pub client_caps: ClientCapsConfig,
2020-04-02 04:33:49 -05:00
pub with_sysroot: bool,
2020-04-01 10:00:37 -05:00
pub publish_diagnostics: bool,
2020-04-02 04:33:49 -05:00
pub use_client_watching: bool,
// TODO: move to experimental capabilities
2020-04-01 07:32:04 -05:00
pub vscode_lldb: bool,
2020-04-01 10:22:56 -05:00
pub lru_capacity: Option<usize>,
2020-04-02 04:33:49 -05:00
pub proc_macro_srv: Option<String>,
2020-04-01 10:22:56 -05:00
pub exclude_globs: Vec<String>,
2020-04-02 04:33:49 -05:00
pub notifications: NotificationsConfig,
2020-04-01 11:51:16 -05:00
pub cargo: CargoConfig,
2020-04-02 04:33:49 -05:00
pub rustfmt: RustfmtConfig,
pub check: Option<FlycheckConfig>,
pub inlay_hints: InlayHintsConfig,
pub completion: CompletionConfig,
pub call_info_full: bool,
2020-04-01 07:32:04 -05:00
}
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>,
},
}
2020-04-01 11:46:26 -05:00
#[derive(Debug, Clone, Default)]
pub struct ClientCapsConfig {
pub location_link: bool,
pub line_folding_only: bool,
}
2020-04-01 11:41:43 -05:00
impl Default for Config {
fn default() -> Self {
Config {
2020-04-02 04:33:49 -05:00
client_caps: ClientCapsConfig::default(),
with_sysroot: true,
2020-04-01 11:41:43 -05:00
publish_diagnostics: true,
2020-04-02 04:33:49 -05:00
use_client_watching: false,
vscode_lldb: false,
lru_capacity: None,
proc_macro_srv: None,
exclude_globs: Vec::new(),
2020-04-01 11:41:43 -05:00
notifications: NotificationsConfig {
workspace_loaded: true,
cargo_toml_not_found: true,
},
2020-04-02 04:33:49 -05:00
cargo: CargoConfig::default(),
rustfmt: RustfmtConfig::Rustfmt { extra_args: Vec::new() },
check: Some(FlycheckConfig::CargoCommand {
command: "check".to_string(),
all_targets: true,
extra_args: Vec::new(),
}),
2020-04-01 11:41:43 -05:00
inlay_hints: InlayHintsConfig {
type_hints: true,
parameter_hints: true,
chaining_hints: true,
max_length: None,
},
completion: CompletionConfig {
enable_postfix_completions: true,
add_call_parenthesis: true,
add_call_argument_snippets: true,
},
call_info_full: true,
2020-04-01 10:00:37 -05:00
}
}
}
2020-04-01 11:41:43 -05:00
impl Config {
#[rustfmt::skip]
pub fn update(&mut self, value: &serde_json::Value) {
2020-04-01 12:27:45 -05:00
log::info!("Config::update({:#})", value);
2020-04-01 11:46:26 -05:00
let client_caps = self.client_caps.clone();
2020-04-01 11:41:43 -05:00
*self = Default::default();
2020-04-01 11:46:26 -05:00
self.client_caps = client_caps;
2020-04-01 11:41:43 -05:00
2020-04-02 04:33:49 -05:00
set(value, "/withSysroot", &mut self.with_sysroot);
set(value, "/featureFlags/lsp.diagnostics", &mut self.publish_diagnostics);
2020-04-01 12:27:45 -05:00
set(value, "/useClientWatching", &mut self.use_client_watching);
2020-04-02 04:33:49 -05:00
set(value, "/vscodeLldb", &mut self.vscode_lldb);
2020-04-01 12:27:45 -05:00
set(value, "/lruCapacity", &mut self.lru_capacity);
2020-04-02 04:33:49 -05:00
set(value, "/excludeGlobs", &mut self.exclude_globs);
set(value, "/featureFlags/notifications.workspace-loaded", &mut self.notifications.workspace_loaded);
set(value, "/featureFlags/notifications.cargo-toml-not-found", &mut self.notifications.cargo_toml_not_found);
2020-04-01 11:41:43 -05:00
2020-04-02 04:33:49 -05:00
set(value, "/cargoFeatures/noDefaultFeatures", &mut self.cargo.no_default_features);
set(value, "/cargoFeatures/allFeatures", &mut self.cargo.all_features);
set(value, "/cargoFeatures/features", &mut self.cargo.features);
set(value, "/cargoFeatures/loadOutDirsFromCheck", &mut self.cargo.load_out_dirs_from_check);
if let RustfmtConfig::Rustfmt { extra_args } = &mut self.rustfmt {
set(value, "/rustfmtArgs", extra_args);
}
2020-04-01 11:41:43 -05:00
if let Some(false) = get(value, "cargo_watch_enable") {
self.check = None
} else {
if let Some(FlycheckConfig::CargoCommand { command, extra_args, all_targets }) = &mut self.check
{
2020-04-01 12:27:45 -05:00
set(value, "/cargoWatchArgs", extra_args);
set(value, "/cargoWatchCommand", command);
set(value, "/cargoWatchAllTargets", all_targets);
2020-04-01 11:41:43 -05:00
}
};
2020-04-02 04:33:49 -05:00
set(value, "/inlayHintsType", &mut self.inlay_hints.type_hints);
set(value, "/inlayHintsParameter", &mut self.inlay_hints.parameter_hints);
set(value, "/inlayHintsChaining", &mut self.inlay_hints.chaining_hints);
set(value, "/inlayHintsMaxLength", &mut self.inlay_hints.max_length);
2020-04-01 12:27:45 -05:00
set(value, "/featureFlags/completion.enable-postfix", &mut self.completion.enable_postfix_completions);
set(value, "/featureFlags/completion.insertion.add-call-parenthesis", &mut self.completion.add_call_parenthesis);
set(value, "/featureFlags/completion.insertion.add-argument-snippets", &mut self.completion.add_call_argument_snippets);
set(value, "/featureFlags/call-info.full", &mut self.call_info_full);
2020-02-17 02:44:58 -06:00
2020-04-01 12:27:45 -05:00
log::info!("Config::update() = {:#?}", self);
2020-04-01 11:41:43 -05:00
fn get<'a, T: Deserialize<'a>>(value: &'a serde_json::Value, pointer: &str) -> Option<T> {
value.pointer(pointer).and_then(|it| T::deserialize(it).ok())
}
2020-04-01 12:27:45 -05:00
fn set<'a, T: Deserialize<'a> + std::fmt::Debug>(value: &'a serde_json::Value, pointer: &str, slot: &mut T) {
2020-04-01 11:41:43 -05:00
if let Some(new_value) = get(value, pointer) {
*slot = new_value
}
}
}
2020-04-01 11:41:43 -05:00
pub fn update_caps(&mut self, caps: &TextDocumentClientCapabilities) {
if let Some(value) = caps.definition.as_ref().and_then(|it| it.link_support) {
2020-04-01 11:46:26 -05:00
self.client_caps.location_link = value;
2020-04-01 11:41:43 -05:00
}
if let Some(value) = caps.folding_range.as_ref().and_then(|it| it.line_folding_only) {
2020-04-01 11:46:26 -05:00
self.client_caps.line_folding_only = value
2020-04-01 11:41:43 -05:00
}
}
}