summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-11-09 12:18:58 +0100
committerLaurenz <laurmaedje@gmail.com>2024-11-13 10:21:40 +0100
commit0b143c7df9bf21db87089cb2aa4dead0f65ed425 (patch)
tree3ab036d57060793edd943d11b4298da7f8a8eca9
parent3d1cad2bec2d7f3f8705465ec91a82af491cf597 (diff)
Star import tooltip
-rw-r--r--crates/typst-ide/src/tooltip.rs35
1 files changed, 34 insertions, 1 deletions
diff --git a/crates/typst-ide/src/tooltip.rs b/crates/typst-ide/src/tooltip.rs
index 318ff0ce..0936f278 100644
--- a/crates/typst-ide/src/tooltip.rs
+++ b/crates/typst-ide/src/tooltip.rs
@@ -6,12 +6,16 @@ use typst::engine::Sink;
use typst::foundations::{repr, Capturer, CastInfo, Repr, Value};
use typst::layout::Length;
use typst::model::Document;
+use typst::syntax::ast::AstNode;
use typst::syntax::{ast, LinkedNode, Side, Source, SyntaxKind};
use typst::utils::{round_with_precision, Numeric};
use typst::World;
use typst_eval::CapturesVisitor;
-use crate::{analyze_expr, analyze_labels, plain_docs_sentence, summarize_font_family};
+use crate::{
+ analyze_expr, analyze_import, analyze_labels, plain_docs_sentence,
+ summarize_font_family,
+};
/// Describe the item under the cursor.
///
@@ -33,6 +37,7 @@ pub fn tooltip(
named_param_tooltip(world, &leaf)
.or_else(|| font_tooltip(world, &leaf))
.or_else(|| document.and_then(|doc| label_tooltip(doc, &leaf)))
+ .or_else(|| import_tooltip(world, &leaf))
.or_else(|| expr_tooltip(world, &leaf))
.or_else(|| closure_tooltip(&leaf))
}
@@ -106,6 +111,25 @@ fn expr_tooltip(world: &dyn World, leaf: &LinkedNode) -> Option<Tooltip> {
(!tooltip.is_empty()).then(|| Tooltip::Code(tooltip.into()))
}
+/// Tooltips for imports.
+fn import_tooltip(world: &dyn World, leaf: &LinkedNode) -> Option<Tooltip> {
+ if_chain! {
+ if let Some(parent) = leaf.parent();
+ if let Some(import) = parent.cast::<ast::ModuleImport>();
+ if let Some(node) = parent.find(import.source().span());
+ if let Some(value) = analyze_import(world, &node);
+ if let Some(scope) = value.scope();
+ then {
+ let names: Vec<_> =
+ scope.iter().map(|(name, ..)| eco_format!("`{name}`")).collect();
+ let list = repr::separated_list(&names, "and");
+ return Some(Tooltip::Text(eco_format!("This star imports {list}")));
+ }
+ }
+
+ None
+}
+
/// Tooltip for a hovered closure.
fn closure_tooltip(leaf: &LinkedNode) -> Option<Tooltip> {
// Only show this tooltip when hovering over the equals sign or arrow of
@@ -317,4 +341,13 @@ mod tests {
test("#let f(x) = x + y", 11, Side::Before)
.must_be_text("This closure captures `y`");
}
+
+ #[test]
+ fn test_tooltip_star_import() {
+ let world = TestWorld::new("#import \"other.typ\": *")
+ .with_source("other.typ", "#let (a, b, c) = (1, 2, 3)");
+ test_with_world(&world, 21, Side::Before).must_be_none();
+ test_with_world(&world, 21, Side::After)
+ .must_be_text("This star imports `a`, `b`, and `c`");
+ }
}