Skip to content

Remove doc comments for generate_trait_from_impl #20407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions crates/ide-assists/src/handlers/generate_trait_from_impl.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::assist_context::{AssistContext, Assists};
use ide_db::assists::AssistId;
use syntax::{
AstNode, SyntaxKind, T,
ast::{self, HasGenericParams, HasName, HasVisibility, edit_in_place::Indent, make},
AstNode, AstToken, SyntaxKind, T,
ast::{
self, HasDocComments, HasGenericParams, HasName, HasVisibility, edit_in_place::Indent, make,
},
syntax_editor::{Position, SyntaxEditor},
};

Expand Down Expand Up @@ -133,6 +135,7 @@ pub(crate) fn generate_trait_from_impl(acc: &mut Assists, ctx: &AssistContext<'_
let mut editor = builder.make_editor(impl_ast.syntax());
impl_assoc_items.assoc_items().for_each(|item| {
remove_items_visibility(&mut editor, &item);
remove_doc_comments(&mut editor, &item);
});

editor.insert_all(Position::before(impl_name.syntax()), elements);
Expand Down Expand Up @@ -175,6 +178,17 @@ fn remove_items_visibility(editor: &mut SyntaxEditor, item: &ast::AssocItem) {
}
}

fn remove_doc_comments(editor: &mut SyntaxEditor, item: &ast::AssocItem) {
for doc in item.doc_comments() {
if let Some(next) = doc.syntax().next_token()
&& next.kind() == SyntaxKind::WHITESPACE
{
editor.delete(next);
}
editor.delete(doc.syntax());
}
}

fn strip_body(editor: &mut SyntaxEditor, item: &ast::AssocItem) {
if let ast::AssocItem::Fn(f) = item
&& let Some(body) = f.body()
Expand Down Expand Up @@ -238,6 +252,42 @@ impl NewTrait for Foo {
)
}

#[test]
fn test_remove_doc_comments() {
check_assist_no_snippet_cap(
generate_trait_from_impl,
r#"
struct Foo(f64);

impl F$0oo {
/// Add `x`
///
/// # Examples
#[cfg(true)]
fn add(&mut self, x: f64) {
self.0 += x;
}
}"#,
r#"
struct Foo(f64);

trait NewTrait {
/// Add `x`
///
/// # Examples
#[cfg(true)]
fn add(&mut self, x: f64);
}

impl NewTrait for Foo {
#[cfg(true)]
fn add(&mut self, x: f64) {
self.0 += x;
}
}"#,
)
}

#[test]
fn test_assoc_item_macro() {
check_assist_no_snippet_cap(
Expand Down
Loading