Skip to content

Commit 020ca66

Browse files
committed
Simplify
1 parent 4d50709 commit 020ca66

File tree

3 files changed

+17
-33
lines changed

3 files changed

+17
-33
lines changed

crates/ra_assists/src/assist_ctx.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ra_syntax::{
1313
};
1414
use ra_text_edit::TextEditBuilder;
1515

16-
use crate::{AssistFile, AssistId, AssistLabel, GroupLabel, ResolvedAssist};
16+
use crate::{AssistId, AssistLabel, GroupLabel, ResolvedAssist};
1717

1818
#[derive(Clone, Debug)]
1919
pub(crate) struct Assist(pub(crate) Vec<AssistInfo>);
@@ -107,7 +107,7 @@ impl<'a> AssistCtx<'a> {
107107
let source_change = {
108108
let mut edit = ActionBuilder::new(&self);
109109
f(&mut edit);
110-
edit.build(change_label, self.frange.file_id)
110+
edit.build(change_label)
111111
};
112112
info = info.resolved(source_change)
113113
};
@@ -166,7 +166,7 @@ impl<'a> AssistGroup<'a> {
166166
let source_change = {
167167
let mut edit = ActionBuilder::new(&self.ctx);
168168
f(&mut edit);
169-
edit.build(change_label, self.ctx.frange.file_id)
169+
edit.build(change_label)
170170
};
171171
info = info.resolved(source_change)
172172
};
@@ -186,7 +186,7 @@ impl<'a> AssistGroup<'a> {
186186
pub(crate) struct ActionBuilder<'a, 'b> {
187187
edit: TextEditBuilder,
188188
cursor_position: Option<TextSize>,
189-
file: AssistFile,
189+
file: FileId,
190190
ctx: &'a AssistCtx<'b>,
191191
}
192192

@@ -195,7 +195,7 @@ impl<'a, 'b> ActionBuilder<'a, 'b> {
195195
Self {
196196
edit: TextEditBuilder::default(),
197197
cursor_position: None,
198-
file: AssistFile::default(),
198+
file: ctx.frange.file_id,
199199
ctx,
200200
}
201201
}
@@ -254,20 +254,16 @@ impl<'a, 'b> ActionBuilder<'a, 'b> {
254254
algo::diff(&node, &new).into_text_edit(&mut self.edit)
255255
}
256256

257-
pub(crate) fn set_file(&mut self, assist_file: AssistFile) {
258-
self.file = assist_file
257+
pub(crate) fn set_file(&mut self, assist_file: FileId) {
258+
self.file = assist_file;
259259
}
260260

261-
fn build(self, change_label: String, current_file: FileId) -> SourceChange {
261+
fn build(self, change_label: String) -> SourceChange {
262262
let edit = self.edit.finish();
263263
if edit.is_empty() && self.cursor_position.is_none() {
264264
panic!("Only call `add_assist` if the assist can be applied")
265265
}
266-
let file = match self.file {
267-
AssistFile::CurrentFile => current_file,
268-
AssistFile::TargetFile(it) => it,
269-
};
270266
SingleFileChange { label: change_label, edit, cursor_position: self.cursor_position }
271-
.into_source_change(file)
267+
.into_source_change(self.file)
272268
}
273269
}

crates/ra_assists/src/handlers/add_function.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ use ra_syntax::{
33
SyntaxKind, SyntaxNode, TextSize,
44
};
55

6-
use crate::{Assist, AssistCtx, AssistFile, AssistId};
6+
use crate::{Assist, AssistCtx, AssistId};
77
use ast::{edit::IndentLevel, ArgListOwner, ModuleItemOwner};
88
use hir::HirDisplay;
9+
use ra_db::FileId;
910
use rustc_hash::{FxHashMap, FxHashSet};
1011

1112
// Assist: add_function
@@ -70,15 +71,15 @@ struct FunctionTemplate {
7071
insert_offset: TextSize,
7172
cursor_offset: TextSize,
7273
fn_def: ast::SourceFile,
73-
file: AssistFile,
74+
file: FileId,
7475
}
7576

7677
struct FunctionBuilder {
7778
target: GeneratedFunctionTarget,
7879
fn_name: ast::Name,
7980
type_params: Option<ast::TypeParamList>,
8081
params: ast::ParamList,
81-
file: AssistFile,
82+
file: FileId,
8283
needs_pub: bool,
8384
}
8485

@@ -92,7 +93,7 @@ impl FunctionBuilder {
9293
target_module: Option<hir::InFile<hir::ModuleSource>>,
9394
) -> Option<Self> {
9495
let needs_pub = target_module.is_some();
95-
let mut file = AssistFile::default();
96+
let mut file = ctx.frange.file_id;
9697
let target = if let Some(target_module) = target_module {
9798
let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, target_module)?;
9899
file = in_file;
@@ -253,9 +254,8 @@ fn next_space_for_fn_after_call_site(expr: &ast::CallExpr) -> Option<GeneratedFu
253254
fn next_space_for_fn_in_module(
254255
db: &dyn hir::db::AstDatabase,
255256
module: hir::InFile<hir::ModuleSource>,
256-
) -> Option<(AssistFile, GeneratedFunctionTarget)> {
257+
) -> Option<(FileId, GeneratedFunctionTarget)> {
257258
let file = module.file_id.original_file(db);
258-
let assist_file = AssistFile::TargetFile(file);
259259
let assist_item = match module.value {
260260
hir::ModuleSource::SourceFile(it) => {
261261
if let Some(last_item) = it.items().last() {
@@ -272,7 +272,7 @@ fn next_space_for_fn_in_module(
272272
}
273273
}
274274
};
275-
Some((assist_file, assist_item))
275+
Some((file, assist_item))
276276
}
277277

278278
#[cfg(test)]

crates/ra_assists/src/lib.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub mod utils;
1818
pub mod ast_transform;
1919

2020
use hir::Semantics;
21-
use ra_db::{FileId, FileRange};
21+
use ra_db::FileRange;
2222
use ra_ide_db::{source_change::SourceChange, RootDatabase};
2323
use ra_syntax::TextRange;
2424

@@ -62,18 +62,6 @@ pub struct ResolvedAssist {
6262
pub source_change: SourceChange,
6363
}
6464

65-
#[derive(Debug, Clone, Copy)]
66-
enum AssistFile {
67-
CurrentFile,
68-
TargetFile(FileId),
69-
}
70-
71-
impl Default for AssistFile {
72-
fn default() -> Self {
73-
Self::CurrentFile
74-
}
75-
}
76-
7765
/// Return all the assists applicable at the given position.
7866
///
7967
/// Assists are returned in the "unresolved" state, that is only labels are

0 commit comments

Comments
 (0)