From ac959b82b3408fafd22f4fbb59e10383a18c545f Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Fri, 12 Feb 2021 11:48:43 +0100 Subject: Add `find_impl_block_end` assist helper --- crates/assists/src/handlers/generate_getter.rs | 40 ++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) (limited to 'crates/assists/src/handlers/generate_getter.rs') diff --git a/crates/assists/src/handlers/generate_getter.rs b/crates/assists/src/handlers/generate_getter.rs index b63dfce41..fbcf8b069 100644 --- a/crates/assists/src/handlers/generate_getter.rs +++ b/crates/assists/src/handlers/generate_getter.rs @@ -3,7 +3,7 @@ use syntax::ast::VisibilityOwner; use syntax::ast::{self, AstNode, NameOwner}; use crate::{ - utils::{find_impl_block, find_struct_impl, generate_impl_text}, + utils::{find_impl_block_end, find_struct_impl, generate_impl_text}, AssistContext, AssistId, AssistKind, Assists, }; @@ -73,7 +73,7 @@ pub(crate) fn generate_getter(acc: &mut Assists, ctx: &AssistContext) -> Option< ); let start_offset = impl_def - .and_then(|impl_def| find_impl_block(impl_def, &mut buf)) + .and_then(|impl_def| find_impl_block_end(impl_def, &mut buf)) .unwrap_or_else(|| { buf = generate_impl_text(&ast::Adt::Struct(strukt.clone()), &buf); strukt.syntax().text_range().end() @@ -150,6 +150,42 @@ impl Context { pub(crate) fn data(&self) -> &T { &self.data } +}"#, + ); + } + + #[test] + fn test_multiple_generate_getter() { + check_assist( + generate_getter, + r#" +struct Context { + data: T, + cou$0nt: usize, +} + +impl Context { + /// Get a reference to the context's data. + fn data(&self) -> &T { + &self.data + } +}"#, + r#" +struct Context { + data: T, + count: usize, +} + +impl Context { + /// Get a reference to the context's data. + fn data(&self) -> &T { + &self.data + } + + /// Get a reference to the context's count. + fn count(&self) -> &usize { + &self.count + } }"#, ); } -- cgit v1.2.3