Skip to content

Commit f696fb5

Browse files
Merge #9950
9950: Fix codegen for is_method documentation r=yoshuawuyts a=yoshuawuyts While authoring rust-lang/rust#88154 I realized that the codegen for the `enum_generate_is_method` assist currently generates invalid paths, and used snake case instead of spaces for the docs description. This fixes both issues. Thanks! Co-authored-by: Yoshua Wuyts <[email protected]>
2 parents ee4505f + 586d6fc commit f696fb5

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

crates/ide_assists/src/handlers/generate_enum_is_method.rs

+57-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ use crate::{
2828
//
2929
// impl Version {
3030
// /// Returns `true` if the version is [`Minor`].
31+
// ///
32+
// /// [`Minor`]: Version::Minor
3133
// fn is_minor(&self) -> bool {
3234
// matches!(self, Self::Minor)
3335
// }
@@ -43,7 +45,8 @@ pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext) ->
4345
ast::StructKind::Unit => "",
4446
};
4547

46-
let enum_lowercase_name = to_lower_snake_case(&parent_enum.name()?.to_string());
48+
let enum_name = parent_enum.name()?;
49+
let enum_lowercase_name = to_lower_snake_case(&enum_name.to_string()).replace('_', " ");
4750
let fn_name = format!("is_{}", &to_lower_snake_case(&variant_name.text()));
4851

4952
// Return early if we've found an existing new fn
@@ -57,11 +60,18 @@ pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext) ->
5760
|builder| {
5861
let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
5962
let method = format!(
60-
" /// Returns `true` if the {} is [`{}`].
63+
" /// Returns `true` if the {} is [`{variant}`].
64+
///
65+
/// [`{variant}`]: {}::{variant}
6166
{}fn {}(&self) -> bool {{
62-
matches!(self, Self::{}{})
67+
matches!(self, Self::{variant}{})
6368
}}",
64-
enum_lowercase_name, variant_name, vis, fn_name, variant_name, pattern_suffix,
69+
enum_lowercase_name,
70+
enum_name,
71+
vis,
72+
fn_name,
73+
pattern_suffix,
74+
variant = variant_name
6575
);
6676

6777
add_method_to_adt(builder, &parent_enum, impl_def, &method);
@@ -93,6 +103,8 @@ enum Variant {
93103
94104
impl Variant {
95105
/// Returns `true` if the variant is [`Minor`].
106+
///
107+
/// [`Minor`]: Variant::Minor
96108
fn is_minor(&self) -> bool {
97109
matches!(self, Self::Minor)
98110
}
@@ -137,6 +149,8 @@ enum Variant {
137149
138150
impl Variant {
139151
/// Returns `true` if the variant is [`Minor`].
152+
///
153+
/// [`Minor`]: Variant::Minor
140154
fn is_minor(&self) -> bool {
141155
matches!(self, Self::Minor(..))
142156
}
@@ -162,6 +176,8 @@ enum Variant {
162176
163177
impl Variant {
164178
/// Returns `true` if the variant is [`Minor`].
179+
///
180+
/// [`Minor`]: Variant::Minor
165181
fn is_minor(&self) -> bool {
166182
matches!(self, Self::Minor { .. })
167183
}
@@ -179,6 +195,8 @@ enum Variant { Undefined }
179195
180196
impl Variant {
181197
/// Returns `true` if the variant is [`Undefined`].
198+
///
199+
/// [`Undefined`]: Variant::Undefined
182200
fn is_undefined(&self) -> bool {
183201
matches!(self, Self::Undefined)
184202
}
@@ -204,6 +222,8 @@ pub(crate) enum Variant {
204222
205223
impl Variant {
206224
/// Returns `true` if the variant is [`Minor`].
225+
///
226+
/// [`Minor`]: Variant::Minor
207227
pub(crate) fn is_minor(&self) -> bool {
208228
matches!(self, Self::Minor)
209229
}
@@ -224,6 +244,8 @@ enum Variant {
224244
225245
impl Variant {
226246
/// Returns `true` if the variant is [`Minor`].
247+
///
248+
/// [`Minor`]: Variant::Minor
227249
fn is_minor(&self) -> bool {
228250
matches!(self, Self::Minor)
229251
}
@@ -236,14 +258,45 @@ impl Variant {
236258
237259
impl Variant {
238260
/// Returns `true` if the variant is [`Minor`].
261+
///
262+
/// [`Minor`]: Variant::Minor
239263
fn is_minor(&self) -> bool {
240264
matches!(self, Self::Minor)
241265
}
242266
243267
/// Returns `true` if the variant is [`Major`].
268+
///
269+
/// [`Major`]: Variant::Major
244270
fn is_major(&self) -> bool {
245271
matches!(self, Self::Major)
246272
}
273+
}"#,
274+
);
275+
}
276+
277+
#[test]
278+
fn test_generate_enum_is_variant_names() {
279+
check_assist(
280+
generate_enum_is_method,
281+
r#"
282+
enum GeneratorState {
283+
Yielded,
284+
Complete$0,
285+
Major,
286+
}"#,
287+
r#"enum GeneratorState {
288+
Yielded,
289+
Complete,
290+
Major,
291+
}
292+
293+
impl GeneratorState {
294+
/// Returns `true` if the generator state is [`Complete`].
295+
///
296+
/// [`Complete`]: GeneratorState::Complete
297+
fn is_complete(&self) -> bool {
298+
matches!(self, Self::Complete)
299+
}
247300
}"#,
248301
);
249302
}

crates/ide_assists/src/tests/generated.rs

+2
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ enum Version {
723723
724724
impl Version {
725725
/// Returns `true` if the version is [`Minor`].
726+
///
727+
/// [`Minor`]: Version::Minor
726728
fn is_minor(&self) -> bool {
727729
matches!(self, Self::Minor)
728730
}

0 commit comments

Comments
 (0)