aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/assits.rs
blob: 2da251df5d1c34deef73a707f6d7731736752c10 (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
mod fill_match_arm;

use ra_syntax::{
    TextRange, SourceFile, AstNode,
    algo::find_node_at_offset,
};
use ra_ide_api_light::{
    LocalEdit,
    assists::{
        Assist,
        AssistBuilder
    }
};
use crate::{
    db::RootDatabase,
    FileId
};

/// Return all the assists applicable at the given position.
pub(crate) fn assists(
    db: &RootDatabase,
    file_id: FileId,
    file: &SourceFile,
    range: TextRange,
) -> Vec<LocalEdit> {
    let ctx = AssistCtx::new(db, file_id, file, range);
    [fill_match_arm::fill_match_arm]
        .iter()
        .filter_map(|&assist| ctx.clone().apply(assist))
        .collect()
}

#[derive(Debug, Clone)]
pub struct AssistCtx<'a> {
    file_id: FileId,
    source_file: &'a SourceFile,
    db: &'a RootDatabase,
    range: TextRange,
    should_compute_edit: bool,
}

impl<'a> AssistCtx<'a> {
    pub(crate) fn new(
        db: &'a RootDatabase,
        file_id: FileId,
        source_file: &'a SourceFile,
        range: TextRange,
    ) -> AssistCtx<'a> {
        AssistCtx {
            source_file,
            file_id,
            db,
            range,
            should_compute_edit: false,
        }
    }

    pub fn apply(mut self, assist: fn(AssistCtx) -> Option<Assist>) -> Option<LocalEdit> {
        self.should_compute_edit = true;
        match assist(self) {
            None => None,
            Some(Assist::Edit(e)) => Some(e),
            Some(Assist::Applicable) => unreachable!(),
        }
    }

    #[allow(unused)]
    pub fn check(mut self, assist: fn(AssistCtx) -> Option<Assist>) -> bool {
        self.should_compute_edit = false;
        match assist(self) {
            None => false,
            Some(Assist::Edit(_)) => unreachable!(),
            Some(Assist::Applicable) => true,
        }
    }

    fn build(self, label: impl Into<String>, f: impl FnOnce(&mut AssistBuilder)) -> Option<Assist> {
        if !self.should_compute_edit {
            return Some(Assist::Applicable);
        }
        let mut edit = AssistBuilder::default();
        f(&mut edit);
        Some(edit.build(label))
    }

    pub(crate) fn node_at_offset<N: AstNode>(&self) -> Option<&'a N> {
        find_node_at_offset(self.source_file.syntax(), self.range.start())
    }
}