Skip to content

Commit b8367c5

Browse files
almost there
1 parent 70cccba commit b8367c5

File tree

1 file changed

+46
-41
lines changed
  • crates/pgt_treesitter/src/context

1 file changed

+46
-41
lines changed

crates/pgt_treesitter/src/context/mod.rs

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod revoke_parser;
99

1010
use crate::queries::{self, QueryResult, TreeSitterQueriesExecutor};
1111
use pgt_schema_cache::SchemaCache;
12-
use pgt_text_size::TextRange;
12+
use pgt_text_size::{TextRange, TextSize};
1313

1414
use crate::{
1515
NodeText,
@@ -147,6 +147,13 @@ impl TryFrom<String> for WrappingNode {
147147
}
148148
}
149149

150+
struct TreeSitterContextParams<'a> {
151+
pub position: TextSize,
152+
pub text: &'a str,
153+
pub schema: &'a pgt_schema_cache::SchemaCache,
154+
pub tree: &'a tree_sitter::Tree,
155+
}
156+
150157
#[derive(Debug)]
151158
pub(crate) struct CompletionContext<'a> {
152159
pub node_under_cursor: Option<NodeUnderCursor<'a>>,
@@ -189,10 +196,10 @@ pub(crate) struct CompletionContext<'a> {
189196
}
190197

