Skip to main content

dsl_hover/
resolver.rs

1//! Pick the right hover renderer for the token / word run under the cursor.
2
3use crate::render;
4use crate::token::Window;
5use dsl_catalog::Catalog;
6use dsl_knowledge as kb;
7
8/// Resolve a single (possibly dotted) token against catalog + knowledge.
9pub fn from_token(token: &str, catalog: &Catalog) -> Option<String> {
10  if let Some((left, right)) = token.split_once('.') {
11    if let Some(t) = catalog.find_table(Some(left), right) {
12      return Some(render::table(t));
13    }
14    if let Some(t) = catalog.find_table(None, left)
15      && let Some(c) = t.columns.iter().find(|c| c.name.eq_ignore_ascii_case(right))
16    {
17      return Some(render::column(t, c));
18    }
19  }
20  if let Some(t) = catalog.find_table(None, token) {
21    return Some(render::table(t));
22  }
23  let columns = catalog.columns_named(token);
24  if !columns.is_empty() {
25    return Some(render::column_in_tables(&columns));
26  }
27  if let Some(entry) = kb::lookup(token) {
28    return Some(kb::render_markdown(entry));
29  }
30  None
31}
32
33/// Try every contiguous 2- or 3-word window inside `window.words` that
34/// includes the cursor word. Longest wins (`IS NOT NULL` beats `IS NULL`
35/// beats `IS`). Returns the rendered markdown when a multi-word keyword
36/// matches.
37pub fn from_window(window: &Window) -> Option<String> {
38  if window.words.is_empty() {
39    return None;
40  }
41  let table = kb::keywords();
42  let upper: Vec<String> = window.words.iter().map(|w| w.to_uppercase()).collect();
43  let n = upper.len();
44  let cur = window.cursor.min(n.saturating_sub(1));
45  for size in [3usize, 2] {
46    if n < size {
47      continue;
48    }
49    let start_lo = cur.saturating_sub(size - 1);
50    let start_hi = cur.min(n - size);
51    for start in start_lo..=start_hi {
52      let candidate = upper[start..start + size].join(" ");
53      if let Some(entry) = table.get(candidate.as_str()) {
54        return Some(kb::render_markdown(entry));
55      }
56    }
57  }
58  None
59}
60
61/// Older single-direction helper kept for tests.
62pub fn from_words(words: &[String]) -> Option<String> {
63  if words.is_empty() {
64    return None;
65  }
66  let table = kb::keywords();
67  let upper: Vec<String> = words.iter().map(|w| w.to_uppercase()).collect();
68  for n in (1..=3.min(upper.len())).rev() {
69    let candidate = upper[..n].join(" ");
70    if let Some(entry) = table.get(candidate.as_str()) {
71      return Some(kb::render_markdown(entry));
72    }
73  }
74  None
75}
76
77pub fn resolve(token: &str, catalog: &Catalog) -> Option<String> {
78  from_token(token, catalog)
79}