mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Use ast::make API in add_function assist
This commit is contained in:
parent
e5fc42cbc1
commit
10667753c7
2 changed files with 67 additions and 61 deletions
|
@ -269,6 +269,31 @@ pub fn unreachable_macro_call() -> ast::MacroCall {
|
|||
ast_from_text(&format!("unreachable!()"))
|
||||
}
|
||||
|
||||
pub fn param(name: String, ty: String) -> ast::Param {
|
||||
ast_from_text(&format!("fn f({}: {}) {{ }}", name, ty))
|
||||
}
|
||||
|
||||
pub fn param_list(pats: impl IntoIterator<Item = ast::Param>) -> ast::ParamList {
|
||||
let args = pats.into_iter().join(", ");
|
||||
ast_from_text(&format!("fn f({}) {{ }}", args))
|
||||
}
|
||||
|
||||
pub fn fn_def(
|
||||
fn_name: ast::Name,
|
||||
type_params: Option<ast::TypeParamList>,
|
||||
params: ast::ParamList,
|
||||
body: ast::BlockExpr,
|
||||
) -> ast::FnDef {
|
||||
let type_params =
|
||||
if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() };
|
||||
ast_from_text(&format!("fn {}{}{} {}", fn_name, type_params, params, body))
|
||||
}
|
||||
|
||||
pub fn add_newlines(amount_of_newlines: usize, t: impl AstNode) -> ast::SourceFile {
|
||||
let newlines = "\n".repeat(amount_of_newlines);
|
||||
ast_from_text(&format!("{}{}", newlines, t.syntax()))
|
||||
}
|
||||
|
||||
fn ast_from_text<N: AstNode>(text: &str) -> N {
|
||||
let parse = SourceFile::parse(text);
|
||||
let node = parse.tree().syntax().descendants().find_map(N::cast).unwrap();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue