-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathcontext.rs
485 lines (397 loc) · 15.4 KB
/
context.rs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use std::collections::{HashMap, HashSet};
use pgt_schema_cache::SchemaCache;
use pgt_treesitter_queries::{
TreeSitterQueriesExecutor,
queries::{self, QueryResult},
};
use crate::CompletionParams;
#[derive(Debug, PartialEq, Eq)]
pub enum ClauseType {
Select,
Where,
From,
Update,
Delete,
}
impl TryFrom<&str> for ClauseType {
type Error = String;
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"select" => Ok(Self::Select),
"where" => Ok(Self::Where),
"from" | "keyword_from" => Ok(Self::From),
"update" => Ok(Self::Update),
"delete" => Ok(Self::Delete),
_ => {
let message = format!("Unimplemented ClauseType: {}", value);
// Err on tests, so we notice that we're lacking an implementation immediately.
if cfg!(test) {
panic!("{}", message);
}
Err(message)
}
}
}
}
impl TryFrom<String> for ClauseType {
type Error = String;
fn try_from(value: String) -> Result<ClauseType, Self::Error> {
ClauseType::try_from(value.as_str())
}
}
pub(crate) struct CompletionContext<'a> {
pub node_under_cursor: Option<tree_sitter::Node<'a>>,
pub previous_node: Option<tree_sitter::Node<'a>>,
pub tree: Option<&'a tree_sitter::Tree>,
pub text: &'a str,
pub schema_cache: &'a SchemaCache,
pub position: usize,
/// If the cursor of the user is offset to the right of the statement,
/// we'll have to move it back to the last node, otherwise, tree-sitter will break.
/// However, knowing that the user is typing on the "next" node lets us prioritize different completion results.
/// We consider an offset of up to two characters as valid.
///
/// Example:
///
/// ```
/// select * from {}
/// ```
///
/// We'll adjust the cursor position so it lies on the "from" token – but we're looking
/// for table completions.
pub cursor_offset_from_end: bool,
pub schema_name: Option<String>,
pub wrapping_clause_type: Option<ClauseType>,
pub is_invocation: bool,
pub wrapping_statement_range: Option<tree_sitter::Range>,
pub mentioned_relations: HashMap<Option<String>, HashSet<String>>,
}
impl<'a> CompletionContext<'a> {
pub fn new(params: &'a CompletionParams) -> Self {
let mut ctx = Self {
tree: params.tree,
text: ¶ms.text,
schema_cache: params.schema,
position: usize::from(params.position),
cursor_offset_from_end: false,
previous_node: None,
node_under_cursor: None,
schema_name: None,
wrapping_clause_type: None,
wrapping_statement_range: None,
is_invocation: false,
mentioned_relations: HashMap::new(),
};
ctx.gather_tree_context();
ctx.gather_info_from_ts_queries();
ctx
}
fn gather_info_from_ts_queries(&mut self) {
let tree = match self.tree.as_ref() {
None => return,
Some(t) => t,
};
let stmt_range = self.wrapping_statement_range.as_ref();
let sql = self.text;
let mut executor = TreeSitterQueriesExecutor::new(tree.root_node(), sql);
executor.add_query_results::<queries::RelationMatch>();
for relation_match in executor.get_iter(stmt_range) {
match relation_match {
QueryResult::Relation(r) => {
let schema_name = r.get_schema(sql);
let table_name = r.get_table(sql);
let current = self.mentioned_relations.get_mut(&schema_name);
match current {
Some(c) => {
c.insert(table_name);
}
None => {
let mut new = HashSet::new();
new.insert(table_name);
self.mentioned_relations.insert(schema_name, new);
}
};
}
};
}
}
pub fn get_ts_node_content(&self, ts_node: tree_sitter::Node<'a>) -> Option<&'a str> {
let source = self.text;
ts_node.utf8_text(source.as_bytes()).ok()
}
fn gather_tree_context(&mut self) {
if self.tree.is_none() {
return;
}
let mut cursor = self.tree.as_ref().unwrap().root_node().walk();
/*
* The head node of any treesitter tree is always the "PROGRAM" node.
*
* We want to enter the next layer and focus on the child node that matches the user's cursor position.
* If there is no node under the users position, however, the cursor won't enter the next level – it
* will stay on the Program node.
*
* This might lead to an unexpected context or infinite recursion.
*
* We'll therefore adjust the cursor position such that it meets the last node of the AST.
* `select * from use {}` becomes `select * from use{}`.
*/
let current_node = cursor.node();
let position_cache = self.position.clone();
while cursor.goto_first_child_for_byte(self.position).is_none() && self.position > 0 {
self.position -= 1;
}
let cursor_offset = position_cache - self.position;
self.cursor_offset_from_end = cursor_offset > 0 && cursor_offset <= 2;
self.gather_context_from_node(cursor, current_node);
}
fn gather_context_from_node(
&mut self,
mut cursor: tree_sitter::TreeCursor<'a>,
parent_node: tree_sitter::Node<'a>,
) {
let current_node = cursor.node();
// prevent infinite recursion – this can happen if we only have a PROGRAM node
if current_node.kind() == parent_node.kind() {
self.node_under_cursor = Some(current_node);
return;
}
match parent_node.kind() {
"statement" | "subquery" => {
self.wrapping_clause_type = current_node.kind().try_into().ok();
self.wrapping_statement_range = Some(parent_node.range());
}
"invocation" => self.is_invocation = true,
_ => {}
}
match current_node.kind() {
"object_reference" => {
let txt = self.get_ts_node_content(current_node);
if let Some(txt) = txt {
let parts: Vec<&str> = txt.split('.').collect();
if parts.len() == 2 {
self.schema_name = Some(parts[0].to_string());
}
}
}
// in Treesitter, the Where clause is nested inside other clauses
"where" => {
self.wrapping_clause_type = "where".try_into().ok();
}
"keyword_from" => {
self.wrapping_clause_type = "keyword_from".try_into().ok();
}
_ => {}
}
// We have arrived at the leaf node
if current_node.child_count() == 0 {
if self.cursor_offset_from_end {
self.node_under_cursor = None;
self.previous_node = Some(current_node);
} else {
// for the previous node, either select the previous sibling,
// or collect the parent's previous sibling's last child.
let previous = match current_node.prev_sibling() {
Some(n) => Some(n),
None => {
let sib_of_parent = parent_node.prev_sibling();
sib_of_parent.and_then(|p| p.children(&mut cursor).last())
}
};
self.node_under_cursor = Some(current_node);
self.previous_node = previous;
}
return;
}
cursor.goto_first_child_for_byte(self.position);
self.gather_context_from_node(cursor, current_node);
}
}
#[cfg(test)]
mod tests {
use crate::{
context::{ClauseType, CompletionContext},
test_helper::{CURSOR_POS, get_text_and_position},
};
fn get_tree(input: &str) -> tree_sitter::Tree {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(tree_sitter_sql::language())
.expect("Couldn't set language");
parser.parse(input, None).expect("Unable to parse tree")
}
#[test]
fn identifies_clauses() {
let test_cases = vec![
(format!("Select {}* from users;", CURSOR_POS), "select"),
(format!("Select * from u{};", CURSOR_POS), "from"),
(
format!("Select {}* from users where n = 1;", CURSOR_POS),
"select",
),
(
format!("Select * from users where {}n = 1;", CURSOR_POS),
"where",
),
(
format!("update users set u{} = 1 where n = 2;", CURSOR_POS),
"update",
),
(
format!("update users set u = 1 where n{} = 2;", CURSOR_POS),
"where",
),
(format!("delete{} from users;", CURSOR_POS), "delete"),
(format!("delete from {}users;", CURSOR_POS), "from"),
(
format!("select name, age, location from public.u{}sers", CURSOR_POS),
"from",
),
];
for (query, expected_clause) in test_cases {
let (position, text) = get_text_and_position(query.as_str().into());
let tree = get_tree(text.as_str());
let params = crate::CompletionParams {
position: (position as u32).into(),
text,
tree: Some(&tree),
schema: &pgt_schema_cache::SchemaCache::default(),
};
let ctx = CompletionContext::new(¶ms);
assert_eq!(ctx.wrapping_clause_type, expected_clause.try_into().ok());
}
}
#[test]
fn identifies_schema() {
let test_cases = vec![
(
format!("Select * from private.u{}", CURSOR_POS),
Some("private"),
),
(
format!("Select * from private.u{}sers()", CURSOR_POS),
Some("private"),
),
(format!("Select * from u{}sers", CURSOR_POS), None),
(format!("Select * from u{}sers()", CURSOR_POS), None),
];
for (query, expected_schema) in test_cases {
let (position, text) = get_text_and_position(query.as_str().into());
let tree = get_tree(text.as_str());
let params = crate::CompletionParams {
position: (position as u32).into(),
text,
tree: Some(&tree),
schema: &pgt_schema_cache::SchemaCache::default(),
};
let ctx = CompletionContext::new(¶ms);
assert_eq!(ctx.schema_name, expected_schema.map(|f| f.to_string()));
}
}
#[test]
fn identifies_invocation() {
let test_cases = vec![
(format!("Select * from u{}sers", CURSOR_POS), false),
(format!("Select * from u{}sers()", CURSOR_POS), true),
(format!("Select cool{};", CURSOR_POS), false),
(format!("Select cool{}();", CURSOR_POS), true),
(
format!("Select upp{}ercase as title from users;", CURSOR_POS),
false,
),
(
format!("Select upp{}ercase(name) as title from users;", CURSOR_POS),
true,
),
];
for (query, is_invocation) in test_cases {
let (position, text) = get_text_and_position(query.as_str().into());
let tree = get_tree(text.as_str());
let params = crate::CompletionParams {
position: (position as u32).into(),
text,
tree: Some(&tree),
schema: &pgt_schema_cache::SchemaCache::default(),
};
let ctx = CompletionContext::new(¶ms);
assert_eq!(ctx.is_invocation, is_invocation);
}
}
#[test]
fn does_not_fail_on_leading_whitespace() {
let cases = vec![
format!("{} select * from", CURSOR_POS),
format!(" {} select * from", CURSOR_POS),
];
for query in cases {
let (position, text) = get_text_and_position(query.as_str().into());
let tree = get_tree(text.as_str());
let params = crate::CompletionParams {
position: (position as u32).into(),
text,
tree: Some(&tree),
schema: &pgt_schema_cache::SchemaCache::default(),
};
let ctx = CompletionContext::new(¶ms);
let node = ctx.node_under_cursor.unwrap();
assert_eq!(ctx.get_ts_node_content(node), Some("select"));
assert_eq!(
ctx.wrapping_clause_type,
Some(crate::context::ClauseType::Select)
);
}
}
#[test]
fn does_not_fail_on_trailing_whitespace() {
let query = format!("select * from {}", CURSOR_POS);
let (position, text) = get_text_and_position(query.as_str().into());
let tree = get_tree(text.as_str());
let params = crate::CompletionParams {
position: (position as u32).into(),
text,
tree: Some(&tree),
schema: &pgt_schema_cache::SchemaCache::default(),
};
let ctx = CompletionContext::new(¶ms);
let node = ctx.node_under_cursor.unwrap();
assert_eq!(ctx.get_ts_node_content(node), Some("from"));
assert_eq!(
ctx.wrapping_clause_type,
Some(crate::context::ClauseType::From)
);
}
#[test]
fn does_not_fail_with_empty_statements() {
let query = format!("{}", CURSOR_POS);
let (position, text) = get_text_and_position(query.as_str().into());
let tree = get_tree(text.as_str());
let params = crate::CompletionParams {
position: (position as u32).into(),
text,
tree: Some(&tree),
schema: &pgt_schema_cache::SchemaCache::default(),
};
let ctx = CompletionContext::new(¶ms);
let node = ctx.node_under_cursor.unwrap();
assert_eq!(ctx.get_ts_node_content(node), Some(""));
assert_eq!(ctx.wrapping_clause_type, None);
}
#[test]
fn does_not_fail_on_incomplete_keywords() {
// Instead of autocompleting "FROM", we'll assume that the user
// is selecting a certain column name, such as `frozen_account`.
let query = format!("select * fro{}", CURSOR_POS);
let (position, text) = get_text_and_position(query.as_str().into());
let tree = get_tree(text.as_str());
let params = crate::CompletionParams {
position: (position as u32).into(),
text,
tree: Some(&tree),
schema: &pgt_schema_cache::SchemaCache::default(),
};
let ctx = CompletionContext::new(¶ms);
let node = ctx.node_under_cursor.unwrap();
assert_eq!(ctx.get_ts_node_content(node), Some("fro"));
assert_eq!(ctx.wrapping_clause_type, Some(ClauseType::Select));
}
}