From 8696c82777f9992fceadc531536bf90b64cf753d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 23 May 2021 23:22:38 +0300 Subject: [PATCH] feat: generate getter assist places the cursor at the generated function --- .../src/handlers/generate_getter.rs | 19 ++++++++++++------- crates/ide_assists/src/tests/generated.rs | 4 ++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/crates/ide_assists/src/handlers/generate_getter.rs b/crates/ide_assists/src/handlers/generate_getter.rs index c7739582446..9faaaf284b0 100644 --- a/crates/ide_assists/src/handlers/generate_getter.rs +++ b/crates/ide_assists/src/handlers/generate_getter.rs @@ -23,7 +23,7 @@ // // impl Person { // /// Get a reference to the person's name. -// fn name(&self) -> &String { +// fn $0name(&self) -> &String { // &self.name // } // } @@ -49,7 +49,7 @@ pub(crate) fn generate_getter(acc: &mut Assists, ctx: &AssistContext) -> Option< // // impl Person { // /// Get a mutable reference to the person's name. -// fn name_mut(&mut self) -> &mut String { +// fn $0name_mut(&mut self) -> &mut String { // &mut self.name // } // } @@ -119,7 +119,12 @@ pub(crate) fn generate_getter_impl( strukt.syntax().text_range().end() }); - builder.insert(start_offset, buf); + match ctx.config.snippet_cap { + Some(cap) => { + builder.insert_snippet(cap, start_offset, buf.replacen("fn ", "fn $0", 1)) + } + None => builder.insert(start_offset, buf), + } }, ) } @@ -146,7 +151,7 @@ struct Context { impl Context { /// Get a reference to the context's data. - fn data(&self) -> &Data { + fn $0data(&self) -> &Data { &self.data } } @@ -167,7 +172,7 @@ struct Context { impl Context { /// Get a mutable reference to the context's data. - fn data_mut(&mut self) -> &mut Data { + fn $0data_mut(&mut self) -> &mut Data { &mut self.data } } @@ -224,7 +229,7 @@ pub(crate) struct Context { impl Context { /// Get a reference to the context's data. - pub(crate) fn data(&self) -> &Data { + pub(crate) fn $0data(&self) -> &Data { &self.data } } @@ -262,7 +267,7 @@ fn data(&self) -> &Data { } /// Get a reference to the context's count. - fn count(&self) -> &usize { + fn $0count(&self) -> &usize { &self.count } } diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs index 4406406a278..ffc915fd4d9 100644 --- a/crates/ide_assists/src/tests/generated.rs +++ b/crates/ide_assists/src/tests/generated.rs @@ -786,7 +786,7 @@ struct Person { impl Person { /// Get a reference to the person's name. - fn name(&self) -> &String { + fn $0name(&self) -> &String { &self.name } } @@ -810,7 +810,7 @@ struct Person { impl Person { /// Get a mutable reference to the person's name. - fn name_mut(&mut self) -> &mut String { + fn $0name_mut(&mut self) -> &mut String { &mut self.name } } -- 2.44.0