Skip to content

Commit f2f882b

Browse files
committed
Add pub(crate) to functions generated in other module
1 parent 74780a1 commit f2f882b

File tree

2 files changed

+27
-46
lines changed

2 files changed

+27
-46
lines changed

crates/ra_assists/src/handlers/add_function.rs

+23-46
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ struct FunctionBuilder {
8181
type_params: Option<ast::TypeParamList>,
8282
params: ast::ParamList,
8383
file: AssistFile,
84+
needs_pub: bool,
8485
}
8586

8687
impl FunctionBuilder {
@@ -90,24 +91,29 @@ impl FunctionBuilder {
9091
ctx: &AssistCtx,
9192
call: &ast::CallExpr,
9293
path: &ast::Path,
93-
generate_in: Option<hir::InFile<hir::ModuleSource>>,
94+
target_module: Option<hir::InFile<hir::ModuleSource>>,
9495
) -> Option<Self> {
96+
let needs_pub = target_module.is_some();
9597
let mut file = AssistFile::default();
96-
let target = if let Some(generate_in_module) = generate_in {
97-
let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, generate_in_module)?;
98+
let target = if let Some(target_module) = target_module {
99+
let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, target_module)?;
98100
file = in_file;
99101
target
100102
} else {
101103
next_space_for_fn_after_call_site(&call)?
102104
};
103105
let fn_name = fn_name(&path)?;
104106
let (type_params, params) = fn_args(ctx, &call)?;
105-
Some(Self { target, fn_name, type_params, params, file })
107+
Some(Self { target, fn_name, type_params, params, file, needs_pub })
106108
}
109+
107110
fn render(self) -> Option<FunctionTemplate> {
108111
let placeholder_expr = ast::make::expr_todo();
109112
let fn_body = ast::make::block_expr(vec![], Some(placeholder_expr));
110-
let fn_def = ast::make::fn_def(self.fn_name, self.type_params, self.params, fn_body);
113+
let mut fn_def = ast::make::fn_def(self.fn_name, self.type_params, self.params, fn_body);
114+
if self.needs_pub {
115+
fn_def = ast::make::add_pub_crate_modifier(fn_def);
116+
}
111117

112118
let (fn_def, insert_offset) = match self.target {
113119
GeneratedFunctionTarget::BehindItem(it) => {
@@ -116,15 +122,14 @@ impl FunctionBuilder {
116122
(indented, it.text_range().end())
117123
}
118124
GeneratedFunctionTarget::InEmptyItemList(it) => {
119-
let with_leading_newline = ast::make::add_leading_newlines(1, fn_def);
120-
let indent = IndentLevel::from_node(it.syntax()).indented();
121-
let mut indented = indent.increase_indent(with_leading_newline);
122-
if !item_list_has_whitespace(&it) {
123-
// In this case we want to make sure there's a newline between the closing
124-
// function brace and the closing module brace (so it doesn't end in `}}`).
125-
indented = ast::make::add_trailing_newlines(1, indented);
126-
}
127-
(indented, it.syntax().text_range().start() + TextUnit::from_usize(1))
125+
let indent_once = IndentLevel(1);
126+
let indent = IndentLevel::from_node(it.syntax());
127+
128+
let fn_def = ast::make::add_leading_newlines(1, fn_def);
129+
let fn_def = indent_once.increase_indent(fn_def);
130+
let fn_def = ast::make::add_trailing_newlines(1, fn_def);
131+
let fn_def = indent.increase_indent(fn_def);
132+
(fn_def, it.syntax().text_range().start() + TextUnit::from_usize(1))
128133
}
129134
};
130135

@@ -140,11 +145,6 @@ impl FunctionBuilder {
140145
}
141146
}
142147

143-
/// Returns true if the given ItemList contains whitespace.
144-
fn item_list_has_whitespace(it: &ast::ItemList) -> bool {
145-
it.syntax().descendants_with_tokens().find(|it| it.kind() == SyntaxKind::WHITESPACE).is_some()
146-
}
147-
148148
enum GeneratedFunctionTarget {
149149
BehindItem(SyntaxNode),
150150
InEmptyItemList(ast::ItemList),
@@ -803,29 +803,7 @@ fn foo() {
803803
",
804804
r"
805805
mod bar {
806-
fn my_fn() {
807-
<|>todo!()
808-
}
809-
}
810-
811-
fn foo() {
812-
bar::my_fn()
813-
}
814-
",
815-
);
816-
check_assist(
817-
add_function,
818-
r"
819-
mod bar {
820-
}
821-
822-
fn foo() {
823-
bar::my_fn<|>()
824-
}
825-
",
826-
r"
827-
mod bar {
828-
fn my_fn() {
806+
pub(crate) fn my_fn() {
829807
<|>todo!()
830808
}
831809
}
@@ -854,7 +832,7 @@ fn foo() {
854832
mod bar {
855833
fn something_else() {}
856834
857-
fn my_fn() {
835+
pub(crate) fn my_fn() {
858836
<|>todo!()
859837
}
860838
}
@@ -872,8 +850,7 @@ fn foo() {
872850
add_function,
873851
r"
874852
mod bar {
875-
mod baz {
876-
}
853+
mod baz {}
877854
}
878855
879856
fn foo() {
@@ -883,7 +860,7 @@ fn foo() {
883860
r"
884861
mod bar {
885862
mod baz {
886-
fn my_fn() {
863+
pub(crate) fn my_fn() {
887864
<|>todo!()
888865
}
889866
}

crates/ra_syntax/src/ast/make.rs

+4
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ pub fn add_trailing_newlines(amount_of_newlines: usize, t: impl AstNode) -> ast:
303303
ast_from_text(&format!("{}{}", t.syntax(), newlines))
304304
}
305305

306+
pub fn add_pub_crate_modifier(fn_def: ast::FnDef) -> ast::FnDef {
307+
ast_from_text(&format!("pub(crate) {}", fn_def))
308+
}
309+
306310
fn ast_from_text<N: AstNode>(text: &str) -> N {
307311
let parse = SourceFile::parse(text);
308312
let node = parse.tree().syntax().descendants().find_map(N::cast).unwrap();

0 commit comments

Comments
 (0)