Skip to content

Commit 680a0d5

Browse files
committed
internal: fix make API
1 parent 5342800 commit 680a0d5

File tree

4 files changed

+49
-44
lines changed

4 files changed

+49
-44
lines changed

crates/ide_assists/src/handlers/extract_function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ fn make_ret_ty(ctx: &AssistContext, module: hir::Module, fun: &Function) -> Opti
11921192
vec![fun_ty.make_ty(ctx, module), handler_ty],
11931193
)
11941194
}
1195-
FlowHandler::If { .. } => make::ty("bool"),
1195+
FlowHandler::If { .. } => make::ty_bool(),
11961196
FlowHandler::IfOption { action } => {
11971197
let handler_ty = action
11981198
.expr_ty(ctx)

crates/ide_assists/src/handlers/introduce_named_lifetime.rs

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,12 @@ fn generate_fn_def_assist(
8989
let loc_needing_lifetime =
9090
loc_needing_lifetime.and_then(|it| it.make_mut(builder).to_position());
9191

92-
add_lifetime_param(fn_def.get_or_create_generic_param_list(), new_lifetime_param);
93-
ted::replace(
94-
lifetime.syntax(),
95-
make_ast_lifetime(new_lifetime_param).clone_for_update().syntax(),
92+
fn_def.get_or_create_generic_param_list().add_generic_param(
93+
make::lifetime_param(new_lifetime_param.clone()).clone_for_update().into(),
9694
);
97-
loc_needing_lifetime.map(|position| {
98-
ted::insert(position, make_ast_lifetime(new_lifetime_param).clone_for_update().syntax())
99-
});
95+
ted::replace(lifetime.syntax(), new_lifetime_param.clone_for_update().syntax());
96+
loc_needing_lifetime
97+
.map(|position| ted::insert(position, new_lifetime_param.clone_for_update().syntax()));
10098
})
10199
}
102100

@@ -112,43 +110,27 @@ fn generate_impl_def_assist(
112110
let impl_def = builder.make_ast_mut(impl_def);
113111
let lifetime = builder.make_ast_mut(lifetime);
114112

115-
add_lifetime_param(impl_def.get_or_create_generic_param_list(), new_lifetime_param);
116-
ted::replace(
117-
lifetime.syntax(),
118-
make_ast_lifetime(new_lifetime_param).clone_for_update().syntax(),
113+
impl_def.get_or_create_generic_param_list().add_generic_param(
114+
make::lifetime_param(new_lifetime_param.clone()).clone_for_update().into(),
119115
);
116+
ted::replace(lifetime.syntax(), new_lifetime_param.clone_for_update().syntax());
120117
})
121118
}
122119

123120
/// Given a type parameter list, generate a unique lifetime parameter name
124121
/// which is not in the list
125122
fn generate_unique_lifetime_param_name(
126123
existing_type_param_list: Option<ast::GenericParamList>,
127-
) -> Option<char> {
124+
) -> Option<ast::Lifetime> {
128125
match existing_type_param_list {
129126
Some(type_params) => {
130-
let used_lifetime_params: FxHashSet<_> = type_params
131-
.lifetime_params()
132-
.map(|p| p.syntax().text().to_string()[1..].to_owned())
133-
.collect();
134-
(b'a'..=b'z').map(char::from).find(|c| !used_lifetime_params.contains(&c.to_string()))
127+
let used_lifetime_params: FxHashSet<_> =
128+
type_params.lifetime_params().map(|p| p.syntax().text().to_string()).collect();
129+
('a'..='z').map(|it| format!("'{}", it)).find(|it| !used_lifetime_params.contains(it))
135130
}
136-
None => Some('a'),
131+
None => Some("'a".to_string()),
137132
}
138-
}
139-
140-
fn add_lifetime_param(type_params: ast::GenericParamList, new_lifetime_param: char) {
141-
let generic_param =
142-
make::generic_param(&format!("'{}", new_lifetime_param), None).clone_for_update();
143-
type_params.add_generic_param(generic_param);
144-
}
145-
146-
fn make_ast_lifetime(new_lifetime_param: char) -> ast::Lifetime {
147-
make::generic_param(&format!("'{}", new_lifetime_param), None)
148-
.syntax()
149-
.descendants()
150-
.find_map(ast::Lifetime::cast)
151-
.unwrap()
133+
.map(|it| make::lifetime(&it))
152134
}
153135

