mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 21:13:37 +00:00
add assist to move type bounds to where clause
This commit is contained in:
parent
36d7b75c95
commit
acb89d2be1
4 changed files with 177 additions and 0 deletions
|
@ -297,6 +297,11 @@ impl AstBuilder<ast::Path> {
|
||||||
ast_node_from_file_text(text)
|
ast_node_from_file_text(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn from_name(name: ast::Name) -> ast::Path {
|
||||||
|
let name = name.syntax().to_string();
|
||||||
|
Self::from_text(name.as_str())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn from_pieces(enum_name: ast::Name, var_name: ast::Name) -> ast::Path {
|
pub fn from_pieces(enum_name: ast::Name, var_name: ast::Name) -> ast::Path {
|
||||||
Self::from_text(&format!("{}::{}", enum_name.syntax(), var_name.syntax()))
|
Self::from_text(&format!("{}::{}", enum_name.syntax(), var_name.syntax()))
|
||||||
}
|
}
|
||||||
|
@ -380,6 +385,31 @@ impl AstBuilder<ast::MatchArmList> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AstBuilder<ast::WherePred> {
|
||||||
|
fn from_text(text: &str) -> ast::WherePred {
|
||||||
|
ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_pieces(
|
||||||
|
path: ast::Path,
|
||||||
|
bounds: impl Iterator<Item = ast::TypeBound>,
|
||||||
|
) -> ast::WherePred {
|
||||||
|
let bounds = bounds.map(|b| b.syntax().to_string()).collect::<Vec<_>>().join(" + ");
|
||||||
|
Self::from_text(&format!("{}: {}", path.syntax(), bounds))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AstBuilder<ast::WhereClause> {
|
||||||
|
fn from_text(text: &str) -> ast::WhereClause {
|
||||||
|
ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_predicates(preds: impl Iterator<Item = ast::WherePred>) -> ast::WhereClause {
|
||||||
|
let preds = preds.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
|
||||||
|
Self::from_text(preds.as_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn ast_node_from_file_text<N: AstNode>(text: &str) -> N {
|
fn ast_node_from_file_text<N: AstNode>(text: &str) -> N {
|
||||||
let parse = SourceFile::parse(text);
|
let parse = SourceFile::parse(text);
|
||||||
let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap();
|
let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap();
|
||||||
|
|
|
@ -102,6 +102,7 @@ mod remove_dbg;
|
||||||
pub mod auto_import;
|
pub mod auto_import;
|
||||||
mod add_missing_impl_members;
|
mod add_missing_impl_members;
|
||||||
mod move_guard;
|
mod move_guard;
|
||||||
|
mod move_bounds;
|
||||||
|
|
||||||
fn all_assists<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assist>] {
|
fn all_assists<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assist>] {
|
||||||
&[
|
&[
|
||||||
|
@ -123,6 +124,7 @@ fn all_assists<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assis
|
||||||
inline_local_variable::inline_local_varialbe,
|
inline_local_variable::inline_local_varialbe,
|
||||||
move_guard::move_guard_to_arm_body,
|
move_guard::move_guard_to_arm_body,
|
||||||
move_guard::move_arm_cond_to_match_guard,
|
move_guard::move_arm_cond_to_match_guard,
|
||||||
|
move_bounds::move_bounds_to_where_clause,
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
135
crates/ra_assists/src/move_bounds.rs
Normal file
135
crates/ra_assists/src/move_bounds.rs
Normal file
|
@ -0,0 +1,135 @@
|
||||||
|
use hir::db::HirDatabase;
|
||||||
|
use ra_syntax::{
|
||||||
|
ast::{self, AstNode, NameOwner, TypeBoundsOwner},
|
||||||
|
SyntaxElement,
|
||||||
|
SyntaxKind::*,
|
||||||
|
TextRange,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{ast_editor::AstBuilder, Assist, AssistCtx, AssistId};
|
||||||
|
|
||||||
|
pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||||
|
let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?;
|
||||||
|
|
||||||
|
let mut type_params = type_param_list.type_params();
|
||||||
|
if type_params.all(|p| p.type_bound_list().is_none()) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let parent = type_param_list.syntax().parent()?;
|
||||||
|
if parent.children_with_tokens().find(|it| it.kind() == WHERE_CLAUSE).is_some() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let anchor: SyntaxElement = match parent.kind() {
|
||||||
|
FN_DEF => ast::FnDef::cast(parent)?.body()?.syntax().clone().into(),
|
||||||
|
TRAIT_DEF => ast::TraitDef::cast(parent)?.item_list()?.syntax().clone().into(),
|
||||||
|
IMPL_BLOCK => ast::ImplBlock::cast(parent)?.item_list()?.syntax().clone().into(),
|
||||||
|
ENUM_DEF => ast::EnumDef::cast(parent)?.variant_list()?.syntax().clone().into(),
|
||||||
|
STRUCT_DEF => parent
|
||||||
|
.children_with_tokens()
|
||||||
|
.find(|it| it.kind() == RECORD_FIELD_DEF_LIST || it.kind() == SEMI)?,
|
||||||
|
_ => return None,
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.add_action(
|
||||||
|
AssistId("move_bounds_to_where_clause"),
|
||||||
|
"move_bounds_to_where_clause",
|
||||||
|
|edit| {
|
||||||
|
let type_params = type_param_list.type_params().collect::<Vec<_>>();
|
||||||
|
|
||||||
|
for param in &type_params {
|
||||||
|
if let Some(bounds) = param.type_bound_list() {
|
||||||
|
let colon = param
|
||||||
|
.syntax()
|
||||||
|
.children_with_tokens()
|
||||||
|
.find(|it| it.kind() == COLON)
|
||||||
|
.unwrap();
|
||||||
|
let start = colon.text_range().start();
|
||||||
|
let end = bounds.syntax().text_range().end();
|
||||||
|
edit.delete(TextRange::from_to(start, end));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let predicates = type_params.iter().filter_map(build_predicate);
|
||||||
|
let where_clause = AstBuilder::<ast::WhereClause>::from_predicates(predicates);
|
||||||
|
|
||||||
|
let to_insert = match anchor.prev_sibling_or_token() {
|
||||||
|
Some(ref elem) if elem.kind() == WHITESPACE => {
|
||||||
|
format!("{} ", where_clause.syntax())
|
||||||
|
}
|
||||||
|
_ => format!(" {}", where_clause.syntax()),
|
||||||
|
};
|
||||||
|
edit.insert(anchor.text_range().start(), to_insert);
|
||||||
|
edit.target(type_param_list.syntax().text_range());
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
ctx.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_predicate(param: &ast::TypeParam) -> Option<ast::WherePred> {
|
||||||
|
let path = AstBuilder::<ast::Path>::from_name(param.name()?);
|
||||||
|
let predicate =
|
||||||
|
AstBuilder::<ast::WherePred>::from_pieces(path, param.type_bound_list()?.bounds());
|
||||||
|
Some(predicate)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use crate::helpers::check_assist;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn move_bounds_to_where_clause_fn() {
|
||||||
|
check_assist(
|
||||||
|
move_bounds_to_where_clause,
|
||||||
|
r#"
|
||||||
|
fn foo<T: u32, <|>F: FnOnce(T) -> T>() {}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
fn foo<T, <|>F>() where T: u32, F: FnOnce(T) -> T {}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn move_bounds_to_where_clause_impl() {
|
||||||
|
check_assist(
|
||||||
|
move_bounds_to_where_clause,
|
||||||
|
r#"
|
||||||
|
impl<U: u32, <|>T> A<U, T> {}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
impl<U, <|>T> A<U, T> where U: u32 {}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn move_bounds_to_where_clause_struct() {
|
||||||
|
check_assist(
|
||||||
|
move_bounds_to_where_clause,
|
||||||
|
r#"
|
||||||
|
struct A<<|>T: Iterator<Item = u32>> {}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
struct A<<|>T> where T: Iterator<Item = u32> {}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn move_bounds_to_where_clause_tuple_struct() {
|
||||||
|
check_assist(
|
||||||
|
move_bounds_to_where_clause,
|
||||||
|
r#"
|
||||||
|
struct Pair<<|>T: u32>(T, T);
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
struct Pair<<|>T>(T, T) where T: u32;
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -435,6 +435,16 @@ fn f() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
- Move type bounds to where clause
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// before:
|
||||||
|
fn foo<T: u32, F: FnOnce(T) -> T>() {}
|
||||||
|
|
||||||
|
// after:
|
||||||
|
fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}
|
||||||
|
```
|
||||||
|
|
||||||
### Magic Completions
|
### Magic Completions
|
||||||
|
|
||||||
In addition to usual reference completion, rust-analyzer provides some ✨magic✨
|
In addition to usual reference completion, rust-analyzer provides some ✨magic✨
|
||||||
|
|
Loading…
Reference in a new issue