Skip to content

Commit 525a258

Browse files
committed
Add target file information to AssistAction
1 parent 4f6d5ed commit 525a258

File tree

4 files changed

+82
-16
lines changed

4 files changed

+82
-16
lines changed

crates/ra_assists/src/assist_ctx.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use ra_syntax::{
1010
};
1111
use ra_text_edit::TextEditBuilder;
1212

13-
use crate::{AssistAction, AssistId, AssistLabel, GroupLabel, ResolvedAssist};
13+
use crate::{AssistAction, AssistFile, AssistId, AssistLabel, GroupLabel, ResolvedAssist};
1414
use algo::SyntaxRewriter;
1515

1616
#[derive(Clone, Debug)]
@@ -180,6 +180,7 @@ pub(crate) struct ActionBuilder {
180180
edit: TextEditBuilder,
181181
cursor_position: Option<TextUnit>,
182182
target: Option<TextRange>,
183+
file: AssistFile,
183184
}
184185

185186
impl ActionBuilder {
@@ -241,11 +242,16 @@ impl ActionBuilder {
241242
algo::diff(&node, &new).into_text_edit(&mut self.edit)
242243
}
243244

245+
pub(crate) fn set_file(&mut self, assist_file: AssistFile) {
246+
self.file = assist_file
247+
}
248+
244249
fn build(self) -> AssistAction {
245250
AssistAction {
246251
edit: self.edit.finish(),
247252
cursor_position: self.cursor_position,
248253
target: self.target,
254+
file: self.file,
249255
}
250256
}
251257
}

crates/ra_assists/src/handlers/add_function.rs

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

6-
use crate::{Assist, AssistCtx, AssistId};
6+
use crate::{Assist, AssistCtx, AssistFile, AssistId};
77
use ast::{edit::IndentLevel, ArgListOwner, ModuleItemOwner};
88
use hir::HirDisplay;
99
use rustc_hash::{FxHashMap, FxHashSet};
@@ -44,10 +44,10 @@ pub(crate) fn add_function(ctx: AssistCtx) -> Option<Assist> {
4444
}
4545

4646
let target_module = if let Some(qualifier) = path.qualifier() {
47-
if let Some(hir::PathResolution::Def(hir::ModuleDef::Module(resolved))) =
47+
if let Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) =
4848
ctx.sema.resolve_path(&qualifier)
4949
{
50-
Some(resolved.definition_source(ctx.sema.db).value)
50+
Some(module.definition_source(ctx.sema.db))
5151
} else {
5252
return None;
5353
}
@@ -61,6 +61,7 @@ pub(crate) fn add_function(ctx: AssistCtx) -> Option<Assist> {
6161
edit.target(call.syntax().text_range());
6262

6363
if let Some(function_template) = function_builder.render() {
64+
edit.set_file(function_template.file);
6465
edit.set_cursor(function_template.cursor_offset);
6566
edit.insert(function_template.insert_offset, function_template.fn_def.to_string());
6667
}
@@ -71,13 +72,15 @@ struct FunctionTemplate {
7172
insert_offset: TextUnit,
7273
cursor_offset: TextUnit,
7374
fn_def: ast::SourceFile,
75+
file: AssistFile,
7476
}
7577

7678
struct FunctionBuilder {
7779
target: GeneratedFunctionTarget,
7880
fn_name: ast::Name,
7981
type_params: Option<ast::TypeParamList>,
8082
params: ast::ParamList,
83+
file: AssistFile,
8184
}
8285

8386
impl FunctionBuilder {
@@ -87,16 +90,19 @@ impl FunctionBuilder {
8790
ctx: &AssistCtx,
8891
call: &ast::CallExpr,
8992
path: &ast::Path,
90-
generate_in: Option<hir::ModuleSource>,
93+
generate_in: Option<hir::InFile<hir::ModuleSource>>,
9194
) -> Option<Self> {
95+
let mut file = AssistFile::default();
9296
let target = if let Some(generate_in_module) = generate_in {
93-
next_space_for_fn_in_module(generate_in_module)?
97+
let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, generate_in_module)?;
98+
file = in_file;
99+
target
94100
} else {
95101
next_space_for_fn_after_call_site(&call)?
96102
};
97103
let fn_name = fn_name(&path)?;
98104
let (type_params, params) = fn_args(ctx, &call)?;
99-
Some(Self { target, fn_name, type_params, params })
105+
Some(Self { target, fn_name, type_params, params, file })
100106
}
101107
fn render(self) -> Option<FunctionTemplate> {
102108
let placeholder_expr = ast::make::expr_todo();
@@ -130,7 +136,7 @@ impl FunctionBuilder {
130136
.text_range()
131137
.start();
132138
let cursor_offset = insert_offset + cursor_offset_from_fn_start;
133-
Some(FunctionTemplate { insert_offset, cursor_offset, fn_def })
139+
Some(FunctionTemplate { insert_offset, cursor_offset, fn_def, file: self.file })
134140
}
135141
}
136142

@@ -250,23 +256,29 @@ fn next_space_for_fn_after_call_site(expr: &ast::CallExpr) -> Option<GeneratedFu
250256
last_ancestor.map(GeneratedFunctionTarget::BehindItem)
251257
}
252258

