|
| 1 | +use crate::{ |
| 2 | + CompletionItem, builder::CompletionBuilder, context::CompletionContext, |
| 3 | + relevance::CompletionRelevanceData, |
| 4 | +}; |
| 5 | + |
| 6 | +pub fn complete_schemas(ctx: &CompletionContext, builder: &mut CompletionBuilder) { |
| 7 | + let available_schemas = &ctx.schema_cache.schemas; |
| 8 | + |
| 9 | + for schema in available_schemas { |
| 10 | + let relevance = CompletionRelevanceData::Schema(&schema); |
| 11 | + |
| 12 | + let item = CompletionItem { |
| 13 | + label: schema.name.clone(), |
| 14 | + description: "Schema".into(), |
| 15 | + preselected: false, |
| 16 | + kind: crate::CompletionItemKind::Schema, |
| 17 | + score: relevance.get_score(ctx), |
| 18 | + }; |
| 19 | + |
| 20 | + builder.add_item(item); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +#[cfg(test)] |
| 25 | +mod tests { |
| 26 | + |
| 27 | + use crate::{ |
| 28 | + CompletionItemKind, complete, |
| 29 | + test_helper::{CURSOR_POS, get_test_deps, get_test_params}, |
| 30 | + }; |
| 31 | + |
| 32 | + #[tokio::test] |
| 33 | + async fn autocompletes_schemas() { |
| 34 | + let setup = r#" |
| 35 | + create schema private; |
| 36 | + create schema auth; |
| 37 | + create schema internal; |
| 38 | +
|
| 39 | + -- add a table to compete against schemas |
| 40 | + create table users ( |
| 41 | + id serial primary key, |
| 42 | + name text, |
| 43 | + password text |
| 44 | + ); |
| 45 | + "#; |
| 46 | + |
| 47 | + let query = format!("select * from {}", CURSOR_POS); |
| 48 | + |
| 49 | + let (tree, cache) = get_test_deps(setup, query.as_str().into()).await; |
| 50 | + let params = get_test_params(&tree, &cache, query.as_str().into()); |
| 51 | + let items = complete(params); |
| 52 | + |
| 53 | + assert!(!items.is_empty()); |
| 54 | + |
| 55 | + assert_eq!( |
| 56 | + items |
| 57 | + .into_iter() |
| 58 | + .take(5) |
| 59 | + .map(|i| (i.label, i.kind)) |
| 60 | + .collect::<Vec<(String, CompletionItemKind)>>(), |
| 61 | + vec![ |
| 62 | + ("public".to_string(), CompletionItemKind::Schema), |
| 63 | + ("auth".to_string(), CompletionItemKind::Schema), |
| 64 | + ("internal".to_string(), CompletionItemKind::Schema), |
| 65 | + ("private".to_string(), CompletionItemKind::Schema), |
| 66 | + ("users".to_string(), CompletionItemKind::Table), |
| 67 | + ] |
| 68 | + ); |
| 69 | + } |
| 70 | +} |
0 commit comments