Skip to content

Commit 081cef7

Browse files
committed
fix: properly parse when there is no affected
1 parent a5ba9cb commit 081cef7

File tree

1 file changed

+76
-12
lines changed
  • crates/pgt_workspace/src/workspace/server

1 file changed

+76
-12
lines changed

crates/pgt_workspace/src/workspace/server/change.rs

+76-12
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,6 @@ impl Document {
137137
diff_size: TextSize,
138138
is_addition: bool,
139139
) -> Affected {
140-
// special case: no previous statements -> always full range
141-
if self.positions.is_empty() {
142-
let full_range = TextRange::new(0.into(), content_size);
143-
return Affected {
144-
affected_range: full_range,
145-
affected_indices: Vec::new(),
146-
prev_index: None,
147-
next_index: None,
148-
full_affected_range: full_range,
149-
};
150-
}
151-
152140
let mut start = change_range.start();
153141
let mut end = change_range.end().min(content_size);
154142

@@ -171,6 +159,16 @@ impl Document {
171159
}
172160
}
173161

162+
if affected_indices.is_empty() && prev_index.is_none() {
163+
// if there is no prev_index and no intersection -> use 0
164+
start = 0.into();
165+
}
166+
167+
if affected_indices.is_empty() && next_index.is_none() {
168+
// if there is no next_index and no intersection -> use content_size
169+
end = content_size;
170+
}
171+
174172
let first_affected_stmt_start = prev_index
175173
.map(|i| self.positions[i].1.start())
176174
.unwrap_or(start);
@@ -460,6 +458,72 @@ mod tests {
460458
assert!(d.has_fatal_error());
461459
}
462460

461+
#[test]
462+
fn comments_at_begin() {
463+
let path = PgTPath::new("test.sql");
464+
let input = "\nselect id from users;\n";
465+
466+
let mut d = Document::new(input.to_string(), 0);
467+
468+
let change1 = ChangeFileParams {
469+
path: path.clone(),
470+
version: 1,
471+
changes: vec![ChangeParams {
472+
text: "-".to_string(),
473+
range: Some(TextRange::new(0.into(), 0.into())),
474+
}],
475+
};
476+
477+
let _changed1 = d.apply_file_change(&change1);
478+
479+
assert_eq!(d.content, "-\nselect id from users;\n");
480+
assert_eq!(d.positions.len(), 2);
481+
482+
let change2 = ChangeFileParams {
483+
path: path.clone(),
484+
version: 2,
485+
changes: vec![ChangeParams {
486+
text: "-".to_string(),
487+
range: Some(TextRange::new(1.into(), 1.into())),
488+
}],
489+
};
490+
491+
let _changed2 = d.apply_file_change(&change2);
492+
493+
assert_eq!(d.content, "--\nselect id from users;\n");
494+
assert_eq!(d.positions.len(), 1);
495+
496+
let change3 = ChangeFileParams {
497+
path: path.clone(),
498+
version: 3,
499+
changes: vec![ChangeParams {
500+
text: " ".to_string(),
501+
range: Some(TextRange::new(2.into(), 2.into())),
502+
}],
503+
};
504+
505+
let _changed3 = d.apply_file_change(&change3);
506+
507+
assert_eq!(d.content, "-- \nselect id from users;\n");
508+
assert_eq!(d.positions.len(), 1);
509+
510+
let change4 = ChangeFileParams {
511+
path: path.clone(),
512+
version: 3,
513+
changes: vec![ChangeParams {
514+
text: "t".to_string(),
515+
range: Some(TextRange::new(3.into(), 3.into())),
516+
}],
517+
};
518+
519+
let _changed4 = d.apply_file_change(&change4);
520+
521+
assert_eq!(d.content, "-- t\nselect id from users;\n");
522+
assert_eq!(d.positions.len(), 1);
523+
524+
assert_document_integrity(&d);
525+
}
526+
463527
#[test]
464528
fn typing_comments() {
465529
let path = PgTPath::new("test.sql");

0 commit comments

Comments
 (0)