aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/handlers')
-rw-r--r--crates/ra_assists/src/handlers/add_function.rs2
-rw-r--r--crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs58
-rw-r--r--crates/ra_assists/src/handlers/fix_visibility.rs4
3 files changed, 29 insertions, 35 deletions
diff --git a/crates/ra_assists/src/handlers/add_function.rs b/crates/ra_assists/src/handlers/add_function.rs
index 24f931a85..1cfbd75aa 100644
--- a/crates/ra_assists/src/handlers/add_function.rs
+++ b/crates/ra_assists/src/handlers/add_function.rs
@@ -64,7 +64,7 @@ pub(crate) fn add_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
64 let target = call.syntax().text_range(); 64 let target = call.syntax().text_range();
65 acc.add(AssistId("add_function"), "Add function", target, |builder| { 65 acc.add(AssistId("add_function"), "Add function", target, |builder| {
66 let function_template = function_builder.render(); 66 let function_template = function_builder.render();
67 builder.set_file(function_template.file); 67 builder.edit_file(function_template.file);
68 let new_fn = function_template.to_string(ctx.config.snippet_cap); 68 let new_fn = function_template.to_string(ctx.config.snippet_cap);
69 match ctx.config.snippet_cap { 69 match ctx.config.snippet_cap {
70 Some(cap) => builder.insert_snippet(cap, function_template.insert_offset, new_fn), 70 Some(cap) => builder.insert_snippet(cap, function_template.insert_offset, new_fn),
diff --git a/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs
index 2c455a1fd..44db7917a 100644
--- a/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs
+++ b/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs
@@ -1,20 +1,17 @@
1use hir::{EnumVariant, Module, ModuleDef, Name};
2use ra_db::FileId;
3use ra_fmt::leading_indent;
1use ra_ide_db::{defs::Definition, search::Reference, RootDatabase}; 4use ra_ide_db::{defs::Definition, search::Reference, RootDatabase};
2use ra_syntax::{ 5use ra_syntax::{
3 algo::find_node_at_offset, 6 algo::find_node_at_offset,
4 ast::{self, AstNode, NameOwner}, 7 ast::{self, ArgListOwner, AstNode, NameOwner, VisibilityOwner},
5 SourceFile, SyntaxNode, TextRange, TextSize, 8 SourceFile, SyntaxNode, TextRange, TextSize,
6}; 9};
10use rustc_hash::FxHashSet;
7 11
8use crate::{ 12use crate::{
9 assist_context::{AssistBuilder, AssistDirector}, 13 assist_context::AssistBuilder, utils::insert_use_statement, AssistContext, AssistId, Assists,
10 utils::insert_use_statement,
11 AssistContext, AssistId, Assists,
12}; 14};
13use ast::{ArgListOwner, VisibilityOwner};
14use hir::{EnumVariant, Module, ModuleDef, Name};
15use ra_db::FileId;
16use ra_fmt::leading_indent;
17use rustc_hash::FxHashSet;
18 15
19// Assist: extract_struct_from_enum_variant 16// Assist: extract_struct_from_enum_variant
20// 17//
@@ -50,11 +47,11 @@ pub(crate) fn extract_struct_from_enum_variant(
50 let enum_module_def = ModuleDef::from(enum_hir); 47 let enum_module_def = ModuleDef::from(enum_hir);
51 let current_module = enum_hir.module(ctx.db); 48 let current_module = enum_hir.module(ctx.db);
52 let target = variant.syntax().text_range(); 49 let target = variant.syntax().text_range();
53 acc.add_in_multiple_files( 50 acc.add(
54 AssistId("extract_struct_from_enum_variant"), 51 AssistId("extract_struct_from_enum_variant"),
55 "Extract struct from enum variant", 52 "Extract struct from enum variant",
56 target, 53 target,
57 |edit| { 54 |builder| {
58 let definition = Definition::ModuleDef(ModuleDef::EnumVariant(variant_hir)); 55 let definition = Definition::ModuleDef(ModuleDef::EnumVariant(variant_hir));
59 let res = definition.find_usages(&ctx.db, None); 56 let res = definition.find_usages(&ctx.db, None);
60 let start_offset = variant.parent_enum().syntax().text_range().start(); 57 let start_offset = variant.parent_enum().syntax().text_range().start();
@@ -64,7 +61,7 @@ pub(crate) fn extract_struct_from_enum_variant(
64 let source_file = ctx.sema.parse(reference.file_range.file_id); 61 let source_file = ctx.sema.parse(reference.file_range.file_id);
65 update_reference( 62 update_reference(
66 ctx, 63 ctx,
67 edit, 64 builder,
68 reference, 65 reference,
69 &source_file, 66 &source_file,
70 &enum_module_def, 67 &enum_module_def,
@@ -73,7 +70,7 @@ pub(crate) fn extract_struct_from_enum_variant(
73 ); 70 );
74 } 71 }
75 extract_struct_def( 72 extract_struct_def(
76 edit, 73 builder,
77 enum_ast.syntax(), 74 enum_ast.syntax(),
78 &variant_name, 75 &variant_name,
79 &field_list.to_string(), 76 &field_list.to_string(),
@@ -82,7 +79,7 @@ pub(crate) fn extract_struct_from_enum_variant(
82 &visibility, 79 &visibility,
83 ); 80 );
84 let list_range = field_list.syntax().text_range(); 81 let list_range = field_list.syntax().text_range();
85 update_variant(edit, &variant_name, ctx.frange.file_id, list_range); 82 update_variant(builder, &variant_name, ctx.frange.file_id, list_range);
86 }, 83 },
87 ) 84 )
88} 85}
@@ -115,7 +112,7 @@ fn insert_import(
115} 112}
116 113
117fn extract_struct_def( 114fn extract_struct_def(
118 edit: &mut AssistDirector, 115 builder: &mut AssistBuilder,
119 enum_ast: &SyntaxNode, 116 enum_ast: &SyntaxNode,
120 variant_name: &str, 117 variant_name: &str,
121 variant_list: &str, 118 variant_list: &str,
@@ -142,14 +139,13 @@ fn extract_struct_def(
142 list_with_visibility(variant_list), 139 list_with_visibility(variant_list),
143 indent 140 indent
144 ); 141 );
145 edit.perform(file_id, |builder| { 142 builder.edit_file(file_id);
146 builder.insert(start_offset, struct_def); 143 builder.insert(start_offset, struct_def);
147 });
148 Some(()) 144 Some(())
149} 145}
150 146
151fn update_variant( 147fn update_variant(
152 edit: &mut AssistDirector, 148 builder: &mut AssistBuilder,
153 variant_name: &str, 149 variant_name: &str,
154 file_id: FileId, 150 file_id: FileId,
155 list_range: TextRange, 151 list_range: TextRange,
@@ -158,15 +154,14 @@ fn update_variant(
158 list_range.start().checked_add(TextSize::from(1))?, 154 list_range.start().checked_add(TextSize::from(1))?,
159 list_range.end().checked_sub(TextSize::from(1))?, 155 list_range.end().checked_sub(TextSize::from(1))?,
160 ); 156 );
161 edit.perform(file_id, |builder| { 157 builder.edit_file(file_id);
162 builder.replace(inside_variant_range, variant_name); 158 builder.replace(inside_variant_range, variant_name);
163 });
164 Some(()) 159 Some(())
165} 160}
166 161
167fn update_reference( 162fn update_reference(
168 ctx: &AssistContext, 163 ctx: &AssistContext,
169 edit: &mut AssistDirector, 164 builder: &mut AssistBuilder,
170 reference: Reference, 165 reference: Reference,
171 source_file: &SourceFile, 166 source_file: &SourceFile,
172 enum_module_def: &ModuleDef, 167 enum_module_def: &ModuleDef,
@@ -186,16 +181,15 @@ fn update_reference(
186 list_range.start().checked_add(TextSize::from(1))?, 181 list_range.start().checked_add(TextSize::from(1))?,
187 list_range.end().checked_sub(TextSize::from(1))?, 182 list_range.end().checked_sub(TextSize::from(1))?,
188 ); 183 );
189 edit.perform(reference.file_range.file_id, |builder| { 184 builder.edit_file(reference.file_range.file_id);
190 if !visited_modules_set.contains(&module) { 185 if !visited_modules_set.contains(&module) {
191 if insert_import(ctx, builder, &path_expr, &module, enum_module_def, variant_hir_name) 186 if insert_import(ctx, builder, &path_expr, &module, enum_module_def, variant_hir_name)
192 .is_some() 187 .is_some()
193 { 188 {
194 visited_modules_set.insert(module); 189 visited_modules_set.insert(module);
195 }
196 } 190 }
197 builder.replace(inside_list_range, format!("{}{}", segment, list)); 191 }
198 }); 192 builder.replace(inside_list_range, format!("{}{}", segment, list));
199 Some(()) 193 Some(())
200} 194}
201 195
diff --git a/crates/ra_assists/src/handlers/fix_visibility.rs b/crates/ra_assists/src/handlers/fix_visibility.rs
index 9ec42f568..531b3560f 100644
--- a/crates/ra_assists/src/handlers/fix_visibility.rs
+++ b/crates/ra_assists/src/handlers/fix_visibility.rs
@@ -63,7 +63,7 @@ fn add_vis_to_referenced_module_def(acc: &mut Assists, ctx: &AssistContext) -> O
63 }; 63 };
64 64
65 acc.add(AssistId("fix_visibility"), assist_label, target, |builder| { 65 acc.add(AssistId("fix_visibility"), assist_label, target, |builder| {
66 builder.set_file(target_file); 66 builder.edit_file(target_file);
67 match ctx.config.snippet_cap { 67 match ctx.config.snippet_cap {
68 Some(cap) => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)), 68 Some(cap) => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)),
69 None => builder.insert(offset, format!("{} ", missing_visibility)), 69 None => builder.insert(offset, format!("{} ", missing_visibility)),
@@ -106,7 +106,7 @@ fn add_vis_to_referenced_record_field(acc: &mut Assists, ctx: &AssistContext) ->
106 format!("Change visibility of {}.{} to {}", parent_name, target_name, missing_visibility); 106 format!("Change visibility of {}.{} to {}", parent_name, target_name, missing_visibility);
107 107
108 acc.add(AssistId("fix_visibility"), assist_label, target, |builder| { 108 acc.add(AssistId("fix_visibility"), assist_label, target, |builder| {
109 builder.set_file(target_file); 109 builder.edit_file(target_file);
110 match ctx.config.snippet_cap { 110 match ctx.config.snippet_cap {
111 Some(cap) => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)), 111 Some(cap) => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)),
112 None => builder.insert(offset, format!("{} ", missing_visibility)), 112 None => builder.insert(offset, format!("{} ", missing_visibility)),