253-
fn next_space_for_fn_in_module(module: hir::ModuleSource) -> Option<GeneratedFunctionTarget> {
254-
match module {
259+
fn next_space_for_fn_in_module(
260+
db: &dyn hir::db::AstDatabase,
261+
module: hir::InFile<hir::ModuleSource>,
262+
) -> Option<(AssistFile, GeneratedFunctionTarget)> {
263+
let file = module.file_id.original_file(db);
264+
let assist_file = AssistFile::TargetFile(file);
265+
let assist_item = match module.value {
255266
hir::ModuleSource::SourceFile(it) => {
256267
if let Some(last_item) = it.items().last() {
257-
Some(GeneratedFunctionTarget::BehindItem(last_item.syntax().clone()))
268+
GeneratedFunctionTarget::BehindItem(last_item.syntax().clone())
258269
} else {
259-
Some(GeneratedFunctionTarget::BehindItem(it.syntax().clone()))
270+
GeneratedFunctionTarget::BehindItem(it.syntax().clone())
260271
}
261272
}
262273
hir::ModuleSource::Module(it) => {
263274
if let Some(last_item) = it.item_list().and_then(|it| it.items().last()) {
264-
Some(GeneratedFunctionTarget::BehindItem(last_item.syntax().clone()))
275+
GeneratedFunctionTarget::BehindItem(last_item.syntax().clone())
265276
} else {
266-
it.item_list().map(GeneratedFunctionTarget::InEmptyItemList)
277+
GeneratedFunctionTarget::InEmptyItemList(it.item_list()?)
267278
}
268279
}
269-
}
280+
};
281+
Some((assist_file, assist_item))
270282
}
271283

272284
#[cfg(test)]
@@ -884,6 +896,37 @@ fn foo() {
884896
)
885897
}
886898

899+
#[test]
900+
fn add_function_in_another_file() {
901+
check_assist(
902+
add_function,
903+
r"
904+
//- /main.rs
905+
mod foo;
906+
907+
fn main() {
908+
foo::bar<|>()
909+
}
910+
911+
//- /foo.rs
912+
913+
",
914+
r"
915+
//- /main.rs
916+
mod foo;
917+
918+
fn main() {
919+
foo::bar()
920+
}
921+
922+
//- /foo.rs
923+
fn bar() {
924+
<|>todo!()
925+
}
926+
",
927+
)
928+
}
929+
887930
#[test]
888931
fn add_function_not_applicable_if_function_already_exists() {
889932
check_assist_not_applicable(

crates/ra_assists/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod doc_tests;
1717
pub mod utils;
1818
pub mod ast_transform;
1919

20-
use ra_db::FileRange;
20+
use ra_db::{FileId, FileRange};
2121
use ra_ide_db::RootDatabase;
2222
use ra_syntax::{TextRange, TextUnit};
2323
use ra_text_edit::TextEdit;
@@ -54,6 +54,7 @@ pub struct AssistAction {
5454
pub cursor_position: Option<TextUnit>,
5555
// FIXME: This belongs to `AssistLabel`
5656
pub target: Option<TextRange>,
57+
pub file: AssistFile,
5758
}
5859

5960
#[derive(Debug, Clone)]
@@ -63,6 +64,18 @@ pub struct ResolvedAssist {
6364
pub action: AssistAction,
6465
}
6566

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

crates/ra_ide/src/assists.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ fn action_to_edit(
3737
file_id: FileId,
3838
assist_label: &AssistLabel,
3939
) -> SourceChange {
40+
let file_id = match action.file {
41+
ra_assists::AssistFile::TargetFile(it) => it,
42+
_ => file_id,
43+
};
4044
let file_edit = SourceFileEdit { file_id, edit: action.edit };
4145
SourceChange::source_file_edit(assist_label.label.clone(), file_edit)
4246
.with_cursor_opt(action.cursor_position.map(|offset| FilePosition { offset, file_id }))

0 commit comments

Comments
 (0)