Skip to content

Commit 2d4d9a5

Browse files
fixie fixie
1 parent dd4ad27 commit 2d4d9a5

File tree

3 files changed

+77
-38
lines changed

3 files changed

+77
-38
lines changed

crates/pgt_completions/src/context.rs

+9
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ impl<'a> CompletionContext<'a> {
181181
})
182182
}
183183

184+
pub fn get_node_under_cursor_content(&self) -> Option<String> {
185+
self.node_under_cursor
186+
.and_then(|n| self.get_ts_node_content(n))
187+
.and_then(|txt| match txt {
188+
NodeText::Replaced => None,
189+
NodeText::Original(c) => Some(c.to_string()),
190+
})
191+
}
192+
184193
fn gather_tree_context(&mut self) {
185194
let mut cursor = self.tree.root_node().walk();
186195

crates/pgt_completions/src/providers/schemas.rs

+52-20
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ pub fn complete_schemas<'a>(ctx: &'a CompletionContext, builder: &mut Completion
2727
mod tests {
2828

2929
use crate::{
30-
CompletionItemKind, complete,
31-
test_helper::{CURSOR_POS, get_test_deps, get_test_params},
30+
CompletionItemKind,
31+
test_helper::{CURSOR_POS, CompletionAssertion, assert_complete_results},
3232
};
3333

3434
#[tokio::test]
@@ -46,27 +46,59 @@ mod tests {
4646
);
4747
"#;
4848

49-
let query = format!("select * from {}", CURSOR_POS);
49+
assert_complete_results(
50+
format!("select * from {}", CURSOR_POS).as_str(),
51+
vec![
52+
CompletionAssertion::LabelAndKind("public".to_string(), CompletionItemKind::Schema),
53+
CompletionAssertion::LabelAndKind("auth".to_string(), CompletionItemKind::Schema),
54+
CompletionAssertion::LabelAndKind(
55+
"internal".to_string(),
56+
CompletionItemKind::Schema,
57+
),
58+
CompletionAssertion::LabelAndKind(
59+
"private".to_string(),
60+
CompletionItemKind::Schema,
61+
),
62+
CompletionAssertion::LabelAndKind(
63+
"information_schema".to_string(),
64+
CompletionItemKind::Schema,
65+
),
66+
CompletionAssertion::LabelAndKind(
67+
"pg_catalog".to_string(),
68+
CompletionItemKind::Schema,
69+
),
70+
CompletionAssertion::LabelAndKind(
71+
"pg_toast".to_string(),
72+
CompletionItemKind::Schema,
73+
),
74+
CompletionAssertion::LabelAndKind("users".to_string(), CompletionItemKind::Table),
75+
],
76+
setup,
77+
)
78+
.await;
79+
}
5080

51-
let (tree, cache) = get_test_deps(setup, query.as_str().into()).await;
52-
let params = get_test_params(&tree, &cache, query.as_str().into());
53-
let items = complete(params);
81+
#[tokio::test]
82+
async fn suggests_tables_and_schemas_with_matching_keys() {
83+
let setup = r#"
84+
create schema ultimate;
5485
55-
assert!(!items.is_empty());
86+
-- add a table to compete against schemas
87+
create table users (
88+
id serial primary key,
89+
name text,
90+
password text
91+
);
92+
"#;
5693

57-
assert_eq!(
58-
items
59-
.into_iter()
60-
.take(5)
61-
.map(|i| (i.label, i.kind))
62-
.collect::<Vec<(String, CompletionItemKind)>>(),
94+
assert_complete_results(
95+
format!("select * from u{}", CURSOR_POS).as_str(),
6396
vec![
64-
("public".to_string(), CompletionItemKind::Schema),
65-
("auth".to_string(), CompletionItemKind::Schema),
66-
("internal".to_string(), CompletionItemKind::Schema),
67-
("private".to_string(), CompletionItemKind::Schema),
68-
("users".to_string(), CompletionItemKind::Table),
69-
]
70-
);
97+
CompletionAssertion::LabelAndKind("users".into(), CompletionItemKind::Table),
98+
CompletionAssertion::LabelAndKind("ultimate".into(), CompletionItemKind::Schema),
99+
],
100+
setup,
101+
)
102+
.await;
71103
}
72104
}

