Add gen modifier to functions

We don't yet lower or maybe even parse them, but blocks already have `gen`, so why not.
This commit is contained in:
Chayim Refael Friedman 2024-08-22 14:27:35 +03:00
parent 506b9663bf
commit cc07652be5
9 changed files with 38 additions and 4 deletions

View file

@ -1617,6 +1617,7 @@ fn format_function(
fun.control_flow.is_async,
fun.mods.is_const,
fun.control_flow.is_unsafe,
false,
)
}

View file

@ -122,6 +122,7 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
let is_async = method_source.async_token().is_some();
let is_const = method_source.const_token().is_some();
let is_unsafe = method_source.unsafe_token().is_some();
let is_gen = method_source.gen_token().is_some();
let fn_name = make::name(&name);
@ -154,6 +155,7 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
is_async,
is_const,
is_unsafe,
is_gen,
)
.clone_for_update();

View file

@ -740,6 +740,7 @@ fn func_assoc_item(
item.async_token().is_some(),
item.const_token().is_some(),
item.unsafe_token().is_some(),
item.gen_token().is_some(),
)
.clone_for_update();

View file

@ -365,6 +365,7 @@ impl FunctionBuilder {
self.is_async,
false, // FIXME : const and unsafe are not handled yet.
false,
false,
)
.clone_for_update();

View file

@ -261,7 +261,19 @@ fn generate_getter_from_info(
let ret_type = Some(make::ret_type(ty));
let body = make::block_expr([], Some(body));
make::fn_(strukt.visibility(), fn_name, None, None, params, body, ret_type, false, false, false)
make::fn_(
strukt.visibility(),
fn_name,
None,
None,
params,
body,
ret_type,
false,
false,
false,
false,
)
}
fn generate_setter_from_info(info: &AssistInfo, record_field_info: &RecordFieldInfo) -> ast::Fn {
@ -285,7 +297,19 @@ fn generate_setter_from_info(info: &AssistInfo, record_field_info: &RecordFieldI
let body = make::block_expr([assign_stmt.into()], None);
// Make the setter fn
make::fn_(strukt.visibility(), fn_name, None, None, params, body, None, false, false, false)
make::fn_(
strukt.visibility(),
fn_name,
None,
None,
params,
body,
None,
false,
false,
false,
false,
)
}
fn extract_and_parse(

View file

@ -115,6 +115,7 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
false,
false,
false,
false,
)
.clone_for_update();
fn_.indent(1.into());

View file

@ -186,7 +186,7 @@ UseTreeList =
Fn =
Attr* Visibility?
'default'? 'const'? 'async'? 'unsafe'? Abi?
'default'? 'const'? 'async'? 'gen'? 'unsafe'? Abi?
'fn' Name GenericParamList? ParamList RetType? WhereClause?
(body:BlockExpr | ';')

View file

@ -484,6 +484,8 @@ impl Fn {
#[inline]
pub fn fn_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![fn]) }
#[inline]
pub fn gen_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![gen]) }
#[inline]
pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) }
}

View file

@ -1035,6 +1035,7 @@ pub fn fn_(
is_async: bool,
is_const: bool,
is_unsafe: bool,
is_gen: bool,
) -> ast::Fn {
let type_params = match type_params {
Some(type_params) => format!("{type_params}"),
@ -1056,9 +1057,10 @@ pub fn fn_(
let async_literal = if is_async { "async " } else { "" };
let const_literal = if is_const { "const " } else { "" };
let unsafe_literal = if is_unsafe { "unsafe " } else { "" };
let gen_literal = if is_gen { "gen " } else { "" };
ast_from_text(&format!(
"{visibility}{async_literal}{const_literal}{unsafe_literal}fn {fn_name}{type_params}{params} {ret_type}{where_clause}{body}",
"{visibility}{const_literal}{async_literal}{gen_literal}{unsafe_literal}fn {fn_name}{type_params}{params} {ret_type}{where_clause}{body}",
))
}
pub fn struct_(