Skip to content

Commit f5e0998

Browse files
author
Côme ALLART
committed
refactor: use hir to check if fn in trait/impl
1 parent c3d151a commit f5e0998

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

crates/ide_assists/src/handlers/generate_documentation_template.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use hir::{HasVisibility, ModuleDef, Visibility};
1+
use hir::{AsAssocItem, HasVisibility, ModuleDef, Visibility};
22
use ide_db::assists::{AssistId, AssistKind};
33
use stdx::to_lower_snake_case;
44
use syntax::{
@@ -42,7 +42,7 @@ pub(crate) fn generate_documentation_template(
4242
) -> Option<()> {
4343
let name = ctx.find_node_at_offset::<ast::Name>()?;
4444
let ast_func = name.syntax().parent().and_then(ast::Fn::cast)?;
45-
if is_in_trait_impl(&ast_func)
45+
if is_in_trait_impl(&ast_func, ctx)
4646
|| !is_public(&ast_func, ctx)?
4747
|| ast_func.doc_comments().next().is_some()
4848
{
@@ -94,7 +94,7 @@ fn introduction_builder(ast_func: &ast::Fn) -> String {
9494

9595
/// Builds an `# Examples` section. An option is returned to be able to manage an error in the AST.
9696
fn examples_builder(ast_func: &ast::Fn, ctx: &AssistContext) -> Option<Vec<String>> {
97-
let (no_panic_ex, panic_ex) = if is_in_trait_def(ast_func) {
97+
let (no_panic_ex, panic_ex) = if is_in_trait_def(ast_func, ctx) {
9898
let message = "// Example template not implemented for trait functions";
9999
(Some(vec![message.into()]), Some(vec![message.into()]))
100100
} else {
@@ -275,18 +275,21 @@ fn self_partial_type(ast_func: &ast::Fn) -> Option<String> {
275275
}
276276

277277
/// Helper function to determine if the function is in a trait implementation
278-
fn is_in_trait_impl(ast_func: &ast::Fn) -> bool {
279-
ast_func
280-
.syntax()
281-
.ancestors()
282-
.find_map(ast::Impl::cast)
283-
.and_then(|impl_| impl_.trait_())
278+
fn is_in_trait_impl(ast_func: &ast::Fn, ctx: &AssistContext) -> bool {
279+
ctx.sema
280+
.to_def(ast_func)
281+
.and_then(|hir_func| hir_func.as_assoc_item(ctx.db()))
282+
.and_then(|assoc_item| assoc_item.containing_trait_impl(ctx.db()))
284283
.is_some()
285284
}
286285

287286
/// Helper function to determine if the function definition is in a trait definition
288-
fn is_in_trait_def(ast_func: &ast::Fn) -> bool {
289-
ast_func.syntax().ancestors().find_map(ast::Trait::cast).is_some()
287+
fn is_in_trait_def(ast_func: &ast::Fn, ctx: &AssistContext) -> bool {
288+
ctx.sema
289+
.to_def(ast_func)
290+
.and_then(|hir_func| hir_func.as_assoc_item(ctx.db()))
291+
.and_then(|assoc_item| assoc_item.containing_trait(ctx.db()))
292+
.is_some()
290293
}
291294

292295
/// Returns `None` if no `self` at all, `Some(true)` if there is `&mut self` else `Some(false)`

0 commit comments

Comments
 (0)