Skip to content

fix: properly parse when there is no affected #356

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ jobs:
GITHUB_REPO: ${{ github.repository }}

- name: Ensure tag matches
if: ${{ steps.create_changelog.outputs.version }} != ${{ needs.extract_version.outputs.version }}
if: steps.create_changelog.outputs.version != needs.extract_version.outputs.version
run: exit 1

- name: 👇 Download Artifacts
Expand Down
88 changes: 76 additions & 12 deletions crates/pgt_workspace/src/workspace/server/change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,6 @@ impl Document {
diff_size: TextSize,
is_addition: bool,
) -> Affected {
// special case: no previous statements -> always full range
if self.positions.is_empty() {
let full_range = TextRange::new(0.into(), content_size);
return Affected {
affected_range: full_range,
affected_indices: Vec::new(),
prev_index: None,
next_index: None,
full_affected_range: full_range,
};
}

let mut start = change_range.start();
let mut end = change_range.end().min(content_size);

Expand All @@ -171,6 +159,16 @@ impl Document {
}
}

if affected_indices.is_empty() && prev_index.is_none() {
// if there is no prev_index and no intersection -> use 0
start = 0.into();
}

if affected_indices.is_empty() && next_index.is_none() {
// if there is no next_index and no intersection -> use content_size
end = content_size;
}

let first_affected_stmt_start = prev_index
.map(|i| self.positions[i].1.start())
.unwrap_or(start);
Expand Down Expand Up @@ -460,6 +458,72 @@ mod tests {
assert!(d.has_fatal_error());
}

#[test]
fn comments_at_begin() {
let path = PgTPath::new("test.sql");
let input = "\nselect id from users;\n";

let mut d = Document::new(input.to_string(), 0);

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

let _changed1 = d.apply_file_change(&change1);

assert_eq!(d.content, "-\nselect id from users;\n");
assert_eq!(d.positions.len(), 2);

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

let _changed2 = d.apply_file_change(&change2);

assert_eq!(d.content, "--\nselect id from users;\n");
assert_eq!(d.positions.len(), 1);

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

let _changed3 = d.apply_file_change(&change3);

assert_eq!(d.content, "-- \nselect id from users;\n");
assert_eq!(d.positions.len(), 1);

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

let _changed4 = d.apply_file_change(&change4);

assert_eq!(d.content, "-- t\nselect id from users;\n");
assert_eq!(d.positions.len(), 1);

assert_document_integrity(&d);
}

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