diff options
Diffstat (limited to 'crates/assists/src/handlers')
-rw-r--r-- | crates/assists/src/handlers/generate_enum_match_method.rs | 20 | ||||
-rw-r--r-- | crates/assists/src/handlers/generate_new.rs | 21 |
2 files changed, 13 insertions, 28 deletions
diff --git a/crates/assists/src/handlers/generate_enum_match_method.rs b/crates/assists/src/handlers/generate_enum_match_method.rs index 270b438b7..2345a61c1 100644 --- a/crates/assists/src/handlers/generate_enum_match_method.rs +++ b/crates/assists/src/handlers/generate_enum_match_method.rs | |||
@@ -1,9 +1,12 @@ | |||
1 | use stdx::{format_to, to_lower_snake_case}; | 1 | use stdx::{format_to, to_lower_snake_case}; |
2 | use syntax::ast::VisibilityOwner; | ||
2 | use syntax::ast::{self, AstNode, NameOwner}; | 3 | use syntax::ast::{self, AstNode, NameOwner}; |
3 | use syntax::{ast::VisibilityOwner, T}; | ||
4 | use test_utils::mark; | 4 | use test_utils::mark; |
5 | 5 | ||
6 | use crate::{utils::find_struct_impl, AssistContext, AssistId, AssistKind, Assists}; | 6 | use crate::{ |
7 | utils::{find_impl_block, find_struct_impl}, | ||
8 | AssistContext, AssistId, AssistKind, Assists, | ||
9 | }; | ||
7 | 10 | ||
8 | // Assist: generate_enum_match_method | 11 | // Assist: generate_enum_match_method |
9 | // | 12 | // |
@@ -61,7 +64,6 @@ pub(crate) fn generate_enum_match_method(acc: &mut Assists, ctx: &AssistContext) | |||
61 | } | 64 | } |
62 | 65 | ||
63 | let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v)); | 66 | let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v)); |
64 | |||
65 | format_to!( | 67 | format_to!( |
66 | buf, | 68 | buf, |
67 | " {}fn is_{}(&self) -> bool {{ | 69 | " {}fn is_{}(&self) -> bool {{ |
@@ -73,17 +75,7 @@ pub(crate) fn generate_enum_match_method(acc: &mut Assists, ctx: &AssistContext) | |||
73 | ); | 75 | ); |
74 | 76 | ||
75 | let start_offset = impl_def | 77 | let start_offset = impl_def |
76 | .and_then(|impl_def| { | 78 | .and_then(|impl_def| find_impl_block(impl_def, &mut buf)) |
77 | buf.push('\n'); | ||
78 | let start = impl_def | ||
79 | .syntax() | ||
80 | .descendants_with_tokens() | ||
81 | .find(|t| t.kind() == T!['{'])? | ||
82 | .text_range() | ||
83 | .end(); | ||
84 | |||
85 | Some(start) | ||
86 | }) | ||
87 | .unwrap_or_else(|| { | 79 | .unwrap_or_else(|| { |
88 | buf = generate_impl_text(&parent_enum, &buf); | 80 | buf = generate_impl_text(&parent_enum, &buf); |
89 | parent_enum.syntax().text_range().end() | 81 | parent_enum.syntax().text_range().end() |
diff --git a/crates/assists/src/handlers/generate_new.rs b/crates/assists/src/handlers/generate_new.rs index 84832273f..307f2e228 100644 --- a/crates/assists/src/handlers/generate_new.rs +++ b/crates/assists/src/handlers/generate_new.rs | |||
@@ -2,10 +2,13 @@ use itertools::Itertools; | |||
2 | use stdx::format_to; | 2 | use stdx::format_to; |
3 | use syntax::{ | 3 | use syntax::{ |
4 | ast::{self, AstNode, GenericParamsOwner, NameOwner, StructKind, VisibilityOwner}, | 4 | ast::{self, AstNode, GenericParamsOwner, NameOwner, StructKind, VisibilityOwner}, |
5 | SmolStr, T, | 5 | SmolStr, |
6 | }; | 6 | }; |
7 | 7 | ||
8 | use crate::{utils::find_struct_impl, AssistContext, AssistId, AssistKind, Assists}; | 8 | use crate::{ |
9 | utils::{find_impl_block, find_struct_impl}, | ||
10 | AssistContext, AssistId, AssistKind, Assists, | ||
11 | }; | ||
9 | 12 | ||
10 | // Assist: generate_new | 13 | // Assist: generate_new |
11 | // | 14 | // |
@@ -58,17 +61,7 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()> | |||
58 | format_to!(buf, " {}fn new({}) -> Self {{ Self {{ {} }} }}", vis, params, fields); | 61 | format_to!(buf, " {}fn new({}) -> Self {{ Self {{ {} }} }}", vis, params, fields); |
59 | 62 | ||
60 | let start_offset = impl_def | 63 | let start_offset = impl_def |
61 | .and_then(|impl_def| { | 64 | .and_then(|impl_def| find_impl_block(impl_def, &mut buf)) |
62 | buf.push('\n'); | ||
63 | let start = impl_def | ||
64 | .syntax() | ||
65 | .descendants_with_tokens() | ||
66 | .find(|t| t.kind() == T!['{'])? | ||
67 | .text_range() | ||
68 | .end(); | ||
69 | |||
70 | Some(start) | ||
71 | }) | ||
72 | .unwrap_or_else(|| { | 65 | .unwrap_or_else(|| { |
73 | buf = generate_impl_text(&strukt, &buf); | 66 | buf = generate_impl_text(&strukt, &buf); |
74 | strukt.syntax().text_range().end() | 67 | strukt.syntax().text_range().end() |
@@ -93,7 +86,7 @@ fn generate_impl_text(strukt: &ast::Struct, code: &str) -> String { | |||
93 | if let Some(type_params) = &type_params { | 86 | if let Some(type_params) = &type_params { |
94 | format_to!(buf, "{}", type_params.syntax()); | 87 | format_to!(buf, "{}", type_params.syntax()); |
95 | } | 88 | } |
96 | buf.push_str(" "); | 89 | buf.push(' '); |
97 | buf.push_str(strukt.name().unwrap().text()); | 90 | buf.push_str(strukt.name().unwrap().text()); |
98 | if let Some(type_params) = type_params { | 91 | if let Some(type_params) = type_params { |
99 | let lifetime_params = type_params | 92 | let lifetime_params = type_params |