Skip to content

Add structured editing API to insert snipperts #11638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
matklad opened this issue Mar 6, 2022 · 1 comment · Fixed by #14442
Closed

Add structured editing API to insert snipperts #11638

matklad opened this issue Mar 6, 2022 · 1 comment · Fixed by #14442
Labels
A-assists S-actionable Someone could pick this issue up and work on it right now

Comments

@matklad
Copy link
Member

matklad commented Mar 6, 2022

Some of our assists don't place cursor correctly:

Peek.2022-03-06.17-00.mp4

Here, I'd really want the cursor to end up on A, such that I can rename it. I've looked into quickly fixing this, and it seems we simply don't have an API for that!

What I'd love the API to look like is either this:

diff --git a/crates/ide_assists/src/handlers/introduce_named_generic.rs b/crates/ide_assists/src/handlers/introduce_named_generic.rs
index 636b05dca..8eb9a462e 100644
--- a/crates/ide_assists/src/handlers/introduce_named_generic.rs
+++ b/crates/ide_assists/src/handlers/introduce_named_generic.rs
@@ -1,5 +1,5 @@
 use syntax::{
-    ast::{self, edit_in_place::GenericParamsOwnerEdit, make, AstNode},
+    ast::{self, edit_in_place::GenericParamsOwnerEdit, make, AstNode, HasGenericParams},
     ted,
 };
 
@@ -39,7 +39,10 @@ pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext) ->
             let new_ty = make::ty(&type_param_name).clone_for_update();
 
             ted::replace(impl_trait_type.syntax(), new_ty.syntax());
-            fn_.get_or_create_generic_param_list().add_generic_param(type_param.into())
+            fn_.get_or_create_generic_param_list().add_generic_param(type_param.into());
+            if let Some(generic_param) = fn_.generic_param_list().and_then(|it| it.generic_params().last()) {
+                edit.move_cursor_to(generic_param)
+            }
         },
     )
 }

or this

diff --git a/crates/ide_assists/src/handlers/introduce_named_generic.rs b/crates/ide_assists/src/handlers/introduce_named_generic.rs
index 636b05dca..f361148cb 100644
--- a/crates/ide_assists/src/handlers/introduce_named_generic.rs
+++ b/crates/ide_assists/src/handlers/introduce_named_generic.rs
@@ -34,7 +34,9 @@ pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext) ->
 
             let type_param_name = suggest_name::for_generic_parameter(&impl_trait_type);
 
-            let type_param = make::type_param(make::name(&type_param_name), Some(type_bound_list))
+            let name: ast::Name = make::name(&type_param_name); // A
+            let name: ast::Name = add_snippert(name); // /*$0*/A
+            let type_param = make::type_param(name, Some(type_bound_list))
                 .clone_for_update();
             let new_ty = make::ty(&type_param_name).clone_for_update();
 

After we have this API, we should

  • check that it is used by all "introduce/generate" assits
  • audit the usages of string-based render_snippet API and see if we can replace that with structured editing.
@matklad matklad added S-actionable Someone could pick this issue up and work on it right now A-assists labels Mar 6, 2022
@matklad
Copy link
Member Author

matklad commented Mar 6, 2022

cc @Veykril this is an API design exercise, you might be interested in that!

@bors bors closed this as completed in da9c0bd Apr 5, 2023
bors added a commit that referenced this issue Feb 8, 2024
…ykril

internal: Migrate assists to the structured snippet API, part 6/7

Continuing from #16082

Migrates the following assists:

- `extract_function`
- `generate_getter_or_setter`
- `generate_impl`
- `generate_new`
- `replace_derive_with_manual_impl`

Would've been the final PR in the structured snippet migration series, but I didn't notice that `generate_trait_from_impl` started to use `{insert,replace}_snippet` when I first started the migration 😅. This appears to be a pretty self-contained change, so I'll leave that for a separate future PR.

This also removes the last usages of `render_snippet`, which was a follow up goal of #11638. 🎉
bors added a commit that referenced this issue Feb 9, 2024
…opDemBits

internal: Migrate assists to the structured snippet API, part 6/7

Continuing from #16082

Migrates the following assists:

- `extract_function`
- `generate_getter_or_setter`
- `generate_impl`
- `generate_new`
- `replace_derive_with_manual_impl`

Would've been the final PR in the structured snippet migration series, but I didn't notice that `generate_trait_from_impl` started to use `{insert,replace}_snippet` when I first started the migration 😅. This appears to be a pretty self-contained change, so I'll leave that for a separate future PR.

This also removes the last usages of `render_snippet`, which was a follow up goal of #11638. 🎉
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-assists S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant