Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 4 pull requests #132595

Closed
wants to merge 50 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
2dfbcb6
docs: create setup documentation
davidkurilla Oct 15, 2024
be7865e
style: fix typos
davidkurilla Oct 15, 2024
32b05c1
docs: remove unnecessary prerequisites setup.md
davidkurilla Oct 22, 2024
3bb20dd
fix: auto-complete import for aliased function and module
dqkqd Oct 23, 2024
af3f990
Add assist to generate a type alias for a function
Giga-Bowser Oct 23, 2024
0ac5c8a
Fix alphabetical order of handlers
Giga-Bowser Oct 23, 2024
75a659e
refactor: separate function for getting import name
dqkqd Oct 24, 2024
1b699b5
fix: handle aliased pattern and simplify testcase
dqkqd Oct 24, 2024
a00b4c2
Add `ty_fn_ptr` function to create function pointer type
Giga-Bowser Oct 24, 2024
d0de3fa
Rework `generate_fn_type_alias`
Giga-Bowser Oct 24, 2024
d7615a9
Cleanup TypeRef lowering
ChayimFriedman2 Oct 28, 2024
a1eb6b3
Merge pull request #18427 from ChayimFriedman2/cleanup-ty
Veykril Oct 29, 2024
1407065
fix: Fix the server not honoring diagnostic refresh support
Veykril Oct 29, 2024
c21de28
Merge pull request #18432 from Veykril/lw-yptzmuxyvxxt
Veykril Oct 29, 2024
d8f6538
Do not render meta info when hovering usages
Veykril Oct 29, 2024
711961d
Publish test-fixture => ra_ap_test_fixture
sam-mccall Oct 29, 2024
8a51cbf
Style hover messages a bit differently
Veykril Oct 29, 2024
3ab7a69
Merge pull request #18436 from Veykril/lw-yvkqwpnwsouo
Veykril Oct 30, 2024
5fe280e
Merge pull request #18385 from Giga-Bowser/master
Veykril Oct 30, 2024
482e63d
Merge pull request #18382 from dqkqd/issue-17042
Veykril Oct 30, 2024
cee3257
Merge pull request #18304 from davidkurilla/docs-create-setup-document
Veykril Oct 30, 2024
6a6b61a
Only parse `safe` as contextual kw in extern blocks
ChayimFriedman2 Oct 30, 2024
2d5b758
Merge pull request #18446 from ChayimFriedman2/safe-statement
Veykril Oct 30, 2024
5aff110
Avoid interior mutability in `TyLoweringContext`
ChayimFriedman2 Oct 30, 2024
7ca05b5
Merge pull request #18437 from sam-mccall/publish_fixture
Veykril Oct 31, 2024
bc862b8
Move child_by_source from hir-def to hir
Veykril Oct 31, 2024
2a9b851
Merge pull request #18448 from Veykril/push-zwuuoytnmnqz
Veykril Oct 31, 2024
b25a3de
Merge pull request #18447 from ChayimFriedman2/cleanup-tylowerctx
Veykril Oct 31, 2024
f0bf75d
Parse patterns with leading pipe properly in all places
ChayimFriedman2 Oct 31, 2024
a5b5b46
Merge pull request #18453 from ChayimFriedman2/leading-or
Veykril Oct 31, 2024
21544d8
Preparing for merge from rust-lang/rust
lnicola Nov 1, 2024
d94c49c
Merge from rust-lang/rust
lnicola Nov 1, 2024
7303227
Bump rustc crates
lnicola Nov 1, 2024
9acf57c
Format code
lnicola Nov 1, 2024
cf6bc48
Merge pull request #18457 from lnicola/sync-from-rust
lnicola Nov 1, 2024
7591eb6
Fix compiler panic with a large number of threads
practicalrs Oct 30, 2024
c10fe34
No need to instantiate binder in confirm_async_closure_candidate
compiler-errors Nov 2, 2024
b431237
Allow static initializers to be const evaluated
Veykril Nov 2, 2024
f5e1dce
Improve error message for too new proc-macro server
benluiwj Nov 2, 2024
db29fd3
Merge pull request #18463 from benluiwj/fix/proc-macro-server-msg-imp…
lnicola Nov 2, 2024
72d9929
Merge pull request #18469 from Veykril/push-zwnywqmvtuts
Veykril Nov 3, 2024
56e89bc
Allow interpreting consts and statics with interpret function command
Veykril Nov 3, 2024
43c20c2
Merge pull request #18470 from Veykril/push-rxmtkvpulotn
Veykril Nov 3, 2024
9f4e719
Fix feature name casing
lnicola Nov 4, 2024
0cd3fb2
Merge pull request #18472 from lnicola/reinterpret-function-casing
lnicola Nov 4, 2024
7faa84e
Stabilise 'const_char_encode_utf16';
bjoernager Nov 4, 2024
2c94cb0
Rollup merge of #132153 - bjoernager:const-char-encode-utf16, r=dtolnay
matthiaskrgr Nov 4, 2024
5fad940
Rollup merge of #132355 - practicalrs:fix_117638, r=SparrowLii
matthiaskrgr Nov 4, 2024
ab693d6
Rollup merge of #132486 - compiler-errors:no-binder, r=lcnr
matthiaskrgr Nov 4, 2024
5e494c4
Rollup merge of #132594 - lnicola:sync-from-ra, r=lnicola
matthiaskrgr Nov 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rework generate_fn_type_alias
Giga-Bowser committed Oct 24, 2024
commit d0de3fa7ca728b41db8aaf3735561895aaeeae5c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use either::Either;
use hir::HirDisplay;
use ide_db::assists::{AssistId, AssistKind, GroupLabel};
use syntax::{
ast::{self, edit::IndentLevel, make, HasGenericParams, HasName},
@@ -39,23 +38,16 @@ use crate::{AssistContext, Assists};
pub(crate) fn generate_fn_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
let name = ctx.find_node_at_offset::<ast::Name>()?;
let func = &name.syntax().parent()?;
let item = func.ancestors().find_map(ast::Item::cast)?;
let assoc_owner =
item.syntax().ancestors().nth(2).and_then(Either::<ast::Trait, ast::Impl>::cast);
let node = assoc_owner.as_ref().map_or_else(
|| item.syntax(),
|impl_| impl_.as_ref().either(AstNode::syntax, AstNode::syntax),
);
let func_node = ast::Fn::cast(func.clone())?;
let param_list = func_node.param_list()?;

for style in ParamStyle::ALL {
let generic_params = func_node.generic_param_list();
let module = match ctx.sema.scope(node) {
Some(scope) => scope.module(),
None => continue,
};
let assoc_owner = func.ancestors().nth(2).and_then(Either::<ast::Trait, ast::Impl>::cast);
// This is where we'll insert the type alias, since type aliases in `impl`s or `trait`s are not supported
let insertion_node = assoc_owner
.as_ref()
.map_or_else(|| func, |impl_| impl_.as_ref().either(AstNode::syntax, AstNode::syntax));

for style in ParamStyle::ALL {
acc.add_group(
&GroupLabel("Generate a type alias for function...".into()),
style.assist_id(),
@@ -66,51 +58,53 @@ pub(crate) fn generate_fn_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>)

let alias_name = format!("{}Fn", stdx::to_camel_case(&name.to_string()));

let fn_abi = match func_node.abi() {
Some(abi) => format!("{} ", abi),
None => "".into(),
};

let fn_unsafe = if func_node.unsafe_token().is_some() { "unsafe " } else { "" };
let mut fn_params_vec = Vec::new();

let fn_qualifiers = format!("{fn_unsafe}{fn_abi}");
if let Some(self_ty) =
param_list.self_param().and_then(|p| ctx.sema.type_of_self(&p))
{
let is_ref = self_ty.is_reference();
let is_mut = self_ty.is_mutable_reference();

let fn_type = return_type(&func_node);
if let Some(adt) = self_ty.strip_references().as_adt() {
let inner_type = make::ty(adt.name(ctx.db()).as_str());

let mut fn_params_vec = Vec::new();
let ast_self_ty =
if is_ref { make::ty_ref(inner_type, is_mut) } else { inner_type };

if let Some(self_param) = param_list.self_param() {
if let Some(local) = ctx.sema.to_def(&self_param) {
let ty = local.ty(ctx.db());
if let Ok(s) = ty.display_source_code(ctx.db(), module.into(), false) {
fn_params_vec.push(s)
}
fn_params_vec.push(make::unnamed_param(ast_self_ty));
}
}

match style {
ParamStyle::Named => {
fn_params_vec.extend(param_list.params().map(|p| p.to_string()))
}
ParamStyle::Unnamed => fn_params_vec.extend(
param_list.params().filter_map(|p| p.ty()).map(|ty| ty.to_string()),
),
};
fn_params_vec.extend(param_list.params().filter_map(|p| match style {
ParamStyle::Named => Some(p),
ParamStyle::Unnamed => p.ty().map(make::unnamed_param),
}));

let fn_params = fn_params_vec.join(", ");
let generic_params = func_node.generic_param_list();

// FIXME: sometime in the far future when we have `make::ty_func`, we should use that
let ty = make::ty(&format!("{fn_qualifiers}fn({fn_params}){fn_type}"))
.clone_for_update();
let is_unsafe = func_node.unsafe_token().is_some();
let ty = make::ty_fn_ptr(
None,
is_unsafe,
func_node.abi(),
fn_params_vec.into_iter(),
func_node.ret_type(),
);

// Insert new alias
let ty_alias =
make::ty_alias(&alias_name, generic_params, None, None, Some((ty, None)))
.clone_for_update();

let indent = IndentLevel::from_node(node);
let ty_alias = make::ty_alias(
&alias_name,
generic_params,
None,
None,
Some((ast::Type::FnPtrType(ty), None)),
)
.clone_for_update();

let indent = IndentLevel::from_node(insertion_node);
edit.insert_all(
syntax_editor::Position::before(node),
syntax_editor::Position::before(insertion_node),
vec![
ty_alias.syntax().clone().into(),
make::tokens::whitespace(&format!("\n\n{indent}")).into(),
@@ -156,12 +150,6 @@ impl ParamStyle {
}
}

fn return_type(func: &ast::Fn) -> String {
func.ret_type()
.and_then(|ret_type| ret_type.ty())
.map_or("".into(), |ty| format!(" -> {} ", ty))
}

#[cfg(test)]
mod tests {
use crate::tests::check_assist_by_label;
@@ -233,7 +221,7 @@ extern "FooABI" fn foo(param: u32) -> i32 { return 42; }
}

#[test]
fn generate_fn_alias_unnamed_unnamed_unsafe_extern_abi() {
fn generate_fn_alias_unnamed_unsafe_extern_abi() {
check_assist_by_label(
generate_fn_type_alias,
r#"
@@ -369,7 +357,7 @@ extern "FooABI" fn foo(param: u32) -> i32 { return 42; }
}

#[test]
fn generate_fn_alias_named_named_unsafe_extern_abi() {
fn generate_fn_alias_named_unsafe_extern_abi() {
check_assist_by_label(
generate_fn_type_alias,
r#"