Skip to content

Commit e26ad21

Browse files
committed
Auto merge of rust-lang#18057 - alibektas:better_ratoml_testing, r=Veykril
internal: Better testing infra for ratoml This PR makes some improvements on how we test configs that come from `rust-analyzer.toml` files. It was primarily used to solve rust-lang#18021 but along the way I could not really determine the cause of the said issue which makes me think that it may not be related to the changes that I made earlier to the ratoml infra. In either way `custom_snippets` are now made `global` because we still don't have a tree that maps a `SourceRootId` to a set of `Snippet`s.
2 parents 91e9cd3 + 81227a3 commit e26ad21

File tree

5 files changed

+228
-103
lines changed

5 files changed

+228
-103
lines changed

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

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ config_data! {
7575
/// How many worker threads to handle priming caches. The default `0` means to pick automatically.
7676
cachePriming_numThreads: NumThreads = NumThreads::Physical,
7777

78+
/// Custom completion snippets.
79+
completion_snippets_custom: FxHashMap<String, SnippetDef> = Config::completion_snippets_default(),
7880

7981

8082
/// These directories will be ignored by rust-analyzer. They are
@@ -442,48 +444,6 @@ config_data! {
442444
completion_postfix_enable: bool = true,
443445
/// Enables completions of private items and fields that are defined in the current workspace even if they are not visible at the current position.
444446
completion_privateEditable_enable: bool = false,
445-
/// Custom completion snippets.
446-
completion_snippets_custom: FxHashMap<String, SnippetDef> = serde_json::from_str(r#"{
447-
"Arc::new": {
448-
"postfix": "arc",
449-
"body": "Arc::new(${receiver})",
450-
"requires": "std::sync::Arc",
451-
"description": "Put the expression into an `Arc`",
452-
"scope": "expr"
453-
},
454-
"Rc::new": {
455-
"postfix": "rc",
456-
"body": "Rc::new(${receiver})",
457-
"requires": "std::rc::Rc",
458-
"description": "Put the expression into an `Rc`",
459-
"scope": "expr"
460-
},
461-
"Box::pin": {
462-
"postfix": "pinbox",
463-
"body": "Box::pin(${receiver})",
464-
"requires": "std::boxed::Box",
465-
"description": "Put the expression into a pinned `Box`",
466-
"scope": "expr"
467-
},
468-
"Ok": {
469-
"postfix": "ok",
470-
"body": "Ok(${receiver})",
471-
"description": "Wrap the expression in a `Result::Ok`",
472-
"scope": "expr"
473-
},
474-
"Err": {
475-
"postfix": "err",
476-
"body": "Err(${receiver})",
477-
"description": "Wrap the expression in a `Result::Err`",
478-
"scope": "expr"
479-
},
480-
"Some": {
481-
"postfix": "some",
482-
"body": "Some(${receiver})",
483-
"description": "Wrap the expression in an `Option::Some`",
484-
"scope": "expr"
485-
}
486-
}"#).unwrap(),
487447
/// Whether to enable term search based snippets like `Some(foo.bar().baz())`.
488448
completion_termSearch_enable: bool = false,
489449
/// Term search fuel in "units of work" for autocompletion (Defaults to 1000).
@@ -893,7 +853,7 @@ impl Config {
893853
// IMPORTANT : This holds as long as ` completion_snippets_custom` is declared `client`.
894854
config.snippets.clear();
895855

896-
let snips = self.completion_snippets_custom(None).to_owned();
856+
let snips = self.completion_snippets_custom().to_owned();
897857

898858
for (name, def) in snips.iter() {
899859
if def.prefix.is_empty() && def.postfix.is_empty() {
@@ -1270,7 +1230,7 @@ pub struct NotificationsConfig {
12701230
pub cargo_toml_not_found: bool,
12711231
}
12721232

1273-
#[derive(Debug, Clone)]
1233+
#[derive(Deserialize, Serialize, Debug, Clone)]
12741234
pub enum RustfmtConfig {
12751235
Rustfmt { extra_args: Vec<String>, enable_range_formatting: bool },
12761236
CustomCommand { command: String, args: Vec<String> },
@@ -1902,6 +1862,53 @@ impl Config {
19021862
}
19031863
}
19041864

1865+
pub(crate) fn completion_snippets_default() -> FxHashMap<String, SnippetDef> {
1866+
serde_json::from_str(
1867+
r#"{
1868+
"Arc::new": {
1869+
"postfix": "arc",
1870+
"body": "Arc::new(${receiver})",
1871+
"requires": "std::sync::Arc",
1872+
"description": "Put the expression into an `Arc`",
1873+
"scope": "expr"
1874+
},
1875+
"Rc::new": {
1876+
"postfix": "rc",
1877+
"body": "Rc::new(${receiver})",
1878+
"requires": "std::rc::Rc",
1879+
"description": "Put the expression into an `Rc`",
1880+
"scope": "expr"
1881+
},
1882+
"Box::pin": {
1883+
"postfix": "pinbox",
1884+
"body": "Box::pin(${receiver})",
1885+
"requires": "std::boxed::Box",
1886+
"description": "Put the expression into a pinned `Box`",
1887+
"scope": "expr"
1888+
},
1889+
"Ok": {
1890+
"postfix": "ok",
1891+
"body": "Ok(${receiver})",
1892+
"description": "Wrap the expression in a `Result::Ok`",
1893+
"scope": "expr"
1894+
},
1895+
"Err": {
1896+
"postfix": "err",
1897+
"body": "Err(${receiver})",
1898+
"description": "Wrap the expression in a `Result::Err`",
1899+
"scope": "expr"
1900+
},
1901+
"Some": {
1902+
"postfix": "some",
1903+
"body": "Some(${receiver})",
1904+
"description": "Wrap the expression in an `Option::Some`",
1905+
"scope": "expr"
1906+
}
1907+
}"#,
1908+
)
1909+
.unwrap()
1910+
}
1911+
19051912
pub fn rustfmt(&self, source_root_id: Option<SourceRootId>) -> RustfmtConfig {
19061913
match &self.rustfmt_overrideCommand(source_root_id) {
19071914
Some(args) if !args.is_empty() => {

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ use crate::{
4040
hack_recover_crate_name,
4141
line_index::LineEndings,
4242
lsp::{
43-
ext::InternalTestingFetchConfigParams,
43+
ext::{
44+
InternalTestingFetchConfigOption, InternalTestingFetchConfigParams,
45+
InternalTestingFetchConfigResponse,
46+
},
4447
from_proto, to_proto,
4548
utils::{all_edits_are_disjoint, invalid_params_error},
4649
LspError,
@@ -2292,7 +2295,7 @@ pub(crate) fn fetch_dependency_list(
22922295
pub(crate) fn internal_testing_fetch_config(
22932296
state: GlobalStateSnapshot,
22942297
params: InternalTestingFetchConfigParams,
2295-
) -> anyhow::Result<serde_json::Value> {
2298+
) -> anyhow::Result<Option<InternalTestingFetchConfigResponse>> {
22962299
let source_root = params
22972300
.text_document
22982301
.map(|it| {
@@ -2302,15 +2305,18 @@ pub(crate) fn internal_testing_fetch_config(
23022305
.map_err(anyhow::Error::from)
23032306
})
23042307
.transpose()?;
2305-
serde_json::to_value(match &*params.config {
2306-
"local" => state.config.assist(source_root).assist_emit_must_use,
2307-
"workspace" => matches!(
2308-
state.config.rustfmt(source_root),
2309-
RustfmtConfig::Rustfmt { enable_range_formatting: true, .. }
2310-
),
2311-
_ => return Err(anyhow::anyhow!("Unknown test config key: {}", params.config)),
2312-
})
2313-
.map_err(Into::into)
2308+
Ok(Some(match params.config {
2309+
InternalTestingFetchConfigOption::AssistEmitMustUse => {
2310+
InternalTestingFetchConfigResponse::AssistEmitMustUse(
2311+
state.config.assist(source_root).assist_emit_must_use,
2312+
)
2313+
}
2314+
InternalTestingFetchConfigOption::CheckWorkspace => {
2315+
InternalTestingFetchConfigResponse::CheckWorkspace(
2316+
state.config.flycheck_workspace(source_root),
2317+
)
2318+
}
2319+
}))
23142320
}
23152321

23162322
/// Searches for the directory of a Rust crate given this crate's root file path.

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,30 @@ use serde::{Deserialize, Serialize};
1616

1717
pub enum InternalTestingFetchConfig {}
1818

19+
#[derive(Deserialize, Serialize, Debug)]
20+
pub enum InternalTestingFetchConfigOption {
21+
AssistEmitMustUse,
22+
CheckWorkspace,
23+
}
24+
25+
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq)]
26+
pub enum InternalTestingFetchConfigResponse {
27+
AssistEmitMustUse(bool),
28+
CheckWorkspace(bool),
29+
}
30+
1931
impl Request for InternalTestingFetchConfig {
2032
type Params = InternalTestingFetchConfigParams;
21-
type Result = serde_json::Value;
33+
// Option is solely to circumvent Default bound.
34+
type Result = Option<InternalTestingFetchConfigResponse>;
2235
const METHOD: &'static str = "rust-analyzer-internal/internalTestingFetchConfig";
2336
}
2437

2538
#[derive(Deserialize, Serialize, Debug)]
2639
#[serde(rename_all = "camelCase")]
2740
pub struct InternalTestingFetchConfigParams {
2841
pub text_document: Option<TextDocumentIdentifier>,
29-
pub config: String,
42+
pub config: InternalTestingFetchConfigOption,
3043
}
3144
pub enum AnalyzerStatus {}
3245

0 commit comments

Comments
 (0)