mun_language_server 0.4.0

Provides a language server protocol server for the Mun language
Documentation
use super::{CompletionItem, CompletionKind, RenderContext};
use crate::SymbolKind;
use mun_hir::HirDisplay;

/// Similar to [`Render<'a>`] but used to render a completion item for a function
pub(super) struct FunctionRender<'a> {
    ctx: RenderContext<'a>,
    name: String,
    func: mun_hir::Function,
}

impl<'a> FunctionRender<'a> {
    /// Constructs a new `FunctionRender`
    pub fn new(
        ctx: RenderContext<'a>,
        local_name: Option<String>,
        func: mun_hir::Function,
    ) -> Option<FunctionRender<'a>> {
        let name = local_name.unwrap_or_else(|| func.name(ctx.db()).to_string());

        Some(Self { ctx, name, func })
    }

    /// Constructs a [`CompletionItem`] for the wrapped function.
    pub fn render(self) -> CompletionItem {
        CompletionItem::builder(CompletionKind::Reference, self.name.clone())
            .kind(SymbolKind::Function)
            .detail(self.detail())
            .finish()
    }

    /// Returns the detail text to add to the completion. This currently returns `-> <ret_ty>`.
    fn detail(&self) -> String {
        let ty = self.func.ret_type(self.ctx.db());
        format!("-> {}", ty.display(self.ctx.db()))
    }
}