1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use crate::ast;
use crate::shims::{to_offset, to_range};
use crate::ProgramInfo;
use collectors::collect_var_asgns;
use libslide::*;
use std::collections::HashSet;
use tower_lsp::lsp_types::*;
pub(crate) fn get_hover_info(
position: Position,
program_info: &ProgramInfo,
context: &ProgramContext,
) -> Option<Hover> {
let position = to_offset(&position, &program_info.source);
let tightest_expr = ast::get_tightest_expr(position, &program_info.original)?;
let range = Some(to_range(&tightest_expr.span, &program_info.source));
let var_asgns = collect_var_asgns(&program_info.simplified);
let simplified = if let Some(var) = tightest_expr.get_var() {
match var_asgns.get(&var) {
Some(asgns) => fmt_asgn_definitions(asgns),
None => "???".to_string(),
}
} else {
let rules = build_rules(context).ok()?;
evaluate_expr(tightest_expr.clone(), &rules, context).to_string()
};
let hover_info = fmt_hover_info(simplified);
Some(Hover {
contents: HoverContents::Scalar(MarkedString::LanguageString(LanguageString {
language: "slide".to_string(),
value: hover_info,
})),
range,
})
}
fn fmt_asgn_definitions(asgns: &[&Assignment]) -> String {
let mut seen = HashSet::new();
asgns
.iter()
.filter_map(|asgn| {
if seen.contains(&asgn.rhs) {
return None;
}
seen.insert(&asgn.rhs);
Some(asgn.rhs.to_string())
})
.collect::<Vec<_>>()
.join("\n")
}
fn fmt_hover_info(simplified_vals: String) -> String {
simplified_vals
.lines()
.map(|l| format!("= {}", l))
.collect::<Vec<_>>()
.join("\n")
}