Auto merge of #17913 - alibektas:ratoml_improvements, r=alibektas

fix: Add workspace level config to ratoml
This commit is contained in:
bors 2024-08-20 11:25:19 +00:00
commit 3723e5910c
4 changed files with 172 additions and 121 deletions

View file

@ -297,18 +297,6 @@ config_data! {
/// This option does not take effect until rust-analyzer is restarted. /// This option does not take effect until rust-analyzer is restarted.
rustc_source: Option<String> = None, rustc_source: Option<String> = None,
/// Additional arguments to `rustfmt`.
rustfmt_extraArgs: Vec<String> = vec![],
/// Advanced option, fully override the command rust-analyzer uses for
/// formatting. This should be the equivalent of `rustfmt` here, and
/// not that of `cargo fmt`. The file contents will be passed on the
/// standard input and the formatted result will be read from the
/// standard output.
rustfmt_overrideCommand: Option<Vec<String>> = None,
/// Enables the use of rustfmt's unstable range formatting command for the
/// `textDocument/rangeFormatting` request. The rustfmt option is unstable and only
/// available on a nightly build.
rustfmt_rangeFormatting_enable: bool = false,
/// Enables automatic discovery of projects using [`DiscoverWorkspaceConfig::command`]. /// Enables automatic discovery of projects using [`DiscoverWorkspaceConfig::command`].
/// ///
@ -439,6 +427,25 @@ config_data! {
} }
} }
config_data! {
workspace: struct WorkspaceDefaultConfigData <- WorkspaceConfigInput -> {
/// Additional arguments to `rustfmt`.
rustfmt_extraArgs: Vec<String> = vec![],
/// Advanced option, fully override the command rust-analyzer uses for
/// formatting. This should be the equivalent of `rustfmt` here, and
/// not that of `cargo fmt`. The file contents will be passed on the
/// standard input and the formatted result will be read from the
/// standard output.
rustfmt_overrideCommand: Option<Vec<String>> = None,
/// Enables the use of rustfmt's unstable range formatting command for the
/// `textDocument/rangeFormatting` request. The rustfmt option is unstable and only
/// available on a nightly build.
rustfmt_rangeFormatting_enable: bool = false,
}
}
config_data! { config_data! {
/// Configs that only make sense when they are set by a client. As such they can only be defined /// Configs that only make sense when they are set by a client. As such they can only be defined
/// by setting them using client's settings (e.g `settings.json` on VS Code). /// by setting them using client's settings (e.g `settings.json` on VS Code).
@ -740,7 +747,7 @@ pub enum RatomlFileKind {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant)]
enum RatomlFile { enum RatomlFile {
Workspace(GlobalLocalConfigInput), Workspace(WorkspaceLocalConfigInput),
Crate(LocalConfigInput), Crate(LocalConfigInput),
} }
@ -760,7 +767,7 @@ pub struct Config {
client_config: (FullConfigInput, ConfigErrors), client_config: (FullConfigInput, ConfigErrors),
/// Config node whose values apply to **every** Rust project. /// Config node whose values apply to **every** Rust project.
user_config: Option<(GlobalLocalConfigInput, ConfigErrors)>, user_config: Option<(GlobalWorkspaceLocalConfigInput, ConfigErrors)>,
ratoml_file: FxHashMap<SourceRootId, (RatomlFile, ConfigErrors)>, ratoml_file: FxHashMap<SourceRootId, (RatomlFile, ConfigErrors)>,
@ -827,13 +834,13 @@ impl Config {
if let Ok(table) = toml::from_str(&change) { if let Ok(table) = toml::from_str(&change) {
let mut toml_errors = vec![]; let mut toml_errors = vec![];
validate_toml_table( validate_toml_table(
GlobalLocalConfigInput::FIELDS, GlobalWorkspaceLocalConfigInput::FIELDS,
&table, &table,
&mut String::new(), &mut String::new(),
&mut toml_errors, &mut toml_errors,
); );
config.user_config = Some(( config.user_config = Some((
GlobalLocalConfigInput::from_toml(table, &mut toml_errors), GlobalWorkspaceLocalConfigInput::from_toml(table, &mut toml_errors),
ConfigErrors( ConfigErrors(
toml_errors toml_errors
.into_iter() .into_iter()
@ -962,7 +969,7 @@ impl Config {
match toml::from_str(&text) { match toml::from_str(&text) {
Ok(table) => { Ok(table) => {
validate_toml_table( validate_toml_table(
GlobalLocalConfigInput::FIELDS, WorkspaceLocalConfigInput::FIELDS,
&table, &table,
&mut String::new(), &mut String::new(),
&mut toml_errors, &mut toml_errors,
@ -971,7 +978,7 @@ impl Config {
source_root_id, source_root_id,
( (
RatomlFile::Workspace( RatomlFile::Workspace(
GlobalLocalConfigInput::from_toml( WorkspaceLocalConfigInput::from_toml(
table, table,
&mut toml_errors, &mut toml_errors,
), ),
@ -1009,7 +1016,7 @@ impl Config {
config.source_root_parent_map = source_root_map; config.source_root_parent_map = source_root_map;
} }
if config.check_command(None).is_empty() { if config.check_command().is_empty() {
config.validation_errors.0.push(Arc::new(ConfigErrorInner::Json { config.validation_errors.0.push(Arc::new(ConfigErrorInner::Json {
config_key: "/check/command".to_owned(), config_key: "/check/command".to_owned(),
error: serde_json::Error::custom("expected a non-empty string"), error: serde_json::Error::custom("expected a non-empty string"),
@ -1436,11 +1443,11 @@ impl Config {
pub fn diagnostics(&self, source_root: Option<SourceRootId>) -> DiagnosticsConfig { pub fn diagnostics(&self, source_root: Option<SourceRootId>) -> DiagnosticsConfig {
DiagnosticsConfig { DiagnosticsConfig {
enabled: *self.diagnostics_enable(source_root), enabled: *self.diagnostics_enable(),
proc_attr_macros_enabled: self.expand_proc_attr_macros(), proc_attr_macros_enabled: self.expand_proc_attr_macros(),
proc_macros_enabled: *self.procMacro_enable(), proc_macros_enabled: *self.procMacro_enable(),
disable_experimental: !self.diagnostics_experimental_enable(source_root), disable_experimental: !self.diagnostics_experimental_enable(),
disabled: self.diagnostics_disabled(source_root).clone(), disabled: self.diagnostics_disabled().clone(),
expr_fill_default: match self.assist_expressionFillDefault(source_root) { expr_fill_default: match self.assist_expressionFillDefault(source_root) {
ExprFillDefaultDef::Todo => ExprFillDefaultMode::Todo, ExprFillDefaultDef::Todo => ExprFillDefaultMode::Todo,
ExprFillDefaultDef::Default => ExprFillDefaultMode::Default, ExprFillDefaultDef::Default => ExprFillDefaultMode::Default,
@ -1450,7 +1457,7 @@ impl Config {
prefer_no_std: self.imports_preferNoStd(source_root).to_owned(), prefer_no_std: self.imports_preferNoStd(source_root).to_owned(),
prefer_prelude: self.imports_preferPrelude(source_root).to_owned(), prefer_prelude: self.imports_preferPrelude(source_root).to_owned(),
prefer_absolute: self.imports_prefixExternPrelude(source_root).to_owned(), prefer_absolute: self.imports_prefixExternPrelude(source_root).to_owned(),
style_lints: self.diagnostics_styleLints_enable(source_root).to_owned(), style_lints: self.diagnostics_styleLints_enable().to_owned(),
term_search_fuel: self.assist_termSearch_fuel(source_root).to_owned() as u64, term_search_fuel: self.assist_termSearch_fuel(source_root).to_owned() as u64,
term_search_borrowck: self.assist_termSearch_borrowcheck(source_root).to_owned(), term_search_borrowck: self.assist_termSearch_borrowcheck(source_root).to_owned(),
} }
@ -1643,11 +1650,11 @@ impl Config {
} }
pub fn has_linked_projects(&self) -> bool { pub fn has_linked_projects(&self) -> bool {
!self.linkedProjects(None).is_empty() !self.linkedProjects().is_empty()
} }
pub fn linked_manifests(&self) -> impl Iterator<Item = &Utf8Path> + '_ { pub fn linked_manifests(&self) -> impl Iterator<Item = &Utf8Path> + '_ {
self.linkedProjects(None).iter().filter_map(|it| match it { self.linkedProjects().iter().filter_map(|it| match it {
ManifestOrProjectJson::Manifest(p) => Some(&**p), ManifestOrProjectJson::Manifest(p) => Some(&**p),
// despite having a buildfile, using this variant as a manifest // despite having a buildfile, using this variant as a manifest
// will fail. // will fail.
@ -1657,20 +1664,20 @@ impl Config {
} }
pub fn has_linked_project_jsons(&self) -> bool { pub fn has_linked_project_jsons(&self) -> bool {
self.linkedProjects(None) self.linkedProjects()
.iter() .iter()
.any(|it| matches!(it, ManifestOrProjectJson::ProjectJson { .. })) .any(|it| matches!(it, ManifestOrProjectJson::ProjectJson { .. }))
} }
pub fn discover_workspace_config(&self) -> Option<&DiscoverWorkspaceConfig> { pub fn discover_workspace_config(&self) -> Option<&DiscoverWorkspaceConfig> {
self.workspace_discoverConfig(None).as_ref() self.workspace_discoverConfig().as_ref()
} }
pub fn linked_or_discovered_projects(&self) -> Vec<LinkedProject> { pub fn linked_or_discovered_projects(&self) -> Vec<LinkedProject> {
match self.linkedProjects(None).as_slice() { match self.linkedProjects().as_slice() {
[] => { [] => {
let exclude_dirs: Vec<_> = let exclude_dirs: Vec<_> =
self.files_excludeDirs(None).iter().map(|p| self.root_path.join(p)).collect(); self.files_excludeDirs().iter().map(|p| self.root_path.join(p)).collect();
self.discovered_projects self.discovered_projects
.iter() .iter()
.filter(|project| { .filter(|project| {
@ -1705,48 +1712,48 @@ impl Config {
} }
pub fn prefill_caches(&self) -> bool { pub fn prefill_caches(&self) -> bool {
self.cachePriming_enable(None).to_owned() self.cachePriming_enable().to_owned()
} }
pub fn publish_diagnostics(&self) -> bool { pub fn publish_diagnostics(&self) -> bool {
self.diagnostics_enable(None).to_owned() self.diagnostics_enable().to_owned()
} }
pub fn diagnostics_map(&self) -> DiagnosticsMapConfig { pub fn diagnostics_map(&self) -> DiagnosticsMapConfig {
DiagnosticsMapConfig { DiagnosticsMapConfig {
remap_prefix: self.diagnostics_remapPrefix(None).clone(), remap_prefix: self.diagnostics_remapPrefix().clone(),
warnings_as_info: self.diagnostics_warningsAsInfo(None).clone(), warnings_as_info: self.diagnostics_warningsAsInfo().clone(),
warnings_as_hint: self.diagnostics_warningsAsHint(None).clone(), warnings_as_hint: self.diagnostics_warningsAsHint().clone(),
check_ignore: self.check_ignore(None).clone(), check_ignore: self.check_ignore().clone(),
} }
} }
pub fn extra_args(&self) -> &Vec<String> { pub fn extra_args(&self) -> &Vec<String> {
self.cargo_extraArgs(None) self.cargo_extraArgs()
} }
pub fn extra_env(&self) -> &FxHashMap<String, String> { pub fn extra_env(&self) -> &FxHashMap<String, String> {
self.cargo_extraEnv(None) self.cargo_extraEnv()
} }
pub fn check_extra_args(&self) -> Vec<String> { pub fn check_extra_args(&self) -> Vec<String> {
let mut extra_args = self.extra_args().clone(); let mut extra_args = self.extra_args().clone();
extra_args.extend_from_slice(self.check_extraArgs(None)); extra_args.extend_from_slice(self.check_extraArgs());
extra_args extra_args
} }
pub fn check_extra_env(&self) -> FxHashMap<String, String> { pub fn check_extra_env(&self) -> FxHashMap<String, String> {
let mut extra_env = self.cargo_extraEnv(None).clone(); let mut extra_env = self.cargo_extraEnv().clone();
extra_env.extend(self.check_extraEnv(None).clone()); extra_env.extend(self.check_extraEnv().clone());
extra_env extra_env
} }
pub fn lru_parse_query_capacity(&self) -> Option<u16> { pub fn lru_parse_query_capacity(&self) -> Option<u16> {
self.lru_capacity(None).to_owned() self.lru_capacity().to_owned()
} }
pub fn lru_query_capacities_config(&self) -> Option<&FxHashMap<Box<str>, u16>> { pub fn lru_query_capacities_config(&self) -> Option<&FxHashMap<Box<str>, u16>> {
self.lru_query_capacities(None).is_empty().not().then(|| self.lru_query_capacities(None)) self.lru_query_capacities().is_empty().not().then(|| self.lru_query_capacities())
} }
pub fn proc_macro_srv(&self) -> Option<AbsPathBuf> { pub fn proc_macro_srv(&self) -> Option<AbsPathBuf> {
@ -1755,7 +1762,7 @@ impl Config {
} }
pub fn ignored_proc_macros(&self) -> &FxHashMap<Box<str>, Box<[Box<str>]>> { pub fn ignored_proc_macros(&self) -> &FxHashMap<Box<str>, Box<[Box<str>]>> {
self.procMacro_ignored(None) self.procMacro_ignored()
} }
pub fn expand_proc_macros(&self) -> bool { pub fn expand_proc_macros(&self) -> bool {
@ -1770,11 +1777,7 @@ impl Config {
} }
_ => FilesWatcher::Server, _ => FilesWatcher::Server,
}, },
exclude: self exclude: self.files_excludeDirs().iter().map(|it| self.root_path.join(it)).collect(),
.files_excludeDirs(None)
.iter()
.map(|it| self.root_path.join(it))
.collect(),
} }
} }
@ -1785,22 +1788,22 @@ impl Config {
} }
pub fn cargo_autoreload_config(&self) -> bool { pub fn cargo_autoreload_config(&self) -> bool {
self.cargo_autoreload(None).to_owned() self.cargo_autoreload().to_owned()
} }
pub fn run_build_scripts(&self) -> bool { pub fn run_build_scripts(&self) -> bool {
self.cargo_buildScripts_enable(None).to_owned() || self.procMacro_enable().to_owned() self.cargo_buildScripts_enable().to_owned() || self.procMacro_enable().to_owned()
} }
pub fn cargo(&self) -> CargoConfig { pub fn cargo(&self) -> CargoConfig {
let rustc_source = self.rustc_source(None).as_ref().map(|rustc_src| { let rustc_source = self.rustc_source().as_ref().map(|rustc_src| {
if rustc_src == "discover" { if rustc_src == "discover" {
RustLibSource::Discover RustLibSource::Discover
} else { } else {
RustLibSource::Path(self.root_path.join(rustc_src)) RustLibSource::Path(self.root_path.join(rustc_src))
} }
}); });
let sysroot = self.cargo_sysroot(None).as_ref().map(|sysroot| { let sysroot = self.cargo_sysroot().as_ref().map(|sysroot| {
if sysroot == "discover" { if sysroot == "discover" {
RustLibSource::Discover RustLibSource::Discover
} else { } else {
@ -1808,24 +1811,24 @@ impl Config {
} }
}); });
let sysroot_src = let sysroot_src =
self.cargo_sysrootSrc(None).as_ref().map(|sysroot| self.root_path.join(sysroot)); self.cargo_sysrootSrc().as_ref().map(|sysroot| self.root_path.join(sysroot));
CargoConfig { CargoConfig {
all_targets: *self.cargo_allTargets(None), all_targets: *self.cargo_allTargets(),
features: match &self.cargo_features(None) { features: match &self.cargo_features() {
CargoFeaturesDef::All => CargoFeatures::All, CargoFeaturesDef::All => CargoFeatures::All,
CargoFeaturesDef::Selected(features) => CargoFeatures::Selected { CargoFeaturesDef::Selected(features) => CargoFeatures::Selected {
features: features.clone(), features: features.clone(),
no_default_features: self.cargo_noDefaultFeatures(None).to_owned(), no_default_features: self.cargo_noDefaultFeatures().to_owned(),
}, },
}, },
target: self.cargo_target(None).clone(), target: self.cargo_target().clone(),
sysroot, sysroot,
sysroot_src, sysroot_src,
rustc_source, rustc_source,
cfg_overrides: project_model::CfgOverrides { cfg_overrides: project_model::CfgOverrides {
global: CfgDiff::new( global: CfgDiff::new(
self.cargo_cfgs(None) self.cargo_cfgs()
.iter() .iter()
.map(|(key, val)| match val { .map(|(key, val)| match val {
Some(val) => CfgAtom::KeyValue { Some(val) => CfgAtom::KeyValue {
@ -1840,14 +1843,14 @@ impl Config {
.unwrap(), .unwrap(),
selective: Default::default(), selective: Default::default(),
}, },
wrap_rustc_in_build_scripts: *self.cargo_buildScripts_useRustcWrapper(None), wrap_rustc_in_build_scripts: *self.cargo_buildScripts_useRustcWrapper(),
invocation_strategy: match self.cargo_buildScripts_invocationStrategy(None) { invocation_strategy: match self.cargo_buildScripts_invocationStrategy() {
InvocationStrategy::Once => project_model::InvocationStrategy::Once, InvocationStrategy::Once => project_model::InvocationStrategy::Once,
InvocationStrategy::PerWorkspace => project_model::InvocationStrategy::PerWorkspace, InvocationStrategy::PerWorkspace => project_model::InvocationStrategy::PerWorkspace,
}, },
run_build_script_command: self.cargo_buildScripts_overrideCommand(None).clone(), run_build_script_command: self.cargo_buildScripts_overrideCommand().clone(),
extra_args: self.cargo_extraArgs(None).clone(), extra_args: self.cargo_extraArgs().clone(),
extra_env: self.cargo_extraEnv(None).clone(), extra_env: self.cargo_extraEnv().clone(),
target_dir: self.target_dir_from_config(), target_dir: self.target_dir_from_config(),
} }
} }
@ -1867,16 +1870,16 @@ impl Config {
} }
pub fn flycheck_workspace(&self) -> bool { pub fn flycheck_workspace(&self) -> bool {
*self.check_workspace(None) *self.check_workspace()
} }
pub(crate) fn cargo_test_options(&self) -> CargoOptions { pub(crate) fn cargo_test_options(&self) -> CargoOptions {
CargoOptions { CargoOptions {
target_triples: self.cargo_target(None).clone().into_iter().collect(), target_triples: self.cargo_target().clone().into_iter().collect(),
all_targets: false, all_targets: false,
no_default_features: *self.cargo_noDefaultFeatures(None), no_default_features: *self.cargo_noDefaultFeatures(),
all_features: matches!(self.cargo_features(None), CargoFeaturesDef::All), all_features: matches!(self.cargo_features(), CargoFeaturesDef::All),
features: match self.cargo_features(None).clone() { features: match self.cargo_features().clone() {
CargoFeaturesDef::All => vec![], CargoFeaturesDef::All => vec![],
CargoFeaturesDef::Selected(it) => it, CargoFeaturesDef::Selected(it) => it,
}, },
@ -1887,7 +1890,7 @@ impl Config {
} }
pub(crate) fn flycheck(&self) -> FlycheckConfig { pub(crate) fn flycheck(&self) -> FlycheckConfig {
match &self.check_overrideCommand(None) { match &self.check_overrideCommand() {
Some(args) if !args.is_empty() => { Some(args) if !args.is_empty() => {
let mut args = args.clone(); let mut args = args.clone();
let command = args.remove(0); let command = args.remove(0);
@ -1895,7 +1898,7 @@ impl Config {
command, command,
args, args,
extra_env: self.check_extra_env(), extra_env: self.check_extra_env(),
invocation_strategy: match self.check_invocationStrategy(None) { invocation_strategy: match self.check_invocationStrategy() {
InvocationStrategy::Once => crate::flycheck::InvocationStrategy::Once, InvocationStrategy::Once => crate::flycheck::InvocationStrategy::Once,
InvocationStrategy::PerWorkspace => { InvocationStrategy::PerWorkspace => {
crate::flycheck::InvocationStrategy::PerWorkspace crate::flycheck::InvocationStrategy::PerWorkspace
@ -1904,30 +1907,28 @@ impl Config {
} }
} }
Some(_) | None => FlycheckConfig::CargoCommand { Some(_) | None => FlycheckConfig::CargoCommand {
command: self.check_command(None).clone(), command: self.check_command().clone(),
options: CargoOptions { options: CargoOptions {
target_triples: self target_triples: self
.check_targets(None) .check_targets()
.clone() .clone()
.and_then(|targets| match &targets.0[..] { .and_then(|targets| match &targets.0[..] {
[] => None, [] => None,
targets => Some(targets.into()), targets => Some(targets.into()),
}) })
.unwrap_or_else(|| self.cargo_target(None).clone().into_iter().collect()), .unwrap_or_else(|| self.cargo_target().clone().into_iter().collect()),
all_targets: self all_targets: self.check_allTargets().unwrap_or(*self.cargo_allTargets()),
.check_allTargets(None)
.unwrap_or(*self.cargo_allTargets(None)),
no_default_features: self no_default_features: self
.check_noDefaultFeatures(None) .check_noDefaultFeatures()
.unwrap_or(*self.cargo_noDefaultFeatures(None)), .unwrap_or(*self.cargo_noDefaultFeatures()),
all_features: matches!( all_features: matches!(
self.check_features(None).as_ref().unwrap_or(self.cargo_features(None)), self.check_features().as_ref().unwrap_or(self.cargo_features()),
CargoFeaturesDef::All CargoFeaturesDef::All
), ),
features: match self features: match self
.check_features(None) .check_features()
.clone() .clone()
.unwrap_or_else(|| self.cargo_features(None).clone()) .unwrap_or_else(|| self.cargo_features().clone())
{ {
CargoFeaturesDef::All => vec![], CargoFeaturesDef::All => vec![],
CargoFeaturesDef::Selected(it) => it, CargoFeaturesDef::Selected(it) => it,
@ -1942,7 +1943,7 @@ impl Config {
} }
fn target_dir_from_config(&self) -> Option<Utf8PathBuf> { fn target_dir_from_config(&self) -> Option<Utf8PathBuf> {
self.cargo_targetDir(None).as_ref().and_then(|target_dir| match target_dir { self.cargo_targetDir().as_ref().and_then(|target_dir| match target_dir {
TargetDirectory::UseSubdirectory(true) => { TargetDirectory::UseSubdirectory(true) => {
Some(Utf8PathBuf::from("target/rust-analyzer")) Some(Utf8PathBuf::from("target/rust-analyzer"))
} }
@ -1953,18 +1954,18 @@ impl Config {
} }
pub fn check_on_save(&self) -> bool { pub fn check_on_save(&self) -> bool {
*self.checkOnSave(None) *self.checkOnSave()
} }
pub fn script_rebuild_on_save(&self) -> bool { pub fn script_rebuild_on_save(&self) -> bool {
*self.cargo_buildScripts_rebuildOnSave(None) *self.cargo_buildScripts_rebuildOnSave()
} }
pub fn runnables(&self) -> RunnablesConfig { pub fn runnables(&self) -> RunnablesConfig {
RunnablesConfig { RunnablesConfig {
override_cargo: self.runnables_command(None).clone(), override_cargo: self.runnables_command().clone(),
cargo_extra_args: self.runnables_extraArgs(None).clone(), cargo_extra_args: self.runnables_extraArgs().clone(),
extra_test_binary_args: self.runnables_extraTestBinaryArgs(None).clone(), extra_test_binary_args: self.runnables_extraTestBinaryArgs().clone(),
} }
} }
@ -2035,7 +2036,7 @@ impl Config {
} }
pub fn prime_caches_num_threads(&self) -> usize { pub fn prime_caches_num_threads(&self) -> usize {
match self.cachePriming_numThreads(None) { match self.cachePriming_numThreads() {
NumThreads::Concrete(0) | NumThreads::Physical => num_cpus::get_physical(), NumThreads::Concrete(0) | NumThreads::Physical => num_cpus::get_physical(),
&NumThreads::Concrete(n) => n, &NumThreads::Concrete(n) => n,
NumThreads::Logical => num_cpus::get(), NumThreads::Logical => num_cpus::get(),
@ -2534,6 +2535,41 @@ macro_rules! _impl_for_config_data {
)* )*
} }
}; };
(workspace, $(
$(#[doc=$doc:literal])*
$vis:vis $field:ident : $ty:ty = $default:expr,
)*
) => {
impl Config {
$(
$($doc)*
#[allow(non_snake_case)]
$vis fn $field(&self, source_root: Option<SourceRootId>) -> &$ty {
let mut source_root = source_root.as_ref();
while let Some(sr) = source_root {
if let Some((RatomlFile::Workspace(config), _)) = self.ratoml_file.get(&sr) {
if let Some(v) = config.workspace.$field.as_ref() {
return &v;
}
}
source_root = self.source_root_parent_map.get(&sr);
}
if let Some(v) = self.client_config.0.workspace.$field.as_ref() {
return &v;
}
if let Some((user_config, _)) = self.user_config.as_ref() {
if let Some(v) = user_config.workspace.$field.as_ref() {
return &v;
}
}
&self.default_config.workspace.$field
}
)*
}
};
(global, $( (global, $(
$(#[doc=$doc:literal])* $(#[doc=$doc:literal])*
$vis:vis $field:ident : $ty:ty = $default:expr, $vis:vis $field:ident : $ty:ty = $default:expr,
@ -2543,18 +2579,7 @@ macro_rules! _impl_for_config_data {
$( $(
$($doc)* $($doc)*
#[allow(non_snake_case)] #[allow(non_snake_case)]
$vis fn $field(&self, source_root : Option<SourceRootId>) -> &$ty { $vis fn $field(&self) -> &$ty {
let mut source_root = source_root.as_ref();
while let Some(sr) = source_root {
if let Some((RatomlFile::Workspace(config), _)) = self.ratoml_file.get(&sr) {
if let Some(v) = config.global.$field.as_ref() {
return &v;
}
}
source_root = self.source_root_parent_map.get(&sr);
}
if let Some(v) = self.client_config.0.global.$field.as_ref() { if let Some(v) = self.client_config.0.global.$field.as_ref() {
return &v; return &v;
} }
@ -2692,6 +2717,7 @@ use _config_data as config_data;
#[derive(Default, Debug, Clone)] #[derive(Default, Debug, Clone)]
struct DefaultConfigData { struct DefaultConfigData {
global: GlobalDefaultConfigData, global: GlobalDefaultConfigData,
workspace: WorkspaceDefaultConfigData,
local: LocalDefaultConfigData, local: LocalDefaultConfigData,
client: ClientDefaultConfigData, client: ClientDefaultConfigData,
} }
@ -2702,6 +2728,7 @@ struct DefaultConfigData {
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
struct FullConfigInput { struct FullConfigInput {
global: GlobalConfigInput, global: GlobalConfigInput,
workspace: WorkspaceConfigInput,
local: LocalConfigInput, local: LocalConfigInput,
client: ClientConfigInput, client: ClientConfigInput,
} }
@ -2715,6 +2742,7 @@ impl FullConfigInput {
global: GlobalConfigInput::from_json(&mut json, error_sink), global: GlobalConfigInput::from_json(&mut json, error_sink),
local: LocalConfigInput::from_json(&mut json, error_sink), local: LocalConfigInput::from_json(&mut json, error_sink),
client: ClientConfigInput::from_json(&mut json, error_sink), client: ClientConfigInput::from_json(&mut json, error_sink),
workspace: WorkspaceConfigInput::from_json(&mut json, error_sink),
} }
} }
@ -2723,6 +2751,7 @@ impl FullConfigInput {
GlobalConfigInput::schema_fields(&mut fields); GlobalConfigInput::schema_fields(&mut fields);
LocalConfigInput::schema_fields(&mut fields); LocalConfigInput::schema_fields(&mut fields);
ClientConfigInput::schema_fields(&mut fields); ClientConfigInput::schema_fields(&mut fields);
WorkspaceConfigInput::schema_fields(&mut fields);
fields.sort_by_key(|&(x, ..)| x); fields.sort_by_key(|&(x, ..)| x);
fields fields
.iter() .iter()
@ -2745,21 +2774,45 @@ impl FullConfigInput {
/// some rust-analyzer.toml file or JSON blob. An empty rust-analyzer.toml corresponds to /// some rust-analyzer.toml file or JSON blob. An empty rust-analyzer.toml corresponds to
/// all fields being None. /// all fields being None.
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
struct GlobalLocalConfigInput { struct GlobalWorkspaceLocalConfigInput {
global: GlobalConfigInput, global: GlobalConfigInput,
local: LocalConfigInput, local: LocalConfigInput,
workspace: WorkspaceConfigInput,
} }
impl GlobalLocalConfigInput { impl GlobalWorkspaceLocalConfigInput {
const FIELDS: &'static [&'static [&'static str]] = const FIELDS: &'static [&'static [&'static str]] =
&[GlobalConfigInput::FIELDS, LocalConfigInput::FIELDS]; &[GlobalConfigInput::FIELDS, LocalConfigInput::FIELDS];
fn from_toml( fn from_toml(
toml: toml::Table, toml: toml::Table,
error_sink: &mut Vec<(String, toml::de::Error)>, error_sink: &mut Vec<(String, toml::de::Error)>,
) -> GlobalLocalConfigInput { ) -> GlobalWorkspaceLocalConfigInput {
GlobalLocalConfigInput { GlobalWorkspaceLocalConfigInput {
global: GlobalConfigInput::from_toml(&toml, error_sink), global: GlobalConfigInput::from_toml(&toml, error_sink),
local: LocalConfigInput::from_toml(&toml, error_sink), local: LocalConfigInput::from_toml(&toml, error_sink),
workspace: WorkspaceConfigInput::from_toml(&toml, error_sink),
}
}
}
/// Workspace and local config levels, all fields `Option<T>`, to describe fields that are actually set by
/// some rust-analyzer.toml file or JSON blob. An empty rust-analyzer.toml corresponds to
/// all fields being None.
#[derive(Debug, Clone, Default)]
#[allow(dead_code)]
struct WorkspaceLocalConfigInput {
workspace: WorkspaceConfigInput,
local: LocalConfigInput,
}
impl WorkspaceLocalConfigInput {
#[allow(dead_code)]
const FIELDS: &'static [&'static [&'static str]] =
&[WorkspaceConfigInput::FIELDS, LocalConfigInput::FIELDS];
fn from_toml(toml: toml::Table, error_sink: &mut Vec<(String, toml::de::Error)>) -> Self {
Self {
workspace: WorkspaceConfigInput::from_toml(&toml, error_sink),
local: LocalConfigInput::from_toml(&toml, error_sink),
} }
} }
} }
@ -3467,7 +3520,7 @@ mod tests {
})); }));
(config, _, _) = config.apply_change(change); (config, _, _) = config.apply_change(change);
assert_eq!(config.cargo_targetDir(None), &None); assert_eq!(config.cargo_targetDir(), &None);
assert!( assert!(
matches!(config.flycheck(), FlycheckConfig::CargoCommand { options, .. } if options.target_dir.is_none()) matches!(config.flycheck(), FlycheckConfig::CargoCommand { options, .. } if options.target_dir.is_none())
); );
@ -3485,7 +3538,7 @@ mod tests {
(config, _, _) = config.apply_change(change); (config, _, _) = config.apply_change(change);
assert_eq!(config.cargo_targetDir(None), &Some(TargetDirectory::UseSubdirectory(true))); assert_eq!(config.cargo_targetDir(), &Some(TargetDirectory::UseSubdirectory(true)));
assert!( assert!(
matches!(config.flycheck(), FlycheckConfig::CargoCommand { options, .. } if options.target_dir == Some(Utf8PathBuf::from("target/rust-analyzer"))) matches!(config.flycheck(), FlycheckConfig::CargoCommand { options, .. } if options.target_dir == Some(Utf8PathBuf::from("target/rust-analyzer")))
); );
@ -3504,7 +3557,7 @@ mod tests {
(config, _, _) = config.apply_change(change); (config, _, _) = config.apply_change(change);
assert_eq!( assert_eq!(
config.cargo_targetDir(None), config.cargo_targetDir(),
&Some(TargetDirectory::Directory(Utf8PathBuf::from("other_folder"))) &Some(TargetDirectory::Directory(Utf8PathBuf::from("other_folder")))
); );
assert!( assert!(

View file

@ -2113,9 +2113,9 @@ fn run_rustfmt(
let edition = editions.iter().copied().max(); let edition = editions.iter().copied().max();
let line_index = snap.file_line_index(file_id)?; let line_index = snap.file_line_index(file_id)?;
let sr = snap.analysis.source_root_id(file_id)?; let source_root_id = snap.analysis.source_root_id(file_id).ok();
let mut command = match snap.config.rustfmt(Some(sr)) { let mut command = match snap.config.rustfmt(source_root_id) {
RustfmtConfig::Rustfmt { extra_args, enable_range_formatting } => { RustfmtConfig::Rustfmt { extra_args, enable_range_formatting } => {
// FIXME: Set RUSTUP_TOOLCHAIN // FIXME: Set RUSTUP_TOOLCHAIN
let mut cmd = process::Command::new(toolchain::Tool::Rustfmt.path()); let mut cmd = process::Command::new(toolchain::Tool::Rustfmt.path());
@ -2303,7 +2303,7 @@ pub(crate) fn internal_testing_fetch_config(
.transpose()?; .transpose()?;
serde_json::to_value(match &*params.config { serde_json::to_value(match &*params.config {
"local" => state.config.assist(source_root).assist_emit_must_use, "local" => state.config.assist(source_root).assist_emit_must_use,
"global" => matches!( "workspace" => matches!(
state.config.rustfmt(source_root), state.config.rustfmt(source_root),
RustfmtConfig::Rustfmt { enable_range_formatting: true, .. } RustfmtConfig::Rustfmt { enable_range_formatting: true, .. }
), ),

View file

@ -122,7 +122,7 @@ impl GlobalState {
}; };
let mut message = String::new(); let mut message = String::new();
if !self.config.cargo_autoreload(None) if !self.config.cargo_autoreload()
&& self.is_quiescent() && self.is_quiescent()
&& self.fetch_workspaces_queue.op_requested() && self.fetch_workspaces_queue.op_requested()
&& self.config.discover_workspace_config().is_none() && self.config.discover_workspace_config().is_none()

View file

@ -18,7 +18,7 @@ enum QueryType {
/// A query whose config key is a part of the global configs, so that /// A query whose config key is a part of the global configs, so that
/// testing for changes to this config means testing if global changes /// testing for changes to this config means testing if global changes
/// take affect. /// take affect.
Global, Workspace,
} }
struct RatomlTest { struct RatomlTest {
@ -161,7 +161,7 @@ impl RatomlTest {
fn query(&self, query: QueryType, source_file_idx: usize) -> bool { fn query(&self, query: QueryType, source_file_idx: usize) -> bool {
let config = match query { let config = match query {
QueryType::Local => "local".to_owned(), QueryType::Local => "local".to_owned(),
QueryType::Global => "global".to_owned(), QueryType::Workspace => "workspace".to_owned(),
}; };
let res = self.server.send_request::<InternalTestingFetchConfig>( let res = self.server.send_request::<InternalTestingFetchConfig>(
InternalTestingFetchConfigParams { InternalTestingFetchConfigParams {
@ -819,10 +819,8 @@ fn ratoml_multiple_ratoml_in_single_source_root() {
// assert!(!server.query(QueryType::AssistEmitMustUse, 5)); // assert!(!server.query(QueryType::AssistEmitMustUse, 5));
// } // }
/// Having a ratoml file at the root of a project enables
/// configuring global level configurations as well.
#[test] #[test]
fn ratoml_in_root_is_global() { fn ratoml_in_root_is_workspace() {
if skip_slow_tests() { if skip_slow_tests() {
return; return;
} }
@ -850,7 +848,7 @@ fn main() {
None, None,
); );
assert!(server.query(QueryType::Global, 2)); assert!(server.query(QueryType::Workspace, 2));
} }
#[test] #[test]
@ -882,9 +880,9 @@ fn main() {
None, None,
); );
assert!(server.query(QueryType::Global, 2)); assert!(server.query(QueryType::Workspace, 2));
server.edit(1, "rustfmt.rangeFormatting.enable = false".to_owned()); server.edit(1, "rustfmt.rangeFormatting.enable = false".to_owned());
assert!(!server.query(QueryType::Global, 2)); assert!(!server.query(QueryType::Workspace, 2));
} }
#[test] #[test]
@ -916,7 +914,7 @@ fn main() {
None, None,
); );
assert!(server.query(QueryType::Global, 2)); assert!(server.query(QueryType::Workspace, 2));
server.delete(1); server.delete(1);
assert!(!server.query(QueryType::Global, 2)); assert!(!server.query(QueryType::Workspace, 2));
} }