Skip to content

Commit 87d57f5

Browse files
committed
Rename checkOnSave settings to flycheck
1 parent 25717af commit 87d57f5

File tree

4 files changed

+302
-295
lines changed

4 files changed

+302
-295
lines changed

crates/rust-analyzer/src/config.rs

Lines changed: 87 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -124,65 +124,8 @@ config_data! {
124124
/// Unsets `#[cfg(test)]` for the specified crates.
125125
cargo_unsetTest: Vec<String> = "[\"core\"]",
126126

127-
/// Check all targets and tests (`--all-targets`).
128-
checkOnSave_allTargets: bool = "true",
129-
/// Cargo command to use for `cargo check`.
130-
checkOnSave_command: String = "\"check\"",
131-
/// Run specified `cargo check` command for diagnostics on save.
132-
checkOnSave_enable: bool = "true",
133-
/// Extra arguments for `cargo check`.
134-
checkOnSave_extraArgs: Vec<String> = "[]",
135-
/// Extra environment variables that will be set when running `cargo check`.
136-
/// Extends `#rust-analyzer.cargo.extraEnv#`.
137-
checkOnSave_extraEnv: FxHashMap<String, String> = "{}",
138-
/// List of features to activate. Defaults to
139-
/// `#rust-analyzer.cargo.features#`.
140-
///
141-
/// Set to `"all"` to pass `--all-features` to Cargo.
142-
checkOnSave_features: Option<CargoFeaturesDef> = "null",
143-
/// Specifies the working directory for running checks.
144-
/// - "workspace": run checks for workspaces in the corresponding workspaces' root directories.
145-
// FIXME: Ideally we would support this in some way
146-
/// This falls back to "root" if `#rust-analyzer.cargo.checkOnSave.invocationStrategy#` is set to `once`.
147-
/// - "root": run checks in the project's root directory.
148-
/// This config only has an effect when `#rust-analyzer.cargo.buildScripts.overrideCommand#`
149-
/// is set.
150-
checkOnSave_invocationLocation: InvocationLocation = "\"workspace\"",
151-
/// Specifies the invocation strategy to use when running the checkOnSave command.
152-
/// If `per_workspace` is set, the command will be executed for each workspace.
153-
/// If `once` is set, the command will be executed once.
154-
/// This config only has an effect when `#rust-analyzer.cargo.buildScripts.overrideCommand#`
155-
/// is set.
156-
checkOnSave_invocationStrategy: InvocationStrategy = "\"per_workspace\"",
157-
/// Whether to pass `--no-default-features` to Cargo. Defaults to
158-
/// `#rust-analyzer.cargo.noDefaultFeatures#`.
159-
checkOnSave_noDefaultFeatures: Option<bool> = "null",
160-
/// Override the command rust-analyzer uses instead of `cargo check` for
161-
/// diagnostics on save. The command is required to output json and
162-
/// should therefore include `--message-format=json` or a similar option.
163-
///
164-
/// If you're changing this because you're using some tool wrapping
165-
/// Cargo, you might also want to change
166-
/// `#rust-analyzer.cargo.buildScripts.overrideCommand#`.
167-
///
168-
/// If there are multiple linked projects, this command is invoked for
169-
/// each of them, with the working directory being the project root
170-
/// (i.e., the folder containing the `Cargo.toml`).
171-
///
172-
/// An example command would be:
173-
///
174-
/// ```bash
175-
/// cargo check --workspace --message-format=json --all-targets
176-
/// ```
177-
/// .
178-
checkOnSave_overrideCommand: Option<Vec<String>> = "null",
179-
/// Check for specific targets. Defaults to `#rust-analyzer.cargo.target#` if empty.
180-
///
181-
/// Can be a single target, e.g. `"x86_64-unknown-linux-gnu"` or a list of targets, e.g.
182-
/// `["aarch64-apple-darwin", "x86_64-apple-darwin"]`.
183-
///
184-
/// Aliased as `"checkOnSave.targets"`.
185-
checkOnSave_target | checkOnSave_targets: Option<CheckOnSaveTargets> = "null",
127+
/// Run the flycheck command for diagnostics on save.
128+
checkOnSave | checkOnSave_enable: bool = "true",
186129

187130
/// Toggles the additional completions that automatically add imports when completed.
188131
/// Note that your client must specify the `additionalTextEdits` LSP client capability to truly have this feature enabled.
@@ -268,6 +211,64 @@ config_data! {
268211
/// Controls file watching implementation.
269212
files_watcher: FilesWatcherDef = "\"client\"",
270213

214+
/// Check all targets and tests (`--all-targets`).
215+
flycheck_allTargets | checkOnSave_allTargets: bool = "true",
216+
/// Cargo command to use for `cargo check`.
217+
flycheck_command | checkOnSave_command: String = "\"check\"",
218+
/// Extra arguments for `cargo check`.
219+
flycheck_extraArgs | checkOnSave_extraArgs: Vec<String> = "[]",
220+
/// Extra environment variables that will be set when running `cargo check`.
221+
/// Extends `#rust-analyzer.cargo.extraEnv#`.
222+
flycheck_extraEnv | checkOnSave_extraEnv: FxHashMap<String, String> = "{}",
223+
/// List of features to activate. Defaults to
224+
/// `#rust-analyzer.cargo.features#`.
225+
///
226+
/// Set to `"all"` to pass `--all-features` to Cargo.
227+
flycheck_features | checkOnSave_features: Option<CargoFeaturesDef> = "null",
228+
/// Specifies the working directory for running checks.
229+
/// - "workspace": run checks for workspaces in the corresponding workspaces' root directories.
230+
// FIXME: Ideally we would support this in some way
231+
/// This falls back to "root" if `#rust-analyzer.cargo.checkOnSave.invocationStrategy#` is set to `once`.
232+
/// - "root": run checks in the project's root directory.
233+
/// This config only has an effect when `#rust-analyzer.cargo.buildScripts.overrideCommand#`
234+
/// is set.
235+
flycheck_invocationLocation | checkOnSave_invocationLocation: InvocationLocation = "\"workspace\"",
236+
/// Specifies the invocation strategy to use when running the checkOnSave command.
237+
/// If `per_workspace` is set, the command will be executed for each workspace.
238+
/// If `once` is set, the command will be executed once.
239+
/// This config only has an effect when `#rust-analyzer.cargo.buildScripts.overrideCommand#`
240+
/// is set.
241+
flycheck_invocationStrategy | checkOnSave_invocationStrategy: InvocationStrategy = "\"per_workspace\"",
242+
/// Whether to pass `--no-default-features` to Cargo. Defaults to
243+
/// `#rust-analyzer.cargo.noDefaultFeatures#`.
244+
flycheck_noDefaultFeatures | checkOnSave_noDefaultFeatures: Option<bool> = "null",
245+
/// Override the command rust-analyzer uses instead of `cargo check` for
246+
/// diagnostics on save. The command is required to output json and
247+
/// should therefore include `--message-format=json` or a similar option.
248+
///
249+
/// If you're changing this because you're using some tool wrapping
250+
/// Cargo, you might also want to change
251+
/// `#rust-analyzer.cargo.buildScripts.overrideCommand#`.
252+
///
253+
/// If there are multiple linked projects, this command is invoked for
254+
/// each of them, with the working directory being the project root
255+
/// (i.e., the folder containing the `Cargo.toml`).
256+
///
257+
/// An example command would be:
258+
///
259+
/// ```bash
260+
/// cargo check --workspace --message-format=json --all-targets
261+
/// ```
262+
/// .
263+
flycheck_overrideCommand | checkOnSave_overrideCommand: Option<Vec<String>> = "null",
264+
/// Check for specific targets. Defaults to `#rust-analyzer.cargo.target#` if empty.
265+
///
266+
/// Can be a single target, e.g. `"x86_64-unknown-linux-gnu"` or a list of targets, e.g.
267+
/// `["aarch64-apple-darwin", "x86_64-apple-darwin"]`.
268+
///
269+
/// Aliased as `"checkOnSave.targets"`.
270+
flycheck_targets | checkOnSave_targets | checkOnSave_target: Option<CheckOnSaveTargets> = "null",
271+
271272
/// Enables highlighting of related references while the cursor is on `break`, `loop`, `while`, or `for` keywords.
272273
highlightRelated_breakPoints_enable: bool = "true",
273274
/// Enables highlighting of all exit points while the cursor is on any `return`, `?`, `fn`, or return type arrow (`->`).
@@ -786,9 +787,9 @@ impl Config {
786787

787788
fn validate(&self, error_sink: &mut Vec<(String, serde_json::Error)>) {
788789
use serde::de::Error;
789-
if self.data.checkOnSave_command.is_empty() {
790+
if self.data.flycheck_command.is_empty() {
790791
error_sink.push((
791-
"/checkOnSave/command".to_string(),
792+
"/flycheck/command".to_string(),
792793
serde_json::Error::custom("expected a non-empty string"),
793794
));
794795
}
@@ -1033,7 +1034,7 @@ impl Config {
10331034

10341035
pub fn check_on_save_extra_env(&self) -> FxHashMap<String, String> {
10351036
let mut extra_env = self.data.cargo_extraEnv.clone();
1036-
extra_env.extend(self.data.checkOnSave_extraEnv.clone());
1037+
extra_env.extend(self.data.flycheck_extraEnv.clone());
10371038
extra_env
10381039
}
10391040

@@ -1145,21 +1146,21 @@ impl Config {
11451146
}
11461147

11471148
pub fn flycheck(&self) -> FlycheckConfig {
1148-
match &self.data.checkOnSave_overrideCommand {
1149+
match &self.data.flycheck_overrideCommand {
11491150
Some(args) if !args.is_empty() => {
11501151
let mut args = args.clone();
11511152
let command = args.remove(0);
11521153
FlycheckConfig::CustomCommand {
11531154
command,
11541155
args,
11551156
extra_env: self.check_on_save_extra_env(),
1156-
invocation_strategy: match self.data.checkOnSave_invocationStrategy {
1157+
invocation_strategy: match self.data.flycheck_invocationStrategy {
11571158
InvocationStrategy::Once => flycheck::InvocationStrategy::Once,
11581159
InvocationStrategy::PerWorkspace => {
11591160
flycheck::InvocationStrategy::PerWorkspace
11601161
}
11611162
},
1162-
invocation_location: match self.data.checkOnSave_invocationLocation {
1163+
invocation_location: match self.data.flycheck_invocationLocation {
11631164
InvocationLocation::Root => {
11641165
flycheck::InvocationLocation::Root(self.root_path.clone())
11651166
}
@@ -1168,42 +1169,42 @@ impl Config {
11681169
}
11691170
}
11701171
Some(_) | None => FlycheckConfig::CargoCommand {
1171-
command: self.data.checkOnSave_command.clone(),
1172+
command: self.data.flycheck_command.clone(),
11721173
target_triples: self
11731174
.data
1174-
.checkOnSave_target
1175+
.flycheck_targets
11751176
.clone()
11761177
.and_then(|targets| match &targets.0[..] {
11771178
[] => None,
11781179
targets => Some(targets.into()),
11791180
})
11801181
.unwrap_or_else(|| self.data.cargo_target.clone().into_iter().collect()),
1181-
all_targets: self.data.checkOnSave_allTargets,
1182+
all_targets: self.data.flycheck_allTargets,
11821183
no_default_features: self
11831184
.data
1184-
.checkOnSave_noDefaultFeatures
1185+
.flycheck_noDefaultFeatures
11851186
.unwrap_or(self.data.cargo_noDefaultFeatures),
11861187
all_features: matches!(
1187-
self.data.checkOnSave_features.as_ref().unwrap_or(&self.data.cargo_features),
1188+
self.data.flycheck_features.as_ref().unwrap_or(&self.data.cargo_features),
11881189
CargoFeaturesDef::All
11891190
),
11901191
features: match self
11911192
.data
1192-
.checkOnSave_features
1193+
.flycheck_features
11931194
.clone()
11941195
.unwrap_or_else(|| self.data.cargo_features.clone())
11951196
{
11961197
CargoFeaturesDef::All => vec![],
11971198
CargoFeaturesDef::Selected(it) => it,
11981199
},
1199-
extra_args: self.data.checkOnSave_extraArgs.clone(),
1200+
extra_args: self.data.flycheck_extraArgs.clone(),
12001201
extra_env: self.check_on_save_extra_env(),
12011202
},
12021203
}
12031204
}
12041205

12051206
pub fn check_on_save(&self) -> bool {
1206-
self.data.checkOnSave_enable
1207+
self.data.checkOnSave
12071208
}
12081209

12091210
pub fn runnables(&self) -> RunnablesConfig {
@@ -1862,25 +1863,27 @@ fn get_field<T: DeserializeOwned>(
18621863
alias: Option<&'static str>,
18631864
default: &str,
18641865
) -> T {
1865-
let default = serde_json::from_str(default).unwrap();
18661866
// XXX: check alias first, to work-around the VS Code where it pre-fills the
18671867
// defaults instead of sending an empty object.
18681868
alias
18691869
.into_iter()
18701870
.chain(iter::once(field))
1871-
.find_map(move |field| {
1871+
.filter_map(move |field| {
18721872
let mut pointer = field.replace('_', "/");
18731873
pointer.insert(0, '/');
1874-
json.pointer_mut(&pointer).and_then(|it| match serde_json::from_value(it.take()) {
1875-
Ok(it) => Some(it),
1876-
Err(e) => {
1877-
tracing::warn!("Failed to deserialize config field at {}: {:?}", pointer, e);
1878-
error_sink.push((pointer, e));
1879-
None
1880-
}
1881-
})
1874+
json.pointer_mut(&pointer)
1875+
.map(|it| serde_json::from_value(it.take()).map_err(|e| (e, pointer)))
1876+
})
1877+
.find(Result::is_ok)
1878+
.and_then(|res| match res {
1879+
Ok(it) => Some(it),
1880+
Err((e, pointer)) => {
1881+
tracing::warn!("Failed to deserialize config field at {}: {:?}", pointer, e);
1882+
error_sink.push((pointer, e));
1883+
None
1884+
}
18821885
})
1883-
.unwrap_or(default)
1886+
.unwrap_or_else(|| serde_json::from_str(default).unwrap())
18841887
}
18851888

18861889
fn schema(fields: &[(&'static str, &'static str, &[&str], &str)]) -> serde_json::Value {
@@ -1964,15 +1967,6 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json
19641967
"type": ["null", "array"],
19651968
"items": { "type": "string" },
19661969
},
1967-
"MergeBehaviorDef" => set! {
1968-
"type": "string",
1969-
"enum": ["none", "crate", "module"],
1970-
"enumDescriptions": [
1971-
"Do not merge imports at all.",
1972-
"Merge imports from the same crate into a single `use` statement.",
1973-
"Merge imports from the same module into a single `use` statement."
1974-
],
1975-
},
19761970
"ExprFillDefaultDef" => set! {
19771971
"type": "string",
19781972
"enum": ["todo", "default"],

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use serde_json::{json, Value};
44
/// This function patches the json config to the new expected keys.
55
/// That is we try to load old known config keys here and convert them to the new ones.
66
/// See https://github.com/rust-lang/rust-analyzer/pull/12010
7+
///
8+
/// We already have an alias system for simple cases, but if we make structural changes
9+
/// the alias infra fails down.
710
pub(super) fn patch_json_for_outdated_configs(json: &mut Value) {
811
let copy = json.clone();
912

@@ -105,9 +108,9 @@ pub(super) fn patch_json_for_outdated_configs(json: &mut Value) {
105108
merge(json, json!({ "cargo": { "features": "all" } }));
106109
}
107110

108-
// checkOnSave_allFeatures, checkOnSave_features -> checkOnSave_features
111+
// checkOnSave_allFeatures, checkOnSave_features -> flycheck_features
109112
if let Some(Value::Bool(true)) = copy.pointer("/checkOnSave/allFeatures") {
110-
merge(json, json!({ "checkOnSave": { "features": "all" } }));
113+
merge(json, json!({ "flycheck": { "features": "all" } }));
111114
}
112115

113116
// completion_addCallArgumentSnippets completion_addCallParenthesis -> completion_callable_snippets
@@ -121,6 +124,16 @@ pub(super) fn patch_json_for_outdated_configs(json: &mut Value) {
121124
(_, _) => return,
122125
};
123126
merge(json, json!({ "completion": { "callable": {"snippets": res }} }));
127+
128+
// We need to do this due to the checkOnSave_enable -> checkOnSave change, as that key now can either be an object or a bool
129+
// checkOnSave_* -> flycheck_*
130+
if let Some(Value::Object(obj)) = copy.pointer("/checkOnSave") {
131+
// checkOnSave_enable -> checkOnSave
132+
if let Some(b @ Value::Bool(_)) = obj.get("enable") {
133+
merge(json, json!({ "checkOnSave": b }));
134+
}
135+
merge(json, json!({ "flycheck": obj }));
136+
}
124137
}
125138

126139
fn merge(dst: &mut Value, src: Value) {

0 commit comments

Comments
 (0)