about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorTarek <tareknaser360@gmail.com>2024-11-12 19:18:26 +0200
committerTarek <tareknaser360@gmail.com>2024-12-04 14:50:03 +0200
commit54b597ccf24a67f8f2e82be50b7f47ae737aaa00 (patch)
treef3cda1410b8d221b216ecc634d3934d201f42c46 /src
parent797eb3ebe8b2fcd41f4b03ae7f8aa72f7a47e1a8 (diff)
downloadrust-54b597ccf24a67f8f2e82be50b7f47ae737aaa00.tar.gz
rust-54b597ccf24a67f8f2e82be50b7f47ae737aaa00.zip
fix: implement `syntax_editor_create_generic_param_list`
Signed-off-by: Tarek <tareknaser360@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/tools/rust-analyzer/crates/ide-assists/src/handlers/introduce_named_generic.rs8
-rw-r--r--src/tools/rust-analyzer/crates/syntax/src/ast/edit_in_place.rs32
2 files changed, 35 insertions, 5 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/introduce_named_generic.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/introduce_named_generic.rs
index ae30dacfd01..c6945d6245e 100644
--- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/introduce_named_generic.rs
+++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/introduce_named_generic.rs
@@ -1,10 +1,7 @@
 use ide_db::syntax_helpers::suggest_name;
 use itertools::Itertools;
 use syntax::{
-    ast::{
-        self, edit_in_place::GenericParamsOwnerEdit, syntax_factory::SyntaxFactory, AstNode,
-        HasGenericParams, HasName,
-    },
+    ast::{self, syntax_factory::SyntaxFactory, AstNode, HasGenericParams, HasName},
     SyntaxElement,
 };
 
@@ -42,7 +39,8 @@ pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext<'_>
         target,
         |edit| {
             let mut editor = edit.make_editor(&parent_node);
-            let fn_generic_param_list = fn_.get_or_create_generic_param_list();
+            let fn_generic_param_list =
+                fn_.syntax_editor_get_or_create_generic_param_list(&mut editor);
 
             let existing_names = fn_generic_param_list
                 .generic_params()
diff --git a/src/tools/rust-analyzer/crates/syntax/src/ast/edit_in_place.rs b/src/tools/rust-analyzer/crates/syntax/src/ast/edit_in_place.rs
index 2b152a45c9a..61580a5cae6 100644
--- a/src/tools/rust-analyzer/crates/syntax/src/ast/edit_in_place.rs
+++ b/src/tools/rust-analyzer/crates/syntax/src/ast/edit_in_place.rs
@@ -55,6 +55,29 @@ impl GenericParamsOwnerEdit for ast::Fn {
     }
 }
 
+impl ast::Fn {
+    pub fn syntax_editor_get_or_create_generic_param_list(
+        &self,
+        editor: &mut SyntaxEditor,
+    ) -> ast::GenericParamList {
+        match self.generic_param_list() {
+            Some(it) => it,
+            None => {
+                let position = if let Some(name) = self.name() {
+                    crate::syntax_editor::Position::after(name.syntax)
+                } else if let Some(fn_token) = self.fn_token() {
+                    crate::syntax_editor::Position::after(fn_token)
+                } else if let Some(param_list) = self.param_list() {
+                    crate::syntax_editor::Position::before(param_list.syntax)
+                } else {
+                    crate::syntax_editor::Position::last_child_of(self.syntax())
+                };
+                syntax_editor_create_generic_param_list(editor, position)
+            }
+        }
+    }
+}
+
 impl GenericParamsOwnerEdit for ast::Impl {
     fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
         match self.generic_param_list() {
@@ -191,6 +214,15 @@ fn create_generic_param_list(position: Position) -> ast::GenericParamList {
     gpl
 }
 
+fn syntax_editor_create_generic_param_list(
+    editor: &mut SyntaxEditor,
+    position: crate::syntax_editor::Position,
+) -> ast::GenericParamList {
+    let gpl = make::generic_param_list(empty()).clone_for_update();
+    editor.insert(position, gpl.syntax());
+    gpl
+}
+
 pub trait AttrsOwnerEdit: ast::HasAttrs {
     fn remove_attrs_and_docs(&self) {
         remove_attrs_and_docs(self.syntax());