make compleion item details private
This commit is contained in:
parent
b5c5995bf1
commit
4092b8d0b5
@ -18,7 +18,7 @@ use crate::{
|
||||
Cancelable, FilePosition
|
||||
};
|
||||
|
||||
pub use crate::completion::completion_item::CompletionItem;
|
||||
pub use crate::completion::completion_item::{CompletionItem, InsertText};
|
||||
|
||||
pub(crate) fn completions(
|
||||
db: &db::RootDatabase,
|
||||
@ -109,13 +109,20 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
fn is_snippet(completion_item: &CompletionItem) -> bool {
|
||||
match completion_item.insert_text() {
|
||||
InsertText::Snippet { .. } => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn check_scope_completion(code: &str, expected_completions: &str) {
|
||||
let (analysis, position) = single_file_with_position(code);
|
||||
let completions = completions(&analysis.imp.db, position)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter(|c| c.snippet.is_none())
|
||||
.filter(|c| !is_snippet(c))
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq_dbg(expected_completions, &completions);
|
||||
}
|
||||
@ -126,7 +133,7 @@ mod tests {
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter(|c| c.snippet.is_some())
|
||||
.filter(is_snippet)
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq_dbg(expected_completions, &completions);
|
||||
}
|
||||
|
@ -1,11 +1,13 @@
|
||||
#[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>,
|
||||
label: String,
|
||||
lookup: Option<String>,
|
||||
snippet: Option<String>,
|
||||
}
|
||||
|
||||
pub enum InsertText {
|
||||
PlainText { text: String },
|
||||
Snippet { text: String },
|
||||
}
|
||||
|
||||
impl CompletionItem {
|
||||
@ -17,6 +19,26 @@ impl CompletionItem {
|
||||
snippet: None,
|
||||
}
|
||||
}
|
||||
/// What user sees in pop-up in the UI.
|
||||
pub fn label(&self) -> &str {
|
||||
&self.label
|
||||
}
|
||||
/// What string is used for filtering.
|
||||
pub fn lookup(&self) -> &str {
|
||||
self.lookup
|
||||
.as_ref()
|
||||
.map(|it| it.as_str())
|
||||
.unwrap_or(self.label())
|
||||
}
|
||||
/// What is inserted.
|
||||
pub fn insert_text(&self) -> InsertText {
|
||||
match &self.snippet {
|
||||
None => InsertText::PlainText {
|
||||
text: self.label.clone(),
|
||||
},
|
||||
Some(it) => InsertText::Snippet { text: it.clone() },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
|
@ -39,25 +39,19 @@ pub(super) fn completions(
|
||||
}
|
||||
|
||||
let module_scope = module.scope(db)?;
|
||||
acc.extend(
|
||||
module_scope
|
||||
.entries()
|
||||
.filter(|(_name, res)| {
|
||||
// Don't expose this item
|
||||
match res.import {
|
||||
None => true,
|
||||
Some(import) => {
|
||||
let range = import.range(db, module.source().file_id());
|
||||
!range.is_subrange(&name_ref.syntax().range())
|
||||
}
|
||||
module_scope
|
||||
.entries()
|
||||
.filter(|(_name, res)| {
|
||||
// Don't expose this item
|
||||
match res.import {
|
||||
None => true,
|
||||
Some(import) => {
|
||||
let range = import.range(db, module.source().file_id());
|
||||
!range.is_subrange(&name_ref.syntax().range())
|
||||
}
|
||||
})
|
||||
.map(|(name, _res)| CompletionItem {
|
||||
label: name.to_string(),
|
||||
lookup: None,
|
||||
snippet: None,
|
||||
}),
|
||||
);
|
||||
}
|
||||
})
|
||||
.for_each(|(name, _res)| CompletionItem::new(name.to_string()).add_to(acc));
|
||||
}
|
||||
NameRefKind::Path(path) => complete_path(acc, db, module, path)?,
|
||||
NameRefKind::BareIdentInMod => {
|
||||
|
@ -30,7 +30,7 @@ use crate::{
|
||||
};
|
||||
|
||||
pub use crate::{
|
||||
completion::CompletionItem,
|
||||
completion::{CompletionItem, InsertText},
|
||||
};
|
||||
pub use ra_editor::{
|
||||
FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode,
|
||||
|
@ -8,7 +8,7 @@ use languageserver_types::{
|
||||
PrepareRenameResponse, RenameParams, SymbolInformation, TextDocumentIdentifier, TextEdit,
|
||||
WorkspaceEdit, ParameterInformation, ParameterLabel, SignatureInformation, Hover, HoverContents,
|
||||
};
|
||||
use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FilePosition};
|
||||
use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FilePosition, InsertText};
|
||||
use ra_syntax::{TextUnit, text_utils::intersect};
|
||||
use ra_text_edit::text_utils::contains_offset_nonstrict;
|
||||
use rustc_hash::FxHashMap;
|
||||
@ -423,15 +423,21 @@ pub fn handle_completion(
|
||||
.into_iter()
|
||||
.map(|item| {
|
||||
let mut res = CompletionItem {
|
||||
label: item.label,
|
||||
filter_text: item.lookup,
|
||||
label: item.label().to_string(),
|
||||
filter_text: Some(item.lookup().to_string()),
|
||||
..Default::default()
|
||||
};
|
||||
if let Some(snip) = item.snippet {
|
||||
res.insert_text = Some(snip);
|
||||
res.insert_text_format = Some(InsertTextFormat::Snippet);
|
||||
res.kind = Some(CompletionItemKind::Keyword);
|
||||
};
|
||||
match item.insert_text() {
|
||||
InsertText::PlainText { text } => {
|
||||
res.insert_text = Some(text);
|
||||
res.insert_text_format = Some(InsertTextFormat::PlainText);
|
||||
}
|
||||
InsertText::Snippet { text } => {
|
||||
res.insert_text = Some(text);
|
||||
res.insert_text_format = Some(InsertTextFormat::Snippet);
|
||||
res.kind = Some(CompletionItemKind::Keyword);
|
||||
}
|
||||
}
|
||||
res
|
||||
})
|
||||
.collect();
|
||||
|
Loading…
x
Reference in New Issue
Block a user