2019-09-30 03:58:53 -05:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2019-02-18 01:31:00 -06:00
|
|
|
use std::fmt;
|
|
|
|
|
2019-02-24 12:46:04 -06:00
|
|
|
use hir::Documentation;
|
2019-02-18 01:31:00 -06:00
|
|
|
use ra_syntax::TextRange;
|
2019-07-04 15:05:17 -05:00
|
|
|
use ra_text_edit::{TextEdit, TextEditBuilder};
|
2019-01-23 14:14:13 -06:00
|
|
|
|
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.
|
|
|
|
pub struct CompletionItem {
|
|
|
|
/// Used only internally in tests, to check only specific kind of
|
2019-02-18 03:05:16 -06:00
|
|
|
/// completion (postfix, keyword, reference, etc).
|
2019-02-18 01:31:00 -06:00
|
|
|
#[allow(unused)]
|
2019-01-08 13:33:36 -06:00
|
|
|
completion_kind: CompletionKind,
|
2019-02-18 03:05:16 -06:00
|
|
|
/// Label in the completion pop up which identifies completion.
|
2019-01-08 13:33:36 -06:00
|
|
|
label: String,
|
2019-02-18 03:05:16 -06:00
|
|
|
/// Range of identifier that is being completed.
|
|
|
|
///
|
|
|
|
/// It should be used primarily for UI, but we also use this to convert
|
|
|
|
/// genetic TextEdit into LSP's completion edit (see conv.rs).
|
|
|
|
///
|
|
|
|
/// `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,
|
|
|
|
/// What happens when user selects this item.
|
|
|
|
///
|
|
|
|
/// Typically, replaces `source_range` with new identifier.
|
|
|
|
text_edit: TextEdit,
|
|
|
|
insert_text_format: InsertTextFormat,
|
|
|
|
|
|
|
|
/// What item (struct, function, etc) are we completing.
|
2019-01-19 08:02:50 -06:00
|
|
|
kind: Option<CompletionItemKind>,
|
2019-02-18 03:05:16 -06:00
|
|
|
|
|
|
|
/// Lookup is used to check if completion item indeed can complete current
|
|
|
|
/// ident.
|
|
|
|
///
|
|
|
|
/// That is, in `foo.bar<|>` lookup of `abracadabra` will be accepted (it
|
|
|
|
/// contains `bar` sub sequence), and `quux` will rejected.
|
2019-02-18 01:31:00 -06:00
|
|
|
lookup: Option<String>,
|
2019-02-18 03:05:16 -06:00
|
|
|
|
|
|
|
/// Additional info to show in the UI pop up.
|
2019-01-09 09:09:49 -06:00
|
|
|
detail: Option<String>,
|
2019-01-23 15:22:10 -06:00
|
|
|
documentation: Option<Documentation>,
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
|
|
|
|
2019-02-18 03:23:31 -06:00
|
|
|
// We use custom debug for CompletionItem to make `insta`'s diffs more readable.
|
2019-02-18 01:31:00 -06:00
|
|
|
impl fmt::Debug for CompletionItem {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let mut s = f.debug_struct("CompletionItem");
|
2019-02-18 03:23:31 -06:00
|
|
|
s.field("label", &self.label()).field("source_range", &self.source_range());
|
|
|
|
if self.text_edit().as_atoms().len() == 1 {
|
|
|
|
let atom = &self.text_edit().as_atoms()[0];
|
|
|
|
s.field("delete", &atom.delete);
|
|
|
|
s.field("insert", &atom.insert);
|
|
|
|
} else {
|
|
|
|
s.field("text_edit", &self.text_edit);
|
|
|
|
}
|
2019-02-18 01:31:00 -06:00
|
|
|
if let Some(kind) = self.kind().as_ref() {
|
|
|
|
s.field("kind", kind);
|
|
|
|
}
|
|
|
|
if self.lookup() != self.label() {
|
|
|
|
s.field("lookup", &self.lookup());
|
|
|
|
}
|
|
|
|
if let Some(detail) = self.detail() {
|
|
|
|
s.field("detail", &detail);
|
|
|
|
}
|
|
|
|
if let Some(documentation) = self.documentation() {
|
|
|
|
s.field("documentation", &documentation);
|
|
|
|
}
|
|
|
|
s.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum CompletionItemKind {
|
|
|
|
Snippet,
|
|
|
|
Keyword,
|
|
|
|
Module,
|
|
|
|
Function,
|
2019-05-30 08:10:07 -05:00
|
|
|
BuiltinType,
|
2019-01-08 13:33:36 -06:00
|
|
|
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-04-24 15:16:50 -05:00
|
|
|
Macro,
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
|
|
|
|
2019-01-19 08:02:50 -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-05-30 08:10:07 -05:00
|
|
|
BuiltinType,
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
|
|
|
|
2019-01-19 08:02:50 -06:00
|
|
|
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
|
|
|
pub enum InsertTextFormat {
|
|
|
|
PlainText,
|
|
|
|
Snippet,
|
|
|
|
}
|
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
impl CompletionItem {
|
2019-01-19 10:38:34 -06:00
|
|
|
pub(crate) fn new(
|
2019-01-19 08:02:50 -06:00
|
|
|
completion_kind: CompletionKind,
|
2019-02-18 03:05:16 -06:00
|
|
|
source_range: TextRange,
|
2019-01-19 08:02:50 -06:00
|
|
|
label: impl Into<String>,
|
2019-01-19 10:38:34 -06:00
|
|
|
) -> Builder {
|
2019-01-08 13:33:36 -06:00
|
|
|
let label = label.into();
|
|
|
|
Builder {
|
2019-02-18 03:05:16 -06:00
|
|
|
source_range,
|
2019-01-08 13:33:36 -06:00
|
|
|
completion_kind,
|
|
|
|
label,
|
2019-01-19 08:02:50 -06:00
|
|
|
insert_text: None,
|
|
|
|
insert_text_format: InsertTextFormat::PlainText,
|
2019-01-09 09:09:49 -06:00
|
|
|
detail: None,
|
2019-01-21 20:41:39 -06:00
|
|
|
documentation: None,
|
2019-01-08 13:33:36 -06:00
|
|
|
lookup: None,
|
|
|
|
kind: None,
|
2019-01-19 22:02:00 -06:00
|
|
|
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-02-18 03:05:16 -06:00
|
|
|
pub fn source_range(&self) -> TextRange {
|
|
|
|
self.source_range
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert_text_format(&self) -> InsertTextFormat {
|
|
|
|
self.insert_text_format
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn text_edit(&self) -> &TextEdit {
|
|
|
|
&self.text_edit
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
}
|
2019-01-21 20:41:39 -06:00
|
|
|
/// A doc-comment
|
2019-01-29 20:39:09 -06:00
|
|
|
pub fn documentation(&self) -> Option<Documentation> {
|
|
|
|
self.documentation.clone()
|
2019-01-21 20:41:39 -06:00
|
|
|
}
|
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
|
|
|
}
|
2019-01-19 08:02:50 -06:00
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
pub fn kind(&self) -> Option<CompletionItemKind> {
|
|
|
|
self.kind
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A helper to make `CompletionItem`s.
|
|
|
|
#[must_use]
|
2019-01-19 10:38:34 -06:00
|
|
|
pub(crate) struct Builder {
|
2019-01-19 22:02:00 -06:00
|
|
|
source_range: TextRange,
|
2019-01-08 13:33:36 -06:00
|
|
|
completion_kind: CompletionKind,
|
|
|
|
label: String,
|
2019-01-19 08:02:50 -06:00
|
|
|
insert_text: Option<String>,
|
|
|
|
insert_text_format: InsertTextFormat,
|
2019-01-09 09:09:49 -06:00
|
|
|
detail: Option<String>,
|
2019-01-23 15:22:10 -06:00
|
|
|
documentation: Option<Documentation>,
|
2019-01-08 13:33:36 -06:00
|
|
|
lookup: Option<String>,
|
|
|
|
kind: Option<CompletionItemKind>,
|
2019-01-19 22:02:00 -06:00
|
|
|
text_edit: Option<TextEdit>,
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
|
|
|
|
2019-01-19 10:38:34 -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 {
|
2019-02-18 03:05:16 -06:00
|
|
|
let label = self.label;
|
|
|
|
let text_edit = match self.text_edit {
|
|
|
|
Some(it) => it,
|
|
|
|
None => {
|
|
|
|
let mut builder = TextEditBuilder::default();
|
|
|
|
builder
|
|
|
|
.replace(self.source_range, self.insert_text.unwrap_or_else(|| label.clone()));
|
|
|
|
builder.finish()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
CompletionItem {
|
2019-01-19 22:02:00 -06:00
|
|
|
source_range: self.source_range,
|
2019-02-18 03:05:16 -06:00
|
|
|
label,
|
|
|
|
insert_text_format: self.insert_text_format,
|
|
|
|
text_edit,
|
2019-01-09 09:09:49 -06:00
|
|
|
detail: self.detail,
|
2019-01-21 20:41:39 -06:00
|
|
|
documentation: self.documentation,
|
2019-01-08 13:33:36 -06:00
|
|
|
lookup: self.lookup,
|
|
|
|
kind: self.kind,
|
|
|
|
completion_kind: self.completion_kind,
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 10:38:34 -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
|
|
|
|
}
|
2019-01-19 10:38:34 -06:00
|
|
|
pub(crate) fn insert_text(mut self, insert_text: impl Into<String>) -> Builder {
|
2019-01-19 08:02:50 -06:00
|
|
|
self.insert_text = Some(insert_text.into());
|
|
|
|
self
|
|
|
|
}
|
2019-02-18 03:07:47 -06:00
|
|
|
pub(crate) fn insert_snippet(mut self, snippet: impl Into<String>) -> Builder {
|
2019-01-19 08:02:50 -06:00
|
|
|
self.insert_text_format = InsertTextFormat::Snippet;
|
|
|
|
self.insert_text(snippet)
|
|
|
|
}
|
2019-01-19 10:38:34 -06:00
|
|
|
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 22:02:00 -06:00
|
|
|
pub(crate) fn text_edit(mut self, edit: TextEdit) -> Builder {
|
|
|
|
self.text_edit = Some(edit);
|
|
|
|
self
|
|
|
|
}
|
2019-02-18 03:05:16 -06:00
|
|
|
pub(crate) fn snippet_edit(mut self, edit: TextEdit) -> Builder {
|
|
|
|
self.insert_text_format = InsertTextFormat::Snippet;
|
|
|
|
self.text_edit(edit)
|
|
|
|
}
|
2019-01-19 22:02:00 -06:00
|
|
|
#[allow(unused)]
|
2019-01-19 10:38:34 -06:00
|
|
|
pub(crate) fn detail(self, detail: impl Into<String>) -> Builder {
|
2019-01-09 09:46:02 -06:00
|
|
|
self.set_detail(Some(detail))
|
|
|
|
}
|
2019-01-19 10:38:34 -06:00
|
|
|
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
|
|
|
|
}
|
2019-01-21 20:41:39 -06:00
|
|
|
#[allow(unused)]
|
2019-01-23 15:22:10 -06:00
|
|
|
pub(crate) fn documentation(self, docs: Documentation) -> Builder {
|
2019-01-21 20:41:39 -06:00
|
|
|
self.set_documentation(Some(docs))
|
|
|
|
}
|
2019-01-23 15:22:10 -06:00
|
|
|
pub(crate) fn set_documentation(mut self, docs: Option<Documentation>) -> Builder {
|
2019-01-21 20:41:39 -06:00
|
|
|
self.documentation = docs.map(Into::into);
|
|
|
|
self
|
|
|
|
}
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
|
|
|
|
2019-01-19 10:38:34 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 08:02:50 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-02-11 14:40:08 -06:00
|
|
|
pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> {
|
2019-01-19 08:02:50 -06:00
|
|
|
use crate::completion::completions;
|
2019-07-04 15:05:17 -05:00
|
|
|
use crate::mock_analysis::{analysis_and_position, single_file_with_position};
|
2019-01-19 08:02:50 -06:00
|
|
|
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());
|
2019-02-11 14:40:08 -06:00
|
|
|
kind_completions
|
|
|
|
}
|