From e8bb153b19ffa0ad815eb8934d40cd89ce550b99 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 8 Jul 2020 22:37:35 +0200 Subject: Add Markup type --- crates/ra_ide/src/markup.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 crates/ra_ide/src/markup.rs (limited to 'crates/ra_ide/src/markup.rs') diff --git a/crates/ra_ide/src/markup.rs b/crates/ra_ide/src/markup.rs new file mode 100644 index 000000000..2f2b3cc25 --- /dev/null +++ b/crates/ra_ide/src/markup.rs @@ -0,0 +1,38 @@ +//! Markdown formatting. +//! +//! Sometimes, we want to display a "rich text" in the UI. At the moment, we use +//! markdown for this purpose. It doesn't feel like a right option, but that's +//! what is used by LSP, so let's keep it simple. +use std::fmt; + +#[derive(Default, Debug)] +pub struct Markup { + text: String, +} + +impl From for String { + fn from(markup: Markup) -> Self { + markup.text + } +} + +impl fmt::Display for Markup { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&self.text, f) + } +} + +impl Markup { + pub fn as_str(&self) -> &str { + self.text.as_str() + } + pub fn is_empty(&self) -> bool { + self.text.is_empty() + } + pub fn push_section(&mut self, section: &str) { + if !self.text.is_empty() { + self.text.push_str("\n\n___\n"); + } + self.text.push_str(section); + } +} -- cgit v1.2.3 From 3a26752c665111499b01c34b90aadd94afcd8b28 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 9 Jul 2020 10:03:28 +0200 Subject: Reduce API --- crates/ra_ide/src/markup.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'crates/ra_ide/src/markup.rs') diff --git a/crates/ra_ide/src/markup.rs b/crates/ra_ide/src/markup.rs index 2f2b3cc25..212ca80b7 100644 --- a/crates/ra_ide/src/markup.rs +++ b/crates/ra_ide/src/markup.rs @@ -16,6 +16,12 @@ impl From for String { } } +impl From for Markup { + fn from(text: String) -> Self { + Markup { text } + } +} + impl fmt::Display for Markup { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.text, f) @@ -26,13 +32,4 @@ impl Markup { pub fn as_str(&self) -> &str { self.text.as_str() } - pub fn is_empty(&self) -> bool { - self.text.is_empty() - } - pub fn push_section(&mut self, section: &str) { - if !self.text.is_empty() { - self.text.push_str("\n\n___\n"); - } - self.text.push_str(section); - } } -- cgit v1.2.3 From e7c47eb7f599da93f64c5a8d4f8e83ddd3fa1baa Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 9 Jul 2020 10:19:37 +0200 Subject: Streamline --- crates/ra_ide/src/markup.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'crates/ra_ide/src/markup.rs') diff --git a/crates/ra_ide/src/markup.rs b/crates/ra_ide/src/markup.rs index 212ca80b7..60c193c40 100644 --- a/crates/ra_ide/src/markup.rs +++ b/crates/ra_ide/src/markup.rs @@ -32,4 +32,7 @@ impl Markup { pub fn as_str(&self) -> &str { self.text.as_str() } + pub fn fenced_block(contents: &impl fmt::Display) -> Markup { + format!("```rust\n{}\n```", contents).into() + } } -- cgit v1.2.3