Skip to content

Commit 31ed8c9

Browse files
committed
Better testing infra for ratoml
1 parent 08c7bbc commit 31ed8c9

File tree

5 files changed

+228
-103
lines changed

5 files changed

+228
-103
lines changed

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
@@ -438,48 +440,6 @@ config_data! {
438440
completion_postfix_enable: bool = true,
439441
/// Enables completions of private items and fields that are defined in the current workspace even if they are not visible at the current position.
440442
completion_privateEditable_enable: bool = false,
441-
/// Custom completion snippets.
442-
completion_snippets_custom: FxHashMap<String, SnippetDef> = serde_json::from_str(r#"{
443-
"Arc::new": {
444-
"postfix": "arc",
445-
"body": "Arc::new(${receiver})",
446-
"requires": "std::sync::Arc",
447-
"description": "Put the expression into an `Arc`",
448-
"scope": "expr"
449-
},
450-
"Rc::new": {
451-
"postfix": "rc",
452-
"body": "Rc::new(${receiver})",
453-
"requires": "std::rc::Rc",
454-
"description": "Put the expression into an `Rc`",
455-
"scope": "expr"
456-
},
457-
"Box::pin": {
458-
"postfix": "pinbox",
459-
"body": "Box::pin(${receiver})",
460-
"requires": "std::boxed::Box",
461-
"description": "Put the expression into a pinned `Box`",
462-
"scope": "expr"
463-
},
464-
"Ok": {
465-
"postfix": "ok",
466-
"body": "Ok(${receiver})",
467-
"description": "Wrap the expression in a `Result::Ok`",
468-
"scope": "expr"
469-
},
470-
"Err": {
471-
"postfix": "err",
472-
"body": "Err(${receiver})",
473-
"description": "Wrap the expression in a `Result::Err`",
474-
"scope": "expr"
475-
},
476-
"Some": {
477-
"postfix": "some",
478-
"body": "Some(${receiver})",
479-
"description": "Wrap the expression in an `Option::Some`",
480-
"scope": "expr"
481-
}
482-
}"#).unwrap(),
483443
/// Whether to enable term search based snippets like `Some(foo.bar().baz())`.
484444
completion_termSearch_enable: bool = false,
485445
/// Term search fuel in "units of work" for autocompletion (Defaults to 1000).
@@ -889,7 +849,7 @@ impl Config {
889849
// IMPORTANT : This holds as long as ` completion_snippets_custom` is declared `client`.
890850
config.snippets.clear();
891851

892-
let snips = self.completion_snippets_custom(None).to_owned();
852+
let snips = self.completion_snippets_custom().to_owned();
893853

894854
for (name, def) in snips.iter() {
895855
if def.prefix.is_empty() && def.postfix.is_empty() {
@@ -1266,7 +1226,7 @@ pub struct NotificationsConfig {
12661226
pub cargo_toml_not_found: bool,
12671227
}
12681228

1269-
#[derive(Debug, Clone)]
1229+
#[derive(Deserialize, Serialize, Debug, Clone)]
12701230
pub enum RustfmtConfig {
12711231
Rustfmt { extra_args: Vec<String>, enable_range_formatting: bool },
12721232
CustomCommand { command: String, args: Vec<String> },
@@ -1897,6 +1857,53 @@ impl Config {
18971857
}
18981858
}
18991859

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

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.

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)