mirror of
				https://github.com/rust-lang/rust-analyzer.git
				synced 2025-10-31 12:04:43 +00:00 
			
		
		
		
	Merge pull request #19792 from A4-Tacks/fix-generate-mut-trait-impl-indent
fix: ide-assists, generate mut trait impl indent
This commit is contained in:
		
						commit
						f98b6220c4
					
				
					 1 changed files with 91 additions and 3 deletions
				
			
		|  | @ -1,7 +1,7 @@ | ||||||
| use ide_db::famous_defs::FamousDefs; | use ide_db::famous_defs::FamousDefs; | ||||||
| use syntax::{ | use syntax::{ | ||||||
|     AstNode, |     AstNode, | ||||||
|     ast::{self, make}, |     ast::{self, edit_in_place::Indent, make}, | ||||||
|     ted, |     ted, | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | @ -46,6 +46,7 @@ use crate::{AssistContext, AssistId, Assists}; | ||||||
| // ```
 | // ```
 | ||||||
| pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { | pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { | ||||||
|     let impl_def = ctx.find_node_at_offset::<ast::Impl>()?.clone_for_update(); |     let impl_def = ctx.find_node_at_offset::<ast::Impl>()?.clone_for_update(); | ||||||
|  |     let indent = impl_def.indent_level(); | ||||||
| 
 | 
 | ||||||
|     let trait_ = impl_def.trait_()?; |     let trait_ = impl_def.trait_()?; | ||||||
|     if let ast::Type::PathType(trait_path) = trait_ { |     if let ast::Type::PathType(trait_path) = trait_ { | ||||||
|  | @ -97,8 +98,8 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_> | ||||||
|     })?; |     })?; | ||||||
| 
 | 
 | ||||||
|     let assoc_list = make::assoc_item_list().clone_for_update(); |     let assoc_list = make::assoc_item_list().clone_for_update(); | ||||||
|     assoc_list.add_item(syntax::ast::AssocItem::Fn(fn_)); |  | ||||||
|     ted::replace(impl_def.assoc_item_list()?.syntax(), assoc_list.syntax()); |     ted::replace(impl_def.assoc_item_list()?.syntax(), assoc_list.syntax()); | ||||||
|  |     impl_def.get_or_create_assoc_item_list().add_item(syntax::ast::AssocItem::Fn(fn_)); | ||||||
| 
 | 
 | ||||||
|     let target = impl_def.syntax().text_range(); |     let target = impl_def.syntax().text_range(); | ||||||
|     acc.add( |     acc.add( | ||||||
|  | @ -106,7 +107,7 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_> | ||||||
|         "Generate `IndexMut` impl from this `Index` trait", |         "Generate `IndexMut` impl from this `Index` trait", | ||||||
|         target, |         target, | ||||||
|         |edit| { |         |edit| { | ||||||
|             edit.insert(target.start(), format!("$0{impl_def}\n\n")); |             edit.insert(target.start(), format!("$0{impl_def}\n\n{indent}")); | ||||||
|         }, |         }, | ||||||
|     ) |     ) | ||||||
| } | } | ||||||
|  | @ -189,6 +190,93 @@ impl<T> core::ops::Index<Axis> for [T; 3] where T: Copy { | ||||||
|         ); |         ); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     #[test] | ||||||
|  |     fn test_generate_mut_trait_impl_non_zero_indent() { | ||||||
|  |         check_assist( | ||||||
|  |             generate_mut_trait_impl, | ||||||
|  |             r#" | ||||||
|  | //- minicore: index
 | ||||||
|  | mod foo { | ||||||
|  |     pub enum Axis { X = 0, Y = 1, Z = 2 } | ||||||
|  | 
 | ||||||
|  |     impl<T> core::ops::Index$0<Axis> for [T; 3] where T: Copy { | ||||||
|  |         type Output = T; | ||||||
|  | 
 | ||||||
|  |         fn index(&self, index: Axis) -> &Self::Output { | ||||||
|  |             let var_name = &self[index as usize]; | ||||||
|  |             var_name | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | "#,
 | ||||||
|  |             r#" | ||||||
|  | mod foo { | ||||||
|  |     pub enum Axis { X = 0, Y = 1, Z = 2 } | ||||||
|  | 
 | ||||||
|  |     $0impl<T> core::ops::IndexMut<Axis> for [T; 3] where T: Copy { | ||||||
|  |         fn index_mut(&mut self, index: Axis) -> &mut Self::Output { | ||||||
|  |             let var_name = &self[index as usize]; | ||||||
|  |             var_name | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     impl<T> core::ops::Index<Axis> for [T; 3] where T: Copy { | ||||||
|  |         type Output = T; | ||||||
|  | 
 | ||||||
|  |         fn index(&self, index: Axis) -> &Self::Output { | ||||||
|  |             let var_name = &self[index as usize]; | ||||||
|  |             var_name | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | "#,
 | ||||||
|  |         ); | ||||||
|  | 
 | ||||||
|  |         check_assist( | ||||||
|  |             generate_mut_trait_impl, | ||||||
|  |             r#" | ||||||
|  | //- minicore: index
 | ||||||
|  | mod foo { | ||||||
|  |     mod bar { | ||||||
|  |         pub enum Axis { X = 0, Y = 1, Z = 2 } | ||||||
|  | 
 | ||||||
|  |         impl<T> core::ops::Index$0<Axis> for [T; 3] where T: Copy { | ||||||
|  |             type Output = T; | ||||||
|  | 
 | ||||||
|  |             fn index(&self, index: Axis) -> &Self::Output { | ||||||
|  |                 let var_name = &self[index as usize]; | ||||||
|  |                 var_name | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | "#,
 | ||||||
|  |             r#" | ||||||
|  | mod foo { | ||||||
|  |     mod bar { | ||||||
|  |         pub enum Axis { X = 0, Y = 1, Z = 2 } | ||||||
|  | 
 | ||||||
|  |         $0impl<T> core::ops::IndexMut<Axis> for [T; 3] where T: Copy { | ||||||
|  |             fn index_mut(&mut self, index: Axis) -> &mut Self::Output { | ||||||
|  |                 let var_name = &self[index as usize]; | ||||||
|  |                 var_name | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         impl<T> core::ops::Index<Axis> for [T; 3] where T: Copy { | ||||||
|  |             type Output = T; | ||||||
|  | 
 | ||||||
|  |             fn index(&self, index: Axis) -> &Self::Output { | ||||||
|  |                 let var_name = &self[index as usize]; | ||||||
|  |                 var_name | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | "#,
 | ||||||
|  |         ); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     #[test] |     #[test] | ||||||
|     fn test_generate_mut_trait_impl_not_applicable() { |     fn test_generate_mut_trait_impl_not_applicable() { | ||||||
|         check_assist_not_applicable( |         check_assist_not_applicable( | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Lukas Wirth
						Lukas Wirth