Skip to content

Commit 855b00c

Browse files
bors[bot]yoshuawuytsVeykril
authored
Merge #7570 #7571
7570: Add doc gen to the `generate_enum_match_method` assist r=yoshuawuyts a=yoshuawuyts Implements a small extension to #7562, generating default comments. I wasn't sure if this would fit the goals of Rust-Analyzer, so I chose to split it into a separate PR. This is especially useful when writing code in a codebase which uses `#![warn(missing_docs)]` lint, as many production-grade libraries do. The comments we're generating here are similar to the ones found on [`Option::is_some`](https://doc.rust-lang.org/std/option/enum.Option.html#method.is_some) and [`Result::is_err`](https://doc.rust-lang.org/std/result/enum.Result.html#method.is_err). I briefly considered only generating these for `pub` types, but they seem small and unobtrusive enough that they're probably useful in the general case. Thanks! ## Example __input__ ```rust pub(crate) enum Variant { Undefined, Minor, // cursor here Major, } ``` __output__ ```rust pub(crate) enum Variant { Undefined, Minor, Major, } impl Variant { /// Returns `true` if the variant is [`Minor`]. pub(crate) fn is_minor(&self) -> bool { matches!(self, Self::Minor) } } ``` ## Future Directions This opens up the path to adding an assist for generating these comments on existing `is_` methods. This would make it both easy to document new code, and update existing code with documentation. 7571: Cleanup decl_check r=Veykril a=Veykril bors r+ Co-authored-by: Yoshua Wuyts <[email protected]> Co-authored-by: Lukas Wirth <[email protected]>
3 parents ae7bee7 + d90bd63 + eeb5bfc commit 855b00c

File tree

6 files changed

+164
-165
lines changed

6 files changed

+164
-165
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/assists/src/handlers/generate_enum_match_method.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use crate::{utils::find_struct_impl, AssistContext, AssistId, AssistKind, Assist
2525
// }
2626
//
2727
// impl Version {
28+
// /// Returns `true` if the version is [`Minor`].
2829
// fn is_minor(&self) -> bool {
2930
// matches!(self, Self::Minor)
3031
// }
@@ -39,6 +40,7 @@ pub(crate) fn generate_enum_match_method(acc: &mut Assists, ctx: &AssistContext)
3940
return None;
4041
}
4142

43+
let enum_lowercase_name = to_lower_snake_case(&parent_enum.name()?.to_string());
4244
let fn_name = to_lower_snake_case(&variant_name.to_string());
4345

4446
// Return early if we've found an existing new fn
@@ -64,9 +66,12 @@ pub(crate) fn generate_enum_match_method(acc: &mut Assists, ctx: &AssistContext)
6466

6567
format_to!(
6668
buf,
67-
" {}fn is_{}(&self) -> bool {{
69+
" /// Returns `true` if the {} is [`{}`].
70+
{}fn is_{}(&self) -> bool {{
6871
matches!(self, Self::{})
6972
}}",
73+
enum_lowercase_name,
74+
variant_name,
7075
vis,
7176
fn_name,
7277
variant_name
@@ -133,6 +138,7 @@ enum Variant {
133138
}
134139
135140
impl Variant {
141+
/// Returns `true` if the variant is [`Minor`].
136142
fn is_minor(&self) -> bool {
137143
matches!(self, Self::Minor)
138144
}
@@ -180,6 +186,7 @@ enum Variant {
180186
enum Variant { Undefined }
181187
182188
impl Variant {
189+
/// Returns `true` if the variant is [`Undefined`].
183190
fn is_undefined(&self) -> bool {
184191
matches!(self, Self::Undefined)
185192
}
@@ -204,6 +211,7 @@ pub(crate) enum Variant {
204211
}
205212
206213
impl Variant {
214+
/// Returns `true` if the variant is [`Minor`].
207215
pub(crate) fn is_minor(&self) -> bool {
208216
matches!(self, Self::Minor)
209217
}

crates/assists/src/tests/generated.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ enum Version {
478478
}
479479
480480
impl Version {
481+
/// Returns `true` if the version is [`Minor`].
481482
fn is_minor(&self) -> bool {
482483
matches!(self, Self::Minor)
483484
}

crates/hir_ty/src/diagnostics.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,37 @@ impl fmt::Display for CaseType {
345345
}
346346
}
347347

348+
#[derive(Debug)]
349+
pub enum IdentType {
350+
Argument,
351+
Constant,
352+
Enum,
353+
Field,
354+
Function,
355+
StaticVariable,
356+
Structure,
357+
Variable,
358+
Variant,
359+
}
360+
361+
impl fmt::Display for IdentType {
362+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
363+
let repr = match self {
364+
IdentType::Argument => "Argument",
365+
IdentType::Constant => "Constant",
366+
IdentType::Enum => "Enum",
367+
IdentType::Field => "Field",
368+
IdentType::Function => "Function",
369+
IdentType::StaticVariable => "Static variable",
370+
IdentType::Structure => "Structure",
371+
IdentType::Variable => "Variable",
372+
IdentType::Variant => "Variant",
373+
};
374+
375+
write!(f, "{}", repr)
376+
}
377+
}
378+
348379
// Diagnostic: incorrect-ident-case
349380
//
350381
// This diagnostic is triggered if an item name doesn't follow https://doc.rust-lang.org/1.0.0/style/style/naming/README.html[Rust naming convention].
@@ -353,7 +384,7 @@ pub struct IncorrectCase {
353384
pub file: HirFileId,
354385
pub ident: AstPtr<ast::Name>,
355386
pub expected_case: CaseType,
356-
pub ident_type: String,
387+
pub ident_type: IdentType,
357388
pub ident_text: String,
358389
pub suggested_text: String,
359390
}

0 commit comments

Comments
 (0)