
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1209 · main · 10 min 03 s
1Étoiles
0Forks
0Issues
| 1 | //! Editor queries over an [`Analysis`]: what the language server answers. |
| 2 | |
| 3 | use louise_hir::body::{Expr, Pat}; |
| 4 | use louise_hir::{DefId, DefKind, TypeRef}; |
| 5 | use louise_span::{FileId, TextRange, TextSize}; |
| 6 | use louise_types::Printer; |
| 7 | |
| 8 | use crate::Analysis; |
| 9 | |
| 10 | /// Hover content: Markdown, and the range it describes. |
| 11 | #[derive(Debug, PartialEq)] |
| 12 | pub struct Hover { |
| 13 | pub range: TextRange, |
| 14 | pub markdown: String, |
| 15 | } |
| 16 | |
| 17 | /// The type (with effects) of whatever is at `offset`: an item's name, a |
| 18 | /// reference to an item, a local binding, or the innermost expression. |
| 19 | pub fn hover(analysis: &Analysis, file: FileId, offset: TextSize) -> Option<Hover> { |
| 20 | let hir = &analysis.hir; |
| 21 | for (id, def) in hir.defs.iter() { |
| 22 | if def.span.file == file |
| 23 | && !def.name.is_empty() |
| 24 | && def.span.range.contains_inclusive(offset) |
| 25 | { |
| 26 | return Some(Hover { |
| 27 | range: def.span.range, |
| 28 | markdown: item_markdown(analysis, id), |
| 29 | }); |
| 30 | } |
| 31 | } |
| 32 | let mut best: Option<(TextRange, String)> = None; |
| 33 | let mut consider = |range: TextRange, text: &dyn Fn() -> String| { |
| 34 | if range.contains_inclusive(offset) |
| 35 | && best.as_ref().is_none_or(|(r, _)| range.len() < r.len()) |
| 36 | { |
| 37 | best = Some((range, text())); |
| 38 | } |
| 39 | }; |
| 40 | for (id, f) in hir.fn_defs() { |
| 41 | if hir.modules[hir.def(id).module].file != file { |
| 42 | continue; |
| 43 | } |
| 44 | let (Some(body), Some(results)) = (&f.body, analysis.typeck.bodies.get(&id)) else { |
| 45 | continue; |
| 46 | }; |
| 47 | for (e, expr) in body.exprs.iter() { |
| 48 | let range = body.expr_span(e); |
| 49 | let ty = &results.types.exprs[e.index()]; |
| 50 | match expr { |
| 51 | Expr::Def(def) => consider(range, &|| item_markdown(analysis, *def)), |
| 52 | Expr::Local(l) if !body.locals[*l].synthetic => consider(range, &|| { |
| 53 | code(&format!( |
| 54 | "{}: {}", |
| 55 | body.locals[*l].name, |
| 56 | results.show(hir, ty) |
| 57 | )) |
| 58 | }), |
| 59 | _ => consider(range, &|| code(&results.show(hir, ty))), |
| 60 | } |
| 61 | } |
| 62 | for (p, pat) in body.pats.iter() { |
| 63 | if let Pat::Bind(l) = pat |
| 64 | && !body.locals[*l].synthetic |
| 65 | { |
| 66 | let ty = &results.types.locals[l.index()]; |
| 67 | let text = || { |
| 68 | code(&format!( |
| 69 | "{}: {}", |
| 70 | body.locals[*l].name, |
| 71 | results.show(hir, ty) |
| 72 | )) |
| 73 | }; |
| 74 | consider(body.pat_span(p), &text); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | best.map(|(range, markdown)| Hover { range, markdown }) |
| 79 | } |
| 80 | |
| 81 | fn code(text: &str) -> String { |
| 82 | format!("```louise\n{text}\n```") |
| 83 | } |
| 84 | |
| 85 | pub(super) fn item_markdown(analysis: &Analysis, id: DefId) -> String { |
| 86 | let hir = &analysis.hir; |
| 87 | let def = hir.def(id); |
| 88 | let scheme = analysis.typeck.schemes.get(&id).map(|s| { |
| 89 | Printer { |
| 90 | hir, |
| 91 | ctx: None, |
| 92 | gen_names: &[], |
| 93 | rigid_names: &[], |
| 94 | } |
| 95 | .scheme(s) |
| 96 | }); |
| 97 | let signature = match (&def.kind, scheme) { |
| 98 | (DefKind::Fn(_) | DefKind::Ctor(_) | DefKind::Op(_), Some(s)) => { |
| 99 | format!("{}: {s}", def.name) |
| 100 | } |
| 101 | (DefKind::Type(t), _) => format!( |
| 102 | "{}type {}{}", |
| 103 | if t.opaque { "opaque " } else { "" }, |
| 104 | def.name, |
| 105 | params(&t.params) |
| 106 | ), |
| 107 | (DefKind::Trait(t), _) => format!("trait {}{}", def.name, params(&t.params)), |
| 108 | // `type Elem` of `trait Container<c>`: written `Elem<c>`. |
| 109 | (DefKind::AssocType(a), _) => { |
| 110 | let owner = hir.def(a.owner); |
| 111 | let trait_params = match &owner.kind { |
| 112 | DefKind::Trait(t) => t.params.clone(), |
| 113 | _ => Vec::new(), |
| 114 | }; |
| 115 | let all: Vec<String> = trait_params |
| 116 | .into_iter() |
| 117 | .chain(a.params.iter().cloned()) |
| 118 | .collect(); |
| 119 | format!( |
| 120 | "type {}{} (of trait {})", |
| 121 | def.name, |
| 122 | params(&all), |
| 123 | owner.name |
| 124 | ) |
| 125 | } |
| 126 | (DefKind::Effect(e), _) => format!("effect {}{}", def.name, params(&e.params)), |
| 127 | // The alias, then its expansion (ADR 0044). |
| 128 | (DefKind::EffectAlias(a), _) => { |
| 129 | let members = a |
| 130 | .members |
| 131 | .iter() |
| 132 | .map(|m| match m { |
| 133 | TypeRef::Def { def: d, args, .. } => { |
| 134 | let args = if args.is_empty() { |
| 135 | String::new() |
| 136 | } else { |
| 137 | format!( |
| 138 | "<{}>", |
| 139 | args.iter() |
| 140 | .map(|a| match a { |
| 141 | TypeRef::Def { def: d, .. } => hir.def(*d).name.clone(), |
| 142 | TypeRef::Var { name, .. } => name.clone(), |
| 143 | _ => "_".into(), |
| 144 | }) |
| 145 | .collect::<Vec<_>>() |
| 146 | .join(", ") |
| 147 | ) |
| 148 | }; |
| 149 | format!("{}{}", hir.def(*d).name, args) |
| 150 | } |
| 151 | _ => "_".into(), |
| 152 | }) |
| 153 | .collect::<Vec<_>>(); |
| 154 | format!("effect alias {} = {}", def.name, members.join(", ")) |
| 155 | } |
| 156 | _ => def.name.clone(), |
| 157 | }; |
| 158 | let module = &hir.modules[def.module].path; |
| 159 | let mut out = code(&signature); |
| 160 | out.push_str(&format!("\n\n*{module}*")); |
| 161 | if let Some(doc) = &def.doc { |
| 162 | out.push_str("\n\n"); |
| 163 | out.push_str(doc); |
| 164 | } |
| 165 | out |
| 166 | } |
| 167 | |
| 168 | fn params(names: &[String]) -> String { |
| 169 | if names.is_empty() { |
| 170 | String::new() |
| 171 | } else { |
| 172 | format!("<{}>", names.join(", ")) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | #[cfg(test)] |
| 177 | mod tests { |
| 178 | use crate::Db; |
| 179 | |
| 180 | fn hover_at(src: &str, marker: &str) -> String { |
| 181 | let mut db = Db::new(); |
| 182 | let file = db.add_module("main", "main.lse", src.replace(marker, "")); |
| 183 | let offset = src.find(marker).expect("marker in source") as u32; |
| 184 | let analysis = db.analyze(); |
| 185 | assert!( |
| 186 | analysis.diagnostics.is_empty(), |
| 187 | "{:#?}", |
| 188 | analysis.diagnostics |
| 189 | ); |
| 190 | super::hover(&analysis, file, offset.into()) |
| 191 | .expect("something to hover") |
| 192 | .markdown |
| 193 | } |
| 194 | |
| 195 | #[test] |
| 196 | fn hovers_show_types_and_effects() { |
| 197 | let src = "effect log { fn log :: m: String -> () }\n\ |
| 198 | /// Logs twice.\n\ |
| 199 | fn tw$0ice :: m { log m\n log m }\n\ |
| 200 | pub fn run :: f: fn(a) -> b / <e> x: a -> b / <e> { f (x$1) }\n\ |
| 201 | fn use_it :: { tw$2ice \"x\" }"; |
| 202 | // Keep one marker, drop the others. |
| 203 | let only = |keep: &str| { |
| 204 | ["$0", "$1", "$2"] |
| 205 | .iter() |
| 206 | .filter(|m| **m != keep) |
| 207 | .fold(src.to_owned(), |s, m| s.replace(m, "")) |
| 208 | }; |
| 209 | let item = hover_at(&only("$0"), "$0"); |
| 210 | assert!(item.contains("twice: fn(String) -> () / <log>"), "{item}"); |
| 211 | assert!(item.contains("Logs twice."), "{item}"); |
| 212 | let local = hover_at(&only("$1"), "$1"); |
| 213 | assert!(local.contains("x: a"), "{local}"); |
| 214 | let reference = hover_at(&only("$2"), "$2"); |
| 215 | assert!( |
| 216 | reference.contains("twice: fn(String) -> () / <log>"), |
| 217 | "{reference}" |
| 218 | ); |
| 219 | } |
| 220 | } |