rust/crates/ra_ide_api/src/completion/completion_item.rs

400 lines
12 KiB
Rust
Raw Normal View History

2019-01-27 13:50:57 -06:00
use hir::{Docs, Documentation, PerNs, Resolution};
use ra_syntax::{
TextRange,
};
use ra_text_edit::TextEdit;
use test_utils::tested_by;
2019-01-08 13:33:36 -06:00
use crate::completion::{
completion_context::CompletionContext,
function_label,
};
2019-01-08 13:33:36 -06:00
/// `CompletionItem` describes a single completion variant in the editor pop-up.
/// It is basically a POD with various properties. To construct a
/// `CompletionItem`, use `new` method and the `Builder` struct.
#[derive(Debug)]
pub struct CompletionItem {
/// Used only internally in tests, to check only specific kind of
/// completion.
completion_kind: CompletionKind,
label: String,
kind: Option<CompletionItemKind>,
2019-01-09 09:09:49 -06:00
detail: Option<String>,
documentation: Option<Documentation>,
2019-01-08 13:33:36 -06:00
lookup: Option<String>,
insert_text: Option<String>,
insert_text_format: InsertTextFormat,
2019-01-20 23:19:51 -06:00
/// Where completion occurs. `source_range` must contain the completion offset.
/// `insert_text` should start with what `source_range` points to, or VSCode
/// will filter out the completion silently.
source_range: TextRange,
2019-01-20 23:19:51 -06:00
/// Additional text edit, ranges in `text_edit` must never intersect with `source_range`.
/// Or VSCode will drop it silently.
text_edit: Option<TextEdit>,
2019-01-08 13:33:36 -06:00
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompletionItemKind {
Snippet,
Keyword,
Module,
Function,
Struct,
Enum,
EnumVariant,
Binding,
Field,
2019-01-11 12:02:12 -06:00
Static,
Const,
Trait,
TypeAlias,
2019-01-07 12:12:19 -06:00
Method,
2019-01-27 13:50:57 -06:00
TypeParam,
2019-01-08 13:33:36 -06:00
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
2019-01-08 13:33:36 -06:00
pub(crate) enum CompletionKind {
/// Parser-based keyword completion.
Keyword,
/// Your usual "complete all valid identifiers".
Reference,
/// "Secret sauce" completions.
Magic,
Snippet,
2019-01-20 23:19:51 -06:00
Postfix,
2019-01-08 13:33:36 -06:00
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum InsertTextFormat {
PlainText,
Snippet,
}
2019-01-08 13:33:36 -06:00
impl CompletionItem {
pub(crate) fn new(
completion_kind: CompletionKind,
replace_range: TextRange,
label: impl Into<String>,
) -> Builder {
2019-01-08 13:33:36 -06:00
let label = label.into();
Builder {
source_range: replace_range,
2019-01-08 13:33:36 -06:00
completion_kind,
label,
insert_text: None,
insert_text_format: InsertTextFormat::PlainText,
2019-01-09 09:09:49 -06:00
detail: None,
documentation: None,
2019-01-08 13:33:36 -06:00
lookup: None,
kind: None,
text_edit: None,
2019-01-08 13:33:36 -06:00
}
}
/// What user sees in pop-up in the UI.
pub fn label(&self) -> &str {
&self.label
}
2019-01-09 09:09:49 -06:00
/// Short one-line additional information, like a type
pub fn detail(&self) -> Option<&str> {
self.detail.as_ref().map(|it| it.as_str())
}
/// A doc-comment
pub fn documentation(&self) -> Option<Documentation> {
self.documentation.clone()
}
2019-01-08 13:33:36 -06:00
/// What string is used for filtering.
pub fn lookup(&self) -> &str {
2019-02-08 05:49:43 -06:00
self.lookup.as_ref().map(|it| it.as_str()).unwrap_or_else(|| self.label())
2019-01-08 13:33:36 -06:00
}
pub fn insert_text_format(&self) -> InsertTextFormat {
2019-02-06 14:50:26 -06:00
self.insert_text_format
2019-01-08 13:33:36 -06:00
}
pub fn insert_text(&self) -> String {
match &self.insert_text {
Some(t) => t.clone(),
None => self.label.clone(),
}
}
2019-01-08 13:33:36 -06:00
pub fn kind(&self) -> Option<CompletionItemKind> {
self.kind
}
pub fn take_text_edit(&mut self) -> Option<TextEdit> {
self.text_edit.take()
}
pub fn source_range(&self) -> TextRange {
self.source_range
}
2019-01-08 13:33:36 -06:00
}
/// A helper to make `CompletionItem`s.
#[must_use]
pub(crate) struct Builder {
source_range: TextRange,
2019-01-08 13:33:36 -06:00
completion_kind: CompletionKind,
label: String,
insert_text: Option<String>,
insert_text_format: InsertTextFormat,
2019-01-09 09:09:49 -06:00
detail: Option<String>,
documentation: Option<Documentation>,
2019-01-08 13:33:36 -06:00
lookup: Option<String>,
kind: Option<CompletionItemKind>,
text_edit: Option<TextEdit>,
2019-01-08 13:33:36 -06:00
}
impl Builder {
2019-01-08 13:33:36 -06:00
pub(crate) fn add_to(self, acc: &mut Completions) {
acc.add(self.build())
}
pub(crate) fn build(self) -> CompletionItem {
CompletionItem {
source_range: self.source_range,
2019-01-08 13:33:36 -06:00
label: self.label,
2019-01-09 09:09:49 -06:00
detail: self.detail,
documentation: self.documentation,
insert_text_format: self.insert_text_format,
2019-01-08 13:33:36 -06:00
lookup: self.lookup,
kind: self.kind,
completion_kind: self.completion_kind,
text_edit: self.text_edit,
insert_text: self.insert_text,
2019-01-08 13:33:36 -06:00
}
}
pub(crate) fn lookup_by(mut self, lookup: impl Into<String>) -> Builder {
2019-01-08 13:33:36 -06:00
self.lookup = Some(lookup.into());
self
}
pub(crate) fn insert_text(mut self, insert_text: impl Into<String>) -> Builder {
self.insert_text = Some(insert_text.into());
self
}
2019-01-19 08:11:38 -06:00
#[allow(unused)]
pub(crate) fn insert_text_format(mut self, insert_text_format: InsertTextFormat) -> Builder {
self.insert_text_format = insert_text_format;
2019-01-08 13:33:36 -06:00
self
}
pub(crate) fn snippet(mut self, snippet: impl Into<String>) -> Builder {
self.insert_text_format = InsertTextFormat::Snippet;
self.insert_text(snippet)
}
pub(crate) fn kind(mut self, kind: CompletionItemKind) -> Builder {
2019-01-08 13:33:36 -06:00
self.kind = Some(kind);
self
}
2019-01-19 08:11:38 -06:00
#[allow(unused)]
pub(crate) fn text_edit(mut self, edit: TextEdit) -> Builder {
self.text_edit = Some(edit);
self
}
#[allow(unused)]
pub(crate) fn detail(self, detail: impl Into<String>) -> Builder {
2019-01-09 09:46:02 -06:00
self.set_detail(Some(detail))
}
pub(crate) fn set_detail(mut self, detail: Option<impl Into<String>>) -> Builder {
2019-01-09 09:46:02 -06:00
self.detail = detail.map(Into::into);
2019-01-09 09:09:49 -06:00
self
}
#[allow(unused)]
pub(crate) fn documentation(self, docs: Documentation) -> Builder {
self.set_documentation(Some(docs))
}
pub(crate) fn set_documentation(mut self, docs: Option<Documentation>) -> Builder {
self.documentation = docs.map(Into::into);
self
}
2019-01-08 13:33:36 -06:00
pub(super) fn from_resolution(
mut self,
ctx: &CompletionContext,
2019-01-27 13:50:57 -06:00
resolution: &PerNs<Resolution>,
) -> Builder {
2019-01-27 13:50:57 -06:00
use hir::ModuleDef::*;
2019-02-08 05:49:43 -06:00
let def = resolution.as_ref().take_types().or_else(|| resolution.as_ref().take_values());
let def = match def {
None => return self,
Some(it) => it,
};
let (kind, docs) = match def {
Resolution::Def(Module(it)) => (CompletionItemKind::Module, it.docs(ctx.db)),
Resolution::Def(Function(func)) => return self.from_function(ctx, *func),
Resolution::Def(Struct(it)) => (CompletionItemKind::Struct, it.docs(ctx.db)),
Resolution::Def(Enum(it)) => (CompletionItemKind::Enum, it.docs(ctx.db)),
Resolution::Def(EnumVariant(it)) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)),
Resolution::Def(Const(it)) => (CompletionItemKind::Const, it.docs(ctx.db)),
Resolution::Def(Static(it)) => (CompletionItemKind::Static, it.docs(ctx.db)),
Resolution::Def(Trait(it)) => (CompletionItemKind::Trait, it.docs(ctx.db)),
Resolution::Def(Type(it)) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)),
Resolution::GenericParam(..) => (CompletionItemKind::TypeParam, None),
Resolution::LocalBinding(..) => (CompletionItemKind::Binding, None),
Resolution::SelfType(..) => (
2019-01-27 13:50:57 -06:00
CompletionItemKind::TypeParam, // (does this need its own kind?)
None,
),
2019-01-08 13:33:36 -06:00
};
self.kind = Some(kind);
self.documentation = docs;
2019-01-08 13:33:36 -06:00
self
}
2019-01-07 12:12:19 -06:00
pub(super) fn from_function(
mut self,
ctx: &CompletionContext,
function: hir::Function,
) -> Builder {
2019-01-08 13:33:36 -06:00
// If not an import, add parenthesis automatically.
if ctx.use_item_syntax.is_none() && !ctx.is_call {
tested_by!(inserts_parens_for_function_calls);
2019-01-23 07:21:20 -06:00
let sig = function.signature(ctx.db);
if sig.params().is_empty() || sig.has_self_param() && sig.params().len() == 1 {
self.insert_text = Some(format!("{}()$0", self.label));
2019-01-08 13:33:36 -06:00
} else {
self.insert_text = Some(format!("{}($0)", self.label));
2019-01-08 13:33:36 -06:00
}
self.insert_text_format = InsertTextFormat::Snippet;
2019-01-08 13:33:36 -06:00
}
2019-01-22 07:55:05 -06:00
if let Some(docs) = function.docs(ctx.db) {
self.documentation = Some(docs);
}
if let Some(label) = function_item_label(ctx, function) {
self.detail = Some(label);
}
2019-01-08 13:33:36 -06:00
self.kind = Some(CompletionItemKind::Function);
self
}
}
impl<'a> Into<CompletionItem> for Builder {
2019-01-08 13:33:36 -06:00
fn into(self) -> CompletionItem {
self.build()
}
}
/// Represents an in-progress set of completions being built.
#[derive(Debug, Default)]
pub(crate) struct Completions {
buf: Vec<CompletionItem>,
}
impl Completions {
pub(crate) fn add(&mut self, item: impl Into<CompletionItem>) {
self.buf.push(item.into())
}
pub(crate) fn add_all<I>(&mut self, items: I)
where
I: IntoIterator,
I::Item: Into<CompletionItem>,
{
items.into_iter().for_each(|item| self.add(item.into()))
}
}
impl Into<Vec<CompletionItem>> for Completions {
fn into(self) -> Vec<CompletionItem> {
self.buf
}
}
fn function_item_label(ctx: &CompletionContext, function: hir::Function) -> Option<String> {
2019-01-22 17:20:40 -06:00
let node = function.source(ctx.db).1;
function_label(&node)
2019-01-22 17:20:40 -06:00
}
#[cfg(test)]
pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> {
use crate::mock_analysis::{single_file_with_position, analysis_and_position};
use crate::completion::completions;
let (analysis, position) = if code.contains("//-") {
analysis_and_position(code)
} else {
single_file_with_position(code)
};
let completions = completions(&analysis.db, position).unwrap();
let completion_items: Vec<CompletionItem> = completions.into();
2019-02-08 05:49:43 -06:00
let mut kind_completions: Vec<CompletionItem> =
completion_items.into_iter().filter(|c| c.completion_kind == kind).collect();
2019-01-27 14:02:24 -06:00
kind_completions.sort_by_key(|c| c.label.clone());
kind_completions
}
#[cfg(test)]
pub(crate) fn check_completion(test_name: &str, code: &str, kind: CompletionKind) {
use insta::assert_debug_snapshot_matches;
let kind_completions = do_completion(code, kind);
assert_debug_snapshot_matches!(test_name, kind_completions);
}
#[cfg(test)]
mod tests {
use test_utils::covers;
use super::*;
fn check_reference_completion(code: &str, expected_completions: &str) {
check_completion(code, expected_completions, CompletionKind::Reference);
}
#[test]
fn inserts_parens_for_function_calls() {
covers!(inserts_parens_for_function_calls);
check_reference_completion(
"inserts_parens_for_function_calls1",
r"
fn no_args() {}
fn main() { no_<|> }
",
);
check_reference_completion(
"inserts_parens_for_function_calls2",
r"
fn with_args(x: i32, y: String) {}
fn main() { with_<|> }
",
);
2019-01-23 07:21:20 -06:00
check_reference_completion(
"inserts_parens_for_function_calls3",
r"
struct S {}
impl S {
fn foo(&self) {}
}
fn bar(s: &S) {
s.f<|>
}
",
)
}
#[test]
fn dont_render_function_parens_in_use_item() {
check_reference_completion(
"dont_render_function_parens_in_use_item",
"
//- /lib.rs
mod m { pub fn foo() {} }
use crate::m::f<|>;
",
)
}
#[test]
fn dont_render_function_parens_if_already_call() {
check_reference_completion(
"dont_render_function_parens_if_already_call",
"
//- /lib.rs
fn frobnicate() {}
fn main() {
frob<|>();
}
",
)
}
}