From a5935687cbc62e893753ec81a00655281b03feae Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 3 Jan 2019 15:08:32 +0300 Subject: split assists over several files --- crates/ra_editor/src/assists/add_impl.rs | 78 ++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 crates/ra_editor/src/assists/add_impl.rs (limited to 'crates/ra_editor/src/assists/add_impl.rs') diff --git a/crates/ra_editor/src/assists/add_impl.rs b/crates/ra_editor/src/assists/add_impl.rs new file mode 100644 index 000000000..50e00688e --- /dev/null +++ b/crates/ra_editor/src/assists/add_impl.rs @@ -0,0 +1,78 @@ +use join_to_string::join; +use ra_text_edit::TextEditBuilder; +use ra_syntax::{ + ast::{self, AstNode, NameOwner, TypeParamsOwner}, + SourceFileNode, + TextUnit, +}; + +use crate::{find_node_at_offset, assists::LocalEdit}; + +pub fn add_impl<'a>( + file: &'a SourceFileNode, + offset: TextUnit, +) -> Option LocalEdit + 'a> { + let nominal = find_node_at_offset::(file.syntax(), offset)?; + let name = nominal.name()?; + + Some(move || { + let type_params = nominal.type_param_list(); + let mut edit = TextEditBuilder::new(); + let start_offset = nominal.syntax().range().end(); + let mut buf = String::new(); + buf.push_str("\n\nimpl"); + if let Some(type_params) = type_params { + type_params.syntax().text().push_to(&mut buf); + } + buf.push_str(" "); + buf.push_str(name.text().as_str()); + if let Some(type_params) = type_params { + let lifetime_params = type_params + .lifetime_params() + .filter_map(|it| it.lifetime()) + .map(|it| it.text()); + let type_params = type_params + .type_params() + .filter_map(|it| it.name()) + .map(|it| it.text()); + join(lifetime_params.chain(type_params)) + .surround_with("<", ">") + .to_buf(&mut buf); + } + buf.push_str(" {\n"); + let offset = start_offset + TextUnit::of_str(&buf); + buf.push_str("\n}"); + edit.insert(start_offset, buf); + LocalEdit { + label: "add impl".to_string(), + edit: edit.finish(), + cursor_position: Some(offset), + } + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::test_utils::check_action; + + #[test] + fn test_add_impl() { + check_action( + "struct Foo {<|>}\n", + "struct Foo {}\n\nimpl Foo {\n<|>\n}\n", + |file, off| add_impl(file, off).map(|f| f()), + ); + check_action( + "struct Foo {<|>}", + "struct Foo {}\n\nimpl Foo {\n<|>\n}", + |file, off| add_impl(file, off).map(|f| f()), + ); + check_action( + "struct Foo<'a, T: Foo<'a>> {<|>}", + "struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n<|>\n}", + |file, off| add_impl(file, off).map(|f| f()), + ); + } + +} -- cgit v1.2.3