aboutsummaryrefslogtreecommitdiff
path: root/crates/completion/src/render.rs
blob: c614a7172617bf5a18c86e17b5e1e7c694526f87 (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
//! `render` module provides utilities for rendering completion suggestions
//! into code pieces that will be presented to user.

mod macro_;
mod function;
mod builder_ext;
mod enum_variant;
mod const_;

use hir::{Documentation, HasAttrs};
use ide_db::RootDatabase;
use syntax::TextRange;

use crate::{config::SnippetCap, CompletionContext};

pub(crate) use crate::render::{
    const_::ConstRender, enum_variant::EnumVariantRender, function::FunctionRender,
    macro_::MacroRender,
};

#[derive(Debug)]
pub(crate) struct RenderContext<'a> {
    completion: &'a CompletionContext<'a>,
}

impl<'a> RenderContext<'a> {
    pub fn new(completion: &'a CompletionContext<'a>) -> RenderContext<'a> {
        RenderContext { completion }
    }

    pub fn snippet_cap(&self) -> Option<SnippetCap> {
        self.completion.config.snippet_cap.clone()
    }

    pub fn db(&self) -> &'a RootDatabase {
        &self.completion.db
    }

    pub fn source_range(&self) -> TextRange {
        self.completion.source_range()
    }

    pub fn is_deprecated(&self, node: impl HasAttrs) -> bool {
        node.attrs(self.db()).by_key("deprecated").exists()
    }

    pub fn docs(&self, node: impl HasAttrs) -> Option<Documentation> {
        node.docs(self.db())
    }
}

impl<'a> From<&'a CompletionContext<'a>> for RenderContext<'a> {
    fn from(ctx: &'a CompletionContext<'a>) -> RenderContext<'a> {
        RenderContext::new(ctx)
    }
}