Skip to main content

dsl_hover/
token.rs

1//! Locate the identifier or word run at the cursor.
2//!
3//! Two helpers:
4//!
5//! - [`token_at`] -- single (possibly dotted) identifier; used for table /
6//!   column resolution.
7//! - [`window_at`] -- up to 5 adjacent words spanning the cursor (2 before,
8//!   the cursor's word, 2 after), plus the index of the cursor word. Lets
9//!   the resolver try every multi-word sliding window that includes the
10//!   cursor, so hovering on the second word of `INNER JOIN` or the middle
11//!   word of `IS NOT NULL` still resolves to the multi-word entry.
12
13use text_size::TextSize;
14
15pub fn token_at(src: &str, offset: TextSize) -> Option<String> {
16  let pos: usize = offset.into();
17  if pos > src.len() {
18    return None;
19  }
20  let bytes = src.as_bytes();
21  let mut start = pos;
22  while start > 0 {
23    let c = bytes[start - 1] as char;
24    if is_token(c) {
25      start -= 1;
26    } else {
27      break;
28    }
29  }
30  let mut end = pos;
31  while end < bytes.len() {
32    let c = bytes[end] as char;
33    if is_token(c) {
34      end += 1;
35    } else {
36      break;
37    }
38  }
39  if start == end {
40    return None;
41  }
42  Some(src[start..end].to_string())
43}
44
45fn is_token(c: char) -> bool {
46  c.is_alphanumeric() || c == '_' || c == '.'
47}
48
49pub struct Window {
50  pub words: Vec<String>,
51  /// Index inside `words` of the word the cursor sits in. Equal to
52  /// `words.len()` if the cursor is on trailing whitespace.
53  pub cursor: usize,
54}
55
56pub fn window_at(src: &str, offset: TextSize) -> Window {
57  let chars: Vec<char> = src.chars().collect();
58  let pos: usize = offset.into();
59  let pos = pos.min(chars.len());
60
61  // Cursor word: walk left to its start, right to its end.
62  let mut cur_start = pos;
63  while cur_start > 0 && is_word(chars[cur_start - 1]) {
64    cur_start -= 1;
65  }
66  let mut cur_end = pos;
67  while cur_end < chars.len() && is_word(chars[cur_end]) {
68    cur_end += 1;
69  }
70
71  // Up to 2 preceding words.
72  let mut prefix: Vec<(usize, usize)> = Vec::new();
73  let mut i = cur_start;
74  for _ in 0..2 {
75    let prev_end = back_skip_ws(&chars, i);
76    if prev_end == 0 {
77      break;
78    }
79    let prev_start = back_word_start(&chars, prev_end);
80    if prev_start == prev_end {
81      break;
82    }
83    prefix.push((prev_start, prev_end));
84    i = prev_start;
85  }
86  prefix.reverse();
87
88  // Up to 2 following words.
89  let mut suffix: Vec<(usize, usize)> = Vec::new();
90  let mut j = cur_end;
91  for _ in 0..2 {
92    let next_start = fwd_skip_ws(&chars, j);
93    if next_start >= chars.len() {
94      break;
95    }
96    let next_end = fwd_word_end(&chars, next_start);
97    if next_end == next_start {
98      break;
99    }
100    suffix.push((next_start, next_end));
101    j = next_end;
102  }
103
104  let mut words: Vec<String> = prefix.iter().map(|(s, e)| chars[*s..*e].iter().collect()).collect();
105  let cursor_idx = words.len();
106  if cur_start < cur_end {
107    words.push(chars[cur_start..cur_end].iter().collect());
108  }
109  for (s, e) in &suffix {
110    words.push(chars[*s..*e].iter().collect());
111  }
112  Window { words, cursor: cursor_idx }
113}
114
115fn back_skip_ws(chars: &[char], mut i: usize) -> usize {
116  while i > 0 && chars[i - 1].is_whitespace() {
117    i -= 1;
118  }
119  i
120}
121fn back_word_start(chars: &[char], mut end: usize) -> usize {
122  while end > 0 && is_word(chars[end - 1]) {
123    end -= 1;
124  }
125  end
126}
127fn fwd_skip_ws(chars: &[char], mut i: usize) -> usize {
128  while i < chars.len() && chars[i].is_whitespace() {
129    i += 1;
130  }
131  i
132}
133fn fwd_word_end(chars: &[char], mut start: usize) -> usize {
134  while start < chars.len() && is_word(chars[start]) {
135    start += 1;
136  }
137  start
138}
139fn is_word(c: char) -> bool {
140  c.is_alphanumeric() || c == '_'
141}
142
143/// Back-compat shim.
144pub fn words_at(src: &str, offset: TextSize) -> Vec<String> {
145  window_at(src, offset).words
146}