aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/completion.rs
blob: 67dfd6f2e6ab07ad14296cbc20e4d7f5c7f1665e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//! FIXME: write short doc here

mod completion_item;
mod completion_context;
mod presentation;

mod complete_dot;
mod complete_record_literal;
mod complete_record_pattern;
mod complete_pattern;
mod complete_fn_param;
mod complete_keyword;
mod complete_snippet;
mod complete_path;
mod complete_scope;
mod complete_postfix;
mod complete_macro_in_item_position;
mod complete_trait_impl;
#[cfg(test)]
mod test_utils;

use ra_ide_db::RootDatabase;

use crate::{
    completion::{
        completion_context::CompletionContext,
        completion_item::{CompletionKind, Completions},
    },
    FilePosition,
};

pub use crate::completion::completion_item::{
    CompletionItem, CompletionItemKind, InsertTextFormat,
};
use either::Either;
use hir::{StructField, Type};
use ra_syntax::{
    ast::{self, NameOwner},
    SmolStr,
};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CompletionConfig {
    pub enable_postfix_completions: bool,
    pub add_call_parenthesis: bool,
    pub add_call_argument_snippets: bool,
}

impl Default for CompletionConfig {
    fn default() -> Self {
        CompletionConfig {
            enable_postfix_completions: true,
            add_call_parenthesis: true,
            add_call_argument_snippets: true,
        }
    }
}

/// 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
/// look at the context and produce completion items. One subtlety about this
/// 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: &RootDatabase,
    position: FilePosition,
    config: &CompletionConfig,
) -> Option<Completions> {
    let ctx = CompletionContext::new(db, position, config)?;

    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);
    complete_record_literal::complete_record_literal(&mut acc, &ctx);
    complete_record_pattern::complete_record_pattern(&mut acc, &ctx);
    complete_pattern::complete_pattern(&mut acc, &ctx);
    complete_postfix::complete_postfix(&mut acc, &ctx);
    complete_macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx);
    complete_trait_impl::complete_trait_impl(&mut acc, &ctx);

    Some(acc)
}

pub(crate) fn get_missing_fields(
    ctx: &CompletionContext,
    record: Either<&ast::RecordLit, &ast::RecordPat>,
) -> Option<Vec<(StructField, Type)>> {
    let (ty, variant) = match record {
        Either::Left(record_lit) => (
            ctx.sema.type_of_expr(&record_lit.clone().into())?,
            ctx.sema.resolve_record_literal(record_lit)?,
        ),
        Either::Right(record_pat) => (
            ctx.sema.type_of_pat(&record_pat.clone().into())?,
            ctx.sema.resolve_record_pattern(record_pat)?,
        ),
    };

    let already_present_names = get_already_present_names(record);
    Some(
        ty.variant_fields(ctx.db, variant)
            .into_iter()
            .filter(|(field, _)| {
                !already_present_names.contains(&SmolStr::from(field.name(ctx.db).to_string()))
            })
            .collect(),
    )
}

fn get_already_present_names(record: Either<&ast::RecordLit, &ast::RecordPat>) -> Vec<SmolStr> {
    // TODO kb have a single match
    match record {
        Either::Left(record_lit) => record_lit
            .record_field_list()
            .map(|field_list| field_list.fields())
            .map(|fields| {
                fields
                    .into_iter()
                    .filter_map(|field| field.name_ref())
                    .map(|name_ref| name_ref.text().clone())
                    .collect()
            })
            .unwrap_or_default(),
        Either::Right(record_pat) => record_pat
            .record_field_pat_list()
            .map(|pat_list| pat_list.bind_pats())
            .map(|bind_pats| {
                bind_pats
                    .into_iter()
                    .filter_map(|pat| pat.name())
                    .map(|name| name.text().clone())
                    .collect()
            })
            .unwrap_or_default(),
    }
}