|
1 |
| -use hir::{HasVisibility, ModuleDef, Visibility}; |
| 1 | +use hir::{AsAssocItem, HasVisibility, ModuleDef, Visibility}; |
2 | 2 | use ide_db::assists::{AssistId, AssistKind};
|
3 | 3 | use stdx::to_lower_snake_case;
|
4 | 4 | use syntax::{
|
@@ -42,7 +42,7 @@ pub(crate) fn generate_documentation_template(
|
42 | 42 | ) -> Option<()> {
|
43 | 43 | let name = ctx.find_node_at_offset::<ast::Name>()?;
|
44 | 44 | 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) |
46 | 46 | || !is_public(&ast_func, ctx)?
|
47 | 47 | || ast_func.doc_comments().next().is_some()
|
48 | 48 | {
|
@@ -94,7 +94,7 @@ fn introduction_builder(ast_func: &ast::Fn) -> String {
|
94 | 94 |
|
95 | 95 | /// Builds an `# Examples` section. An option is returned to be able to manage an error in the AST.
|
96 | 96 | 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) { |
98 | 98 | let message = "// Example template not implemented for trait functions";
|
99 | 99 | (Some(vec![message.into()]), Some(vec![message.into()]))
|
100 | 100 | } else {
|
@@ -275,18 +275,21 @@ fn self_partial_type(ast_func: &ast::Fn) -> Option<String> {
|
275 | 275 | }
|
276 | 276 |
|
277 | 277 | /// 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())) |
284 | 283 | .is_some()
|
285 | 284 | }
|
286 | 285 |
|
287 | 286 | /// 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() |
290 | 293 | }
|
291 | 294 |
|
292 | 295 | /// Returns `None` if no `self` at all, `Some(true)` if there is `&mut self` else `Some(false)`
|
|
0 commit comments