Skip to content

Commit 6f7a787

Browse files
yes
1 parent b187a06 commit 6f7a787

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed

crates/pgt_completions/src/providers/helper.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,30 @@ pub(crate) fn find_matching_alias_for_table(
1515
None
1616
}
1717

18+
pub(crate) fn node_text_surrounded_by_quotes(ctx: &TreesitterContext) -> bool {
19+
ctx.get_node_under_cursor_content()
20+
.is_some_and(|c| c.starts_with('"') && c.ends_with('"') && c != "\"\"")
21+
}
22+
1823
pub(crate) fn get_range_to_replace(ctx: &TreesitterContext) -> TextRange {
1924
match ctx.node_under_cursor.as_ref() {
2025
Some(node) => {
2126
let content = ctx.get_node_under_cursor_content().unwrap_or("".into());
2227
let content = content.as_str();
2328

24-
let length = remove_sanitized_token(content).len();
29+
let sanitized = remove_sanitized_token(content);
30+
let length = sanitized.len();
2531

26-
let start = node.start_byte();
32+
let mut start = node.start_byte();
2733
let mut end = start + length;
2834

29-
if is_sanitized_token_with_quote(content) {
30-
end += 1;
35+
if sanitized.starts_with('"') {
36+
start += 1;
37+
}
38+
39+
// might be '"' so we need to check for length
40+
if sanitized.ends_with('"') && sanitized.len() != 1 {
41+
end -= 1;
3142
}
3243

3344
TextRange::new(start.try_into().unwrap(), end.try_into().unwrap())

crates/pgt_completions/src/providers/policies.rs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use pgt_treesitter::TreesitterContext;
55
use crate::{
66
CompletionItemKind, CompletionText,
77
builder::{CompletionBuilder, PossibleCompletionItem},
8+
providers::helper::node_text_surrounded_by_quotes,
89
relevance::{CompletionRelevanceData, filtering::CompletionFilter, scoring::CompletionScore},
910
};
1011

@@ -17,33 +18,13 @@ pub fn complete_policies<'a>(
1718
) {
1819
let available_policies = &schema_cache.policies;
1920

20-
let surrounded_by_quotes = ctx
21-
.get_node_under_cursor_content()
22-
.is_some_and(|c| c.starts_with('"') && c.ends_with('"') && c != "\"\"");
23-
2421
for pol in available_policies {
25-
let completion_text = if surrounded_by_quotes {
22+
let text = if node_text_surrounded_by_quotes(ctx) {
2623
// If we're within quotes, we want to change the content
2724
// *within* the quotes.
28-
// If we attempt to replace outside the quotes, the VSCode
29-
// client won't show the suggestions.
30-
let range = get_range_to_replace(ctx);
31-
Some(CompletionText {
32-
text: pol.name.clone(),
33-
is_snippet: false,
34-
range: TextRange::new(
35-
range.start() + TextSize::new(1),
36-
range.end() - TextSize::new(1),
37-
),
38-
})
25+
pol.name.to_string()
3926
} else {
40-
// If we aren't within quotes, we want to complete the
41-
// full policy including quotation marks.
42-
Some(CompletionText {
43-
is_snippet: false,
44-
text: format!("\"{}\"", pol.name),
45-
range: get_range_to_replace(ctx),
46-
})
27+
format!("\"{}\"", pol.name)
4728
};
4829

4930
let relevance = CompletionRelevanceData::Policy(pol);
@@ -54,7 +35,11 @@ pub fn complete_policies<'a>(
5435
filter: CompletionFilter::from(relevance),
5536
description: pol.table_name.to_string(),
5637
kind: CompletionItemKind::Policy,
57-
completion_text,
38+
completion_text: Some(CompletionText {
39+
text,
40+
range: get_range_to_replace(ctx),
41+
is_snippet: false,
42+
}),
5843
detail: None,
5944
};
6045

crates/pgt_completions/src/sanitization.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ pub fn benchmark_sanitization(params: CompletionParams) -> String {
2121
}
2222

2323
pub(crate) fn remove_sanitized_token(it: &str) -> String {
24-
it.replace(SANITIZED_TOKEN, "")
25-
.replace(SANITIZED_TOKEN_WITH_QUOTE, "")
24+
it.replace(SANITIZED_TOKEN_WITH_QUOTE, "")
25+
.replace(SANITIZED_TOKEN, "")
2626
}
2727

2828
pub(crate) fn is_sanitized_token(node_under_cursor_txt: &str) -> bool {

0 commit comments

Comments
 (0)