Skip to content

Commit 1950b4c

Browse files
better
1 parent 809aa84 commit 1950b4c

File tree

2 files changed

+30
-44
lines changed

2 files changed

+30
-44
lines changed

crates/pgt_completions/src/providers/policies.rs

+24-39
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,28 @@ pub fn complete_policies<'a>(ctx: &CompletionContext<'a>, builder: &mut Completi
1717
.is_some_and(|c| c.starts_with('"') && c.ends_with('"') && c != "\"\"");
1818

1919
for pol in available_policies {
20+
let completion_text = if surrounded_by_quotes {
21+
// If we're within quotes, we want to change the content
22+
// *within* the quotes.
23+
// If we attempt to replace outside the quotes, the VSCode
24+
// client won't show the suggestions.
25+
let range = get_range_to_replace(ctx);
26+
Some(CompletionText {
27+
text: pol.name.clone(),
28+
range: TextRange::new(
29+
range.start() + TextSize::new(1),
30+
range.end() - TextSize::new(1),
31+
),
32+
})
33+
} else {
34+
// If we aren't within quotes, we want to complete the
35+
// full policy including quotation marks.
36+
Some(CompletionText {
37+
text: format!("\"{}\"", pol.name),
38+
range: get_range_to_replace(ctx),
39+
})
40+
};
41+
2042
let relevance = CompletionRelevanceData::Policy(pol);
2143

2244
let item = PossibleCompletionItem {
@@ -25,23 +47,7 @@ pub fn complete_policies<'a>(ctx: &CompletionContext<'a>, builder: &mut Completi
2547
filter: CompletionFilter::from(relevance),
2648
description: format!("{}", pol.table_name),
2749
kind: CompletionItemKind::Policy,
28-
completion_text: if !surrounded_by_quotes {
29-
Some(CompletionText {
30-
text: format!("\"{}\"", pol.name),
31-
range: get_range_to_replace(ctx),
32-
})
33-
} else {
34-
let range = get_range_to_replace(ctx);
35-
Some(CompletionText {
36-
text: pol.name.clone(),
37-
38-
// trim the quotes.
39-
range: TextRange::new(
40-
range.start() + TextSize::new(1),
41-
range.end() - TextSize::new(1),
42-
),
43-
})
44-
},
50+
completion_text,
4551
};
4652

4753
builder.add_item(item);
@@ -50,13 +56,7 @@ pub fn complete_policies<'a>(ctx: &CompletionContext<'a>, builder: &mut Completi
5056

5157
#[cfg(test)]
5258
mod tests {
53-
use crate::{
54-
complete,
55-
test_helper::{
56-
CURSOR_POS, CompletionAssertion, assert_complete_results, get_test_params,
57-
test_against_connection_string,
58-
},
59-
};
59+
use crate::test_helper::{CURSOR_POS, CompletionAssertion, assert_complete_results};
6060

6161
#[tokio::test]
6262
async fn completes_within_quotation_marks() {
@@ -100,19 +100,4 @@ mod tests {
100100
)
101101
.await;
102102
}
103-
104-
#[tokio::test]
105-
async fn sb_test() {
106-
let input = format!("alter policy \"u{}\" on public.fcm_tokens;", CURSOR_POS);
107-
108-
let (tree, cache) = test_against_connection_string(
109-
"postgresql://postgres:[email protected]:54322/postgres",
110-
input.as_str().into(),
111-
)
112-
.await;
113-
114-
let result = complete(get_test_params(&tree, &cache, input.as_str().into()));
115-
116-
println!("{:#?}", result);
117-
}
118103
}

crates/pgt_completions/src/relevance/filtering.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ impl CompletionFilter<'_> {
7171

7272
match self.data {
7373
CompletionRelevanceData::Table(_) => {
74-
if in_clause(WrappingClause::Select) || in_clause(WrappingClause::Where)
75-
// || in_clause(WrappingClause::PolicyName)
74+
if in_clause(WrappingClause::Select)
75+
|| in_clause(WrappingClause::Where)
76+
|| in_clause(WrappingClause::PolicyName)
7677
{
7778
return None;
7879
};
@@ -106,9 +107,9 @@ impl CompletionFilter<'_> {
106107
}
107108
}
108109
_ => {
109-
// if in_clause(WrappingClause::PolicyName) {
110-
// return None;
111-
// }
110+
if in_clause(WrappingClause::PolicyName) {
111+
return None;
112+
}
112113
}
113114
}
114115

0 commit comments

Comments
 (0)