rust/crates/completion/src/render.rs

58 lines
1.4 KiB
Rust
Raw Normal View History

2020-11-01 03:35:04 -06:00
//! `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;
2020-11-01 03:59:43 -06:00
mod const_;
2020-11-01 04:10:02 -06:00
mod type_alias;
2020-11-01 03:35:04 -06:00
2020-11-01 03:59:43 -06:00
use hir::{Documentation, HasAttrs};
2020-11-01 03:35:04 -06:00
use ide_db::RootDatabase;
use syntax::TextRange;
use crate::{config::SnippetCap, CompletionContext};
pub(crate) use crate::render::{
2020-11-01 03:59:43 -06:00
const_::ConstRender, enum_variant::EnumVariantRender, function::FunctionRender,
2020-11-01 04:10:02 -06:00
macro_::MacroRender, type_alias::TypeAliasRender,
2020-11-01 03:35:04 -06:00
};
#[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()
}
2020-11-01 03:59:43 -06:00
pub fn docs(&self, node: impl HasAttrs) -> Option<Documentation> {
node.docs(self.db())
}
2020-11-01 03:35:04 -06:00
}
impl<'a> From<&'a CompletionContext<'a>> for RenderContext<'a> {
fn from(ctx: &'a CompletionContext<'a>) -> RenderContext<'a> {
RenderContext::new(ctx)
}
}