Skip to content

refactor: parser #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/pgt_completions/src/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct CompletionParams<'a> {
pub position: TextSize,
pub schema: &'a pgt_schema_cache::SchemaCache,
pub text: String,
pub tree: Option<&'a tree_sitter::Tree>,
pub tree: &'a tree_sitter::Tree,
}

pub fn complete(params: CompletionParams) -> Vec<CompletionItem> {
Expand Down
27 changes: 10 additions & 17 deletions crates/pgt_completions/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl TryFrom<String> for ClauseType {

pub(crate) struct CompletionContext<'a> {
pub ts_node: Option<tree_sitter::Node<'a>>,
pub tree: Option<&'a tree_sitter::Tree>,
pub tree: &'a tree_sitter::Tree,
pub text: &'a str,
pub schema_cache: &'a SchemaCache,
pub position: usize,
Expand Down Expand Up @@ -85,10 +85,7 @@ impl<'a> CompletionContext<'a> {
}

fn gather_info_from_ts_queries(&mut self) {
let tree = match self.tree.as_ref() {
None => return,
Some(t) => t,
};
let tree = self.tree;

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

fn gather_tree_context(&mut self) {
if self.tree.is_none() {
return;
}

let mut cursor = self.tree.as_ref().unwrap().root_node().walk();
let mut cursor = self.tree.root_node().walk();

/*
* The head node of any treesitter tree is always the "PROGRAM" node.
Expand Down Expand Up @@ -262,7 +255,7 @@ mod tests {
let params = crate::CompletionParams {
position: (position as u32).into(),
text,
tree: Some(&tree),
tree: &tree,
schema: &pgt_schema_cache::SchemaCache::default(),
};

Expand Down Expand Up @@ -294,7 +287,7 @@ mod tests {
let params = crate::CompletionParams {
position: (position as u32).into(),
text,
tree: Some(&tree),
tree: &tree,
schema: &pgt_schema_cache::SchemaCache::default(),
};

Expand Down Expand Up @@ -328,7 +321,7 @@ mod tests {
let params = crate::CompletionParams {
position: (position as u32).into(),
text,
tree: Some(&tree),
tree: &tree,
schema: &pgt_schema_cache::SchemaCache::default(),
};

Expand All @@ -353,7 +346,7 @@ mod tests {
let params = crate::CompletionParams {
position: (position as u32).into(),
text,
tree: Some(&tree),
tree: &tree,
schema: &pgt_schema_cache::SchemaCache::default(),
};

Expand Down Expand Up @@ -381,7 +374,7 @@ mod tests {
let params = crate::CompletionParams {
position: (position as u32).into(),
text,
tree: Some(&tree),
tree: &tree,
schema: &pgt_schema_cache::SchemaCache::default(),
};

Expand All @@ -407,7 +400,7 @@ mod tests {
let params = crate::CompletionParams {
position: (position as u32).into(),
text,
tree: Some(&tree),
tree: &tree,
schema: &pgt_schema_cache::SchemaCache::default(),
};

Expand All @@ -432,7 +425,7 @@ mod tests {
let params = crate::CompletionParams {
position: (position as u32).into(),
text,
tree: Some(&tree),
tree: &tree,
schema: &pgt_schema_cache::SchemaCache::default(),
};

Expand Down
2 changes: 1 addition & 1 deletion crates/pgt_completions/src/test_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub(crate) fn get_test_params<'a>(
CompletionParams {
position: (position as u32).into(),
schema: schema_cache,
tree: Some(tree),
tree,
text,
}
}
11 changes: 5 additions & 6 deletions crates/pgt_lsp/src/handlers/code_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn get_actions(
title: title.clone(),
command: command_id,
arguments: Some(vec![
serde_json::Value::Number(stmt_id.into()),
serde_json::to_value(&stmt_id).unwrap(),
serde_json::to_value(&url).unwrap(),
]),
}
Expand Down Expand Up @@ -81,17 +81,16 @@ pub async fn execute_command(

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

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

let result = session
.workspace
.execute_statement(ExecuteStatementParams {
statement_id: id,
path,
})?;
.execute_statement(ExecuteStatementParams { statement_id, path })?;

/*
* Updating all diagnostics: the changes caused by the statement execution
Expand Down
20 changes: 9 additions & 11 deletions crates/pgt_typecheck/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,22 @@ impl Advices for TypecheckAdvices {

pub(crate) fn create_type_error(
pg_err: &PgDatabaseError,
ts: Option<&tree_sitter::Tree>,
ts: &tree_sitter::Tree,
) -> TypecheckDiagnostic {
let position = pg_err.position().and_then(|pos| match pos {
sqlx::postgres::PgErrorPosition::Original(pos) => Some(pos - 1),
_ => None,
});

let range = position.and_then(|pos| {
ts.and_then(|tree| {
tree.root_node()
.named_descendant_for_byte_range(pos, pos)
.map(|node| {
TextRange::new(
node.start_byte().try_into().unwrap(),
node.end_byte().try_into().unwrap(),
)
})
})
ts.root_node()
.named_descendant_for_byte_range(pos, pos)
.map(|node| {
TextRange::new(
node.start_byte().try_into().unwrap(),
node.end_byte().try_into().unwrap(),
)
})
});

let severity = match pg_err.severity() {
Expand Down
2 changes: 1 addition & 1 deletion crates/pgt_typecheck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct TypecheckParams<'a> {
pub conn: &'a PgPool,
pub sql: &'a str,
pub ast: &'a pgt_query_ext::NodeEnum,
pub tree: Option<&'a tree_sitter::Tree>,
pub tree: &'a tree_sitter::Tree,
}

#[derive(Debug, Clone)]
Expand Down
4 changes: 2 additions & 2 deletions crates/pgt_typecheck/tests/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ async fn test(name: &str, query: &str, setup: &str) {
.expect("Error loading sql language");

let root = pgt_query_ext::parse(query).unwrap();
let tree = parser.parse(query, None);
let tree = parser.parse(query, None).unwrap();

let conn = &test_db;
let result = check_sql(TypecheckParams {
conn,
sql: query,
ast: &root,
tree: tree.as_ref(),
tree: &tree,
})
.await;

Expand Down
2 changes: 1 addition & 1 deletion crates/pgt_workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
mod client;
mod server;

pub(crate) use server::StatementId;
pub use server::StatementId;

#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
Expand Down
Loading