Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/pgt_workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pgt_configuration = { workspace = true }
pgt_console = { workspace = true }
pgt_diagnostics = { workspace = true }
pgt_fs = { workspace = true, features = ["serde"] }
pgt_lexer = { workspace = true }
pgt_query_ext = { workspace = true }
pgt_schema_cache = { workspace = true }
pgt_statement_splitter = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions crates/pgt_workspace/src/workspace/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use super::{
pub use statement_identifier::StatementId;

mod analyser;
mod annotation;
mod async_helper;
mod change;
mod db_connection;
Expand Down
88 changes: 88 additions & 0 deletions crates/pgt_workspace/src/workspace/server/annotation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use std::sync::Arc;

use dashmap::DashMap;
use pgt_lexer::{SyntaxKind, WHITESPACE_TOKENS};

use super::statement_identifier::StatementId;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StatementAnnotations {
ends_with_semicolon: bool,
}

pub struct AnnotationStore {
db: DashMap<StatementId, Option<Arc<StatementAnnotations>>>,
}

impl AnnotationStore {
pub fn new() -> AnnotationStore {
AnnotationStore { db: DashMap::new() }
}

#[allow(unused)]
pub fn get_annotations(
&self,
statement: &StatementId,
content: &str,
) -> Option<Arc<StatementAnnotations>> {
if let Some(existing) = self.db.get(statement).map(|x| x.clone()) {
return existing;
}

// we swallow the error here because the lexing within the document would have already
// thrown and we wont even get here if that happened.
let annotations = pgt_lexer::lex(content).ok().map(|tokens| {
let ends_with_semicolon = tokens
.iter()
.rev()
.find(|token| !WHITESPACE_TOKENS.contains(&token.kind))
.is_some_and(|token| token.kind == SyntaxKind::Ascii59);

Arc::new(StatementAnnotations {
ends_with_semicolon,
})
});

self.db.insert(statement.clone(), None);
annotations
}

#[allow(unused)]
pub fn clear_statement(&self, id: &StatementId) {
self.db.remove(id);

if let Some(child_id) = id.get_child_id() {
self.db.remove(&child_id);
}
}
}

#[cfg(test)]
mod tests {
use crate::workspace::StatementId;

use super::AnnotationStore;

#[test]
fn annotates_correctly() {
let store = AnnotationStore::new();

let test_cases = [
("SELECT * FROM foo", false),
("SELECT * FROM foo;", true),
("SELECT * FROM foo ;", true),
("SELECT * FROM foo ; ", true),
("SELECT * FROM foo ;\n", true),
("SELECT * FROM foo\n", false),
];

for (idx, (content, expected)) in test_cases.iter().enumerate() {
let statement_id = StatementId::Root(idx.into());

let annotations = store.get_annotations(&statement_id, content);

assert!(annotations.is_some());
assert_eq!(annotations.unwrap().ends_with_semicolon, *expected);
}
}
}
6 changes: 3 additions & 3 deletions crates/pgt_workspace/src/workspace/server/change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ mod tests {
use pgt_diagnostics::Diagnostic;
use pgt_text_size::TextRange;

use crate::workspace::{ChangeFileParams, ChangeParams, server::statement_identifier::root_id};
use crate::workspace::{ChangeFileParams, ChangeParams};

use pgt_fs::PgTPath;

Expand Down Expand Up @@ -886,14 +886,14 @@ mod tests {
assert_eq!(
changed[2],
StatementChange::Added(AddedStatement {
stmt: StatementId::Root(root_id(2)),
stmt: StatementId::Root(2.into()),
text: "select id,test from users".to_string()
})
);
assert_eq!(
changed[3],
StatementChange::Added(AddedStatement {
stmt: StatementId::Root(root_id(3)),
stmt: StatementId::Root(3.into()),
text: "select 1;".to_string()
})
);
Expand Down
4 changes: 4 additions & 0 deletions crates/pgt_workspace/src/workspace/server/parsed_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use pgt_text_size::{TextRange, TextSize};
use crate::workspace::ChangeFileParams;

use super::{
annotation::AnnotationStore,
change::StatementChange,
document::{Document, StatementIterator},
pg_query::PgQueryStore,
Expand All @@ -24,6 +25,7 @@ pub struct ParsedDocument {
ast_db: PgQueryStore,
cst_db: TreeSitterStore,
sql_fn_db: SQLFunctionBodyStore,
annoation_db: AnnotationStore,
Comment thread
psteinroe marked this conversation as resolved.
Outdated
}

impl ParsedDocument {
Expand All @@ -33,6 +35,7 @@ impl ParsedDocument {
let cst_db = TreeSitterStore::new();
let ast_db = PgQueryStore::new();
let sql_fn_db = SQLFunctionBodyStore::new();
let annoation_db = AnnotationStore::new();

doc.iter().for_each(|(stmt, _, content)| {
cst_db.add_statement(&stmt, content);
Expand All @@ -44,6 +47,7 @@ impl ParsedDocument {
ast_db,
cst_db,
sql_fn_db,
annoation_db,
}
}

Expand Down
13 changes: 10 additions & 3 deletions crates/pgt_workspace/src/workspace/server/statement_identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ pub struct RootId {
inner: usize,
}

#[cfg(test)]
pub fn root_id(inner: usize) -> RootId {
RootId { inner }
impl From<RootId> for usize {
fn from(val: RootId) -> Self {
val.inner
}
}

impl From<usize> for RootId {
Comment thread
psteinroe marked this conversation as resolved.
fn from(inner: usize) -> Self {
RootId { inner }
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
Expand Down