Skip to content

Commit 8e31598

Browse files
committed
Next up : generating configs for workspace level configs
1 parent a42c732 commit 8e31598

File tree

6 files changed

+48
-110
lines changed

6 files changed

+48
-110
lines changed

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

+35-35
Original file line numberDiff line numberDiff line change
@@ -294,18 +294,6 @@ config_data! {
294294
/// This option does not take effect until rust-analyzer is restarted.
295295
rustc_source: Option<String> = None,
296296

297-
/// Additional arguments to `rustfmt`.
298-
rustfmt_extraArgs: Vec<String> = vec![],
299-
/// Advanced option, fully override the command rust-analyzer uses for
300-
/// formatting. This should be the equivalent of `rustfmt` here, and
301-
/// not that of `cargo fmt`. The file contents will be passed on the
302-
/// standard input and the formatted result will be read from the
303-
/// standard output.
304-
rustfmt_overrideCommand: Option<Vec<String>> = None,
305-
/// Enables the use of rustfmt's unstable range formatting command for the
306-
/// `textDocument/rangeFormatting` request. The rustfmt option is unstable and only
307-
/// available on a nightly build.
308-
rustfmt_rangeFormatting_enable: bool = false,
309297

310298
/// Enables automatic discovery of projects using [`DiscoverWorkspaceConfig::command`].
311299
///
@@ -439,6 +427,18 @@ config_data! {
439427
config_data! {
440428
workspace: struct WorkspaceDefaultConfigData <- WorkspaceConfigInput -> {
441429

430+
/// Additional arguments to `rustfmt`.
431+
rustfmt_extraArgs: Vec<String> = vec![],
432+
/// Advanced option, fully override the command rust-analyzer uses for
433+
/// formatting. This should be the equivalent of `rustfmt` here, and
434+
/// not that of `cargo fmt`. The file contents will be passed on the
435+
/// standard input and the formatted result will be read from the
436+
/// standard output.
437+
rustfmt_overrideCommand: Option<Vec<String>> = None,
438+
/// Enables the use of rustfmt's unstable range formatting command for the
439+
/// `textDocument/rangeFormatting` request. The rustfmt option is unstable and only
440+
/// available on a nightly build.
441+
rustfmt_rangeFormatting_enable: bool = false,
442442

443443
}
444444
}
@@ -775,7 +775,7 @@ pub struct Config {
775775
user_config_path: VfsPath,
776776

777777
/// Config node whose values apply to **every** Rust project.
778-
user_config: Option<(GlobalLocalConfigInput, ConfigErrors)>,
778+
user_config: Option<(GlobalWorkspaceLocalConfigInput, ConfigErrors)>,
779779

780780
ratoml_file: FxHashMap<SourceRootId, (RatomlFile, ConfigErrors)>,
781781

@@ -825,13 +825,13 @@ impl Config {
825825
if let Ok(table) = toml::from_str(&change) {
826826
let mut toml_errors = vec![];
827827
validate_toml_table(
828-
GlobalLocalConfigInput::FIELDS,
828+
GlobalWorkspaceLocalConfigInput::FIELDS,
829829
&table,
830830
&mut String::new(),
831831
&mut toml_errors,
832832
);
833833
config.user_config = Some((
834-
GlobalLocalConfigInput::from_toml(table, &mut toml_errors),
834+
GlobalWorkspaceLocalConfigInput::from_toml(table, &mut toml_errors),
835835
ConfigErrors(
836836
toml_errors
837837
.into_iter()
@@ -960,7 +960,7 @@ impl Config {
960960
match toml::from_str(&text) {
961961
Ok(table) => {
962962
validate_toml_table(
963-
GlobalLocalConfigInput::FIELDS,
963+
WorkspaceLocalConfigInput::FIELDS,
964964
&table,
965965
&mut String::new(),
966966
&mut toml_errors,
@@ -1863,16 +1863,16 @@ impl Config {
18631863
}
18641864
}
18651865

1866-
pub fn rustfmt(&self) -> RustfmtConfig {
1867-
match &self.rustfmt_overrideCommand() {
1866+
pub fn rustfmt(&self, source_root_id: Option<SourceRootId>) -> RustfmtConfig {
1867+
match &self.rustfmt_overrideCommand(source_root_id) {
18681868
Some(args) if !args.is_empty() => {
18691869
let mut args = args.clone();
18701870
let command = args.remove(0);
18711871
RustfmtConfig::CustomCommand { command, args }
18721872
}
18731873
Some(_) | None => RustfmtConfig::Rustfmt {
1874-
extra_args: self.rustfmt_extraArgs().clone(),
1875-
enable_range_formatting: *self.rustfmt_rangeFormatting_enable(),
1874+
extra_args: self.rustfmt_extraArgs(source_root_id).clone(),
1875+
enable_range_formatting: *self.rustfmt_rangeFormatting_enable(source_root_id),
18761876
},
18771877
}
18781878
}
@@ -2555,24 +2555,20 @@ macro_rules! _impl_for_config_data {
25552555
$vis fn $field(&self, source_root: Option<SourceRootId>) -> &$ty {
25562556
let mut source_root = source_root.as_ref();
25572557
while let Some(sr) = source_root {
2558-
if let Some((file, _)) = self.ratoml_file.get(&sr) {
2559-
match file {
2560-
RatomlFile::Workspace(config) => {
2561-
if let Some(v) = config.workspace.$field.as_ref() {
2562-
return &v;
2563-
}
2564-
},
2558+
if let Some((RatomlFile::Workspace(config), _)) = self.ratoml_file.get(&sr) {
2559+
if let Some(v) = config.workspace.$field.as_ref() {
2560+
return &v;
25652561
}
25662562
}
25672563
source_root = self.source_root_parent_map.get(&sr);
25682564
}
25692565

2570-
if let Some(v) = self.client_config.0.local.$field.as_ref() {
2566+
if let Some(v) = self.client_config.0.workspace.$field.as_ref() {
25712567
return &v;
25722568
}
25732569

25742570
if let Some((user_config, _)) = self.user_config.as_ref() {
2575-
if let Some(v) = user_config.local.$field.as_ref() {
2571+
if let Some(v) = user_config.workspace.$field.as_ref() {
25762572
return &v;
25772573
}
25782574
}
@@ -2591,7 +2587,6 @@ macro_rules! _impl_for_config_data {
25912587
$(
25922588
$($doc)*
25932589
#[allow(non_snake_case)]
2594-
// TODO Remove source_root
25952590
$vis fn $field(&self) -> &$ty {
25962591
if let Some(v) = self.client_config.0.global.$field.as_ref() {
25972592
return &v;
@@ -2730,6 +2725,7 @@ use _config_data as config_data;
27302725
#[derive(Default, Debug, Clone)]
27312726
struct DefaultConfigData {
27322727
global: GlobalDefaultConfigData,
2728+
workspace: WorkspaceDefaultConfigData,
27332729
local: LocalDefaultConfigData,
27342730
client: ClientDefaultConfigData,
27352731
}
@@ -2740,6 +2736,7 @@ struct DefaultConfigData {
27402736
#[derive(Debug, Clone, Default)]
27412737
struct FullConfigInput {
27422738
global: GlobalConfigInput,
2739+
workspace: WorkspaceConfigInput,
27432740
local: LocalConfigInput,
27442741
client: ClientConfigInput,
27452742
}
@@ -2753,6 +2750,7 @@ impl FullConfigInput {
27532750
global: GlobalConfigInput::from_json(&mut json, error_sink),
27542751
local: LocalConfigInput::from_json(&mut json, error_sink),
27552752
client: ClientConfigInput::from_json(&mut json, error_sink),
2753+
workspace: WorkspaceConfigInput::from_json(&mut json, error_sink),
27562754
}
27572755
}
27582756

@@ -2783,26 +2781,28 @@ impl FullConfigInput {
27832781
/// some rust-analyzer.toml file or JSON blob. An empty rust-analyzer.toml corresponds to
27842782
/// all fields being None.
27852783
#[derive(Debug, Clone, Default)]
2786-
struct GlobalLocalConfigInput {
2784+
struct GlobalWorkspaceLocalConfigInput {
27872785
global: GlobalConfigInput,
27882786
local: LocalConfigInput,
2787+
workspace: WorkspaceConfigInput,
27892788
}
27902789

2791-
impl GlobalLocalConfigInput {
2790+
impl GlobalWorkspaceLocalConfigInput {
27922791
const FIELDS: &'static [&'static [&'static str]] =
27932792
&[GlobalConfigInput::FIELDS, LocalConfigInput::FIELDS];
27942793
fn from_toml(
27952794
toml: toml::Table,
27962795
error_sink: &mut Vec<(String, toml::de::Error)>,
2797-
) -> GlobalLocalConfigInput {
2798-
GlobalLocalConfigInput {
2796+
) -> GlobalWorkspaceLocalConfigInput {
2797+
GlobalWorkspaceLocalConfigInput {
27992798
global: GlobalConfigInput::from_toml(&toml, error_sink),
28002799
local: LocalConfigInput::from_toml(&toml, error_sink),
2800+
workspace: WorkspaceConfigInput::from_toml(&toml, error_sink),
28012801
}
28022802
}
28032803
}
28042804

2805-
/// All of the config levels, all fields `Option<T>`, to describe fields that are actually set by
2805+
/// Workspace and local config levels, all fields `Option<T>`, to describe fields that are actually set by
28062806
/// some rust-analyzer.toml file or JSON blob. An empty rust-analyzer.toml corresponds to
28072807
/// all fields being None.
28082808
#[derive(Debug, Clone, Default)]

src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/request.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2113,8 +2113,9 @@ fn run_rustfmt(
21132113
let edition = editions.iter().copied().max();
21142114

21152115
let line_index = snap.file_line_index(file_id)?;
2116+
let source_root_id = snap.analysis.source_root_id(file_id).ok();
21162117

2117-
let mut command = match snap.config.rustfmt() {
2118+
let mut command = match snap.config.rustfmt(source_root_id) {
21182119
RustfmtConfig::Rustfmt { extra_args, enable_range_formatting } => {
21192120
// FIXME: Set RUSTUP_TOOLCHAIN
21202121
let mut cmd = process::Command::new(toolchain::Tool::Rustfmt.path());
@@ -2302,9 +2303,8 @@ pub(crate) fn internal_testing_fetch_config(
23022303
.transpose()?;
23032304
serde_json::to_value(match &*params.config {
23042305
"local" => state.config.assist(source_root).assist_emit_must_use,
2305-
// TODO Most probably will fail because it was not renamed to workspace
2306-
"global" => matches!(
2307-
state.config.rustfmt(),
2306+
"workspace" => matches!(
2307+
state.config.rustfmt(source_root),
23082308
RustfmtConfig::Rustfmt { enable_range_formatting: true, .. }
23092309
),
23102310
_ => return Err(anyhow::anyhow!("Unknown test config key: {}", params.config)),

src/tools/rust-analyzer/crates/rust-analyzer/src/lsp/capabilities.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub fn server_capabilities(config: &Config) -> ServerCapabilities {
6767
code_action_provider: Some(config.caps().code_action_capabilities()),
6868
code_lens_provider: Some(CodeLensOptions { resolve_provider: Some(true) }),
6969
document_formatting_provider: Some(OneOf::Left(true)),
70-
document_range_formatting_provider: match config.rustfmt() {
70+
document_range_formatting_provider: match config.rustfmt(None) {
7171
RustfmtConfig::Rustfmt { enable_range_formatting: true, .. } => Some(OneOf::Left(true)),
7272
_ => Some(OneOf::Left(false)),
7373
},

src/tools/rust-analyzer/crates/rust-analyzer/tests/slow-tests/ratoml.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ enum QueryType {
1717
/// A query whose config key is a part of the global configs, so that
1818
/// testing for changes to this config means testing if global changes
1919
/// take affect.
20-
Global,
20+
Workspace,
2121
}
2222

2323
struct RatomlTest {
@@ -165,7 +165,7 @@ impl RatomlTest {
165165
fn query(&self, query: QueryType, source_file_idx: usize) -> bool {
166166
let config = match query {
167167
QueryType::Local => "local".to_owned(),
168-
QueryType::Global => "global".to_owned(),
168+
QueryType::Workspace => "workspace".to_owned(),
169169
};
170170
let res = self.server.send_request::<InternalTestingFetchConfig>(
171171
InternalTestingFetchConfigParams {
@@ -823,10 +823,8 @@ fn ratoml_multiple_ratoml_in_single_source_root() {
823823
// assert!(!server.query(QueryType::AssistEmitMustUse, 5));
824824
// }
825825

826-
/// Having a ratoml file at the root of a project enables
827-
/// configuring global level configurations as well.
828826
#[test]
829-
fn ratoml_in_root_is_global() {
827+
fn ratoml_in_root_is_workspace() {
830828
if skip_slow_tests() {
831829
return;
832830
}
@@ -854,7 +852,7 @@ fn main() {
854852
None,
855853
);
856854

857-
assert!(server.query(QueryType::Global, 2));
855+
assert!(server.query(QueryType::Workspace, 2));
858856
}
859857

860858
#[test]
@@ -886,9 +884,9 @@ fn main() {
886884
None,
887885
);
888886

889-
assert!(server.query(QueryType::Global, 2));
887+
assert!(server.query(QueryType::Workspace, 2));
890888
server.edit(1, "rustfmt.rangeFormatting.enable = false".to_owned());
891-
assert!(!server.query(QueryType::Global, 2));
889+
assert!(!server.query(QueryType::Workspace, 2));
892890
}
893891

894892
#[test]
@@ -920,7 +918,7 @@ fn main() {
920918
None,
921919
);
922920

923-
assert!(server.query(QueryType::Global, 2));
921+
assert!(server.query(QueryType::Workspace, 2));
924922
server.delete(1);
925-
assert!(!server.query(QueryType::Global, 2));
923+
assert!(!server.query(QueryType::Workspace, 2));
926924
}

src/tools/rust-analyzer/docs/user/generated_config.adoc

-21
Original file line numberDiff line numberDiff line change
@@ -878,27 +878,6 @@ crates must set `[package.metadata.rust-analyzer] rustc_private=true` to use it.
878878

879879
This option does not take effect until rust-analyzer is restarted.
880880
--
881-
[[rust-analyzer.rustfmt.extraArgs]]rust-analyzer.rustfmt.extraArgs (default: `[]`)::
882-
+
883-
--
884-
Additional arguments to `rustfmt`.
885-
--
886-
[[rust-analyzer.rustfmt.overrideCommand]]rust-analyzer.rustfmt.overrideCommand (default: `null`)::
887-
+
888-
--
889-
Advanced option, fully override the command rust-analyzer uses for
890-
formatting. This should be the equivalent of `rustfmt` here, and
891-
not that of `cargo fmt`. The file contents will be passed on the
892-
standard input and the formatted result will be read from the
893-
standard output.
894-
--
895-
[[rust-analyzer.rustfmt.rangeFormatting.enable]]rust-analyzer.rustfmt.rangeFormatting.enable (default: `false`)::
896-
+
897-
--
898-
Enables the use of rustfmt's unstable range formatting command for the
899-
`textDocument/rangeFormatting` request. The rustfmt option is unstable and only
900-
available on a nightly build.
901-
--
902881
[[rust-analyzer.semanticHighlighting.doc.comment.inject.enable]]rust-analyzer.semanticHighlighting.doc.comment.inject.enable (default: `true`)::
903882
+
904883
--

src/tools/rust-analyzer/editors/code/package.json

-39
Original file line numberDiff line numberDiff line change
@@ -2358,45 +2358,6 @@
23582358
}
23592359
}
23602360
},
2361-
{
2362-
"title": "rustfmt",
2363-
"properties": {
2364-
"rust-analyzer.rustfmt.extraArgs": {
2365-
"markdownDescription": "Additional arguments to `rustfmt`.",
2366-
"default": [],
2367-
"type": "array",
2368-
"items": {
2369-
"type": "string"
2370-
}
2371-
}
2372-
}
2373-
},
2374-
{
2375-
"title": "rustfmt",
2376-
"properties": {
2377-
"rust-analyzer.rustfmt.overrideCommand": {
2378-
"markdownDescription": "Advanced option, fully override the command rust-analyzer uses for\nformatting. This should be the equivalent of `rustfmt` here, and\nnot that of `cargo fmt`. The file contents will be passed on the\nstandard input and the formatted result will be read from the\nstandard output.",
2379-
"default": null,
2380-
"type": [
2381-
"null",
2382-
"array"
2383-
],
2384-
"items": {
2385-
"type": "string"
2386-
}
2387-
}
2388-
}
2389-
},
2390-
{
2391-
"title": "rustfmt",
2392-
"properties": {
2393-
"rust-analyzer.rustfmt.rangeFormatting.enable": {
2394-
"markdownDescription": "Enables the use of rustfmt's unstable range formatting command for the\n`textDocument/rangeFormatting` request. The rustfmt option is unstable and only\navailable on a nightly build.",
2395-
"default": false,
2396-
"type": "boolean"
2397-
}
2398-
}
2399-
},
24002361
{
24012362
"title": "semanticHighlighting",
24022363
"properties": {

0 commit comments

Comments
 (0)