-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathschemas.rs
71 lines (59 loc) · 2.19 KB
/
schemas.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
use crate::{
builder::{CompletionBuilder, PossibleCompletionItem},
context::CompletionContext,
relevance::{CompletionRelevanceData, filtering::CompletionFilter, scoring::CompletionScore},
};
pub fn complete_schemas<'a>(ctx: &'a CompletionContext, builder: &mut CompletionBuilder<'a>) {
let available_schemas = &ctx.schema_cache.schemas;
for schema in available_schemas {
let relevance = CompletionRelevanceData::Schema(schema);
let item = PossibleCompletionItem {
label: schema.name.clone(),
description: "Schema".into(),
kind: crate::CompletionItemKind::Schema,
score: CompletionScore::from(relevance.clone()),
filter: CompletionFilter::from(relevance),
};
builder.add_item(item);
}
}
#[cfg(test)]
mod tests {
use crate::{
CompletionItemKind, complete,
test_helper::{CURSOR_POS, get_test_deps, get_test_params},
};
#[tokio::test]
async fn autocompletes_schemas() {
let setup = r#"
create schema private;
create schema auth;
create schema internal;
-- add a table to compete against schemas
create table users (
id serial primary key,
name text,
password text
);
"#;
let query = format!("select * from {}", CURSOR_POS);
let (tree, cache) = get_test_deps(setup, query.as_str().into()).await;
let params = get_test_params(&tree, &cache, query.as_str().into());
let items = complete(params);
assert!(!items.is_empty());
assert_eq!(
items
.into_iter()
.take(5)
.map(|i| (i.label, i.kind))
.collect::<Vec<(String, CompletionItemKind)>>(),
vec![
("public".to_string(), CompletionItemKind::Schema),
("auth".to_string(), CompletionItemKind::Schema),
("internal".to_string(), CompletionItemKind::Schema),
("private".to_string(), CompletionItemKind::Schema),
("users".to_string(), CompletionItemKind::Table),
]
);
}
}