Hover tooltips

This commit is contained in:
Laurenz 2022-12-14 10:10:07 +01:00
parent 9ba4d2c134
commit 0c7fb7d30f
2 changed files with 21 additions and 0 deletions

View File

@ -1,5 +1,7 @@
//! Capabilities for IDE support.
mod highlight;
mod tooltip;
pub use highlight::*;
pub use tooltip::*;

19
src/ide/tooltip.rs Normal file
View File

@ -0,0 +1,19 @@
use crate::model::Value;
use crate::syntax::{LinkedNode, Source, SyntaxKind};
use crate::World;
/// Produce a tooltip which can be shown when a cursor position is hovered.
pub fn tooltip(world: &dyn World, source: &Source, cursor: usize) -> Option<String> {
let leaf = LinkedNode::new(source.root()).leaf_at(cursor)?;
// If a known identifier is under the cursor, provide its documentation.
if let SyntaxKind::Ident(ident) = leaf.kind() {
if let Some(value) = world.library().scope.get(ident) {
if let Value::Func(func) = value {
return func.doc().map(Into::into);
}
}
}
None
}