Skip to content
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

naive statement splitter #142

Merged
merged 13 commits into from
Oct 21, 2024
85 changes: 80 additions & 5 deletions Cargo.lock

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

45 changes: 17 additions & 28 deletions crates/pg_base_db/src/change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ impl ChangedStatement {
}
}

fn apply_text_change(text: &String, range: Option<TextRange>, change_text: &String) -> String {
fn apply_text_change(text: &str, range: Option<TextRange>, change_text: &str) -> String {
if range.is_none() {
return change_text.clone();
return change_text.to_string();
}

let range = range.unwrap();
Expand All @@ -53,7 +53,7 @@ fn apply_text_change(text: &String, range: Option<TextRange>, change_text: &Stri

let mut new_text = String::new();
new_text.push_str(&text[..start]);
new_text.push_str(&change_text);
new_text.push_str(change_text);
new_text.push_str(&text[end..]);

new_text
Expand Down Expand Up @@ -97,7 +97,7 @@ impl Change {
self.range.is_some() && self.text.len() < self.range.unwrap().len().into()
}

pub fn apply_to_text(&self, text: &String) -> String {
pub fn apply_to_text(&self, text: &str) -> String {
if self.range.is_none() {
return self.text.clone();
}
Expand All @@ -122,14 +122,10 @@ impl Change {
changed_statements.extend(
doc.drain_statements()
.into_iter()
.map(|s| StatementChange::Deleted(s)),
.map(StatementChange::Deleted),
);
// TODO also use errors returned by extract sql statement ranges
doc.statement_ranges = pg_statement_splitter::split(&self.text)
.ranges
.iter()
.map(|r| r.clone())
.collect();
doc.statement_ranges = pg_statement_splitter::split(&self.text).ranges.to_vec();
doc.text = self.text.clone();
doc.line_index = LineIndex::new(&doc.text);

Expand All @@ -155,7 +151,7 @@ impl Change {
changed_statements.push(StatementChange::Modified(ChangedStatement {
statement: StatementRef {
idx: pos,
text: doc.text[r.clone()].to_string(),
text: doc.text[*r].to_string(),
document_url: doc.url.clone(),
},
// change must be relative to statement
Expand All @@ -166,15 +162,9 @@ impl Change {
// if addition, expand the range
// if deletion, shrink the range
if self.is_addition() {
*r = TextRange::new(
r.start(),
r.end() + TextSize::from(self.diff_size()),
);
*r = TextRange::new(r.start(), r.end() + self.diff_size());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, strange that there's no method on the type for increasing the range 🤷

} else if self.is_deletion() {
*r = TextRange::new(
r.start(),
r.end() - TextSize::from(self.diff_size()),
);
*r = TextRange::new(r.start(), r.end() - self.diff_size());
}
} else if self.is_addition() {
*r += self.diff_size();
Expand Down Expand Up @@ -206,7 +196,7 @@ impl Change {
{
changed_statements.push(StatementChange::Deleted(StatementRef {
idx,
text: doc.text[r.clone()].to_string(),
text: doc.text[*r].to_string(),
document_url: doc.url.clone(),
}));

Expand Down Expand Up @@ -344,15 +334,14 @@ mod tests {
assert_eq!(d.statement_ranges.len(), 2);

for r in &pg_statement_splitter::split(&d.text).ranges {
assert_eq!(
d.statement_ranges.iter().position(|x| r == x).is_some(),
true,
assert!(
d.statement_ranges.iter().any(|x| r == x),
"should have stmt with range {:#?}",
r
);
}

assert_eq!(d.statement_ranges[0], TextRange::new(0.into(), 26.into()));
assert_eq!(d.statement_ranges[0], TextRange::new(0.into(), 25.into()));
assert_eq!(d.statement_ranges[1], TextRange::new(26.into(), 35.into()));
}

Expand All @@ -364,8 +353,8 @@ mod tests {

assert_eq!(d.statement_ranges.len(), 2);

let stmt_1_range = d.statement_ranges[0].clone();
let stmt_2_range = d.statement_ranges[1].clone();
let stmt_1_range = d.statement_ranges[0];
let stmt_2_range = d.statement_ranges[1];

let update_text = " contacts;";

Expand Down Expand Up @@ -522,8 +511,8 @@ mod tests {

assert_eq!(d.statement_ranges.len(), 2);

let stmt_1_range = d.statement_ranges[0].clone();
let stmt_2_range = d.statement_ranges[1].clone();
let stmt_1_range = d.statement_ranges[0];
let stmt_2_range = d.statement_ranges[1];

let update_text = ",test";

Expand Down
31 changes: 12 additions & 19 deletions crates/pg_base_db/src/document.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{hash::Hash, hash::Hasher, ops::RangeBounds, usize};
use std::{hash::Hash, hash::Hasher, ops::RangeBounds};

use line_index::LineIndex;
use text_size::{TextRange, TextSize};
Expand Down Expand Up @@ -44,18 +44,11 @@ impl Document {
pub fn new(url: PgLspPath, text: Option<String>) -> Document {
Document {
version: 0,
line_index: LineIndex::new(&text.as_ref().unwrap_or(&"".to_string())),
line_index: LineIndex::new(text.as_ref().unwrap_or(&"".to_string())),
// TODO: use errors returned by split
statement_ranges: text.as_ref().map_or_else(
|| Vec::new(),
|f| {
pg_statement_splitter::split(&f)
.ranges
.iter()
.map(|range| range.clone())
.collect()
},
),
statement_ranges: text.as_ref().map_or_else(Vec::new, |f| {
pg_statement_splitter::split(f).ranges.to_vec()
}),
text: text.unwrap_or("".to_string()),
Comment on lines +49 to +51
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pretty cool changes here in the file!

url,
}
Expand Down Expand Up @@ -99,7 +92,7 @@ impl Document {
.enumerate()
.map(|(idx, range)| StatementRef {
document_url: self.url.clone(),
text: self.text[range.clone()].to_string(),
text: self.text[range].to_string(),
idx,
})
.collect()
Expand All @@ -112,10 +105,10 @@ impl Document {
.enumerate()
.map(|(idx, range)| {
(
range.clone(),
*range,
StatementRef {
document_url: self.url.clone(),
text: self.text[range.clone()].to_string(),
text: self.text[*range].to_string(),
idx,
},
)
Expand All @@ -130,7 +123,7 @@ impl Document {
.enumerate()
.map(|(idx, range)| StatementRef {
document_url: self.url.clone(),
text: self.text[range.clone()].to_string(),
text: self.text[*range].to_string(),
idx,
})
.collect()
Expand All @@ -142,7 +135,7 @@ impl Document {
.get(pos)
.map(|range| StatementRef {
document_url: self.url.clone(),
text: self.text[range.clone()].to_string(),
text: self.text[*range].to_string(),
idx: pos,
})
.unwrap()
Expand All @@ -154,10 +147,10 @@ impl Document {
.get(pos)
.map(|range| {
(
range.clone(),
*range,
StatementRef {
document_url: self.url.clone(),
text: self.text[range.clone()].to_string(),
text: self.text[*range].to_string(),
idx: pos,
},
)
Expand Down
6 changes: 3 additions & 3 deletions crates/pg_lexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static PATTERN_LEXER: LazyLock<Regex> =
fn whitespace_tokens(input: &str) -> VecDeque<Token> {
let mut tokens = VecDeque::new();

for cap in PATTERN_LEXER.captures_iter(&input) {
for cap in PATTERN_LEXER.captures_iter(input) {
if let Some(whitespace) = cap.name("whitespace") {
tokens.push_back(Token {
token_type: TokenType::Whitespace,
Expand Down Expand Up @@ -139,8 +139,8 @@ pub fn lex(text: &str) -> Vec<Token> {
kind: SyntaxKind::from(&pg_query_token),
text: token_text,
span: TextRange::new(
TextSize::try_from(u32::try_from(pg_query_token.start).unwrap()).unwrap(),
TextSize::try_from(u32::try_from(pg_query_token.end).unwrap()).unwrap(),
TextSize::from(u32::try_from(pg_query_token.start).unwrap()),
TextSize::from(u32::try_from(pg_query_token.end).unwrap()),
),
});
pos += len;
Expand Down
2 changes: 2 additions & 0 deletions crates/pg_lsp/src/server/debouncer/thread.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dead_code)]

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::thread::{self, JoinHandle};
Expand Down
1 change: 1 addition & 0 deletions crates/pg_statement_splitter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ text-size = "1.1.1"

[dev-dependencies]
pg_query = "0.8"
ntest = "0.9.3"

Loading
Loading