introduce completion_item module

This commit is contained in:
Aleksey Kladov 2018-12-21 14:02:14 +03:00
parent 463e5af3f2
commit 74406ca8ea
3 changed files with 54 additions and 21 deletions

View File

@ -1,3 +1,4 @@
mod completion_item;
mod reference_completion;
use ra_editor::find_node_at_offset;
@ -17,15 +18,7 @@ use crate::{
Cancelable, FilePosition
};
#[derive(Debug)]
pub struct CompletionItem {
/// What user sees in pop-up
pub label: String,
/// What string is used for filtering, defaults to label
pub lookup: Option<String>,
/// What is inserted, defaults to label
pub snippet: Option<String>,
}
pub use crate::completion::completion_item::CompletionItem;
pub(crate) fn completions(
db: &db::RootDatabase,
@ -63,6 +56,10 @@ pub(crate) fn completions(
Ok(res)
}
/// Complete repeated parametes, both name and type. For example, if all
/// functions in a file have a `spam: &mut Spam` parameter, a completion with
/// `spam: &mut Spam` insert text/label and `spam` lookup string will be
/// suggested.
fn param_completions(ctx: SyntaxNodeRef, acc: &mut Vec<CompletionItem>) {
let mut params = FxHashMap::default();
for node in ctx.ancestors() {
@ -81,13 +78,7 @@ fn param_completions(ctx: SyntaxNodeRef, acc: &mut Vec<CompletionItem>) {
Some((label, lookup))
}
})
.for_each(|(label, lookup)| {
acc.push(CompletionItem {
label,
lookup: Some(lookup),
snippet: None,
})
});
.for_each(|(label, lookup)| CompletionItem::new(label).lookup_by(lookup).add_to(acc));
fn process<'a, N: ast::FnDefOwner<'a>>(
node: N,

View File

@ -0,0 +1,44 @@
#[derive(Debug)]
pub struct CompletionItem {
/// What user sees in pop-up in the UI.
pub label: String,
/// What string is used for filtering, defaults to label.
pub lookup: Option<String>,
/// What is inserted, defaults to label.
pub snippet: Option<String>,
}
impl CompletionItem {
pub(crate) fn new(label: impl Into<String>) -> Builder {
let label = label.into();
Builder {
label,
lookup: None,
snippet: None,
}
}
}
pub(crate) struct Builder {
label: String,
lookup: Option<String>,
snippet: Option<String>,
}
impl Builder {
pub fn add_to(self, acc: &mut Vec<CompletionItem>) {
acc.push(self.build())
}
pub fn build(self) -> CompletionItem {
CompletionItem {
label: self.label,
lookup: self.lookup,
snippet: self.snippet,
}
}
pub fn lookup_by(mut self, lookup: impl Into<String>) -> Builder {
self.lookup = Some(lookup.into());
self
}
}

View File

@ -6,11 +6,9 @@ use ra_syntax::{
ast::{self, LoopBodyOwner},
SyntaxKind::*,
};
use hir::{
self,
FnScopes,
Def,
Path,
use hir::{
self,
FnScopes, Def, Path
};
use crate::{