Skip to content

Commit edb61b1

Browse files
committed
feat: support UpdateTest in codelens
1 parent dd78825 commit edb61b1

File tree

12 files changed

+204
-57
lines changed

12 files changed

+204
-57
lines changed

crates/ide/src/annotations.rs

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ fn main() {
316316
},
317317
kind: Bin,
318318
cfg: None,
319+
update_test: UpdateTest {
320+
expect_test: false,
321+
insta: false,
322+
snapbox: false,
323+
},
319324
},
320325
),
321326
},
@@ -401,6 +406,11 @@ fn main() {
401406
},
402407
kind: Bin,
403408
cfg: None,
409+
update_test: UpdateTest {
410+
expect_test: false,
411+
insta: false,
412+
snapbox: false,
413+
},
404414
},
405415
),
406416
},
@@ -537,6 +547,11 @@ fn main() {
537547
},
538548
kind: Bin,
539549
cfg: None,
550+
update_test: UpdateTest {
551+
expect_test: false,
552+
insta: false,
553+
snapbox: false,
554+
},
540555
},
541556
),
542557
},
@@ -597,6 +612,11 @@ fn main() {}
597612
},
598613
kind: Bin,
599614
cfg: None,
615+
update_test: UpdateTest {
616+
expect_test: false,
617+
insta: false,
618+
snapbox: false,
619+
},
600620
},
601621
),
602622
},
@@ -709,6 +729,11 @@ fn main() {
709729
},
710730
kind: Bin,
711731
cfg: None,
732+
update_test: UpdateTest {
733+
expect_test: false,
734+
insta: false,
735+
snapbox: false,
736+
},
712737
},
713738
),
714739
},
@@ -744,6 +769,20 @@ mod tests {
744769
"#,
745770
expect![[r#"
746771
[
772+
Annotation {
773+
range: 3..7,
774+
kind: HasReferences {
775+
pos: FilePositionWrapper {
776+
file_id: FileId(
777+
0,
778+
),
779+
offset: 3,
780+
},
781+
data: Some(
782+
[],
783+
),
784+
},
785+
},
747786
Annotation {
748787
range: 3..7,
749788
kind: Runnable(
@@ -760,23 +799,14 @@ mod tests {
760799
},
761800
kind: Bin,
762801
cfg: None,
802+
update_test: UpdateTest {
803+
expect_test: false,
804+
insta: false,
805+
snapbox: false,
806+
},
763807
},
764808
),
765809
},
766-
Annotation {
767-
range: 3..7,
768-
kind: HasReferences {
769-
pos: FilePositionWrapper {
770-
file_id: FileId(
771-
0,
772-
),
773-
offset: 3,
774-
},
775-
data: Some(
776-
[],
777-
),
778-
},
779-
},
780810
Annotation {
781811
range: 18..23,
782812
kind: Runnable(
@@ -796,6 +826,11 @@ mod tests {
796826
path: "tests",
797827
},
798828
cfg: None,
829+
update_test: UpdateTest {
830+
expect_test: false,
831+
insta: false,
832+
snapbox: false,
833+
},
799834
},
800835
),
801836
},
@@ -822,6 +857,11 @@ mod tests {
822857
},
823858
},
824859
cfg: None,
860+
update_test: UpdateTest {
861+
expect_test: false,
862+
insta: false,
863+
snapbox: false,
864+
},
825865
},
826866
),
827867
},

