From b494e479202ae3da2745ef4ff05db9da0c8427a0 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sat, 27 Mar 2021 18:51:06 +0100 Subject: Snippet support in extract_type_alias --- .../ide_assists/src/handlers/extract_type_alias.rs | 37 ++++++++++++++-------- crates/ide_assists/src/tests/generated.rs | 4 +-- 2 files changed, 26 insertions(+), 15 deletions(-) (limited to 'crates/ide_assists/src') diff --git a/crates/ide_assists/src/handlers/extract_type_alias.rs b/crates/ide_assists/src/handlers/extract_type_alias.rs index 771868234..171b87918 100644 --- a/crates/ide_assists/src/handlers/extract_type_alias.rs +++ b/crates/ide_assists/src/handlers/extract_type_alias.rs @@ -16,10 +16,10 @@ use crate::{AssistContext, AssistId, AssistKind, Assists}; // ``` // -> // ``` -// type Type = (u8, u8, u8); +// type ${0:Type} = (u8, u8, u8); // // struct S { -// field: Type, +// field: ${0:Type}, // } // ``` pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { @@ -56,9 +56,20 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Opti target, |builder| { builder.edit_file(ctx.frange.file_id); - // FIXME: add snippet support - builder.replace(target, "Type"); - builder.insert(insert, format!("type Type = {};\n\n", node)); + match ctx.config.snippet_cap { + Some(cap) => { + builder.replace_snippet(cap, target, "${0:Type}"); + builder.insert_snippet( + cap, + insert, + format!("type ${{0:Type}} = {};\n\n", node), + ); + } + None => { + builder.replace(target, "Type"); + builder.insert(insert, format!("type Type = {};\n\n", node)); + } + } }, ) } @@ -91,10 +102,10 @@ struct S { } ", r#" -type Type = u8; +type ${0:Type} = u8; struct S { - field: Type, + field: ${0:Type}, } "#, ); @@ -114,10 +125,10 @@ fn f() { r#" fn generic() {} -type Type = (); +type ${0:Type} = (); fn f() { - generic::(); + generic::<${0:Type}>(); } "#, ); @@ -135,10 +146,10 @@ struct S { ", r#" struct Vec {} -type Type = Vec; +type ${0:Type} = Vec; struct S { - v: Vec>, + v: Vec>, } "#, ); @@ -154,10 +165,10 @@ struct S { } ", r#" -type Type = u8; +type ${0:Type} = u8; struct S { - field: (Type,), + field: (${0:Type},), } "#, ); diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs index 6bb65e6bc..becd640b1 100644 --- a/crates/ide_assists/src/tests/generated.rs +++ b/crates/ide_assists/src/tests/generated.rs @@ -338,10 +338,10 @@ struct S { } "#####, r#####" -type Type = (u8, u8, u8); +type ${0:Type} = (u8, u8, u8); struct S { - field: Type, + field: ${0:Type}, } "#####, ) -- cgit v1.2.3