Skip to content

fix: broken text after receiving batched changes #413

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
50 changes: 46 additions & 4 deletions crates/pgt_workspace/src/workspace/server/change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ impl Document {

// when we recieive more than one change, we need to push back the changes based on the
// total range of the previous ones. This is because the ranges are always related to the original state.
// BUT: only for the statement range changes, not for the text changes
// this is why we pass both varaints to apply_change
let mut changes = Vec::new();

let mut offset: i64 = 0;
Expand All @@ -86,9 +88,9 @@ impl Document {
change
};

changes.extend(self.apply_change(adjusted_change));
changes.extend(self.apply_change(adjusted_change, change));

offset += change.change_size();
offset += adjusted_change.change_size();
}

self.version = change.version;
Expand Down Expand Up @@ -240,9 +242,17 @@ impl Document {
}

/// Applies a single change to the document and returns the affected statements
fn apply_change(&mut self, change: &ChangeParams) -> Vec<StatementChange> {
///
/// * `change`: The range-adjusted change to use for statement changes
/// * `original_change`: The original change to use for text changes (yes, this is a bit confusing, and we might want to refactor this entire thing at some point.)
fn apply_change(
&mut self,
change: &ChangeParams,
original_change: &ChangeParams,
) -> Vec<StatementChange> {
// if range is none, we have a full change
if change.range.is_none() {
// doesnt matter what change since range is null
return self.apply_full_change(change);
}

Expand All @@ -255,7 +265,7 @@ impl Document {

let change_range = change.range.unwrap();
let previous_content = self.content.clone();
let new_content = change.apply_to_text(&self.content);
let new_content = original_change.apply_to_text(&self.content);

// we first need to determine the affected range and all affected statements, as well as
// the index of the prev and the next statement, if any. The full affected range is the
Expand Down Expand Up @@ -1663,6 +1673,38 @@ KEY (\"organisation_id\") REFERENCES \"public\".\"organisation\"(\"id\") ON UPDA
assert_document_integrity(&doc);
}

#[test]
fn test_content_out_of_sync() {
let path = PgTPath::new("test.sql");
let initial_content = "select 1, 2, 2232231313393319 from unknown_users;\n";

let mut doc = Document::new(initial_content.to_string(), 0);

let change1 = ChangeFileParams {
path: path.clone(),
version: 1,
changes: vec![
ChangeParams {
range: Some(TextRange::new(29.into(), 29.into())),
text: "3".to_string(),
},
ChangeParams {
range: Some(TextRange::new(30.into(), 30.into())),
text: "1".to_string(),
},
],
};

let _changes = doc.apply_file_change(&change1);

assert_eq!(
doc.content,
"select 1, 2, 223223131339331931 from unknown_users;\n"
);

assert_document_integrity(&doc);
}

#[test]
fn test_comments_only() {
let path = PgTPath::new("test.sql");
Expand Down
Loading