aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-05-06 15:39:11 +0100
committerAleksey Kladov <[email protected]>2020-05-06 15:39:11 +0100
commit020ca6695f4d58f651984c4fbe2227d891896bb3 (patch)
tree5189803adf9b8d89cb4823362e568748de039396
parent4d50709a96f92f3927b3ac59110d593b49c53008 (diff)
Simplify
-rw-r--r--crates/ra_assists/src/assist_ctx.rs22
-rw-r--r--crates/ra_assists/src/handlers/add_function.rs14
-rw-r--r--crates/ra_assists/src/lib.rs14
3 files changed, 17 insertions, 33 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs
index af6756d17..871671de2 100644
--- a/crates/ra_assists/src/assist_ctx.rs
+++ b/crates/ra_assists/src/assist_ctx.rs
@@ -13,7 +13,7 @@ use ra_syntax::{
13}; 13};
14use ra_text_edit::TextEditBuilder; 14use ra_text_edit::TextEditBuilder;
15 15
16use crate::{AssistFile, AssistId, AssistLabel, GroupLabel, ResolvedAssist}; 16use crate::{AssistId, AssistLabel, GroupLabel, ResolvedAssist};
17 17
18#[derive(Clone, Debug)] 18#[derive(Clone, Debug)]
19pub(crate) struct Assist(pub(crate) Vec<AssistInfo>); 19pub(crate) struct Assist(pub(crate) Vec<AssistInfo>);
@@ -107,7 +107,7 @@ impl<'a> AssistCtx<'a> {
107 let source_change = { 107 let source_change = {
108 let mut edit = ActionBuilder::new(&self); 108 let mut edit = ActionBuilder::new(&self);
109 f(&mut edit); 109 f(&mut edit);
110 edit.build(change_label, self.frange.file_id) 110 edit.build(change_label)
111 }; 111 };
112 info = info.resolved(source_change) 112 info = info.resolved(source_change)
113 }; 113 };
@@ -166,7 +166,7 @@ impl<'a> AssistGroup<'a> {
166 let source_change = { 166 let source_change = {
167 let mut edit = ActionBuilder::new(&self.ctx); 167 let mut edit = ActionBuilder::new(&self.ctx);
168 f(&mut edit); 168 f(&mut edit);
169 edit.build(change_label, self.ctx.frange.file_id) 169 edit.build(change_label)
170 }; 170 };
171 info = info.resolved(source_change) 171 info = info.resolved(source_change)
172 }; 172 };
@@ -186,7 +186,7 @@ impl<'a> AssistGroup<'a> {
186pub(crate) struct ActionBuilder<'a, 'b> { 186pub(crate) struct ActionBuilder<'a, 'b> {
187 edit: TextEditBuilder, 187 edit: TextEditBuilder,
188 cursor_position: Option<TextSize>, 188 cursor_position: Option<TextSize>,
189 file: AssistFile, 189 file: FileId,
190 ctx: &'a AssistCtx<'b>, 190 ctx: &'a AssistCtx<'b>,
191} 191}
192 192
@@ -195,7 +195,7 @@ impl<'a, 'b> ActionBuilder<'a, 'b> {
195 Self { 195 Self {
196 edit: TextEditBuilder::default(), 196 edit: TextEditBuilder::default(),
197 cursor_position: None, 197 cursor_position: None,
198 file: AssistFile::default(), 198 file: ctx.frange.file_id,
199 ctx, 199 ctx,
200 } 200 }
201 } 201 }
@@ -254,20 +254,16 @@ impl<'a, 'b> ActionBuilder<'a, 'b> {
254 algo::diff(&node, &new).into_text_edit(&mut self.edit) 254 algo::diff(&node, &new).into_text_edit(&mut self.edit)
255 } 255 }
256 256
257 pub(crate) fn set_file(&mut self, assist_file: AssistFile) { 257 pub(crate) fn set_file(&mut self, assist_file: FileId) {
258 self.file = assist_file 258 self.file = assist_file;
259 } 259 }
260 260
261 fn build(self, change_label: String, current_file: FileId) -> SourceChange { 261 fn build(self, change_label: String) -> SourceChange {
262 let edit = self.edit.finish(); 262 let edit = self.edit.finish();
263 if edit.is_empty() && self.cursor_position.is_none() { 263 if edit.is_empty() && self.cursor_position.is_none() {
264 panic!("Only call `add_assist` if the assist can be applied") 264 panic!("Only call `add_assist` if the assist can be applied")
265 } 265 }
266 let file = match self.file {
267 AssistFile::CurrentFile => current_file,
268 AssistFile::TargetFile(it) => it,
269 };
270 SingleFileChange { label: change_label, edit, cursor_position: self.cursor_position } 266 SingleFileChange { label: change_label, edit, cursor_position: self.cursor_position }
271 .into_source_change(file) 267 .into_source_change(self.file)
272 } 268 }
273} 269}
diff --git a/crates/ra_assists/src/handlers/add_function.rs b/crates/ra_assists/src/handlers/add_function.rs
index 1d9d4e638..278079665 100644
--- a/crates/ra_assists/src/handlers/add_function.rs
+++ b/crates/ra_assists/src/handlers/add_function.rs
@@ -3,9 +3,10 @@ use ra_syntax::{
3 SyntaxKind, SyntaxNode, TextSize, 3 SyntaxKind, SyntaxNode, TextSize,
4}; 4};
5 5
6use crate::{Assist, AssistCtx, AssistFile, AssistId}; 6use crate::{Assist, AssistCtx, AssistId};
7use ast::{edit::IndentLevel, ArgListOwner, ModuleItemOwner}; 7use ast::{edit::IndentLevel, ArgListOwner, ModuleItemOwner};
8use hir::HirDisplay; 8use hir::HirDisplay;
9use ra_db::FileId;
9use rustc_hash::{FxHashMap, FxHashSet}; 10use rustc_hash::{FxHashMap, FxHashSet};
10 11
11// Assist: add_function 12// Assist: add_function
@@ -70,7 +71,7 @@ struct FunctionTemplate {
70 insert_offset: TextSize, 71 insert_offset: TextSize,
71 cursor_offset: TextSize, 72 cursor_offset: TextSize,
72 fn_def: ast::SourceFile, 73 fn_def: ast::SourceFile,
73 file: AssistFile, 74 file: FileId,
74} 75}
75 76
76struct FunctionBuilder { 77struct FunctionBuilder {
@@ -78,7 +79,7 @@ struct FunctionBuilder {
78 fn_name: ast::Name, 79 fn_name: ast::Name,
79 type_params: Option<ast::TypeParamList>, 80 type_params: Option<ast::TypeParamList>,
80 params: ast::ParamList, 81 params: ast::ParamList,
81 file: AssistFile, 82 file: FileId,
82 needs_pub: bool, 83 needs_pub: bool,
83} 84}
84 85
@@ -92,7 +93,7 @@ impl FunctionBuilder {
92 target_module: Option<hir::InFile<hir::ModuleSource>>, 93 target_module: Option<hir::InFile<hir::ModuleSource>>,
93 ) -> Option<Self> { 94 ) -> Option<Self> {
94 let needs_pub = target_module.is_some(); 95 let needs_pub = target_module.is_some();
95 let mut file = AssistFile::default(); 96 let mut file = ctx.frange.file_id;
96 let target = if let Some(target_module) = target_module { 97 let target = if let Some(target_module) = target_module {
97 let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, target_module)?; 98 let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, target_module)?;
98 file = in_file; 99 file = in_file;
@@ -253,9 +254,8 @@ fn next_space_for_fn_after_call_site(expr: &ast::CallExpr) -> Option<GeneratedFu
253fn next_space_for_fn_in_module( 254fn next_space_for_fn_in_module(
254 db: &dyn hir::db::AstDatabase, 255 db: &dyn hir::db::AstDatabase,
255 module: hir::InFile<hir::ModuleSource>, 256 module: hir::InFile<hir::ModuleSource>,
256) -> Option<(AssistFile, GeneratedFunctionTarget)> { 257) -> Option<(FileId, GeneratedFunctionTarget)> {
257 let file = module.file_id.original_file(db); 258 let file = module.file_id.original_file(db);
258 let assist_file = AssistFile::TargetFile(file);
259 let assist_item = match module.value { 259 let assist_item = match module.value {
260 hir::ModuleSource::SourceFile(it) => { 260 hir::ModuleSource::SourceFile(it) => {
261 if let Some(last_item) = it.items().last() { 261 if let Some(last_item) = it.items().last() {
@@ -272,7 +272,7 @@ fn next_space_for_fn_in_module(
272 } 272 }
273 } 273 }
274 }; 274 };
275 Some((assist_file, assist_item)) 275 Some((file, assist_item))
276} 276}
277 277
278#[cfg(test)] 278#[cfg(test)]
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index 6e81ea8ee..13ea45ec7 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -18,7 +18,7 @@ pub mod utils;
18pub mod ast_transform; 18pub mod ast_transform;
19 19
20use hir::Semantics; 20use hir::Semantics;
21use ra_db::{FileId, FileRange}; 21use ra_db::FileRange;
22use ra_ide_db::{source_change::SourceChange, RootDatabase}; 22use ra_ide_db::{source_change::SourceChange, RootDatabase};
23use ra_syntax::TextRange; 23use ra_syntax::TextRange;
24 24
@@ -62,18 +62,6 @@ pub struct ResolvedAssist {
62 pub source_change: SourceChange, 62 pub source_change: SourceChange,
63} 63}
64 64
65#[derive(Debug, Clone, Copy)]
66enum AssistFile {
67 CurrentFile,
68 TargetFile(FileId),
69}
70
71impl Default for AssistFile {
72 fn default() -> Self {
73 Self::CurrentFile
74 }
75}
76
77/// Return all the assists applicable at the given position. 65/// Return all the assists applicable at the given position.
78/// 66///
79/// Assists are returned in the "unresolved" state, that is only labels are 67/// Assists are returned in the "unresolved" state, that is only labels are