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-09-30 03:58:53 -05:00
|
|
|
|
2020-04-23 11:50:25 -05:00
|
|
|
use std::{ffi::OsString, path::PathBuf};
|
|
|
|
|
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;
|
2019-03-07 14:06:25 -06:00
|
|
|
|
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-01 10:22:56 -05:00
|
|
|
pub lru_capacity: Option<usize>,
|
2020-04-23 11:50:25 -05:00
|
|
|
pub proc_macro_srv: Option<(PathBuf, Vec<OsString>)>,
|
2020-04-02 04:55:04 -05:00
|
|
|
pub files: FilesConfig,
|
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-02 04:55:04 -05:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct FilesConfig {
|
2020-04-02 05:47:58 -05:00
|
|
|
pub watcher: FilesWatcher,
|
|
|
|
pub exclude: Vec<String>,
|
2020-04-02 04:55:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2020-04-02 05:47:58 -05:00
|
|
|
pub enum FilesWatcher {
|
2020-04-02 04:55:04 -05:00
|
|
|
Client,
|
|
|
|
Notify,
|
|
|
|
}
|
|
|
|
|
2020-04-01 10:00:37 -05:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct NotificationsConfig {
|
|
|
|
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-24 09:08:45 -05:00
|
|
|
pub hierarchical_symbols: bool,
|
2020-04-26 17:54:05 -05:00
|
|
|
pub code_action_literals: bool,
|
2020-04-01 11:46:26 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
lru_capacity: None,
|
|
|
|
proc_macro_srv: None,
|
2020-04-02 04:55:04 -05:00
|
|
|
files: FilesConfig { watcher: FilesWatcher::Notify, exclude: Vec::new() },
|
2020-05-01 17:59:19 -05:00
|
|
|
notifications: NotificationsConfig { 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,
|
2020-05-05 15:44:39 -05:00
|
|
|
all_features: true,
|
2020-04-02 04:33:49 -05:00
|
|
|
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,
|
2020-04-23 19:06:12 -05:00
|
|
|
..CompletionConfig::default()
|
2020-04-01 11:41:43 -05:00
|
|
|
},
|
|
|
|
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);
|
2020-04-22 10:22:18 -05:00
|
|
|
set(value, "/diagnostics/enable", &mut self.publish_diagnostics);
|
2020-04-01 12:27:45 -05:00
|
|
|
set(value, "/lruCapacity", &mut self.lru_capacity);
|
2020-04-04 08:12:09 -05:00
|
|
|
self.files.watcher = match get(value, "/files/watcher") {
|
2020-04-04 08:04:49 -05:00
|
|
|
Some("client") => FilesWatcher::Client,
|
|
|
|
Some("notify") | _ => FilesWatcher::Notify
|
|
|
|
};
|
2020-04-02 04:55:04 -05:00
|
|
|
set(value, "/notifications/cargoTomlNotFound", &mut self.notifications.cargo_toml_not_found);
|
|
|
|
|
|
|
|
set(value, "/cargo/noDefaultFeatures", &mut self.cargo.no_default_features);
|
|
|
|
set(value, "/cargo/allFeatures", &mut self.cargo.all_features);
|
|
|
|
set(value, "/cargo/features", &mut self.cargo.features);
|
|
|
|
set(value, "/cargo/loadOutDirsFromCheck", &mut self.cargo.load_out_dirs_from_check);
|
2020-05-05 11:01:54 -05:00
|
|
|
set(value, "/cargo/target", &mut self.cargo.target);
|
2020-04-12 11:05:33 -05:00
|
|
|
|
2020-04-22 17:30:55 -05:00
|
|
|
match get(value, "/procMacro/enable") {
|
2020-04-12 11:05:33 -05:00
|
|
|
Some(true) => {
|
2020-04-16 15:42:05 -05:00
|
|
|
if let Ok(path) = std::env::current_exe() {
|
2020-04-23 11:50:25 -05:00
|
|
|
self.proc_macro_srv = Some((path, vec!["proc-macro".into()]));
|
2020-04-16 08:13:57 -05:00
|
|
|
}
|
2020-04-12 11:05:33 -05:00
|
|
|
}
|
|
|
|
_ => self.proc_macro_srv = None,
|
|
|
|
}
|
|
|
|
|
2020-04-06 23:41:31 -05:00
|
|
|
match get::<Vec<String>>(value, "/rustfmt/overrideCommand") {
|
|
|
|
Some(mut args) if !args.is_empty() => {
|
2020-04-02 07:30:28 -05:00
|
|
|
let command = args.remove(0);
|
|
|
|
self.rustfmt = RustfmtConfig::CustomCommand {
|
|
|
|
command,
|
|
|
|
args,
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 23:41:31 -05:00
|
|
|
_ => {
|
|
|
|
if let RustfmtConfig::Rustfmt { extra_args } = &mut self.rustfmt {
|
|
|
|
set(value, "/rustfmt/extraArgs", extra_args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-04-04 08:04:49 -05:00
|
|
|
|
2020-04-02 04:55:04 -05:00
|
|
|
if let Some(false) = get(value, "/checkOnSave/enable") {
|
2020-04-06 23:41:31 -05:00
|
|
|
// check is disabled
|
2020-04-04 08:04:49 -05:00
|
|
|
self.check = None;
|
2020-04-01 11:41:43 -05:00
|
|
|
} else {
|
2020-04-06 23:41:31 -05:00
|
|
|
// check is enabled
|
|
|
|
match get::<Vec<String>>(value, "/checkOnSave/overrideCommand") {
|
|
|
|
// first see if the user has completely overridden the command
|
|
|
|
Some(mut args) if !args.is_empty() => {
|
2020-04-02 07:30:28 -05:00
|
|
|
let command = args.remove(0);
|
|
|
|
self.check = Some(FlycheckConfig::CustomCommand {
|
|
|
|
command,
|
|
|
|
args,
|
2020-04-04 08:04:49 -05:00
|
|
|
});
|
2020-04-02 07:30:28 -05:00
|
|
|
}
|
2020-04-06 23:41:31 -05:00
|
|
|
// otherwise configure command customizations
|
|
|
|
_ => {
|
2020-05-05 15:44:39 -05:00
|
|
|
if let Some(FlycheckConfig::CargoCommand { command, extra_args, all_targets, all_features })
|
2020-04-06 23:41:31 -05:00
|
|
|
= &mut self.check
|
|
|
|
{
|
|
|
|
set(value, "/checkOnSave/extraArgs", extra_args);
|
|
|
|
set(value, "/checkOnSave/command", command);
|
|
|
|
set(value, "/checkOnSave/allTargets", all_targets);
|
2020-05-05 15:44:39 -05:00
|
|
|
set(value, "/checkOnSave/allFeatures", all_features);
|
2020-04-06 23:41:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-04-01 11:41:43 -05:00
|
|
|
|
2020-04-02 04:55:04 -05:00
|
|
|
set(value, "/inlayHints/typeHints", &mut self.inlay_hints.type_hints);
|
|
|
|
set(value, "/inlayHints/parameterHints", &mut self.inlay_hints.parameter_hints);
|
|
|
|
set(value, "/inlayHints/chainingHints", &mut self.inlay_hints.chaining_hints);
|
|
|
|
set(value, "/inlayHints/maxLength", &mut self.inlay_hints.max_length);
|
|
|
|
set(value, "/completion/postfix/enable", &mut self.completion.enable_postfix_completions);
|
|
|
|
set(value, "/completion/addCallParenthesis", &mut self.completion.add_call_parenthesis);
|
|
|
|
set(value, "/completion/addCallArgumentSnippets", &mut self.completion.add_call_argument_snippets);
|
|
|
|
set(value, "/callInfo/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-03-12 16:31:47 -05:00
|
|
|
|
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())
|
|
|
|
}
|
2019-03-07 14:06:25 -06:00
|
|
|
|
2020-04-04 08:04:49 -05:00
|
|
|
fn set<'a, T: Deserialize<'a>>(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
|
|
|
|
}
|
2019-08-06 06:34:28 -05:00
|
|
|
}
|
2019-03-07 14:06:25 -06:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2020-04-24 09:08:45 -05:00
|
|
|
if let Some(value) =
|
|
|
|
caps.document_symbol.as_ref().and_then(|it| it.hierarchical_document_symbol_support)
|
|
|
|
{
|
|
|
|
self.client_caps.hierarchical_symbols = value
|
|
|
|
}
|
2020-04-26 17:54:05 -05:00
|
|
|
if let Some(value) =
|
|
|
|
caps.code_action.as_ref().and_then(|it| Some(it.code_action_literal_support.is_some()))
|
|
|
|
{
|
|
|
|
self.client_caps.code_action_literals = value;
|
|
|
|
}
|
2020-04-23 19:39:07 -05:00
|
|
|
self.completion.allow_snippets(false);
|
|
|
|
if let Some(completion) = &caps.completion {
|
|
|
|
if let Some(completion_item) = &completion.completion_item {
|
|
|
|
if let Some(value) = completion_item.snippet_support {
|
|
|
|
self.completion.allow_snippets(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-07 14:06:25 -06:00
|
|
|
}
|
|
|
|
}
|