Add signature help for record struct patterns
This commit is contained in:
parent
510e4b4029
commit
03a6ab0b62
@ -411,7 +411,7 @@ pub fn resolve_record_field(
|
||||
self.imp.resolve_record_field(field)
|
||||
}
|
||||
|
||||
pub fn resolve_record_pat_field(&self, field: &ast::RecordPatField) -> Option<Field> {
|
||||
pub fn resolve_record_pat_field(&self, field: &ast::RecordPatField) -> Option<(Field, Type)> {
|
||||
self.imp.resolve_record_pat_field(field)
|
||||
}
|
||||
|
||||
@ -1201,7 +1201,7 @@ fn resolve_record_field(
|
||||
self.analyze(field.syntax())?.resolve_record_field(self.db, field)
|
||||
}
|
||||
|
||||
fn resolve_record_pat_field(&self, field: &ast::RecordPatField) -> Option<Field> {
|
||||
fn resolve_record_pat_field(&self, field: &ast::RecordPatField) -> Option<(Field, Type)> {
|
||||
self.analyze(field.syntax())?.resolve_record_pat_field(self.db, field)
|
||||
}
|
||||
|
||||
|
@ -441,14 +441,17 @@ pub(crate) fn resolve_record_pat_field(
|
||||
&self,
|
||||
db: &dyn HirDatabase,
|
||||
field: &ast::RecordPatField,
|
||||
) -> Option<Field> {
|
||||
) -> Option<(Field, Type)> {
|
||||
let field_name = field.field_name()?.as_name();
|
||||
let record_pat = ast::RecordPat::cast(field.syntax().parent().and_then(|p| p.parent())?)?;
|
||||
let pat_id = self.pat_id(&record_pat.into())?;
|
||||
let variant = self.infer.as_ref()?.variant_resolution_for_pat(pat_id)?;
|
||||
let variant_data = variant.variant_data(db.upcast());
|
||||
let field = FieldId { parent: variant, local_id: variant_data.field(&field_name)? };
|
||||
Some(field.into())
|
||||
let (_, subst) = self.infer.as_ref()?.type_of_pat.get(pat_id)?.as_adt()?;
|
||||
let field_ty =
|
||||
db.field_types(variant).get(field.local_id)?.clone().substitute(Interner, subst);
|
||||
Some((field.into(), Type::new_with_resolver(db, &self.resolver, field_ty)))
|
||||
}
|
||||
|
||||
pub(crate) fn resolve_macro_call(
|
||||
|
@ -327,7 +327,7 @@ fn classify_ident_pat(
|
||||
let pat_parent = ident_pat.syntax().parent();
|
||||
if let Some(record_pat_field) = pat_parent.and_then(ast::RecordPatField::cast) {
|
||||
if record_pat_field.name_ref().is_none() {
|
||||
if let Some(field) = sema.resolve_record_pat_field(&record_pat_field) {
|
||||
if let Some((field, _)) = sema.resolve_record_pat_field(&record_pat_field) {
|
||||
return Some(NameClass::PatFieldShorthand {
|
||||
local_def: local,
|
||||
field_ref: field,
|
||||
@ -483,6 +483,13 @@ pub fn classify(
|
||||
},
|
||||
ast::RecordPatField(record_pat_field) => {
|
||||
sema.resolve_record_pat_field(&record_pat_field)
|
||||
.map(|(field, ..)|field)
|
||||
.map(Definition::Field)
|
||||
.map(NameRefClass::Definition)
|
||||
},
|
||||
ast::RecordExprField(record_expr_field) => {
|
||||
sema.resolve_record_field(&record_expr_field)
|
||||
.map(|(field, ..)|field)
|
||||
.map(Definition::Field)
|
||||
.map(NameRefClass::Definition)
|
||||
},
|
||||
|
@ -55,7 +55,7 @@ pub(crate) fn goto_type_definition(
|
||||
ty
|
||||
} else {
|
||||
let record_field = ast::RecordPatField::for_field_name_ref(&it)?;
|
||||
sema.resolve_record_pat_field(&record_field)?.ty(db)
|
||||
sema.resolve_record_pat_field(&record_field)?.1
|
||||
}
|
||||
},
|
||||
_ => return None,
|
||||
|
@ -16,7 +16,7 @@
|
||||
use syntax::{
|
||||
algo,
|
||||
ast::{self, HasArgList},
|
||||
match_ast, AstNode, Direction, SyntaxToken, TextRange, TextSize,
|
||||
match_ast, AstNode, Direction, SyntaxElementChildren, SyntaxToken, TextRange, TextSize,
|
||||
};
|
||||
|
||||
use crate::RootDatabase;
|
||||
@ -102,6 +102,14 @@ pub(crate) fn signature_help(db: &RootDatabase, position: FilePosition) -> Optio
|
||||
}
|
||||
return signature_help_for_record_lit(&sema, record, token);
|
||||
},
|
||||
ast::RecordPat(record) => {
|
||||
let cursor_outside = record.record_pat_field_list().and_then(|list| list.r_curly_token()).as_ref() == Some(&token);
|
||||
if cursor_outside {
|
||||
continue;
|
||||
}
|
||||
return signature_help_for_record_pat(&sema, record, token);
|
||||
},
|
||||
ast::TupleStructPat(tuple_pat) => {},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
@ -346,10 +354,27 @@ fn signature_help_for_record_lit(
|
||||
record: ast::RecordExpr,
|
||||
token: SyntaxToken,
|
||||
) -> Option<SignatureHelp> {
|
||||
let active_parameter = record
|
||||
signature_help_for_record_(
|
||||
sema,
|
||||
record.record_expr_field_list()?.syntax().children_with_tokens(),
|
||||
&record.path()?,
|
||||
record
|
||||
.record_expr_field_list()?
|
||||
.syntax()
|
||||
.children_with_tokens()
|
||||
.fields()
|
||||
.filter_map(|field| sema.resolve_record_field(&field))
|
||||
.map(|(field, _, ty)| (field, ty)),
|
||||
token,
|
||||
)
|
||||
}
|
||||
|
||||
fn signature_help_for_record_(
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
field_list_children: SyntaxElementChildren,
|
||||
path: &ast::Path,
|
||||
fields2: impl Iterator<Item = (hir::Field, hir::Type)>,
|
||||
token: SyntaxToken,
|
||||
) -> Option<SignatureHelp> {
|
||||
let active_parameter = field_list_children
|
||||
.filter_map(syntax::NodeOrToken::into_token)
|
||||
.filter(|t| t.kind() == syntax::T![,])
|
||||
.take_while(|t| t.text_range().start() <= token.text_range().start())
|
||||
@ -365,7 +390,7 @@ fn signature_help_for_record_lit(
|
||||
let fields;
|
||||
|
||||
let db = sema.db;
|
||||
let path_res = sema.resolve_path(&record.path()?)?;
|
||||
let path_res = sema.resolve_path(path)?;
|
||||
if let PathResolution::Def(ModuleDef::Variant(variant)) = path_res {
|
||||
fields = variant.fields(db);
|
||||
let en = variant.parent_enum(db);
|
||||
@ -397,8 +422,7 @@ fn signature_help_for_record_lit(
|
||||
let mut fields =
|
||||
fields.into_iter().map(|field| (field.name(db), Some(field))).collect::<FxIndexMap<_, _>>();
|
||||
let mut buf = String::new();
|
||||
for field in record.record_expr_field_list()?.fields() {
|
||||
let Some((field, _, ty)) = sema.resolve_record_field(&field) else { continue };
|
||||
for (field, ty) in fields2 {
|
||||
let name = field.name(db);
|
||||
format_to!(buf, "{name}: {}", ty.display_truncated(db, Some(20)));
|
||||
res.push_record_field(&buf);
|
||||
@ -418,6 +442,23 @@ fn signature_help_for_record_lit(
|
||||
Some(res)
|
||||
}
|
||||
|
||||
fn signature_help_for_record_pat(
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
record: ast::RecordPat,
|
||||
token: SyntaxToken,
|
||||
) -> Option<SignatureHelp> {
|
||||
signature_help_for_record_(
|
||||
sema,
|
||||
record.record_pat_field_list()?.syntax().children_with_tokens(),
|
||||
&record.path()?,
|
||||
record
|
||||
.record_pat_field_list()?
|
||||
.fields()
|
||||
.filter_map(|field| sema.resolve_record_pat_field(&field)),
|
||||
token,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::iter;
|
||||
@ -1550,6 +1591,29 @@ struct S { t: u8 }
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn record_pat() {
|
||||
check(
|
||||
r#"
|
||||
struct Strukt<T, U = ()> {
|
||||
t: T,
|
||||
u: U,
|
||||
unit: (),
|
||||
}
|
||||
fn f() {
|
||||
let Strukt {
|
||||
u: 0,
|
||||
$0
|
||||
}
|
||||
}
|
||||
"#,
|
||||
expect![[r#"
|
||||
struct Strukt { u: i32, t: T, unit: () }
|
||||
------ ^^^^ --------
|
||||
"#]],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_enum_in_nested_method_in_lambda() {
|
||||
check(
|
||||
|
Loading…
Reference in New Issue
Block a user