Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
psteinroe merged 6 commits intomainfromfix/bug
Jun 10, 2025
Merged
Changes fromall 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
80 changes: 62 additions & 18 deletionscrates/pgt_workspace/src/workspace/server/change.rs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand Up@@ -1560,28 +1570,29 @@ mod tests {
fn multiple_deletions_at_once() {
let path = PgTPath::new("test.sql");

let mut doc = Document::new("\n\n\n\nALTER TABLE ONLY \"public\".\"sendout\"\n ADD CONSTRAINT \"sendout_organisation_id_fkey\" FOREIGN
KEY (\"organisation_id\") REFERENCES \"public\".\"organisation\"(\"id\") ON UPDATE RESTRICT ON DELETE CASCADE;\n".to_string(), 0);
let mut doc = Document::new("ALTER TABLE ONLY public.omni_channel_message ADD CONSTRAINT omni_channel_message_organisation_id_fkey FOREIGN KEY (organisation_id) REFERENCES public.organisation(id) ON UPDATE RESTRICT ON DELETE CASCADE;".to_string(), 0);

let change = ChangeFileParams {
path: path.clone(),
version: 1,
changes: vec![
ChangeParams {
range: Some(TextRange::new(31.into(),38.into())),
text: "te".to_string(),
range: Some(TextRange::new(60.into(),80.into())),
text: "sendout".to_string(),
},
ChangeParams {
range: Some(TextRange::new(60.into(),67.into())),
text: "te".to_string(),
range: Some(TextRange::new(24.into(),44.into())),
text: "sendout".to_string(),
},
],
};

let changed = doc.apply_file_change(&change);

assert_eq!(doc.content, "\n\n\n\nALTER TABLE ONLY \"public\".\"te\"\n ADD CONSTRAINT \"te_organisation_id_fkey\" FOREIGN
KEY (\"organisation_id\") REFERENCES \"public\".\"organisation\"(\"id\") ON UPDATE RESTRICT ON DELETE CASCADE;\n");
assert_eq!(
doc.content,
"ALTER TABLE ONLY public.sendout ADD CONSTRAINT sendout_organisation_id_fkey FOREIGN KEY (organisation_id) REFERENCES public.organisation(id) ON UPDATE RESTRICT ON DELETE CASCADE;"
);

assert_eq!(changed.len(), 2);

Expand All@@ -1592,28 +1603,29 @@ KEY (\"organisation_id\") REFERENCES \"public\".\"organisation\"(\"id\") ON UPDA
fn multiple_additions_at_once() {
let path = PgTPath::new("test.sql");

let mut doc = Document::new("\n\n\n\nALTER TABLE ONLY \"public\".\"sendout\"\n ADD CONSTRAINT \"sendout_organisation_id_fkey\" FOREIGN
KEY (\"organisation_id\") REFERENCES \"public\".\"organisation\"(\"id\") ON UPDATE RESTRICT ON DELETE CASCADE;\n".to_string(), 0);
let mut doc = Document::new("ALTER TABLE ONLY public.sendout ADD CONSTRAINT sendout_organisation_id_fkey FOREIGN KEY (organisation_id) REFERENCES public.organisation(id) ON UPDATE RESTRICT ON DELETE CASCADE;".to_string(), 0);

let change = ChangeFileParams {
path: path.clone(),
version: 1,
changes: vec![
ChangeParams {
range: Some(TextRange::new(31.into(),38.into())),
range: Some(TextRange::new(47.into(),54.into())),
text: "omni_channel_message".to_string(),
},
ChangeParams {
range: Some(TextRange::new(60.into(),67.into())),
range: Some(TextRange::new(24.into(),31.into())),
text: "omni_channel_message".to_string(),
},
],
};

let changed = doc.apply_file_change(&change);

assert_eq!(doc.content, "\n\n\n\nALTER TABLE ONLY \"public\".\"omni_channel_message\"\n ADD CONSTRAINT \"omni_channel_message_organisation_id_fkey\" FOREIGN
KEY (\"organisation_id\") REFERENCES \"public\".\"organisation\"(\"id\") ON UPDATE RESTRICT ON DELETE CASCADE;\n");
assert_eq!(
doc.content,
"ALTER TABLE ONLY public.omni_channel_message ADD CONSTRAINT omni_channel_message_organisation_id_fkey FOREIGN KEY (organisation_id) REFERENCES public.organisation(id) ON UPDATE RESTRICT ON DELETE CASCADE;"
);

assert_eq!(changed.len(), 2);

Expand DownExpand Up@@ -1663,6 +1675,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

[8]ページ先頭

©2009-2025 Movatter.jp