From d0a261468e67be9213a3fae20f1511e303cac064 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Feb 2019 18:51:38 +0300 Subject: introduce completion presentation This module should remove completion rendering boilerplate from the "brains" of completion engine. --- crates/ra_ide_api/src/completion/complete_dot.rs | 24 +++------------- .../src/completion/complete_struct_literal.rs | 15 ++-------- crates/ra_ide_api/src/completion/presentation.rs | 33 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 32 deletions(-) create mode 100644 crates/ra_ide_api/src/completion/presentation.rs (limited to 'crates/ra_ide_api/src/completion') diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index 20fa323ce..c9099bcd7 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs @@ -1,7 +1,6 @@ -use hir::{Ty, AdtDef, Docs}; +use hir::{Ty, AdtDef}; -use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind}; -use crate::completion::completion_item::CompletionKind; +use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind, CompletionKind}; /// Complete dot accesses, i.e. fields or methods (currently only fields). pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { @@ -29,15 +28,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) match def_id { AdtDef::Struct(s) => { for field in s.fields(ctx.db) { - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - field.name(ctx.db).to_string(), - ) - .kind(CompletionItemKind::Field) - .detail(field.ty(ctx.db).subst(substs).to_string()) - .set_documentation(field.docs(ctx.db)) - .add_to(acc); + acc.add_field(CompletionKind::Reference, ctx, field, substs); } } @@ -47,14 +38,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) } Ty::Tuple(fields) => { for (i, ty) in fields.iter().enumerate() { - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - i.to_string(), - ) - .kind(CompletionItemKind::Field) - .detail(ty.to_string()) - .add_to(acc); + acc.add_pos_field(CompletionKind::Reference, ctx, i, ty); } } _ => {} diff --git a/crates/ra_ide_api/src/completion/complete_struct_literal.rs b/crates/ra_ide_api/src/completion/complete_struct_literal.rs index f8dd2baad..c617bff5f 100644 --- a/crates/ra_ide_api/src/completion/complete_struct_literal.rs +++ b/crates/ra_ide_api/src/completion/complete_struct_literal.rs @@ -1,7 +1,6 @@ -use hir::{Ty, AdtDef, Docs}; +use hir::{Ty, AdtDef}; -use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind}; -use crate::completion::completion_item::CompletionKind; +use crate::completion::{CompletionContext, Completions, CompletionKind}; /// Complete fields in fields literals. pub(super) fn complete_struct_literal(acc: &mut Completions, ctx: &CompletionContext) { @@ -23,15 +22,7 @@ pub(super) fn complete_struct_literal(acc: &mut Completions, ctx: &CompletionCon match adt { AdtDef::Struct(s) => { for field in s.fields(ctx.db) { - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - field.name(ctx.db).to_string(), - ) - .kind(CompletionItemKind::Field) - .detail(field.ty(ctx.db).subst(substs).to_string()) - .set_documentation(field.docs(ctx.db)) - .add_to(acc); + acc.add_field(CompletionKind::Reference, ctx, field, substs); } } diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs new file mode 100644 index 000000000..6b2de56bc --- /dev/null +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -0,0 +1,33 @@ +//! This modules takes care of rendering various defenitions as completion items. +use hir::Docs; + +use crate::completion::{Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem}; + +impl Completions { + pub(crate) fn add_field( + &mut self, + kind: CompletionKind, + ctx: &CompletionContext, + field: hir::StructField, + substs: &hir::Substs, + ) { + CompletionItem::new(kind, ctx.source_range(), field.name(ctx.db).to_string()) + .kind(CompletionItemKind::Field) + .detail(field.ty(ctx.db).subst(substs).to_string()) + .set_documentation(field.docs(ctx.db)) + .add_to(self); + } + + pub(crate) fn add_pos_field( + &mut self, + kind: CompletionKind, + ctx: &CompletionContext, + field: usize, + ty: &hir::Ty, + ) { + CompletionItem::new(kind, ctx.source_range(), field.to_string()) + .kind(CompletionItemKind::Field) + .detail(ty.to_string()) + .add_to(self); + } +} -- cgit v1.2.3