191198
impl<'a> CompletionContext<'a> {
192-
pub fn new(params: &'a SanitizedCompletionParams) -> Self {
199+
pub fn new(params: TreeSitterContextParams<'a>) -> Self {
193200
let mut ctx = Self {
194-
tree: params.tree.as_ref(),
195-
text: &params.text,
201+
tree: params.tree,
202+
text: params.text,
196203
schema_cache: params.schema,
197204
position: usize::from(params.position),
198205
node_under_cursor: None,
@@ -816,9 +823,7 @@ impl<'a> CompletionContext<'a> {
816823
mod tests {
817824
use crate::{
818825
NodeText,
819-
context::{CompletionContext, WrappingClause},
820-
sanitization::SanitizedCompletionParams,
821-
test_helper::get_text_and_position,
826+
context::{CompletionContext, TreeSitterContextParams, WrappingClause},
822827
};
823828

824829
use pgt_test_utils::QueryWithCursorPosition;
@@ -907,14 +912,14 @@ mod tests {
907912

908913
let tree = get_tree(text.as_str());
909914

910-
let params = SanitizedCompletionParams {
915+
let params = TreeSitterContextParams {
911916
position: (position as u32).into(),
912-
text,
913-
tree: std::borrow::Cow::Owned(tree),
917+
text: &text,
918+
tree: &tree,
914919
schema: &pgt_schema_cache::SchemaCache::default(),
915920
};
916921

917-
let ctx = CompletionContext::new(&params);
922+
let ctx = CompletionContext::new(params);
918923

919924
assert_eq!(ctx.wrapping_clause_type, Some(expected_clause));
920925
}
@@ -954,17 +959,17 @@ mod tests {
954959
];
955960

956961
for (query, expected_schema) in test_cases {
957-
let (position, text) = get_text_and_position(query.as_str().into());
962+
let (position, text) = QueryWithCursorPosition::from(query).get_text_and_position();
958963

959964
let tree = get_tree(text.as_str());
960-
let params = SanitizedCompletionParams {
965+
let params = TreeSitterContextParams {
961966
position: (position as u32).into(),
962-
text,
963-
tree: std::borrow::Cow::Owned(tree),
967+
text: &text,
968+
tree: &tree,
964969
schema: &pgt_schema_cache::SchemaCache::default(),
965970
};
966971

967-
let ctx = CompletionContext::new(&params);
972+
let ctx = CompletionContext::new(params);
968973

969974
assert_eq!(
970975
ctx.schema_or_alias_name,
@@ -1015,17 +1020,17 @@ mod tests {
10151020
];
10161021

10171022
for (query, is_invocation) in test_cases {
1018-
let (position, text) = get_text_and_position(query.as_str().into());
1023+
let (position, text) = QueryWithCursorPosition::from(query).get_text_and_position();
10191024

10201025
let tree = get_tree(text.as_str());
1021-
let params = SanitizedCompletionParams {
1026+
let params = TreeSitterContextParams {
10221027
position: (position as u32).into(),
1023-
text,
1024-
tree: std::borrow::Cow::Owned(tree),
1028+
text: text.as_str(),
1029+
tree: &tree,
10251030
schema: &pgt_schema_cache::SchemaCache::default(),
10261031
};
10271032

1028-
let ctx = CompletionContext::new(&params);
1033+
let ctx = CompletionContext::new(params);
10291034

10301035
assert_eq!(ctx.is_invocation, is_invocation);
10311036
}
@@ -1045,18 +1050,18 @@ mod tests {
10451050
];
10461051

10471052
for query in cases {
1048-
let (position, text) = get_text_and_position(query.as_str().into());
1053+
let (position, text) = QueryWithCursorPosition::from(query).get_text_and_position();
10491054

10501055
let tree = get_tree(text.as_str());
10511056

1052-
let params = SanitizedCompletionParams {
1057+
let params = TreeSitterContextParams {
10531058
position: (position as u32).into(),
1054-
text,
1055-
tree: std::borrow::Cow::Owned(tree),
1059+
text: &text,
1060+
tree: &tree,
10561061
schema: &pgt_schema_cache::SchemaCache::default(),
10571062
};
10581063

1059-
let ctx = CompletionContext::new(&params);
1064+
let ctx = CompletionContext::new(params);
10601065

10611066
let node = ctx.node_under_cursor.as_ref().unwrap();
10621067

@@ -1084,18 +1089,18 @@ mod tests {
10841089
QueryWithCursorPosition::cursor_marker()
10851090
);
10861091

1087-
let (position, text) = get_text_and_position(query.as_str().into());
1092+
let (position, text) = QueryWithCursorPosition::from(query).get_text_and_position();
10881093

10891094
let tree = get_tree(text.as_str());
10901095

1091-
let params = SanitizedCompletionParams {
1096+
let params = TreeSitterContextParams {
10921097
position: (position as u32).into(),
1093-
text,
1094-
tree: std::borrow::Cow::Owned(tree),
1098+
text: &text,
1099+
tree: &tree,
10951100
schema: &pgt_schema_cache::SchemaCache::default(),
10961101
};
10971102

1098-
let ctx = CompletionContext::new(&params);
1103+
let ctx = CompletionContext::new(params);
10991104

11001105
let node = ctx.node_under_cursor.as_ref().unwrap();
11011106

@@ -1114,18 +1119,18 @@ mod tests {
11141119
fn does_not_fail_with_empty_statements() {
11151120
let query = format!("{}", QueryWithCursorPosition::cursor_marker());
11161121

1117-
let (position, text) = get_text_and_position(query.as_str().into());
1122+
let (position, text) = QueryWithCursorPosition::from(query).get_text_and_position();
11181123

11191124
let tree = get_tree(text.as_str());
11201125

1121-
let params = SanitizedCompletionParams {
1126+
let params = TreeSitterContextParams {
11221127
position: (position as u32).into(),
1123-
text,
1124-
tree: std::borrow::Cow::Owned(tree),
1128+
text: &text,
1129+
tree: &tree,
11251130
schema: &pgt_schema_cache::SchemaCache::default(),
11261131
};
11271132

1128-
let ctx = CompletionContext::new(&params);
1133+
let ctx = CompletionContext::new(params);
11291134

11301135
let node = ctx.node_under_cursor.as_ref().unwrap();
11311136

@@ -1147,18 +1152,18 @@ mod tests {
11471152
// is selecting a certain column name, such as `frozen_account`.
11481153
let query = format!("select * fro{}", QueryWithCursorPosition::cursor_marker());
11491154

1150-
let (position, text) = get_text_and_position(query.as_str().into());
1155+
let (position, text) = QueryWithCursorPosition::from(query).get_text_and_position();
11511156

11521157
let tree = get_tree(text.as_str());
11531158

1154-
let params = SanitizedCompletionParams {
1159+
let params = TreeSitterContextParams {
11551160
position: (position as u32).into(),
1156-
text,
1157-
tree: std::borrow::Cow::Owned(tree),
1161+
text: &text,
1162+
tree: &tree,
11581163
schema: &pgt_schema_cache::SchemaCache::default(),
11591164
};
11601165

1161-
let ctx = CompletionContext::new(&params);
1166+
let ctx = CompletionContext::new(params);
11621167

11631168
let node = ctx.node_under_cursor.as_ref().unwrap();
11641169

0 commit comments

Comments
 (0)