use lsp_types::{
CompletionItem,
CompletionItemKind,
Documentation,
InsertTextFormat,
MarkupContent,
MarkupKind,
};
use crate::server::LspServerConfig;
pub(super) trait Snippet {
const LABEL: &'static str;
const DOCUMENTATION: &'static str;
const SNIPPET: &'static str;
fn item(config: &LspServerConfig) -> CompletionItem {
let documentation = match config.capabilities.completion_markdown {
true => Some(Documentation::MarkupContent(MarkupContent {
kind: MarkupKind::Markdown,
value: String::from(
Self::DOCUMENTATION
.replace("```<adastra>", &format!("```{}", config.language_id)),
),
})),
false => Some(Documentation::MarkupContent(MarkupContent {
kind: MarkupKind::PlainText,
value: {
let mut documentation = String::new();
let mut is_first = true;
for line in Self::DOCUMENTATION.lines() {
if line.starts_with("```") {
continue;
}
match is_first {
true => is_first = false,
false => documentation.push('\n'),
}
documentation.push_str(line);
}
documentation
},
})),
};
let mut text = String::new();
let mut is_first = true;
for mut line in Self::SNIPPET.lines() {
match is_first {
true => is_first = false,
false => text.push('\n'),
}
if line.starts_with(' ') {
line = &line[1..];
}
text.push_str(line);
}
CompletionItem {
label: String::from(Self::LABEL),
kind: Some(CompletionItemKind::SNIPPET),
documentation,
insert_text: Some(text),
insert_text_format: Some(InsertTextFormat::SNIPPET),
..Default::default()
}
}
}
macro_rules! markup {
(
$(#[doc = $doc:expr])*
) => { concat!($($doc, "\n"),*) };
}
pub(super) struct SnippetFn;
impl Snippet for SnippetFn {
const LABEL: &'static str = "fn";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}
pub(super) struct SnippetStruct;
impl Snippet for SnippetStruct {
const LABEL: &'static str = "struct";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}
pub(super) struct SnippetSelf;
impl Snippet for SnippetSelf {
const LABEL: &'static str = "self";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}
pub(super) struct SnippetCrate;
impl Snippet for SnippetCrate {
const LABEL: &'static str = "crate";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}
pub(super) struct SnippetTrue;
impl Snippet for SnippetTrue {
const LABEL: &'static str = "true";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}
pub(super) struct SnippetFalse;
impl Snippet for SnippetFalse {
const LABEL: &'static str = "false";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}
pub(super) struct SnippetMax;
impl Snippet for SnippetMax {
const LABEL: &'static str = "max";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}
pub(super) struct SnippetLen;
impl Snippet for SnippetLen {
const LABEL: &'static str = "len";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}
pub(super) struct SnippetUse;
impl Snippet for SnippetUse {
const LABEL: &'static str = "use";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}
pub(super) struct SnippetLet;
impl Snippet for SnippetLet {
const LABEL: &'static str = "let";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}
pub(super) struct SnippetIf;
impl Snippet for SnippetIf {
const LABEL: &'static str = "if";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}
pub(super) struct SnippetMatch;
impl Snippet for SnippetMatch {
const LABEL: &'static str = "match";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}
pub(super) struct SnippetMatchArm;
impl Snippet for SnippetMatchArm {
const LABEL: &'static str = "case";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}
pub(super) struct SnippetMatchElse;
impl Snippet for SnippetMatchElse {
const LABEL: &'static str = "else";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}
pub(super) struct SnippetFor;
impl Snippet for SnippetFor {
const LABEL: &'static str = "for";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}
pub(super) struct SnippetLoop;
impl Snippet for SnippetLoop {
const LABEL: &'static str = "loop";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}
pub(super) struct SnippetBreak;
impl Snippet for SnippetBreak {
const LABEL: &'static str = "break";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}
pub(super) struct SnippetContinue;
impl Snippet for SnippetContinue {
const LABEL: &'static str = "continue";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}
pub(super) struct SnippetReturn;
impl Snippet for SnippetReturn {
const LABEL: &'static str = "return";
const DOCUMENTATION: &'static str = markup! {
};
const SNIPPET: &'static str = markup! {
};
}