Auto merge of #11985 - Veykril:config-valid, r=Veykril

minor: Simplify config.rs a bit
This commit is contained in:
bors 2022-04-14 10:16:58 +00:00
commit 5620d25972

View file

@ -380,6 +380,8 @@ pub struct Config {
snippets: Vec<Snippet>, snippets: Vec<Snippet>,
} }
type ParallelPrimeCachesNumThreads = u8;
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub enum LinkedProject { pub enum LinkedProject {
ProjectManifest(ProjectManifest), ProjectManifest(ProjectManifest),
@ -400,9 +402,14 @@ impl From<ProjectJson> for LinkedProject {
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct LensConfig { pub struct LensConfig {
// runnables
pub run: bool, pub run: bool,
pub debug: bool, pub debug: bool,
// implementations
pub implementations: bool, pub implementations: bool,
// references
pub method_refs: bool, pub method_refs: bool,
pub refs: bool, // for Struct, Enum, Union and Trait pub refs: bool, // for Struct, Enum, Union and Trait
pub enum_variant_refs: bool, pub enum_variant_refs: bool,
@ -410,7 +417,12 @@ pub struct LensConfig {
impl LensConfig { impl LensConfig {
pub fn any(&self) -> bool { pub fn any(&self) -> bool {
self.implementations || self.runnable() || self.references() self.run
|| self.debug
|| self.implementations
|| self.method_refs
|| self.refs
|| self.enum_variant_refs
} }
pub fn none(&self) -> bool { pub fn none(&self) -> bool {
@ -623,6 +635,12 @@ macro_rules! try_or {
}; };
} }
macro_rules! try_or_def {
($expr:expr) => {
try_!($expr).unwrap_or_default()
};
}
impl Config { impl Config {
pub fn linked_projects(&self) -> Vec<LinkedProject> { pub fn linked_projects(&self) -> Vec<LinkedProject> {
match self.data.linkedProjects.as_slice() { match self.data.linkedProjects.as_slice() {
@ -651,14 +669,13 @@ impl Config {
} }
pub fn did_save_text_document_dynamic_registration(&self) -> bool { pub fn did_save_text_document_dynamic_registration(&self) -> bool {
let caps = let caps = try_or_def!(self.caps.text_document.as_ref()?.synchronization.clone()?);
try_or!(self.caps.text_document.as_ref()?.synchronization.clone()?, Default::default());
caps.did_save == Some(true) && caps.dynamic_registration == Some(true) caps.did_save == Some(true) && caps.dynamic_registration == Some(true)
} }
pub fn did_change_watched_files_dynamic_registration(&self) -> bool { pub fn did_change_watched_files_dynamic_registration(&self) -> bool {
try_or!( try_or_def!(
self.caps.workspace.as_ref()?.did_change_watched_files.as_ref()?.dynamic_registration?, self.caps.workspace.as_ref()?.did_change_watched_files.as_ref()?.dynamic_registration?
false
) )
} }
@ -667,22 +684,24 @@ impl Config {
} }
pub fn location_link(&self) -> bool { pub fn location_link(&self) -> bool {
try_or!(self.caps.text_document.as_ref()?.definition?.link_support?, false) try_or_def!(self.caps.text_document.as_ref()?.definition?.link_support?)
} }
pub fn line_folding_only(&self) -> bool { pub fn line_folding_only(&self) -> bool {
try_or!(self.caps.text_document.as_ref()?.folding_range.as_ref()?.line_folding_only?, false) try_or_def!(self.caps.text_document.as_ref()?.folding_range.as_ref()?.line_folding_only?)
} }
pub fn hierarchical_symbols(&self) -> bool { pub fn hierarchical_symbols(&self) -> bool {
try_or!( try_or_def!(
self.caps self.caps
.text_document .text_document
.as_ref()? .as_ref()?
.document_symbol .document_symbol
.as_ref()? .as_ref()?
.hierarchical_document_symbol_support?, .hierarchical_document_symbol_support?
false
) )
} }
pub fn code_action_literals(&self) -> bool { pub fn code_action_literals(&self) -> bool {
try_!(self try_!(self
.caps .caps
@ -694,12 +713,15 @@ impl Config {
.as_ref()?) .as_ref()?)
.is_some() .is_some()
} }
pub fn work_done_progress(&self) -> bool { pub fn work_done_progress(&self) -> bool {
try_or!(self.caps.window.as_ref()?.work_done_progress?, false) try_or_def!(self.caps.window.as_ref()?.work_done_progress?)
} }
pub fn will_rename(&self) -> bool { pub fn will_rename(&self) -> bool {
try_or!(self.caps.workspace.as_ref()?.file_operations.as_ref()?.will_rename?, false) try_or_def!(self.caps.workspace.as_ref()?.file_operations.as_ref()?.will_rename?)
} }
pub fn change_annotation_support(&self) -> bool { pub fn change_annotation_support(&self) -> bool {
try_!(self try_!(self
.caps .caps
@ -711,24 +733,24 @@ impl Config {
.as_ref()?) .as_ref()?)
.is_some() .is_some()
} }
pub fn code_action_resolve(&self) -> bool { pub fn code_action_resolve(&self) -> bool {
try_or!( try_or_def!(self
self.caps .caps
.text_document .text_document
.as_ref()? .as_ref()?
.code_action .code_action
.as_ref()? .as_ref()?
.resolve_support .resolve_support
.as_ref()? .as_ref()?
.properties .properties
.as_slice(), .as_slice())
&[]
)
.iter() .iter()
.any(|it| it == "edit") .any(|it| it == "edit")
} }
pub fn signature_help_label_offsets(&self) -> bool { pub fn signature_help_label_offsets(&self) -> bool {
try_or!( try_or_def!(
self.caps self.caps
.text_document .text_document
.as_ref()? .as_ref()?
@ -738,10 +760,10 @@ impl Config {
.as_ref()? .as_ref()?
.parameter_information .parameter_information
.as_ref()? .as_ref()?
.label_offset_support?, .label_offset_support?
false
) )
} }
pub fn offset_encoding(&self) -> OffsetEncoding { pub fn offset_encoding(&self) -> OffsetEncoding {
if supports_utf8(&self.caps) { if supports_utf8(&self.caps) {
OffsetEncoding::Utf8 OffsetEncoding::Utf8
@ -751,11 +773,13 @@ impl Config {
} }
fn experimental(&self, index: &'static str) -> bool { fn experimental(&self, index: &'static str) -> bool {
try_or!(self.caps.experimental.as_ref()?.get(index)?.as_bool()?, false) try_or_def!(self.caps.experimental.as_ref()?.get(index)?.as_bool()?)
} }
pub fn code_action_group(&self) -> bool { pub fn code_action_group(&self) -> bool {
self.experimental("codeActionGroup") self.experimental("codeActionGroup")
} }
pub fn server_status_notification(&self) -> bool { pub fn server_status_notification(&self) -> bool {
self.experimental("serverStatusNotification") self.experimental("serverStatusNotification")
} }
@ -763,6 +787,7 @@ impl Config {
pub fn publish_diagnostics(&self) -> bool { pub fn publish_diagnostics(&self) -> bool {
self.data.diagnostics_enable self.data.diagnostics_enable
} }
pub fn diagnostics(&self) -> DiagnosticsConfig { pub fn diagnostics(&self) -> DiagnosticsConfig {
DiagnosticsConfig { DiagnosticsConfig {
disable_experimental: !self.data.diagnostics_enableExperimental, disable_experimental: !self.data.diagnostics_enableExperimental,
@ -773,6 +798,7 @@ impl Config {
}, },
} }
} }
pub fn diagnostics_map(&self) -> DiagnosticsMapConfig { pub fn diagnostics_map(&self) -> DiagnosticsMapConfig {
DiagnosticsMapConfig { DiagnosticsMapConfig {
remap_prefix: self.data.diagnostics_remapPrefix.clone(), remap_prefix: self.data.diagnostics_remapPrefix.clone(),
@ -780,9 +806,11 @@ impl Config {
warnings_as_hint: self.data.diagnostics_warningsAsHint.clone(), warnings_as_hint: self.data.diagnostics_warningsAsHint.clone(),
} }
} }
pub fn lru_capacity(&self) -> Option<usize> { pub fn lru_capacity(&self) -> Option<usize> {
self.data.lruCapacity self.data.lruCapacity
} }
pub fn proc_macro_srv(&self) -> Option<(AbsPathBuf, Vec<OsString>)> { pub fn proc_macro_srv(&self) -> Option<(AbsPathBuf, Vec<OsString>)> {
if !self.data.procMacro_enable { if !self.data.procMacro_enable {
return None; return None;
@ -793,12 +821,15 @@ impl Config {
}; };
Some((path, vec!["proc-macro".into()])) Some((path, vec!["proc-macro".into()]))
} }
pub fn dummy_replacements(&self) -> &FxHashMap<Box<str>, Box<[Box<str>]>> { pub fn dummy_replacements(&self) -> &FxHashMap<Box<str>, Box<[Box<str>]>> {
&self.data.procMacro_ignored &self.data.procMacro_ignored
} }
pub fn expand_proc_attr_macros(&self) -> bool { pub fn expand_proc_attr_macros(&self) -> bool {
self.data.experimental_procAttrMacros self.data.experimental_procAttrMacros
} }
pub fn files(&self) -> FilesConfig { pub fn files(&self) -> FilesConfig {
FilesConfig { FilesConfig {
watcher: match self.data.files_watcher.as_str() { watcher: match self.data.files_watcher.as_str() {
@ -811,15 +842,19 @@ impl Config {
exclude: self.data.files_excludeDirs.iter().map(|it| self.root_path.join(it)).collect(), exclude: self.data.files_excludeDirs.iter().map(|it| self.root_path.join(it)).collect(),
} }
} }
pub fn notifications(&self) -> NotificationsConfig { pub fn notifications(&self) -> NotificationsConfig {
NotificationsConfig { cargo_toml_not_found: self.data.notifications_cargoTomlNotFound } NotificationsConfig { cargo_toml_not_found: self.data.notifications_cargoTomlNotFound }
} }
pub fn cargo_autoreload(&self) -> bool { pub fn cargo_autoreload(&self) -> bool {
self.data.cargo_autoreload self.data.cargo_autoreload
} }
pub fn run_build_scripts(&self) -> bool { pub fn run_build_scripts(&self) -> bool {
self.data.cargo_runBuildScripts || self.data.procMacro_enable self.data.cargo_runBuildScripts || self.data.procMacro_enable
} }
pub fn cargo(&self) -> CargoConfig { pub fn cargo(&self) -> CargoConfig {
let rustc_source = self.data.rustcSource.as_ref().map(|rustc_src| { let rustc_source = self.data.rustcSource.as_ref().map(|rustc_src| {
if rustc_src == "discover" { if rustc_src == "discover" {
@ -855,6 +890,7 @@ impl Config {
}, },
} }
} }
pub fn flycheck(&self) -> Option<FlycheckConfig> { pub fn flycheck(&self) -> Option<FlycheckConfig> {
if !self.data.checkOnSave_enable { if !self.data.checkOnSave_enable {
return None; return None;
@ -891,12 +927,14 @@ impl Config {
}; };
Some(flycheck_config) Some(flycheck_config)
} }
pub fn runnables(&self) -> RunnablesConfig { pub fn runnables(&self) -> RunnablesConfig {
RunnablesConfig { RunnablesConfig {
override_cargo: self.data.runnables_overrideCargo.clone(), override_cargo: self.data.runnables_overrideCargo.clone(),
cargo_extra_args: self.data.runnables_cargoExtraArgs.clone(), cargo_extra_args: self.data.runnables_cargoExtraArgs.clone(),
} }
} }
pub fn inlay_hints(&self) -> InlayHintsConfig { pub fn inlay_hints(&self) -> InlayHintsConfig {
InlayHintsConfig { InlayHintsConfig {
render_colons: self.data.inlayHints_renderColons, render_colons: self.data.inlayHints_renderColons,
@ -917,6 +955,7 @@ impl Config {
max_length: self.data.inlayHints_maxLength, max_length: self.data.inlayHints_maxLength,
} }
} }
fn insert_use_config(&self) -> InsertUseConfig { fn insert_use_config(&self) -> InsertUseConfig {
InsertUseConfig { InsertUseConfig {
granularity: match self.data.assist_importGranularity { granularity: match self.data.assist_importGranularity {
@ -935,6 +974,7 @@ impl Config {
skip_glob_imports: !self.data.assist_allowMergingIntoGlobImports, skip_glob_imports: !self.data.assist_allowMergingIntoGlobImports,
} }
} }
pub fn completion(&self) -> CompletionConfig { pub fn completion(&self) -> CompletionConfig {
CompletionConfig { CompletionConfig {
enable_postfix_completions: self.data.completion_postfix_enable, enable_postfix_completions: self.data.completion_postfix_enable,
@ -945,7 +985,7 @@ impl Config {
add_call_parenthesis: self.data.completion_addCallParenthesis, add_call_parenthesis: self.data.completion_addCallParenthesis,
add_call_argument_snippets: self.data.completion_addCallArgumentSnippets, add_call_argument_snippets: self.data.completion_addCallArgumentSnippets,
insert_use: self.insert_use_config(), insert_use: self.insert_use_config(),
snippet_cap: SnippetCap::new(try_or!( snippet_cap: SnippetCap::new(try_or_def!(
self.caps self.caps
.text_document .text_document
.as_ref()? .as_ref()?
@ -953,12 +993,12 @@ impl Config {
.as_ref()? .as_ref()?
.completion_item .completion_item
.as_ref()? .as_ref()?
.snippet_support?, .snippet_support?
false
)), )),
snippets: self.snippets.clone(), snippets: self.snippets.clone(),
} }
} }
pub fn assist(&self) -> AssistConfig { pub fn assist(&self) -> AssistConfig {
AssistConfig { AssistConfig {
snippet_cap: SnippetCap::new(self.experimental("snippetTextEdit")), snippet_cap: SnippetCap::new(self.experimental("snippetTextEdit")),
@ -966,6 +1006,7 @@ impl Config {
insert_use: self.insert_use_config(), insert_use: self.insert_use_config(),
} }
} }
pub fn join_lines(&self) -> JoinLinesConfig { pub fn join_lines(&self) -> JoinLinesConfig {
JoinLinesConfig { JoinLinesConfig {
join_else_if: self.data.joinLines_joinElseIf, join_else_if: self.data.joinLines_joinElseIf,
@ -974,9 +1015,11 @@ impl Config {
join_assignments: self.data.joinLines_joinAssignments, join_assignments: self.data.joinLines_joinAssignments,
} }
} }
pub fn call_info_full(&self) -> bool { pub fn call_info_full(&self) -> bool {
self.data.callInfo_full self.data.callInfo_full
} }
pub fn lens(&self) -> LensConfig { pub fn lens(&self) -> LensConfig {
LensConfig { LensConfig {
run: self.data.lens_enable && self.data.lens_run, run: self.data.lens_enable && self.data.lens_run,
@ -987,6 +1030,7 @@ impl Config {
enum_variant_refs: self.data.lens_enable && self.data.lens_enumVariantReferences, enum_variant_refs: self.data.lens_enable && self.data.lens_enumVariantReferences,
} }
} }
pub fn hover_actions(&self) -> HoverActionsConfig { pub fn hover_actions(&self) -> HoverActionsConfig {
let enable = self.experimental("hoverActions") && self.data.hoverActions_enable; let enable = self.experimental("hoverActions") && self.data.hoverActions_enable;
HoverActionsConfig { HoverActionsConfig {
@ -997,24 +1041,24 @@ impl Config {
goto_type_def: enable && self.data.hoverActions_gotoTypeDef, goto_type_def: enable && self.data.hoverActions_gotoTypeDef,
} }
} }
pub fn highlighting_strings(&self) -> bool { pub fn highlighting_strings(&self) -> bool {
self.data.highlighting_strings self.data.highlighting_strings
} }
pub fn hover(&self) -> HoverConfig { pub fn hover(&self) -> HoverConfig {
HoverConfig { HoverConfig {
links_in_hover: self.data.hover_linksInHover, links_in_hover: self.data.hover_linksInHover,
documentation: self.data.hover_documentation.then(|| { documentation: self.data.hover_documentation.then(|| {
let is_markdown = try_or!( let is_markdown = try_or_def!(self
self.caps .caps
.text_document .text_document
.as_ref()? .as_ref()?
.hover .hover
.as_ref()? .as_ref()?
.content_format .content_format
.as_ref()? .as_ref()?
.as_slice(), .as_slice())
&[]
)
.contains(&MarkupKind::Markdown); .contains(&MarkupKind::Markdown);
if is_markdown { if is_markdown {
HoverDocFormat::Markdown HoverDocFormat::Markdown
@ -1042,13 +1086,15 @@ impl Config {
} }
pub fn semantic_tokens_refresh(&self) -> bool { pub fn semantic_tokens_refresh(&self) -> bool {
try_or!(self.caps.workspace.as_ref()?.semantic_tokens.as_ref()?.refresh_support?, false) try_or_def!(self.caps.workspace.as_ref()?.semantic_tokens.as_ref()?.refresh_support?)
} }
pub fn code_lens_refresh(&self) -> bool { pub fn code_lens_refresh(&self) -> bool {
try_or!(self.caps.workspace.as_ref()?.code_lens.as_ref()?.refresh_support?, false) try_or_def!(self.caps.workspace.as_ref()?.code_lens.as_ref()?.refresh_support?)
} }
pub fn insert_replace_support(&self) -> bool { pub fn insert_replace_support(&self) -> bool {
try_or!( try_or_def!(
self.caps self.caps
.text_document .text_document
.as_ref()? .as_ref()?
@ -1056,10 +1102,10 @@ impl Config {
.as_ref()? .as_ref()?
.completion_item .completion_item
.as_ref()? .as_ref()?
.insert_replace_support?, .insert_replace_support?
false
) )
} }
pub fn client_commands(&self) -> ClientCommandsConfig { pub fn client_commands(&self) -> ClientCommandsConfig {
let commands = let commands =
try_or!(self.caps.experimental.as_ref()?.get("commands")?, &serde_json::Value::Null); try_or!(self.caps.experimental.as_ref()?.get("commands")?, &serde_json::Value::Null);
@ -1096,6 +1142,8 @@ impl Config {
} }
} }
// Deserialization definitions
#[derive(Deserialize, Debug, Clone, Copy)] #[derive(Deserialize, Debug, Clone, Copy)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
enum SnippetScopeDef { enum SnippetScopeDef {
@ -1218,8 +1266,6 @@ enum WorkspaceSymbolSearchKindDef {
AllSymbols, AllSymbols,
} }
type ParallelPrimeCachesNumThreads = u8;
macro_rules! _config_data { macro_rules! _config_data {
(struct $name:ident { (struct $name:ident {
$( $(