154136
enum NeedsLifetime {

crates/ide_assists/src/handlers/replace_impl_trait_with_generic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ pub(crate) fn replace_impl_trait_with_generic(
3737

3838
let type_param_name = suggest_name::for_generic_parameter(&impl_trait_type);
3939

40-
let type_param =
41-
make::generic_param(&type_param_name, Some(type_bound_list)).clone_for_update();
40+
let type_param = make::type_param(make::name(&type_param_name), Some(type_bound_list))
41+
.clone_for_update();
4242
let new_ty = make::ty(&type_param_name).clone_for_update();
4343

4444
ted::replace(impl_trait_type.syntax(), new_ty.syntax());
45-
fn_.get_or_create_generic_param_list().add_generic_param(type_param)
45+
fn_.get_or_create_generic_param_list().add_generic_param(type_param.into())
4646
},
4747
)
4848
}

crates/syntax/src/ast/make.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! `parse(format!())` we use internally is an implementation detail -- long
1111
//! term, it will be replaced with direct tree manipulation.
1212
use itertools::Itertools;
13-
use stdx::format_to;
13+
use stdx::{format_to, never};
1414

1515
use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken};
1616

@@ -22,6 +22,16 @@ pub fn name_ref(text: &str) -> ast::NameRef {
2222
ast_from_text(&format!("fn f() {{ {}{}; }}", raw_ident_esc(text), text))
2323
}
2424

25+
pub fn lifetime(text: &str) -> ast::Lifetime {
26+
let mut text = text;
27+
let tmp;
28+
if never!(!text.starts_with('\'')) {
29+
tmp = format!("'{}", text);
30+
text = &tmp;
31+
}
32+
ast_from_text(&format!("fn f<{}>() {{ }}", text))
33+
}
34+
2535
fn raw_ident_esc(ident: &str) -> &'static str {
2636
let is_keyword = parser::SyntaxKind::from_keyword(ident).is_some();
2737
if is_keyword && !matches!(ident, "self" | "crate" | "super" | "Self") {
@@ -34,10 +44,13 @@ fn raw_ident_esc(ident: &str) -> &'static str {
3444
// FIXME: replace stringly-typed constructor with a family of typed ctors, a-la
3545
// `expr_xxx`.
3646
pub fn ty(text: &str) -> ast::Type {
37-
ast_from_text(&format!("fn f() -> {} {{}}", text))
47+
ty_from_text(text)
3848
}
3949
pub fn ty_unit() -> ast::Type {
40-
ty("()")
50+
ty_from_text("()")
51+
}
52+
pub fn ty_bool() -> ast::Type {
53+
ty_path(path_unqualified(path_segment(name_ref("bool"))))
4154
}
4255
pub fn ty_tuple(types: impl IntoIterator<Item = ast::Type>) -> ast::Type {
4356
let mut count: usize = 0;
@@ -46,15 +59,21 @@ pub fn ty_tuple(types: impl IntoIterator<Item = ast::Type>) -> ast::Type {
4659
contents.push(',');
4760
}
4861

49-
ty(&format!("({})", contents))
62+
ty_from_text(&format!("({})", contents))
5063
}
5164
// FIXME: handle path to type
5265
pub fn ty_generic(name: ast::NameRef, types: impl IntoIterator<Item = ast::Type>) -> ast::Type {
5366
let contents = types.into_iter().join(", ");
54-
ty(&format!("{}<{}>", name, contents))
67+
ty_from_text(&format!("{}<{}>", name, contents))
5568
}
5669
pub fn ty_ref(target: ast::Type, exclusive: bool) -> ast::Type {
57-
ty(&if exclusive { format!("&mut {}", target) } else { format!("&{}", target) })
70+
ty_from_text(&if exclusive { format!("&mut {}", target) } else { format!("&{}", target) })
71+
}
72+
pub fn ty_path(path: ast::Path) -> ast::Type {
73+
ty_from_text(&path.to_string())
74+
}
75+
fn ty_from_text(text: &str) -> ast::Type {
76+
ast_from_text(&format!("type _T = {};", text))
5877
}
5978

6079
pub fn assoc_item_list() -> ast::AssocItemList {
@@ -475,15 +494,19 @@ pub fn param_list(
475494
};
476495
ast_from_text(&list)
477496
}
478-
// FIXME: s/&str/ast:Name
479-
pub fn generic_param(name: &str, ty: Option<ast::TypeBoundList>) -> ast::GenericParam {
497+
498+
pub fn type_param(name: ast::Name, ty: Option<ast::TypeBoundList>) -> ast::TypeParam {
480499
let bound = match ty {
481500
Some(it) => format!(": {}", it),
482501
None => String::new(),
483502
};
484503
ast_from_text(&format!("fn f<{}{}>() {{ }}", name, bound))
485504
}
486505

506+
pub fn lifetime_param(lifetime: ast::Lifetime) -> ast::LifetimeParam {
507+
ast_from_text(&format!("fn f<{}>() {{ }}", lifetime))
508+
}
509+
487510
pub fn generic_param_list(
488511
pats: impl IntoIterator<Item = ast::GenericParam>,
489512
) -> ast::GenericParamList {

0 commit comments

Comments
 (0)