475: Show types of fields in completion r=matklad a=matklad

![image](https://user-images.githubusercontent.com/1711539/50910524-0f146200-143f-11e9-84d6-0ba80761cd89.png)

r? @flodiebold 


Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-01-10 13:07:19 +00:00
commit aca14c591f
6 changed files with 99 additions and 59 deletions

View file

@ -28,13 +28,13 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
Ty::Adt { def_id, .. } => {
match def_id.resolve(ctx.db)? {
Def::Struct(s) => {
let variant_data = s.variant_data(ctx.db)?;
for field in variant_data.fields() {
for field in s.fields(ctx.db)? {
CompletionItem::new(
CompletionKind::Reference,
field.name().to_string(),
)
.kind(CompletionItemKind::Field)
.set_detail(field.ty(ctx.db)?.map(|ty| ty.to_string()))
.add_to(acc);
}
}
@ -72,7 +72,7 @@ mod tests {
a.<|>
}
",
r#"the_field"#,
r#"the_field "u32""#,
);
}
@ -80,14 +80,14 @@ mod tests {
fn test_struct_field_completion_self() {
check_ref_completion(
r"
struct A { the_field: u32 }
struct A { the_field: (u32,) }
impl A {
fn foo(self) {
self.<|>
}
}
",
r#"the_field"#,
r#"the_field "(u32,)""#,
);
}
@ -95,14 +95,14 @@ mod tests {
fn test_struct_field_completion_autoderef() {
check_ref_completion(
r"
struct A { the_field: u32 }
struct A { the_field: (u32, i32) }
impl A {
fn foo(&self) {
self.<|>
}
}
",
r#"the_field"#,
r#"the_field "(u32, i32)""#,
);
}

View file

@ -11,6 +11,7 @@ pub struct CompletionItem {
/// completion.
completion_kind: CompletionKind,
label: String,
detail: Option<String>,
lookup: Option<String>,
snippet: Option<String>,
kind: Option<CompletionItemKind>,
@ -51,6 +52,7 @@ impl CompletionItem {
Builder {
completion_kind,
label,
detail: None,
lookup: None,
snippet: None,
kind: None,
@ -60,6 +62,10 @@ impl CompletionItem {
pub fn label(&self) -> &str {
&self.label
}
/// Short one-line additional information, like a type
pub fn detail(&self) -> Option<&str> {
self.detail.as_ref().map(|it| it.as_str())
}
/// What string is used for filtering.
pub fn lookup(&self) -> &str {
self.lookup
@ -87,6 +93,7 @@ impl CompletionItem {
pub(crate) struct Builder {
completion_kind: CompletionKind,
label: String,
detail: Option<String>,
lookup: Option<String>,
snippet: Option<String>,
kind: Option<CompletionItemKind>,
@ -100,6 +107,7 @@ impl Builder {
pub(crate) fn build(self) -> CompletionItem {
CompletionItem {
label: self.label,
detail: self.detail,
lookup: self.lookup,
snippet: self.snippet,
kind: self.kind,
@ -118,6 +126,14 @@ impl Builder {
self.kind = Some(kind);
self
}
#[allow(unused)]
pub(crate) fn detail(self, detail: impl Into<String>) -> Builder {
self.set_detail(Some(detail))
}
pub(crate) fn set_detail(mut self, detail: Option<impl Into<String>>) -> Builder {
self.detail = detail.map(Into::into);
self
}
pub(super) fn from_resolution(
mut self,
ctx: &CompletionContext,
@ -227,6 +243,9 @@ impl Completions {
} else {
res.push_str(&c.label);
}
if let Some(detail) = &c.detail {
res.push_str(&format!(" {:?}", detail));
}
if let Some(snippet) = &c.snippet {
res.push_str(&format!(" {:?}", snippet));
}