2020-11-01 10:48:42 +00:00
|
|
|
//! Renderer for `const` fields.
|
|
|
|
|
2021-12-21 15:34:55 +00:00
|
|
|
use hir::{AsAssocItem, HirDisplay};
|
2021-01-20 17:38:12 +00:00
|
|
|
use ide_db::SymbolKind;
|
2020-11-01 09:59:43 +00:00
|
|
|
|
2021-10-27 15:18:42 +00:00
|
|
|
use crate::{item::CompletionItem, render::RenderContext};
|
2020-11-01 09:59:43 +00:00
|
|
|
|
2021-07-21 17:50:28 +00:00
|
|
|
pub(crate) fn render_const(ctx: RenderContext<'_>, const_: hir::Const) -> Option<CompletionItem> {
|
2021-11-08 18:41:16 +00:00
|
|
|
let _p = profile::span("render_const");
|
2021-12-21 14:04:19 +00:00
|
|
|
render(ctx, const_)
|
2020-11-03 07:33:13 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 14:04:19 +00:00
|
|
|
fn render(ctx: RenderContext<'_>, const_: hir::Const) -> Option<CompletionItem> {
|
|
|
|
let db = ctx.db();
|
2022-06-26 06:45:30 +00:00
|
|
|
let name = const_.name(db)?;
|
2022-08-01 06:19:49 +00:00
|
|
|
let (name, escaped_name) = (name.unescaped().to_smol_str(), name.to_smol_str());
|
2021-12-21 15:34:55 +00:00
|
|
|
let detail = const_.display(db).to_string();
|
2021-12-21 14:04:19 +00:00
|
|
|
|
2022-12-23 07:08:08 +00:00
|
|
|
let mut item = CompletionItem::new(SymbolKind::Const, ctx.source_range(), name);
|
2021-12-21 14:04:19 +00:00
|
|
|
item.set_documentation(ctx.docs(const_))
|
|
|
|
.set_deprecated(ctx.is_deprecated(const_) || ctx.is_deprecated_assoc_item(const_))
|
2022-02-02 01:05:49 +00:00
|
|
|
.detail(detail)
|
|
|
|
.set_relevance(ctx.completion_relevance());
|
2021-12-21 14:04:19 +00:00
|
|
|
|
|
|
|
if let Some(actm) = const_.as_assoc_item(db) {
|
|
|
|
if let Some(trt) = actm.containing_trait_or_trait_impl(db) {
|
|
|
|
item.trait_name(trt.name(db).to_smol_str());
|
2021-07-03 20:29:47 +00:00
|
|
|
}
|
2020-11-01 09:59:43 +00:00
|
|
|
}
|
2022-06-26 06:45:30 +00:00
|
|
|
item.insert_text(escaped_name);
|
2020-11-01 09:59:43 +00:00
|
|
|
|
2023-05-24 16:04:29 +00:00
|
|
|
Some(item.build(ctx.db()))
|
2020-11-01 09:59:43 +00:00
|
|
|
}
|