Skip to content

Commit 5dd2fde

Browse files
it works!
1 parent bf3d96c commit 5dd2fde

File tree

5 files changed

+33
-21
lines changed

5 files changed

+33
-21
lines changed

crates/pgt_completions/src/context/context.rs

+2
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ impl<'a> CompletionContext<'a> {
209209
ctx.gather_info_from_ts_queries();
210210
}
211211

212+
tracing::warn!("{:#?}", ctx.get_node_under_cursor_content());
213+
212214
ctx
213215
}
214216

crates/pgt_completions/src/providers/helper.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use pgt_text_size::{TextRange, TextSize};
22

3-
use crate::{CompletionText, context::CompletionContext};
3+
use crate::{CompletionText, context::CompletionContext, remove_sanitized_token};
44

55
pub(crate) fn find_matching_alias_for_table(
66
ctx: &CompletionContext,
@@ -15,22 +15,18 @@ pub(crate) fn find_matching_alias_for_table(
1515
}
1616

1717
pub(crate) fn get_range_to_replace(ctx: &CompletionContext) -> TextRange {
18-
let start = ctx
19-
.node_under_cursor
20-
.as_ref()
21-
.map(|n| n.start_byte())
22-
.unwrap_or(0);
18+
match ctx.node_under_cursor.as_ref() {
19+
Some(node) => {
20+
let content = ctx.get_node_under_cursor_content().unwrap_or("".into());
21+
let length = remove_sanitized_token(content.as_str()).len();
2322

24-
let end = ctx
25-
.get_node_under_cursor_content()
26-
.unwrap_or("".into())
27-
.len()
28-
+ start;
23+
let start = node.start_byte();
24+
let end = start + length;
2925

30-
TextRange::new(
31-
TextSize::new(start.try_into().unwrap()),
32-
end.try_into().unwrap(),
33-
)
26+
TextRange::new(start.try_into().unwrap(), end.try_into().unwrap())
27+
}
28+
None => TextRange::empty(TextSize::new(0)),
29+
}
3430
}
3531

3632
pub(crate) fn get_completion_text_with_schema_or_alias(

crates/pgt_completions/src/providers/policies.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use pgt_text_size::{TextRange, TextSize};
2+
13
use crate::{
24
CompletionItemKind, CompletionText,
35
builder::{CompletionBuilder, PossibleCompletionItem},
@@ -10,9 +12,9 @@ use super::helper::get_range_to_replace;
1012
pub fn complete_policies<'a>(ctx: &CompletionContext<'a>, builder: &mut CompletionBuilder<'a>) {
1113
let available_policies = &ctx.schema_cache.policies;
1214

13-
let has_quotes = ctx
15+
let surrounded_by_quotes = ctx
1416
.get_node_under_cursor_content()
15-
.is_some_and(|c| c.starts_with('"') && c.ends_with('"'));
17+
.is_some_and(|c| c.starts_with('"') && c.ends_with('"') && c != "\"\"");
1618

1719
for pol in available_policies {
1820
let relevance = CompletionRelevanceData::Policy(pol);
@@ -23,13 +25,22 @@ pub fn complete_policies<'a>(ctx: &CompletionContext<'a>, builder: &mut Completi
2325
filter: CompletionFilter::from(relevance),
2426
description: format!("{}", pol.table_name),
2527
kind: CompletionItemKind::Policy,
26-
completion_text: if !has_quotes {
28+
completion_text: if !surrounded_by_quotes {
2729
Some(CompletionText {
2830
text: format!("\"{}\"", pol.name),
2931
range: get_range_to_replace(ctx),
3032
})
3133
} else {
32-
None
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+
})
3344
},
3445
};
3546

crates/pgt_completions/src/sanitization.rs

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ pub fn benchmark_sanitization(params: CompletionParams) -> String {
1818
params.text
1919
}
2020

21+
pub(crate) fn remove_sanitized_token(it: &str) -> String {
22+
it.replace(SANITIZED_TOKEN, "")
23+
}
24+
2125
#[derive(PartialEq, Eq, Debug)]
2226
pub(crate) enum NodeText {
2327
Replaced,
@@ -157,6 +161,7 @@ fn cursor_on_a_dot(sql: &str, position: TextSize) -> bool {
157161
}
158162

159163
fn cursor_between_double_quotes(sql: &str, position: TextSize) -> bool {
164+
return false;
160165
let position: usize = position.into();
161166
let mut chars = sql.chars();
162167
chars.nth(position - 1).is_some_and(|c| c == '"') && chars.next().is_some_and(|c| c == '"')

crates/pgt_lsp/src/handlers/completions.rs

-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ pub fn get_completions(
5454
})
5555
.collect();
5656

57-
tracing::warn!("{:#?}", items);
58-
5957
Ok(lsp_types::CompletionResponse::Array(items))
6058
}
6159

0 commit comments

Comments
 (0)