crates/pgt_completions/src/relevance/scoring.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::context::{ClauseType, CompletionContext, NodeText, WrappingNode};
1+
use crate::context::{ClauseType, CompletionContext, WrappingNode};
22

33
use super::CompletionRelevanceData;
44

@@ -33,16 +33,8 @@ impl CompletionScore<'_> {
3333
}
3434

3535
fn check_matches_query_input(&mut self, ctx: &CompletionContext) {
36-
let node = match ctx.node_under_cursor {
37-
Some(node) => node,
38-
None => return,
39-
};
40-
41-
let content = match ctx.get_ts_node_content(node) {
42-
Some(c) => match c {
43-
NodeText::Original(s) => s,
44-
NodeText::Replaced => return,
45-
},
36+
let content = match ctx.get_node_under_cursor_content() {
37+
Some(c) => c,
4638
None => return,
4739
};
4840

@@ -53,7 +45,7 @@ impl CompletionScore<'_> {
5345
CompletionRelevanceData::Schema(s) => s.name.as_str(),
5446
};
5547

56-
if name.starts_with(content) {
48+
if name.starts_with(content.as_str()) {
5749
let len: i32 = content
5850
.len()
5951
.try_into()
@@ -70,12 +62,13 @@ impl CompletionScore<'_> {
7062
};
7163

7264
let has_mentioned_tables = !ctx.mentioned_relations.is_empty();
65+
let has_mentioned_schema = ctx.schema_name.is_some();
7366

7467
self.score += match self.data {
7568
CompletionRelevanceData::Table(_) => match clause_type {
7669
ClauseType::From => 5,
77-
ClauseType::Update => 15,
78-
ClauseType::Delete => 15,
70+
ClauseType::Update => 10,
71+
ClauseType::Delete => 10,
7972
_ => -50,
8073
},
8174
CompletionRelevanceData::Function(_) => match clause_type {
@@ -91,7 +84,9 @@ impl CompletionScore<'_> {
9184
_ => -15,
9285
},
9386
CompletionRelevanceData::Schema(_) => match clause_type {
94-
ClauseType::From => 10,
87+
ClauseType::From if !has_mentioned_schema => 15,
88+
ClauseType::Update if !has_mentioned_schema => 15,
89+
ClauseType::Delete if !has_mentioned_schema => 15,
9590
_ => -50,
9691
},
9792
}
@@ -103,11 +98,13 @@ impl CompletionScore<'_> {
10398
Some(wn) => wn,
10499
};
105100

106-
let has_schema = ctx.schema_name.is_some();
101+
let has_mentioned_schema = ctx.schema_name.is_some();
102+
let has_node_text = ctx.get_node_under_cursor_content().is_some();
107103

108104
self.score += match self.data {
109105
CompletionRelevanceData::Table(_) => match wrapping_node {
110-
WrappingNode::Relation => 15,
106+
WrappingNode::Relation if has_mentioned_schema => 15,
107+
WrappingNode::Relation if !has_mentioned_schema => 10,
111108
WrappingNode::BinaryExpression => 5,
112109
_ => -50,
113110
},
@@ -121,7 +118,8 @@ impl CompletionScore<'_> {
121118
_ => -15,
122119
},
123120
CompletionRelevanceData::Schema(_) => match wrapping_node {
124-
WrappingNode::Relation if !has_schema => 5,
121+
WrappingNode::Relation if !has_mentioned_schema && !has_node_text => 15,
122+
WrappingNode::Relation if !has_mentioned_schema && has_node_text => 0,
125123
_ => -50,
126124
},
127125
}

0 commit comments

Comments
 (0)