-
Notifications
You must be signed in to change notification settings - Fork 102
feat: annotations #331
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
feat: annotations #331
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
62aac65
feat: annotations
psteinroe 4579400
Update crates/pgt_workspace/src/workspace/server/parsed_document.rs
psteinroe 783bb10
fix: only impl From in test
psteinroe b9a4dee
fix: typo
psteinroe 027e4ab
fix: add missing clear_statement calls
psteinroe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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 | ||
} | ||
|
||
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); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.