Skip to main content

logicaffeine_lsp/
teach_md.rs

1//! Markdown rendering for the shared teaching brain
2//! (`logicaffeine_language::teach`) — the LSP's presentation of a lesson.
3//!
4//! One renderer feeds hover AND completion documentation, so the two can
5//! never phrase a lesson differently; the terminal REPL renders the same
6//! lessons in ANSI. The quickguide link goes through [`guide_url`] — one
7//! function to change if the guide ever moves off the repository.
8
9use lsp_types::{Documentation, MarkupContent, MarkupKind};
10
11use logicaffeine_language::teach::ConstructDoc;
12
13/// The canonical "read more" URL for a quickguide heading slug.
14pub fn guide_url(anchor: &str) -> String {
15    format!(
16        "https://github.com/Brahmastra-Labs/logicaffeine/blob/main/LOGOS_QUICKGUIDE.md#{anchor}"
17    )
18}
19
20/// The full lesson as hover markdown: name, one plain sentence, the runnable
21/// example, the socratic question or tip, and the guide link when one fits.
22pub fn keyword_hover_md(doc: &ConstructDoc) -> String {
23    let mut md = format!(
24        "**{}**\n\n{}\n\n```\n{}\n```\n\n{}",
25        doc.name, doc.what, doc.example, doc.question_or_tip
26    );
27    push_guide_link(&mut md, doc);
28    md
29}
30
31/// The lesson for a `##` block header, keeping the Block Header banner.
32pub fn block_hover_md(doc: &ConstructDoc) -> String {
33    let mut md = format!(
34        "**Block Header** — {}\n\n```\n{}\n```\n\n{}",
35        doc.what, doc.example, doc.question_or_tip
36    );
37    push_guide_link(&mut md, doc);
38    md
39}
40
41/// The lesson as completion-item documentation (same rendering as hover —
42/// the editor's docs panel and the hover card always agree).
43pub fn completion_docs(doc: &ConstructDoc) -> Documentation {
44    Documentation::MarkupContent(MarkupContent {
45        kind: MarkupKind::Markdown,
46        value: keyword_hover_md(doc),
47    })
48}
49
50fn push_guide_link(md: &mut String, doc: &ConstructDoc) {
51    if let Some(anchor) = doc.guide_anchor {
52        md.push_str(&format!("\n\n[Quick Guide]({})", guide_url(anchor)));
53    }
54}
55
56#[cfg(test)]
57#[path = "../../logicaffeine_tests/unit/logicaffeine_lsp/teach_md/tests.rs"]
58mod tests;