Skip to content

Commit 4867189

Browse files
authored
refactor: parser (#322)
1 parent 451579d commit 4867189

File tree

16 files changed

+892
-492
lines changed

16 files changed

+892
-492
lines changed

crates/pgt_completions/src/complete.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct CompletionParams<'a> {
1414
pub position: TextSize,
1515
pub schema: &'a pgt_schema_cache::SchemaCache,
1616
pub text: String,
17-
pub tree: Option<&'a tree_sitter::Tree>,
17+
pub tree: &'a tree_sitter::Tree,
1818
}
1919

2020
pub fn complete(params: CompletionParams) -> Vec<CompletionItem> {

crates/pgt_completions/src/context.rs

+10-17
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl TryFrom<String> for ClauseType {
5050

5151
pub(crate) struct CompletionContext<'a> {
5252
pub ts_node: Option<tree_sitter::Node<'a>>,
53-
pub tree: Option<&'a tree_sitter::Tree>,
53+
pub tree: &'a tree_sitter::Tree,
5454
pub text: &'a str,
5555
pub schema_cache: &'a SchemaCache,
5656
pub position: usize,
@@ -85,10 +85,7 @@ impl<'a> CompletionContext<'a> {
8585
}
8686

8787
fn gather_info_from_ts_queries(&mut self) {
88-
let tree = match self.tree.as_ref() {
89-
None => return,
90-
Some(t) => t,
91-
};
88+
let tree = self.tree;
9289

9390
let stmt_range = self.wrapping_statement_range.as_ref();
9491
let sql = self.text;
@@ -126,11 +123,7 @@ impl<'a> CompletionContext<'a> {
126123
}
127124

128125
fn gather_tree_context(&mut self) {
129-
if self.tree.is_none() {
130-
return;
131-
}
132-
133-
let mut cursor = self.tree.as_ref().unwrap().root_node().walk();
126+
let mut cursor = self.tree.root_node().walk();
134127

135128
/*
136129
* The head node of any treesitter tree is always the "PROGRAM" node.
@@ -262,7 +255,7 @@ mod tests {
262255
let params = crate::CompletionParams {
263256
position: (position as u32).into(),
264257
text,
265-
tree: Some(&tree),
258+
tree: &tree,
266259
schema: &pgt_schema_cache::SchemaCache::default(),
267260
};
268261

@@ -294,7 +287,7 @@ mod tests {
294287
let params = crate::CompletionParams {
295288
position: (position as u32).into(),
296289
text,
297-
tree: Some(&tree),
290+
tree: &tree,
298291
schema: &pgt_schema_cache::SchemaCache::default(),
299292
};
300293

@@ -328,7 +321,7 @@ mod tests {
328321
let params = crate::CompletionParams {
329322
position: (position as u32).into(),
330323
text,
331-
tree: Some(&tree),
324+
tree: &tree,
332325
schema: &pgt_schema_cache::SchemaCache::default(),
333326
};
334327

@@ -353,7 +346,7 @@ mod tests {
353346
let params = crate::CompletionParams {
354347
position: (position as u32).into(),
355348
text,
356-
tree: Some(&tree),
349+
tree: &tree,
357350
schema: &pgt_schema_cache::SchemaCache::default(),
358351
};
359352

@@ -381,7 +374,7 @@ mod tests {
381374
let params = crate::CompletionParams {
382375
position: (position as u32).into(),
383376
text,
384-
tree: Some(&tree),
377+
tree: &tree,
385378
schema: &pgt_schema_cache::SchemaCache::default(),
386379
};
387380

@@ -407,7 +400,7 @@ mod tests {
407400
let params = crate::CompletionParams {
408401
position: (position as u32).into(),
409402
text,
410-
tree: Some(&tree),
403+
tree: &tree,
411404
schema: &pgt_schema_cache::SchemaCache::default(),
412405
};
413406

@@ -432,7 +425,7 @@ mod tests {
432425
let params = crate::CompletionParams {
433426
position: (position as u32).into(),
434427
text,
435-
tree: Some(&tree),
428+
tree: &tree,
436429
schema: &pgt_schema_cache::SchemaCache::default(),
437430
};
438431

crates/pgt_completions/src/test_helper.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub(crate) fn get_test_params<'a>(
7070
CompletionParams {
7171
position: (position as u32).into(),
7272
schema: schema_cache,
73-
tree: Some(tree),
73+
tree,
7474
text,
7575
}
7676
}

crates/pgt_lsp/src/handlers/code_actions.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn get_actions(
4343
title: title.clone(),
4444
command: command_id,
4545
arguments: Some(vec![
46-
serde_json::Value::Number(stmt_id.into()),
46+
serde_json::to_value(&stmt_id).unwrap(),
4747
serde_json::to_value(&url).unwrap(),
4848
]),
4949
}
@@ -81,17 +81,16 @@ pub async fn execute_command(
8181

8282
match command.as_str() {
8383
"pgt.executeStatement" => {
84-
let id: usize = serde_json::from_value(params.arguments[0].clone())?;
84+
let statement_id = serde_json::from_value::<pgt_workspace::workspace::StatementId>(
85+
params.arguments[0].clone(),
86+
)?;
8587
let doc_url: lsp_types::Url = serde_json::from_value(params.arguments[1].clone())?;
8688

8789
let path = session.file_path(&doc_url)?;
8890

8991
let result = session
9092
.workspace
91-
.execute_statement(ExecuteStatementParams {
92-
statement_id: id,
93-
path,
94-
})?;
93+
.execute_statement(ExecuteStatementParams { statement_id, path })?;
9594

9695
/*
9796
* Updating all diagnostics: the changes caused by the statement execution

crates/pgt_typecheck/src/diagnostics.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,22 @@ impl Advices for TypecheckAdvices {
9696

9797
pub(crate) fn create_type_error(
9898
pg_err: &PgDatabaseError,
99-
ts: Option<&tree_sitter::Tree>,
99+
ts: &tree_sitter::Tree,
100100
) -> TypecheckDiagnostic {
101101
let position = pg_err.position().and_then(|pos| match pos {
102102
sqlx::postgres::PgErrorPosition::Original(pos) => Some(pos - 1),
103103
_ => None,
104104
});
105105

106106
let range = position.and_then(|pos| {
107-
ts.and_then(|tree| {
108-
tree.root_node()
109-
.named_descendant_for_byte_range(pos, pos)
110-
.map(|node| {
111-
TextRange::new(
112-
node.start_byte().try_into().unwrap(),
113-
node.end_byte().try_into().unwrap(),
114-
)
115-
})
116-
})
107+
ts.root_node()
108+
.named_descendant_for_byte_range(pos, pos)
109+
.map(|node| {
110+
TextRange::new(
111+
node.start_byte().try_into().unwrap(),
112+
node.end_byte().try_into().unwrap(),
113+
)
114+
})
117115
});
118116

119117
let severity = match pg_err.severity() {

crates/pgt_typecheck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct TypecheckParams<'a> {
1313
pub conn: &'a PgPool,
1414
pub sql: &'a str,
1515
pub ast: &'a pgt_query_ext::NodeEnum,
16-
pub tree: Option<&'a tree_sitter::Tree>,
16+
pub tree: &'a tree_sitter::Tree,
1717
}
1818

1919
#[derive(Debug, Clone)]

crates/pgt_typecheck/tests/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ async fn test(name: &str, query: &str, setup: &str) {
2121
.expect("Error loading sql language");
2222

2323
let root = pgt_query_ext::parse(query).unwrap();
24-
let tree = parser.parse(query, None);
24+
let tree = parser.parse(query, None).unwrap();
2525

2626
let conn = &test_db;
2727
let result = check_sql(TypecheckParams {
2828
conn,
2929
sql: query,
3030
ast: &root,
31-
tree: tree.as_ref(),
31+
tree: &tree,
3232
})
3333
.await;
3434

crates/pgt_workspace/src/workspace.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{
2121
mod client;
2222
mod server;
2323

24-
pub(crate) use server::StatementId;
24+
pub use server::StatementId;
2525

2626
#[derive(Debug, serde::Serialize, serde::Deserialize)]
2727
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]

0 commit comments

Comments
 (0)