Skip to content

Commit f7b16f4

Browse files
committed
fix: project json runnable kind bin substitution
fixes a bug related to substitution in project json bin not correctly replacing the `{label}` tag with the label of the current item.
1 parent 529d3b9 commit f7b16f4

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

crates/ide/src/runnables.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,21 @@ pub enum TestId {
4343
Path(String),
4444
}
4545

46-
impl fmt::Display for TestId {
47-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46+
impl AsRef<str> for TestId {
47+
fn as_ref(&self) -> &str {
4848
match self {
49-
TestId::Name(name) => name.fmt(f),
50-
TestId::Path(path) => path.fmt(f),
49+
Self::Name(n) => &n,
50+
Self::Path(p) => &p,
5151
}
5252
}
5353
}
5454

55+
impl fmt::Display for TestId {
56+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57+
f.write_str(self.as_ref())
58+
}
59+
}
60+
5561
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
5662
pub enum RunnableKind {
5763
TestMod { path: String },

crates/rust-analyzer/src/target_spec.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,34 +70,32 @@ pub(crate) struct ProjectJsonTargetSpec {
7070
impl ProjectJsonTargetSpec {
7171
pub(crate) fn runnable_args(&self, kind: &RunnableKind) -> Option<Runnable> {
7272
match kind {
73-
RunnableKind::Bin => {
74-
for runnable in &self.shell_runnables {
75-
if matches!(runnable.kind, project_model::project_json::RunnableKind::Run) {
76-
return Some(runnable.clone());
77-
}
78-
}
79-
80-
None
81-
}
82-
RunnableKind::Test { test_id, .. } => {
83-
for runnable in &self.shell_runnables {
84-
if matches!(runnable.kind, project_model::project_json::RunnableKind::TestOne) {
85-
let mut runnable = runnable.clone();
86-
87-
let replaced_args: Vec<_> = runnable
88-
.args
89-
.iter()
90-
.map(|arg| arg.replace("{test_id}", &test_id.to_string()))
91-
.map(|arg| arg.replace("{label}", &self.label))
92-
.collect();
93-
runnable.args = replaced_args;
94-
95-
return Some(runnable);
96-
}
97-
}
98-
99-
None
100-
}
73+
RunnableKind::Bin => self
74+
.shell_runnables
75+
.iter()
76+
.find(|r| matches!(r.kind, project_model::project_json::RunnableKind::Run))
77+
.cloned()
78+
.map(|mut runnable| {
79+
runnable.args.iter_mut().for_each(|arg| {
80+
*arg = arg.replace("{label}", &self.label);
81+
});
82+
83+
runnable
84+
}),
85+
RunnableKind::Test { test_id, .. } => self
86+
.shell_runnables
87+
.iter()
88+
.find(|r| matches!(r.kind, project_model::project_json::RunnableKind::TestOne))
89+
.cloned()
90+
.map(|mut runnable| {
91+
runnable.args.iter_mut().for_each(|arg| {
92+
*arg = arg
93+
.replace("{label}", &self.label)
94+
.replace("{test_id}", test_id.as_ref());
95+
});
96+
97+
runnable
98+
}),
10199
RunnableKind::TestMod { .. } => None,
102100
RunnableKind::Bench { .. } => None,
103101
RunnableKind::DocTest { .. } => None,

0 commit comments

Comments
 (0)