mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
introduce completion_item module
This commit is contained in:
parent
463e5af3f2
commit
74406ca8ea
3 changed files with 54 additions and 21 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
mod completion_item;
|
||||||
mod reference_completion;
|
mod reference_completion;
|
||||||
|
|
||||||
use ra_editor::find_node_at_offset;
|
use ra_editor::find_node_at_offset;
|
||||||
|
@ -17,15 +18,7 @@ use crate::{
|
||||||
Cancelable, FilePosition
|
Cancelable, FilePosition
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
pub use crate::completion::completion_item::CompletionItem;
|
||||||
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(crate) fn completions(
|
pub(crate) fn completions(
|
||||||
db: &db::RootDatabase,
|
db: &db::RootDatabase,
|
||||||
|
@ -63,6 +56,10 @@ pub(crate) fn completions(
|
||||||
Ok(res)
|
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>) {
|
fn param_completions(ctx: SyntaxNodeRef, acc: &mut Vec<CompletionItem>) {
|
||||||
let mut params = FxHashMap::default();
|
let mut params = FxHashMap::default();
|
||||||
for node in ctx.ancestors() {
|
for node in ctx.ancestors() {
|
||||||
|
@ -81,13 +78,7 @@ fn param_completions(ctx: SyntaxNodeRef, acc: &mut Vec<CompletionItem>) {
|
||||||
Some((label, lookup))
|
Some((label, lookup))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.for_each(|(label, lookup)| {
|
.for_each(|(label, lookup)| CompletionItem::new(label).lookup_by(lookup).add_to(acc));
|
||||||
acc.push(CompletionItem {
|
|
||||||
label,
|
|
||||||
lookup: Some(lookup),
|
|
||||||
snippet: None,
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
fn process<'a, N: ast::FnDefOwner<'a>>(
|
fn process<'a, N: ast::FnDefOwner<'a>>(
|
||||||
node: N,
|
node: N,
|
||||||
|
|
44
crates/ra_analysis/src/completion/completion_item.rs
Normal file
44
crates/ra_analysis/src/completion/completion_item.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,11 +6,9 @@ use ra_syntax::{
|
||||||
ast::{self, LoopBodyOwner},
|
ast::{self, LoopBodyOwner},
|
||||||
SyntaxKind::*,
|
SyntaxKind::*,
|
||||||
};
|
};
|
||||||
use hir::{
|
use hir::{
|
||||||
self,
|
self,
|
||||||
FnScopes,
|
FnScopes, Def, Path
|
||||||
Def,
|
|
||||||
Path,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue