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

fix: make sure range is correct for eof deletions #311

Merged
merged 3 commits into from
Apr 4, 2025
Merged
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
131 changes: 131 additions & 0 deletions crates/pgt_lsp/tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,3 +981,134 @@ async fn test_issue_281() -> Result<()> {

Ok(())
}

#[tokio::test]
async fn test_issue_303() -> Result<()> {
let factory = ServerFactory::default();
let mut fs = MemoryFileSystem::default();
let test_db = get_new_test_db().await;

let setup = r#"
create table public.users (
id serial primary key,
name varchar(255) not null
);
"#;

test_db
.execute(setup)
.await
.expect("Failed to setup test database");

let mut conf = PartialConfiguration::init();
conf.merge_with(PartialConfiguration {
db: Some(PartialDatabaseConfiguration {
database: Some(
test_db
.connect_options()
.get_database()
.unwrap()
.to_string(),
),
..Default::default()
}),
..Default::default()
});
fs.insert(
url!("postgrestools.jsonc").to_file_path().unwrap(),
serde_json::to_string_pretty(&conf).unwrap(),
);

let (service, client) = factory
.create_with_fs(None, DynRef::Owned(Box::new(fs)))
.into_inner();

let (stream, sink) = client.split();
let mut server = Server::new(service);

let (sender, _) = channel(CHANNEL_BUFFER_SIZE);
let reader = tokio::spawn(client_handler(stream, sink, sender));

server.initialize().await?;
server.initialized().await?;

server.load_configuration().await?;

server.open_document("").await?;

let chars = [
"c", "r", "e", "a", "t", "e", " ", "t", "a", "b", "l", "e", " ", "\"\"", "h", "e", "l",
"l", "o",
];
let mut version = 1;

for (i, c) in chars.iter().enumerate() {
version += 1;
server
.change_document(
version,
vec![TextDocumentContentChangeEvent {
range: Some(Range {
start: Position {
line: 0,
character: i as u32,
},
end: Position {
line: 0,
character: i as u32,
},
}),
range_length: Some(0),
text: c.to_string(),
}],
)
.await?;
}

version += 1;
server
.change_document(
version,
vec![TextDocumentContentChangeEvent {
range: Some(Range {
start: Position {
line: 0,
character: 20,
},
end: Position {
line: 0,
character: 20,
},
}),
range_length: Some(0),
text: " ".to_string(),
}],
)
.await?;

version += 1;
server
.change_document(
version,
vec![TextDocumentContentChangeEvent {
range: Some(Range {
start: Position {
line: 0,
character: 20,
},
end: Position {
line: 0,
character: 21,
},
}),
range_length: Some(0),
text: "".to_string(),
}],
)
.await?;

server.shutdown().await?;
reader.abort();

Ok(())
}
16 changes: 8 additions & 8 deletions crates/pgt_workspace/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,32 +449,32 @@ mod tests {
#[test]
fn should_identify_allowed_statement_executions() {
let partial_config = PartialDatabaseConfiguration {
allow_statement_executions_against: Some(StringSet::from_iter(
vec![String::from("localhost/*")].into_iter(),
)),
allow_statement_executions_against: Some(StringSet::from_iter(vec![String::from(
"localhost/*",
)])),
host: Some("localhost".into()),
database: Some("test-db".into()),
..Default::default()
};

let config = DatabaseSettings::from(partial_config);

assert_eq!(config.allow_statement_executions, true)
assert!(config.allow_statement_executions)
}

#[test]
fn should_identify_not_allowed_statement_executions() {
let partial_config = PartialDatabaseConfiguration {
allow_statement_executions_against: Some(StringSet::from_iter(
vec![String::from("localhost/*")].into_iter(),
)),
allow_statement_executions_against: Some(StringSet::from_iter(vec![String::from(
"localhost/*",
)])),
host: Some("production".into()),
database: Some("test-db".into()),
..Default::default()
};

let config = DatabaseSettings::from(partial_config);

assert_eq!(config.allow_statement_executions, false)
assert!(!config.allow_statement_executions)
}
}
5 changes: 4 additions & 1 deletion crates/pgt_workspace/src/workspace/server/change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ impl Document {
affected_indices,
prev_index,
next_index,
full_affected_range: TextRange::new(start_incl, end_incl.min(content_size)),
full_affected_range: TextRange::new(
start_incl,
end_incl.min(content_size).max(start_incl),
),
}
}

Expand Down
Loading