rough, but appears to work

This commit is contained in:
Jeroen Vannevel 2022-02-15 14:39:22 +00:00
parent f0210f8a43
commit 73e49493bd
No known key found for this signature in database
GPG Key ID: 78EF5F52F38C49BD
9 changed files with 81 additions and 15 deletions

View File

@ -5,7 +5,7 @@ exclude = ["crates/proc_macro_test/imp"]
[profile.dev] [profile.dev]
# Disabling debug info speeds up builds a bunch, # Disabling debug info speeds up builds a bunch,
# and we don't rely on it for debugging that much. # and we don't rely on it for debugging that much.
debug = 0 debug = 2
[profile.dev.package] [profile.dev.package]
# These speed up local tests. # These speed up local tests.

View File

@ -22,7 +22,9 @@ use crate::{
impl HirDisplay for Function { impl HirDisplay for Function {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
println!("Formatting for Function");
let data = f.db.function_data(self.id); let data = f.db.function_data(self.id);
println!("data: {:?}", &data);
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
if data.is_default() { if data.is_default() {
write!(f, "default ")?; write!(f, "default ")?;
@ -461,6 +463,7 @@ impl HirDisplay for Trait {
impl HirDisplay for TypeAlias { impl HirDisplay for TypeAlias {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
println!("Formatting for TypeAlias");
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
let data = f.db.type_alias_data(self.id); let data = f.db.type_alias_data(self.id);
write!(f, "type {}", data.name)?; write!(f, "type {}", data.name)?;
@ -468,6 +471,7 @@ impl HirDisplay for TypeAlias {
write!(f, ": ")?; write!(f, ": ")?;
f.write_joined(&data.bounds, " + ")?; f.write_joined(&data.bounds, " + ")?;
} }
println!("type_ref: {:?}", &data.type_ref);
if let Some(ty) = &data.type_ref { if let Some(ty) = &data.type_ref {
write!(f, " = ")?; write!(f, " = ")?;
ty.hir_fmt(f)?; ty.hir_fmt(f)?;

View File

@ -496,11 +496,11 @@ impl<'a> Printer<'a> {
let (ret, args) = let (ret, args) =
args_and_ret.split_last().expect("TypeRef::Fn is missing return type"); args_and_ret.split_last().expect("TypeRef::Fn is missing return type");
w!(self, "fn("); w!(self, "fn(");
for (i, arg) in args.iter().enumerate() { for (i, (name, typeref)) in args.iter().enumerate() {
if i != 0 { if i != 0 {
w!(self, ", "); w!(self, ", ");
} }
self.print_type_ref(arg); self.print_type_ref(&typeref);
} }
if *varargs { if *varargs {
if !args.is_empty() { if !args.is_empty() {
@ -509,7 +509,7 @@ impl<'a> Printer<'a> {
w!(self, "..."); w!(self, "...");
} }
w!(self, ") -> "); w!(self, ") -> ");
self.print_type_ref(ret); self.print_type_ref(&ret.1);
} }
TypeRef::Macro(_ast_id) => { TypeRef::Macro(_ast_id) => {
w!(self, "<macro>"); w!(self, "<macro>");

View File

@ -3,7 +3,7 @@
use hir_expand::{name::Name, AstId, InFile}; use hir_expand::{name::Name, AstId, InFile};
use std::convert::TryInto; use std::convert::TryInto;
use syntax::ast; use syntax::{ast, AstNode};
use crate::{body::LowerCtx, intern::Interned, path::Path}; use crate::{body::LowerCtx, intern::Interned, path::Path};
@ -89,7 +89,7 @@ pub enum TypeRef {
Array(Box<TypeRef>, ConstScalar), Array(Box<TypeRef>, ConstScalar),
Slice(Box<TypeRef>), Slice(Box<TypeRef>),
/// A fn pointer. Last element of the vector is the return type. /// A fn pointer. Last element of the vector is the return type.
Fn(Vec<TypeRef>, bool /*varargs*/), Fn(Vec<(Option<String>, TypeRef)>, bool /*varargs*/),
// For // For
ImplTrait(Vec<Interned<TypeBound>>), ImplTrait(Vec<Interned<TypeBound>>),
DynTrait(Vec<Interned<TypeBound>>), DynTrait(Vec<Interned<TypeBound>>),
@ -188,11 +188,16 @@ impl TypeRef {
is_varargs = param.dotdotdot_token().is_some(); is_varargs = param.dotdotdot_token().is_some();
} }
pl.params().map(|p| p.ty()).map(|it| TypeRef::from_ast_opt(ctx, it)).collect() pl.params().map(|p| (p.pat(), p.ty())).map(|it| {
println!("{it:?}");
let type_ref = TypeRef::from_ast_opt(ctx, it.1);
let name = it.0.unwrap().syntax().text().to_string();
(Some(name), type_ref)
}).collect()
} else { } else {
Vec::new() Vec::new()
}; };
params.push(ret_ty); params.push((None, ret_ty));
TypeRef::Fn(params, is_varargs) TypeRef::Fn(params, is_varargs)
} }
// for types are close enough for our purposes to the inner type for now... // for types are close enough for our purposes to the inner type for now...
@ -230,7 +235,10 @@ impl TypeRef {
fn go(type_ref: &TypeRef, f: &mut impl FnMut(&TypeRef)) { fn go(type_ref: &TypeRef, f: &mut impl FnMut(&TypeRef)) {
f(type_ref); f(type_ref);
match type_ref { match type_ref {
TypeRef::Fn(types, _) | TypeRef::Tuple(types) => { TypeRef::Fn(types, _) => {
types.iter().for_each(|t| go(&t.1, f))
}
TypeRef::Tuple(types) => {
types.iter().for_each(|t| go(t, f)) types.iter().for_each(|t| go(t, f))
} }
TypeRef::RawPtr(type_ref, _) TypeRef::RawPtr(type_ref, _)

View File

@ -239,6 +239,7 @@ where
T: HirDisplay, T: HirDisplay,
{ {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
println!("formatting..");
match self.t.hir_fmt(&mut HirFormatter { match self.t.hir_fmt(&mut HirFormatter {
db: self.db, db: self.db,
fmt: f, fmt: f,
@ -341,6 +342,9 @@ impl HirDisplay for Ty {
return write!(f, "{}", TYPE_HINT_TRUNCATION); return write!(f, "{}", TYPE_HINT_TRUNCATION);
} }
let interner_kind = self.kind(Interner);
println!("interner kind: {interner_kind:?}");
match self.kind(Interner) { match self.kind(Interner) {
TyKind::Never => write!(f, "!")?, TyKind::Never => write!(f, "!")?,
TyKind::Str => write!(f, "str")?, TyKind::Str => write!(f, "str")?,
@ -1094,15 +1098,27 @@ impl HirDisplay for TypeRef {
inner.hir_fmt(f)?; inner.hir_fmt(f)?;
write!(f, "]")?; write!(f, "]")?;
} }
TypeRef::Fn(tys, is_varargs) => { TypeRef::Fn(parameters, is_varargs) => {
// FIXME: Function pointer qualifiers.
write!(f, "fn(")?; write!(f, "fn(")?;
f.write_joined(&tys[..tys.len() - 1], ", ")?; for index in 0..parameters.len() - 1 {
let (param_name,param_type) = &parameters[index];
match param_name {
Some(name) => {
write!(f, "{}: ", name)?;
param_type.hir_fmt(f)?;
},
None => write!(f, " : {:?}", param_type)?,
};
if index != parameters.len() - 2 {
write!(f, ", ")?;
}
}
if *is_varargs { if *is_varargs {
write!(f, "{}...", if tys.len() == 1 { "" } else { ", " })?; write!(f, "{}...", if parameters.len() == 1 { "" } else { ", " })?;
} }
write!(f, ")")?; write!(f, ")")?;
let ret_ty = tys.last().unwrap(); let ret_ty = &parameters.last().unwrap().1;
match ret_ty { match ret_ty {
TypeRef::Tuple(tup) if tup.is_empty() => {} TypeRef::Tuple(tup) if tup.is_empty() => {}
_ => { _ => {

View File

@ -201,7 +201,7 @@ impl<'a> TyLoweringContext<'a> {
TypeRef::Placeholder => TyKind::Error.intern(Interner), TypeRef::Placeholder => TyKind::Error.intern(Interner),
TypeRef::Fn(params, is_varargs) => { TypeRef::Fn(params, is_varargs) => {
let substs = self.with_shifted_in(DebruijnIndex::ONE, |ctx| { let substs = self.with_shifted_in(DebruijnIndex::ONE, |ctx| {
Substitution::from_iter(Interner, params.iter().map(|tr| ctx.lower_ty(tr))) Substitution::from_iter(Interner, params.iter().map(|tr| ctx.lower_ty(&tr.1)))
}); });
TyKind::Function(FnPointer { TyKind::Function(FnPointer {
num_binders: 0, // FIXME lower `for<'a> fn()` correctly num_binders: 0, // FIXME lower `for<'a> fn()` correctly

View File

@ -172,6 +172,7 @@ pub(crate) fn hover_for_definition(
Definition::BuiltinType(_) => Some(FamousDefs(sema, sema.scope(node).krate())), Definition::BuiltinType(_) => Some(FamousDefs(sema, sema.scope(node).krate())),
_ => None, _ => None,
}; };
println!("definition: {definition:?}");
if let Some(markup) = render::definition(sema.db, definition, famous_defs.as_ref(), config) { if let Some(markup) = render::definition(sema.db, definition, famous_defs.as_ref(), config) {
let mut res = HoverResult::default(); let mut res = HoverResult::default();
res.markup = render::process_markup(sema.db, definition, &markup, config); res.markup = render::process_markup(sema.db, definition, &markup, config);

View File

@ -411,6 +411,7 @@ where
D: HasAttrs + HirDisplay, D: HasAttrs + HirDisplay,
{ {
let label = def.display(db).to_string(); let label = def.display(db).to_string();
println!("label: {label:?}");
let docs = def.attrs(db).docs(); let docs = def.attrs(db).docs();
(label, docs) (label, docs)
} }

View File

@ -1310,6 +1310,42 @@ fn test_hover_function_show_qualifiers() {
); );
} }
#[test]
fn test_hover_function_show_types() {
check(
r#"fn foo$0(a: i32, b:i32) -> i32 { 0 }"#,
expect![[r#"
*foo*
```rust
test
```
```rust
fn foo(a: i32, b: i32) -> i32
```
"#]],
);
}
#[test]
fn test_hover_function_pointer_show_types() {
check(
r#"type foo$0 = fn(a: i32, b: i32) -> i32;"#,
expect![[r#"
*foo*
```rust
test
```
```rust
type foo = fn(a: i32, b: i32) -> i32
```
"#]],
);
}
#[test] #[test]
fn test_hover_trait_show_qualifiers() { fn test_hover_trait_show_qualifiers() {
check_actions( check_actions(