From 18e2aa2cea9e89558683c95d050febef63c4af53 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 29 Jan 2023 00:18:42 +0100 Subject: [PATCH] Differentiate between text and code tooltips --- src/ide/tooltip.rs | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/ide/tooltip.rs b/src/ide/tooltip.rs index 26eff6867..4a03e5b80 100644 --- a/src/ide/tooltip.rs +++ b/src/ide/tooltip.rs @@ -13,7 +13,7 @@ pub fn tooltip( world: &(dyn World + 'static), source: &Source, cursor: usize, -) -> Option { +) -> Option { let leaf = LinkedNode::new(source.root()).leaf_at(cursor)?; named_param_tooltip(world, &leaf) @@ -21,8 +21,17 @@ pub fn tooltip( .or_else(|| expr_tooltip(world, &leaf)) } +/// A hover tooltip. +#[derive(Debug, Clone)] +pub enum Tooltip { + /// A string of text. + Text(String), + /// A string of Typst code. + Code(String), +} + /// Tooltip for a hovered expression. -fn expr_tooltip(world: &(dyn World + 'static), leaf: &LinkedNode) -> Option { +fn expr_tooltip(world: &(dyn World + 'static), leaf: &LinkedNode) -> Option { let expr = leaf.cast::()?; if !expr.hashtag() { return None; @@ -32,12 +41,12 @@ fn expr_tooltip(world: &(dyn World + 'static), leaf: &LinkedNode) -> Option Option Option { fn named_param_tooltip( world: &(dyn World + 'static), leaf: &LinkedNode, -) -> Option { +) -> Option { let (info, named) = if_chain! { // Ensure that we are in a named pair in the arguments to a function // call or set rule. @@ -120,7 +129,7 @@ fn named_param_tooltip( if let Some(ident) = leaf.cast::(); if let Some(param) = info.param(&ident); then { - return Some(plain_docs_sentence(param.docs)); + return Some(Tooltip::Text(plain_docs_sentence(param.docs))); } } @@ -130,7 +139,7 @@ fn named_param_tooltip( if let Some(param) = info.param(&named.name()); if let Some(docs) = find_string_doc(¶m.cast, &string.get()); then { - return Some(docs.into()); + return Some(Tooltip::Text(docs.into())); } } @@ -152,7 +161,7 @@ fn find_string_doc(info: &CastInfo, string: &str) -> Option<&'static str> { fn font_family_tooltip( world: &(dyn World + 'static), leaf: &LinkedNode, -) -> Option { +) -> Option { if_chain! { // Ensure that we are on top of a string. if let Some(string) = leaf.cast::(); @@ -178,7 +187,7 @@ fn font_family_tooltip( then { let detail = summarize_font_family(iter); - return Some(detail); + return Some(Tooltip::Text(detail)); } };