crates/ide/src/hover/tests.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3213,6 +3213,11 @@ fn foo_$0test() {}
32133213
},
32143214
},
32153215
cfg: None,
3216+
update_test: UpdateTest {
3217+
expect_test: false,
3218+
insta: false,
3219+
snapbox: false,
3220+
},
32163221
},
32173222
),
32183223
]
@@ -3230,28 +3235,33 @@ mod tests$0 {
32303235
}
32313236
"#,
32323237
expect![[r#"
3233-
[
3234-
Runnable(
3235-
Runnable {
3236-
use_name_in_title: false,
3237-
nav: NavigationTarget {
3238-
file_id: FileId(
3239-
0,
3240-
),
3241-
full_range: 0..46,
3242-
focus_range: 4..9,
3243-
name: "tests",
3244-
kind: Module,
3245-
description: "mod tests",
3246-
},
3247-
kind: TestMod {
3248-
path: "tests",
3249-
},
3250-
cfg: None,
3238+
[
3239+
Runnable(
3240+
Runnable {
3241+
use_name_in_title: false,
3242+
nav: NavigationTarget {
3243+
file_id: FileId(
3244+
0,
3245+
),
3246+
full_range: 0..46,
3247+
focus_range: 4..9,
3248+
name: "tests",
3249+
kind: Module,
3250+
description: "mod tests",
32513251
},
3252-
),
3253-
]
3254-
"#]],
3252+
kind: TestMod {
3253+
path: "tests",
3254+
},
3255+
cfg: None,
3256+
update_test: UpdateTest {
3257+
expect_test: false,
3258+
insta: false,
3259+
snapbox: false,
3260+
},
3261+
},
3262+
),
3263+
]
3264+
"#]],
32553265
);
32563266
}
32573267

crates/rust-analyzer/src/config.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ config_data! {
243243
/// Whether to show `Run` lens. Only applies when
244244
/// `#rust-analyzer.lens.enable#` is set.
245245
lens_run_enable: bool = true,
246+
/// Whether to show `Update Test` lens. Only applies when
247+
/// `#rust-analyzer.lens.enable#` and `#rust-analyzer.lens.run.enable#` are set.
248+
lens_update_test_enable: bool = true,
246249

247250
/// Disable project auto-discovery in favor of explicitly specified set
248251
/// of projects.
@@ -1161,6 +1164,7 @@ pub struct LensConfig {
11611164
// runnables
11621165
pub run: bool,
11631166
pub debug: bool,
1167+
pub update_test: bool,
11641168
pub interpret: bool,
11651169

11661170
// implementations
@@ -1196,6 +1200,7 @@ impl LensConfig {
11961200
pub fn any(&self) -> bool {
11971201
self.run
11981202
|| self.debug
1203+
|| self.update_test
11991204
|| self.implementations
12001205
|| self.method_refs
12011206
|| self.refs_adt
@@ -1208,7 +1213,7 @@ impl LensConfig {
12081213
}
12091214

12101215
pub fn runnable(&self) -> bool {
1211-
self.run || self.debug
1216+
self.run || self.debug || self.update_test
12121217
}
12131218

12141219
pub fn references(&self) -> bool {
@@ -2120,6 +2125,9 @@ impl Config {
21202125
LensConfig {
21212126
run: *self.lens_enable() && *self.lens_run_enable(),
21222127
debug: *self.lens_enable() && *self.lens_debug_enable(),
2128+
update_test: *self.lens_enable()
2129+
&& *self.lens_update_test_enable()
2130+
&& *self.lens_run_enable(),
21232131
interpret: *self.lens_enable() && *self.lens_run_enable() && *self.interpret_tests(),
21242132
implementations: *self.lens_enable() && *self.lens_implementations_enable(),
21252133
method_refs: *self.lens_enable() && *self.lens_references_method_enable(),

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,14 +427,14 @@ impl Request for Runnables {
427427
const METHOD: &'static str = "experimental/runnables";
428428
}
429429

430-
#[derive(Serialize, Deserialize, Debug)]
430+
#[derive(Serialize, Deserialize, Debug, Clone)]
431431
#[serde(rename_all = "camelCase")]
432432
pub struct RunnablesParams {
433433
pub text_document: TextDocumentIdentifier,
434434
pub position: Option<Position>,
435435
}
436436

437-
#[derive(Deserialize, Serialize, Debug)]
437+
#[derive(Deserialize, Serialize, Debug, Clone)]
438438
#[serde(rename_all = "camelCase")]
439439
pub struct Runnable {
440440
pub label: String,
@@ -444,22 +444,22 @@ pub struct Runnable {
444444
pub args: RunnableArgs,
445445
}
446446

447-
#[derive(Deserialize, Serialize, Debug)]
447+
#[derive(Deserialize, Serialize, Debug, Clone)]
448448
#[serde(rename_all = "camelCase")]
449449
#[serde(untagged)]
450450
pub enum RunnableArgs {
451451
Cargo(CargoRunnableArgs),
452452
Shell(ShellRunnableArgs),
453453
}
454454

455-
#[derive(Serialize, Deserialize, Debug)]
455+
#[derive(Serialize, Deserialize, Debug, Clone)]
456456
#[serde(rename_all = "lowercase")]
457457
pub enum RunnableKind {
458458
Cargo,
459459
Shell,
460460
}
461461

462-
#[derive(Deserialize, Serialize, Debug)]
462+
#[derive(Deserialize, Serialize, Debug, Clone)]
463463
#[serde(rename_all = "camelCase")]
464464
pub struct CargoRunnableArgs {
465465
#[serde(skip_serializing_if = "FxHashMap::is_empty")]
@@ -475,7 +475,7 @@ pub struct CargoRunnableArgs {
475475
pub executable_args: Vec<String>,
476476
}
477477

478-
#[derive(Deserialize, Serialize, Debug)]
478+
#[derive(Deserialize, Serialize, Debug, Clone)]
479479
#[serde(rename_all = "camelCase")]
480480
pub struct ShellRunnableArgs {
481481
#[serde(skip_serializing_if = "FxHashMap::is_empty")]

crates/rust-analyzer/src/lsp/to_proto.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use itertools::Itertools;
2020
use paths::{Utf8Component, Utf8Prefix};
2121
use semver::VersionReq;
2222
use serde_json::to_value;
23+
use syntax::SmolStr;
2324
use vfs::AbsPath;
2425

2526
use crate::{
@@ -1567,6 +1568,7 @@ pub(crate) fn code_lens(
15671568
let line_index = snap.file_line_index(run.nav.file_id)?;
15681569
let annotation_range = range(&line_index, annotation.range);
15691570

1571+
let update_test = run.update_test;
15701572
let title = run.title();
15711573
let can_debug = match run.kind {
15721574
ide::RunnableKind::DocTest { .. } => false,
@@ -1602,6 +1604,17 @@ pub(crate) fn code_lens(
16021604
data: None,
16031605
})
16041606
}
1607+
if lens_config.update_test && client_commands_config.run_single {
1608+
let label = update_test.label();
1609+
if let Some(r) = make_update_runnable(&r, &label) {
1610+
let command = command::run_single(&r, label.unwrap().as_str());
1611+
acc.push(lsp_types::CodeLens {
1612+
range: annotation_range,
1613+
command: Some(command),
1614+
data: None,
1615+
})
1616+
}
1617+
}
16051618
}
16061619

16071620
if lens_config.interpret {
@@ -1786,7 +1799,7 @@ pub(crate) mod command {
17861799

17871800
pub(crate) fn debug_single(runnable: &lsp_ext::Runnable) -> lsp_types::Command {
17881801
lsp_types::Command {
1789-
title: "Debug".into(),
1802+
title: "\u{fe0e} Debug".into(),
17901803
command: "rust-analyzer.debugSingle".into(),
17911804
arguments: Some(vec![to_value(runnable).unwrap()]),
17921805
}
@@ -1838,6 +1851,29 @@ pub(crate) mod command {
18381851
}
18391852
}
18401853

1854+
fn make_update_runnable(
1855+
runnable: &lsp_ext::Runnable,
1856+
label: &Option<SmolStr>,
1857+
) -> Option<lsp_ext::Runnable> {
1858+
if !matches!(runnable.args, lsp_ext::RunnableArgs::Cargo(_)) {
1859+
return None;
1860+
}
1861+
let label = label.as_ref()?;
1862+
1863+
let mut runnable = runnable.clone();
1864+
runnable.label = format!("{} + {}", runnable.label, label);
1865+
1866+
let lsp_ext::RunnableArgs::Cargo(r) = &mut runnable.args else {
1867+
unreachable!();
1868+
};
1869+
1870+
let environment_vars =
1871+
[("UPDATE_EXPECT", "1"), ("INSTA_UPDATE", "always"), ("SNAPSHOTS", "overwrite")];
1872+
r.environment.extend(environment_vars.into_iter().map(|(k, v)| (k.to_owned(), v.to_owned())));
1873+
1874+
Some(runnable)
1875+
}
1876+
18411877
pub(crate) fn implementation_title(count: usize) -> String {
18421878
if count == 1 {
18431879
"1 implementation".into()

docs/dev/lsp-extensions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!---
2-
lsp/ext.rs hash: 9790509d87670c22
2+
lsp/ext.rs hash: 512c06cd8b46a21d
33
44
If you need to change the above hash to make the test pass, please check if you
55
need to adjust this doc as well and ping this issue:

docs/user/generated_config.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,12 @@ Only applies when `#rust-analyzer.lens.enable#` is set.
808808
Whether to show `Run` lens. Only applies when
809809
`#rust-analyzer.lens.enable#` is set.
810810
--
811+
[[rust-analyzer.lens.update.test.enable]]rust-analyzer.lens.update.test.enable (default: `true`)::
812+
+
813+
--
814+
Whether to show `Update Test` lens. Only applies when
815+
`#rust-analyzer.lens.enable#` and `#rust-analyzer.lens.run.enable#` are set.
816+
--
811817
[[rust-analyzer.linkedProjects]]rust-analyzer.linkedProjects (default: `[]`)::
812818
+
813819
--

0 commit comments

Comments
 (0)