summaryrefslogtreecommitdiff
path: root/src/ide/mod.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-12-27 16:07:26 +0100
committerLaurenz <laurmaedje@gmail.com>2022-12-27 16:07:26 +0100
commit94b90761ebbdf33686e82feb5a89f98d049c5b65 (patch)
tree19ede419461efc0a9316b4188e092de42ca4be0b /src/ide/mod.rs
parentfd3d3b10ceff64386da93d46dc3ac7ffab83b8e1 (diff)
Bugfixes
Diffstat (limited to 'src/ide/mod.rs')
-rw-r--r--src/ide/mod.rs81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/ide/mod.rs b/src/ide/mod.rs
index 8068d421..f52fa076 100644
--- a/src/ide/mod.rs
+++ b/src/ide/mod.rs
@@ -7,3 +7,84 @@ mod tooltip;
pub use complete::*;
pub use highlight::*;
pub use tooltip::*;
+
+use std::fmt::Write;
+
+use crate::font::{FontInfo, FontStyle};
+
+/// Extract the first sentence of plain text of a piece of documentation.
+///
+/// Removes Markdown formatting.
+fn plain_docs_sentence(docs: &str) -> String {
+ let mut s = unscanny::Scanner::new(docs);
+ let mut output = String::new();
+ let mut link = false;
+ while let Some(c) = s.eat() {
+ match c {
+ '`' => {
+ let mut raw = s.eat_until('`');
+ if (raw.starts_with('{') && raw.ends_with('}'))
+ || (raw.starts_with('[') && raw.ends_with(']'))
+ {
+ raw = &raw[1..raw.len() - 1];
+ }
+
+ s.eat();
+ output.push('`');
+ output.push_str(raw);
+ output.push('`');
+ }
+ '[' => link = true,
+ ']' if link => {
+ if s.eat_if('(') {
+ s.eat_until(')');
+ s.eat();
+ } else if s.eat_if('[') {
+ s.eat_until(']');
+ s.eat();
+ }
+ link = false
+ }
+ '*' | '_' => {}
+ '.' => {
+ output.push('.');
+ break;
+ }
+ _ => output.push(c),
+ }
+ }
+
+ output
+}
+
+/// Create a short description of a font family.
+pub fn summarize_font_family<'a>(variants: impl Iterator<Item = &'a FontInfo>) -> String {
+ let mut infos: Vec<_> = variants.collect();
+ infos.sort_by_key(|info| info.variant);
+
+ let mut has_italic = false;
+ let mut min_weight = u16::MAX;
+ let mut max_weight = 0;
+ for info in &infos {
+ let weight = info.variant.weight.to_number();
+ has_italic |= info.variant.style == FontStyle::Italic;
+ min_weight = min_weight.min(weight);
+ max_weight = min_weight.max(weight);
+ }
+
+ let count = infos.len();
+ let s = if count == 1 { "" } else { "s" };
+ let mut detail = format!("{count} variant{s}.");
+
+ if min_weight == max_weight {
+ write!(detail, " Weight {min_weight}.").unwrap();
+ } else {
+ write!(detail, " Weights {min_weight}–{max_weight}.").unwrap();
+ }
+
+ if has_italic {
+ detail.push_str(" Has italics.");
+ }
+
+ detail
+}