rust/crates/ra_ide_api/src/completion.rs

116 lines
3.8 KiB
Rust
Raw Normal View History

2019-01-08 13:33:36 -06:00
mod completion_item;
mod completion_context;
mod presentation;
2019-01-08 13:33:36 -06:00
mod complete_dot;
2019-02-24 08:01:56 -06:00
mod complete_struct_literal;
2019-02-24 14:49:47 -06:00
mod complete_pattern;
2019-01-08 13:33:36 -06:00
mod complete_fn_param;
mod complete_keyword;
mod complete_snippet;
mod complete_path;
mod complete_scope;
2019-01-20 23:19:51 -06:00
mod complete_postfix;
2019-01-08 13:33:36 -06:00
2019-01-26 02:20:30 -06:00
use ra_db::SourceDatabase;
use ra_syntax::ast::{self, AstNode};
2019-01-08 13:33:36 -06:00
use crate::{
db,
FilePosition,
2019-01-08 13:33:36 -06:00
completion::{
completion_item::{Completions, CompletionKind},
completion_context::CompletionContext,
},
2019-02-24 12:54:13 -06:00
2019-01-08 13:33:36 -06:00
};
2019-02-24 12:54:13 -06:00
#[cfg(test)]
use crate::completion::completion_item::{do_completion, check_completion};
2019-01-08 13:33:36 -06:00
pub use crate::completion::completion_item::{CompletionItem, CompletionItemKind, InsertTextFormat};
2019-01-08 13:33:36 -06:00
/// Main entry point for completion. We run completion as a two-phase process.
///
/// First, we look at the position and collect a so-called `CompletionContext.
/// This is a somewhat messy process, because, during completion, syntax tree is
/// incomplete and can look really weird.
///
/// Once the context is collected, we run a series of completion routines which
2019-02-11 10:18:27 -06:00
/// look at the context and produce completion items. One subtlety about this
2019-01-08 13:33:36 -06:00
/// phase is that completion engine should not filter by the substring which is
/// already present, it should give all possible variants for the identifier at
/// the caret. In other words, for
///
/// ```no-run
/// fn f() {
/// let foo = 92;
/// let _ = bar<|>
/// }
/// ```
///
/// `foo` *should* be present among the completion variants. Filtering by
/// identifier prefix/fuzzy match should be done higher in the stack, together
/// with ordering of completions (currently this is done by the client).
pub(crate) fn completions(db: &db::RootDatabase, position: FilePosition) -> Option<Completions> {
2019-01-26 02:51:36 -06:00
let original_file = db.parse(position.file_id);
let ctx = CompletionContext::new(db, &original_file, position)?;
2019-01-08 13:33:36 -06:00
let mut acc = Completions::default();
complete_fn_param::complete_fn_param(&mut acc, &ctx);
complete_keyword::complete_expr_keyword(&mut acc, &ctx);
complete_keyword::complete_use_tree_keyword(&mut acc, &ctx);
complete_snippet::complete_expr_snippet(&mut acc, &ctx);
complete_snippet::complete_item_snippet(&mut acc, &ctx);
complete_path::complete_path(&mut acc, &ctx);
complete_scope::complete_scope(&mut acc, &ctx);
complete_dot::complete_dot(&mut acc, &ctx);
2019-02-24 08:01:56 -06:00
complete_struct_literal::complete_struct_literal(&mut acc, &ctx);
2019-02-24 14:49:47 -06:00
complete_pattern::complete_pattern(&mut acc, &ctx);
2019-01-20 23:19:51 -06:00
complete_postfix::complete_postfix(&mut acc, &ctx);
Some(acc)
2019-01-08 13:33:36 -06:00
}
pub fn function_label(node: &ast::FnDef) -> Option<String> {
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
.filter(|child| ast::Attr::cast(child).is_none()) // Filter out attributes
.map(|node| node.text().to_string())
.collect();
label
} else {
node.syntax().text().to_string()
};
Some(label.trim().to_owned())
}
pub fn const_label(node: &ast::ConstDef) -> String {
let label: String = node
.syntax()
.children()
.filter(|child| ast::Comment::cast(child).is_none())
.filter(|child| ast::Attr::cast(child).is_none())
.map(|node| node.text().to_string())
.collect();
label.trim().to_owned()
}
pub fn type_label(node: &ast::TypeAliasDef) -> String {
let label: String = node
.syntax()
.children()
.filter(|child| ast::Comment::cast(child).is_none())
.filter(|child| ast::Attr::cast(child).is_none())
.map(|node| node.text().to_string())
.collect();
label.trim().to_owned()
}