aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src/utils.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/assists/src/utils.rs')
-rw-r--r--crates/assists/src/utils.rs31
1 files changed, 29 insertions, 2 deletions
diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs
index cd80c2958..643dade23 100644
--- a/crates/assists/src/utils.rs
+++ b/crates/assists/src/utils.rs
@@ -5,12 +5,13 @@ use std::ops;
5use hir::{Adt, HasSource}; 5use hir::{Adt, HasSource};
6use ide_db::{helpers::SnippetCap, RootDatabase}; 6use ide_db::{helpers::SnippetCap, RootDatabase};
7use itertools::Itertools; 7use itertools::Itertools;
8use stdx::format_to;
8use syntax::{ 9use syntax::{
9 ast::edit::AstNodeEdit, 10 ast::edit::AstNodeEdit,
10 ast::AttrsOwner, 11 ast::AttrsOwner,
11 ast::NameOwner, 12 ast::NameOwner,
12 ast::{self, edit, make, ArgListOwner}, 13 ast::{self, edit, make, ArgListOwner, GenericParamsOwner},
13 AstNode, Direction, 14 AstNode, Direction, SmolStr,
14 SyntaxKind::*, 15 SyntaxKind::*,
15 SyntaxNode, TextSize, T, 16 SyntaxNode, TextSize, T,
16}; 17};
@@ -354,3 +355,29 @@ pub(crate) fn find_impl_block(impl_def: ast::Impl, buf: &mut String) -> Option<T
354 .end(); 355 .end();
355 Some(start) 356 Some(start)
356} 357}
358
359// Generates the surrounding `impl Type { <code> }` including type and lifetime
360// parameters
361pub(crate) fn generate_impl_text(adt: &ast::Adt, code: &str) -> String {
362 let type_params = adt.generic_param_list();
363 let mut buf = String::with_capacity(code.len());
364 buf.push_str("\n\nimpl");
365 if let Some(type_params) = &type_params {
366 format_to!(buf, "{}", type_params.syntax());
367 }
368 buf.push(' ');
369 buf.push_str(adt.name().unwrap().text());
370 if let Some(type_params) = type_params {
371 let lifetime_params = type_params
372 .lifetime_params()
373 .filter_map(|it| it.lifetime())
374 .map(|it| SmolStr::from(it.text()));
375 let type_params =
376 type_params.type_params().filter_map(|it| it.name()).map(|it| SmolStr::from(it.text()));
377 format_to!(buf, "<{}>", lifetime_params.chain(type_params).format(", "))
378 }
379
380 format_to!(buf, " {{\n{}\n}}", code);
381
382 buf
383}