2019-01-08 19:33:36 +00:00
|
|
|
use hir::PerNs;
|
|
|
|
|
2019-01-19 14:02:50 +00:00
|
|
|
use crate::completion::completion_context::CompletionContext;
|
2019-01-22 23:20:40 +00:00
|
|
|
use ra_syntax::{
|
|
|
|
ast::{self, AstNode},
|
2019-01-23 13:05:13 +00:00
|
|
|
TextRange,
|
2019-01-22 23:20:40 +00:00
|
|
|
};
|
2019-01-20 04:02:00 +00:00
|
|
|
use ra_text_edit::TextEdit;
|
2019-01-23 13:05:13 +00:00
|
|
|
use test_utils::tested_by;
|
2019-01-08 19:33:36 +00: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,
|
2019-01-19 14:02:50 +00:00
|
|
|
kind: Option<CompletionItemKind>,
|
2019-01-09 15:09:49 +00:00
|
|
|
detail: Option<String>,
|
2019-01-22 02:41:39 +00:00
|
|
|
documentation: Option<String>,
|
2019-01-08 19:33:36 +00:00
|
|
|
lookup: Option<String>,
|
2019-01-19 16:38:34 +00:00
|
|
|
insert_text: Option<String>,
|
2019-01-19 14:02:50 +00:00
|
|
|
insert_text_format: InsertTextFormat,
|
2019-01-21 05:19:51 +00: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.
|
2019-01-20 04:02:00 +00:00
|
|
|
source_range: TextRange,
|
2019-01-21 05:19:51 +00:00
|
|
|
/// Additional text edit, ranges in `text_edit` must never intersect with `source_range`.
|
|
|
|
/// Or VSCode will drop it silently.
|
2019-01-20 04:02:00 +00:00
|
|
|
text_edit: Option<TextEdit>,
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum CompletionItemKind {
|
|
|
|
Snippet,
|
|
|
|
Keyword,
|
|
|
|
Module,
|
|
|
|
Function,
|
|
|
|
Struct,
|
|
|
|
Enum,
|
|
|
|
EnumVariant,
|
|
|
|
Binding,
|
|
|
|
Field,
|
2019-01-11 18:02:12 +00:00
|
|
|
Static,
|
|
|
|
Const,
|
|
|
|
Trait,
|
|
|
|
TypeAlias,
|
2019-01-07 18:12:19 +00:00
|
|
|
Method,
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 14:02:50 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
2019-01-08 19:33:36 +00:00
|
|
|
pub(crate) enum CompletionKind {
|
|
|
|
/// Parser-based keyword completion.
|
|
|
|
Keyword,
|
|
|
|
/// Your usual "complete all valid identifiers".
|
|
|
|
Reference,
|
|
|
|
/// "Secret sauce" completions.
|
|
|
|
Magic,
|
|
|
|
Snippet,
|
2019-01-21 05:19:51 +00:00
|
|
|
Postfix,
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 14:02:50 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
|
|
|
pub enum InsertTextFormat {
|
|
|
|
PlainText,
|
|
|
|
Snippet,
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
impl CompletionItem {
|
2019-01-19 16:38:34 +00:00
|
|
|
pub(crate) fn new(
|
2019-01-19 14:02:50 +00:00
|
|
|
completion_kind: CompletionKind,
|
2019-01-19 16:38:34 +00:00
|
|
|
replace_range: TextRange,
|
2019-01-19 14:02:50 +00:00
|
|
|
label: impl Into<String>,
|
2019-01-19 16:38:34 +00:00
|
|
|
) -> Builder {
|
2019-01-08 19:33:36 +00:00
|
|
|
let label = label.into();
|
|
|
|
Builder {
|
2019-01-20 04:02:00 +00:00
|
|
|
source_range: replace_range,
|
2019-01-08 19:33:36 +00:00
|
|
|
completion_kind,
|
|
|
|
label,
|
2019-01-19 14:02:50 +00:00
|
|
|
insert_text: None,
|
|
|
|
insert_text_format: InsertTextFormat::PlainText,
|
2019-01-09 15:09:49 +00:00
|
|
|
detail: None,
|
2019-01-22 02:41:39 +00:00
|
|
|
documentation: None,
|
2019-01-08 19:33:36 +00:00
|
|
|
lookup: None,
|
|
|
|
kind: None,
|
2019-01-20 04:02:00 +00:00
|
|
|
text_edit: None,
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/// What user sees in pop-up in the UI.
|
|
|
|
pub fn label(&self) -> &str {
|
|
|
|
&self.label
|
|
|
|
}
|
2019-01-09 15:09:49 +00: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-22 02:41:39 +00:00
|
|
|
/// A doc-comment
|
|
|
|
pub fn documentation(&self) -> Option<&str> {
|
|
|
|
self.documentation.as_ref().map(|it| it.as_str())
|
|
|
|
}
|
2019-01-08 19:33:36 +00:00
|
|
|
/// What string is used for filtering.
|
|
|
|
pub fn lookup(&self) -> &str {
|
|
|
|
self.lookup
|
|
|
|
.as_ref()
|
|
|
|
.map(|it| it.as_str())
|
|
|
|
.unwrap_or(self.label())
|
|
|
|
}
|
2019-01-19 14:02:50 +00:00
|
|
|
|
|
|
|
pub fn insert_text_format(&self) -> InsertTextFormat {
|
|
|
|
self.insert_text_format.clone()
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-19 16:38:34 +00:00
|
|
|
pub fn insert_text(&self) -> String {
|
|
|
|
match &self.insert_text {
|
|
|
|
Some(t) => t.clone(),
|
|
|
|
None => self.label.clone(),
|
|
|
|
}
|
|
|
|
}
|
2019-01-08 19:33:36 +00:00
|
|
|
pub fn kind(&self) -> Option<CompletionItemKind> {
|
|
|
|
self.kind
|
|
|
|
}
|
2019-01-20 04:02:00 +00:00
|
|
|
pub fn take_text_edit(&mut self) -> Option<TextEdit> {
|
|
|
|
self.text_edit.take()
|
2019-01-19 14:02:50 +00:00
|
|
|
}
|
2019-01-20 04:02:00 +00:00
|
|
|
pub fn source_range(&self) -> TextRange {
|
|
|
|
self.source_range
|
2019-01-19 14:02:50 +00:00
|
|
|
}
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A helper to make `CompletionItem`s.
|
|
|
|
#[must_use]
|
2019-01-19 16:38:34 +00:00
|
|
|
pub(crate) struct Builder {
|
2019-01-20 04:02:00 +00:00
|
|
|
source_range: TextRange,
|
2019-01-08 19:33:36 +00:00
|
|
|
completion_kind: CompletionKind,
|
|
|
|
label: String,
|
2019-01-19 14:02:50 +00:00
|
|
|
insert_text: Option<String>,
|
|
|
|
insert_text_format: InsertTextFormat,
|
2019-01-09 15:09:49 +00:00
|
|
|
detail: Option<String>,
|
2019-01-22 02:41:39 +00:00
|
|
|
documentation: Option<String>,
|
2019-01-08 19:33:36 +00:00
|
|
|
lookup: Option<String>,
|
|
|
|
kind: Option<CompletionItemKind>,
|
2019-01-20 04:02:00 +00:00
|
|
|
text_edit: Option<TextEdit>,
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 16:38:34 +00:00
|
|
|
impl Builder {
|
2019-01-08 19:33:36 +00:00
|
|
|
pub(crate) fn add_to(self, acc: &mut Completions) {
|
|
|
|
acc.add(self.build())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn build(self) -> CompletionItem {
|
|
|
|
CompletionItem {
|
2019-01-20 04:02:00 +00:00
|
|
|
source_range: self.source_range,
|
2019-01-08 19:33:36 +00:00
|
|
|
label: self.label,
|
2019-01-09 15:09:49 +00:00
|
|
|
detail: self.detail,
|
2019-01-22 02:41:39 +00:00
|
|
|
documentation: self.documentation,
|
2019-01-19 14:02:50 +00:00
|
|
|
insert_text_format: self.insert_text_format,
|
2019-01-08 19:33:36 +00:00
|
|
|
lookup: self.lookup,
|
|
|
|
kind: self.kind,
|
|
|
|
completion_kind: self.completion_kind,
|
2019-01-20 04:02:00 +00:00
|
|
|
text_edit: self.text_edit,
|
2019-01-19 16:38:34 +00:00
|
|
|
insert_text: self.insert_text,
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-19 16:38:34 +00:00
|
|
|
pub(crate) fn lookup_by(mut self, lookup: impl Into<String>) -> Builder {
|
2019-01-08 19:33:36 +00:00
|
|
|
self.lookup = Some(lookup.into());
|
|
|
|
self
|
|
|
|
}
|
2019-01-19 16:38:34 +00:00
|
|
|
pub(crate) fn insert_text(mut self, insert_text: impl Into<String>) -> Builder {
|
2019-01-19 14:02:50 +00:00
|
|
|
self.insert_text = Some(insert_text.into());
|
|
|
|
self
|
|
|
|
}
|
2019-01-19 14:11:38 +00:00
|
|
|
#[allow(unused)]
|
2019-01-19 16:38:34 +00:00
|
|
|
pub(crate) fn insert_text_format(mut self, insert_text_format: InsertTextFormat) -> Builder {
|
2019-01-19 14:02:50 +00:00
|
|
|
self.insert_text_format = insert_text_format;
|
2019-01-08 19:33:36 +00:00
|
|
|
self
|
|
|
|
}
|
2019-01-19 16:38:34 +00:00
|
|
|
pub(crate) fn snippet(mut self, snippet: impl Into<String>) -> Builder {
|
2019-01-19 14:02:50 +00:00
|
|
|
self.insert_text_format = InsertTextFormat::Snippet;
|
|
|
|
self.insert_text(snippet)
|
|
|
|
}
|
2019-01-19 16:38:34 +00:00
|
|
|
pub(crate) fn kind(mut self, kind: CompletionItemKind) -> Builder {
|
2019-01-08 19:33:36 +00:00
|
|
|
self.kind = Some(kind);
|
|
|
|
self
|
|
|
|
}
|
2019-01-19 14:11:38 +00:00
|
|
|
#[allow(unused)]
|
2019-01-20 04:02:00 +00:00
|
|
|
pub(crate) fn text_edit(mut self, edit: TextEdit) -> Builder {
|
|
|
|
self.text_edit = Some(edit);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
#[allow(unused)]
|
2019-01-19 16:38:34 +00:00
|
|
|
pub(crate) fn detail(self, detail: impl Into<String>) -> Builder {
|
2019-01-09 15:46:02 +00:00
|
|
|
self.set_detail(Some(detail))
|
|
|
|
}
|
2019-01-19 16:38:34 +00:00
|
|
|
pub(crate) fn set_detail(mut self, detail: Option<impl Into<String>>) -> Builder {
|
2019-01-09 15:46:02 +00:00
|
|
|
self.detail = detail.map(Into::into);
|
2019-01-09 15:09:49 +00:00
|
|
|
self
|
|
|
|
}
|
2019-01-22 02:41:39 +00:00
|
|
|
#[allow(unused)]
|
|
|
|
pub(crate) fn documentation(self, docs: impl Into<String>) -> Builder {
|
|
|
|
self.set_documentation(Some(docs))
|
|
|
|
}
|
|
|
|
pub(crate) fn set_documentation(mut self, docs: Option<impl Into<String>>) -> Builder {
|
|
|
|
self.documentation = docs.map(Into::into);
|
|
|
|
self
|
|
|
|
}
|
2019-01-08 19:33:36 +00:00
|
|
|
pub(super) fn from_resolution(
|
|
|
|
mut self,
|
|
|
|
ctx: &CompletionContext,
|
|
|
|
resolution: &hir::Resolution,
|
2019-01-19 16:38:34 +00:00
|
|
|
) -> Builder {
|
2019-01-15 15:50:16 +00:00
|
|
|
let resolved = resolution.def_id.map(|d| d.resolve(ctx.db));
|
2019-01-08 19:33:36 +00:00
|
|
|
let kind = match resolved {
|
|
|
|
PerNs {
|
|
|
|
types: Some(hir::Def::Module(..)),
|
|
|
|
..
|
|
|
|
} => CompletionItemKind::Module,
|
|
|
|
PerNs {
|
|
|
|
types: Some(hir::Def::Struct(..)),
|
|
|
|
..
|
|
|
|
} => CompletionItemKind::Struct,
|
|
|
|
PerNs {
|
|
|
|
types: Some(hir::Def::Enum(..)),
|
|
|
|
..
|
|
|
|
} => CompletionItemKind::Enum,
|
2019-01-11 18:02:12 +00:00
|
|
|
PerNs {
|
|
|
|
types: Some(hir::Def::Trait(..)),
|
|
|
|
..
|
|
|
|
} => CompletionItemKind::Trait,
|
|
|
|
PerNs {
|
|
|
|
types: Some(hir::Def::Type(..)),
|
|
|
|
..
|
|
|
|
} => CompletionItemKind::TypeAlias,
|
|
|
|
PerNs {
|
|
|
|
values: Some(hir::Def::Const(..)),
|
|
|
|
..
|
|
|
|
} => CompletionItemKind::Const,
|
|
|
|
PerNs {
|
|
|
|
values: Some(hir::Def::Static(..)),
|
|
|
|
..
|
|
|
|
} => CompletionItemKind::Static,
|
2019-01-08 19:33:36 +00:00
|
|
|
PerNs {
|
|
|
|
values: Some(hir::Def::Function(function)),
|
|
|
|
..
|
|
|
|
} => return self.from_function(ctx, function),
|
|
|
|
_ => return self,
|
|
|
|
};
|
|
|
|
self.kind = Some(kind);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-01-07 18:12:19 +00:00
|
|
|
pub(super) fn from_function(
|
|
|
|
mut self,
|
|
|
|
ctx: &CompletionContext,
|
|
|
|
function: hir::Function,
|
2019-01-19 16:38:34 +00:00
|
|
|
) -> Builder {
|
2019-01-08 19:33:36 +00:00
|
|
|
// If not an import, add parenthesis automatically.
|
2019-01-10 18:38:04 +00:00
|
|
|
if ctx.use_item_syntax.is_none() && !ctx.is_call {
|
2019-01-23 13:05:13 +00:00
|
|
|
tested_by!(inserts_parens_for_function_calls);
|
2019-01-23 13:21:20 +00:00
|
|
|
let sig = function.signature(ctx.db);
|
|
|
|
if sig.params().is_empty() || sig.has_self_param() && sig.params().len() == 1 {
|
2019-01-19 14:02:50 +00:00
|
|
|
self.insert_text = Some(format!("{}()$0", self.label));
|
2019-01-08 19:33:36 +00:00
|
|
|
} else {
|
2019-01-19 14:02:50 +00:00
|
|
|
self.insert_text = Some(format!("{}($0)", self.label));
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-19 14:02:50 +00:00
|
|
|
self.insert_text_format = InsertTextFormat::Snippet;
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-01-22 13:55:05 +00:00
|
|
|
if let Some(docs) = function.docs(ctx.db) {
|
|
|
|
self.documentation = Some(docs);
|
2019-01-22 02:42:37 +00:00
|
|
|
}
|
|
|
|
|
2019-01-22 23:20:40 +00:00
|
|
|
if let Some(label) = function_label(ctx, function) {
|
2019-01-22 15:54:50 +00:00
|
|
|
self.detail = Some(label);
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
self.kind = Some(CompletionItemKind::Function);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 16:38:34 +00:00
|
|
|
impl<'a> Into<CompletionItem> for Builder {
|
2019-01-08 19:33:36 +00: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 14:02:50 +00:00
|
|
|
|
2019-01-22 23:20:40 +00:00
|
|
|
fn function_label(ctx: &CompletionContext, function: hir::Function) -> Option<String> {
|
|
|
|
let node = function.source(ctx.db).1;
|
|
|
|
|
|
|
|
let label: String = if let Some(body) = node.body() {
|
|
|
|
let body_range = body.syntax().range();
|
|
|
|
let label: String = node
|
|
|
|
.syntax()
|
|
|
|
.children()
|
|
|
|
.filter(|child| !child.range().is_subrange(&body_range)) // Filter out body
|
|
|
|
.filter(|child| ast::Comment::cast(child).is_none()) // Filter out comments
|
|
|
|
.map(|node| node.text().to_string())
|
|
|
|
.collect();
|
|
|
|
label
|
|
|
|
} else {
|
|
|
|
node.syntax().text().to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(label.trim().to_owned())
|
|
|
|
}
|
|
|
|
|
2019-01-19 14:02:50 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn check_completion(test_name: &str, code: &str, kind: CompletionKind) {
|
|
|
|
use crate::mock_analysis::{single_file_with_position, analysis_and_position};
|
|
|
|
use crate::completion::completions;
|
|
|
|
use insta::assert_debug_snapshot_matches;
|
|
|
|
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();
|
|
|
|
let kind_completions: Vec<CompletionItem> = completion_items
|
|
|
|
.into_iter()
|
|
|
|
.filter(|c| c.completion_kind == kind)
|
|
|
|
.collect();
|
|
|
|
assert_debug_snapshot_matches!(test_name, kind_completions);
|
|
|
|
}
|
2019-01-23 13:05:13 +00:00
|
|
|
|
|
|
|
#[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 13:21:20 +00:00
|
|
|
check_reference_completion(
|
|
|
|
"inserts_parens_for_function_calls3",
|
|
|
|
r"
|
|
|
|
struct S {}
|
|
|
|
impl S {
|
|
|
|
fn foo(&self) {}
|
|
|
|
}
|
|
|
|
fn bar(s: &S) {
|
|
|
|
s.f<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
2019-01-23 13:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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<|>();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|