Skip to content

Commit b53ff21

Browse files
bors[bot]matklad
andauthored
Merge #3374
3374: More orthogonal API for building paths r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 7cf710c + ca713e4 commit b53ff21

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

crates/ra_assists/src/handlers/early_return.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,19 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option<Assist> {
112112
Some((path, bound_ident)) => {
113113
// If-let.
114114
let match_expr = {
115-
let happy_arm = make::match_arm(
116-
once(
117-
make::tuple_struct_pat(
118-
path,
119-
once(make::bind_pat(make::name("it")).into()),
120-
)
121-
.into(),
122-
),
123-
make::expr_path(make::path_from_name_ref(make::name_ref("it"))),
124-
);
115+
let happy_arm = {
116+
let pat = make::tuple_struct_pat(
117+
path,
118+
once(make::bind_pat(make::name("it")).into()),
119+
);
120+
let expr = {
121+
let name_ref = make::name_ref("it");
122+
let segment = make::path_segment(name_ref);
123+
let path = make::path_unqalified(segment);
124+
make::expr_path(path)
125+
};
126+
make::match_arm(once(pat.into()), expr)
127+
};
125128

126129
let sad_arm = make::match_arm(
127130
// FIXME: would be cool to use `None` or `Err(_)` if appropriate

crates/ra_assists/src/handlers/move_bounds.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ pub(crate) fn move_bounds_to_where_clause(ctx: AssistCtx) -> Option<Assist> {
7272
}
7373

7474
fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> {
75-
let path = make::path_from_name_ref(make::name_ref(&param.name()?.syntax().to_string()));
75+
let path = {
76+
let name_ref = make::name_ref(&param.name()?.syntax().to_string());
77+
let segment = make::path_segment(name_ref);
78+
make::path_unqalified(segment)
79+
};
7680
let predicate = make::where_pred(path, param.type_bound_list()?.bounds());
7781
Some(predicate)
7882
}

crates/ra_syntax/src/ast/make.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ pub fn name_ref(text: &str) -> ast::NameRef {
1212
ast_from_text(&format!("fn f() {{ {}; }}", text))
1313
}
1414

15-
pub fn path_from_name_ref(name_ref: ast::NameRef) -> ast::Path {
16-
path_from_text(&name_ref.syntax().to_string())
15+
pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment {
16+
ast_from_text(&format!("use {};", name_ref.syntax()))
1717
}
18-
pub fn path_qualified(qual: ast::Path, name_ref: ast::NameRef) -> ast::Path {
19-
path_from_text(&format!("{}::{}", qual.syntax(), name_ref.syntax()))
18+
pub fn path_unqalified(segment: ast::PathSegment) -> ast::Path {
19+
path_from_text(&format!("use {}", segment.syntax()))
20+
}
21+
pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path {
22+
path_from_text(&format!("{}::{}", qual.syntax(), segment.syntax()))
2023
}
2124
fn path_from_text(text: &str) -> ast::Path {
2225
ast_from_text(text)

0 commit comments

Comments
 (0)