From 4d0d075e7d4aa809f6a0bef5bedb574c6c2f73a8 Mon Sep 17 00:00:00 2001 From: psteinroe Date: Fri, 4 Apr 2025 18:33:25 +0200 Subject: [PATCH 1/3] fix: make sure range is correct for eof deletions --- crates/pgt_lsp/tests/server.rs | 134 +++++++++++++++++- .../src/workspace/server/change.rs | 5 +- 2 files changed, 137 insertions(+), 2 deletions(-) diff --git a/crates/pgt_lsp/tests/server.rs b/crates/pgt_lsp/tests/server.rs index 4a5655e6..a992b4e4 100644 --- a/crates/pgt_lsp/tests/server.rs +++ b/crates/pgt_lsp/tests/server.rs @@ -23,6 +23,7 @@ use sqlx::Executor; use std::any::type_name; use std::fmt::Display; use std::time::Duration; +use test_log::test; use tower::timeout::Timeout; use tower::{Service, ServiceExt}; use tower_lsp::LspService; @@ -457,7 +458,7 @@ async fn server_shutdown() -> Result<()> { Ok(()) } -#[tokio::test] +#[test(tokio::test)] async fn test_completions() -> Result<()> { let factory = ServerFactory::default(); let mut fs = MemoryFileSystem::default(); @@ -981,3 +982,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(()) +} diff --git a/crates/pgt_workspace/src/workspace/server/change.rs b/crates/pgt_workspace/src/workspace/server/change.rs index 226a0ffd..e31e4178 100644 --- a/crates/pgt_workspace/src/workspace/server/change.rs +++ b/crates/pgt_workspace/src/workspace/server/change.rs @@ -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), + ), } } From 15f3df6a8b5d9dbeaba9549b0555b90605d639ee Mon Sep 17 00:00:00 2001 From: psteinroe Date: Fri, 4 Apr 2025 18:35:41 +0200 Subject: [PATCH 2/3] fix: cleanup --- crates/pgt_lsp/tests/server.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/pgt_lsp/tests/server.rs b/crates/pgt_lsp/tests/server.rs index a992b4e4..8e40c097 100644 --- a/crates/pgt_lsp/tests/server.rs +++ b/crates/pgt_lsp/tests/server.rs @@ -23,7 +23,6 @@ use sqlx::Executor; use std::any::type_name; use std::fmt::Display; use std::time::Duration; -use test_log::test; use tower::timeout::Timeout; use tower::{Service, ServiceExt}; use tower_lsp::LspService; @@ -458,7 +457,7 @@ async fn server_shutdown() -> Result<()> { Ok(()) } -#[test(tokio::test)] +#[tokio::test] async fn test_completions() -> Result<()> { let factory = ServerFactory::default(); let mut fs = MemoryFileSystem::default(); From e258bea675e1fdb75a840ebfaca6726e5cda714a Mon Sep 17 00:00:00 2001 From: psteinroe Date: Fri, 4 Apr 2025 18:36:47 +0200 Subject: [PATCH 3/3] fix: lint-fix --- crates/pgt_workspace/src/settings.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/pgt_workspace/src/settings.rs b/crates/pgt_workspace/src/settings.rs index 2e7d8f53..d4ea462a 100644 --- a/crates/pgt_workspace/src/settings.rs +++ b/crates/pgt_workspace/src/settings.rs @@ -449,9 +449,9 @@ 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() @@ -459,15 +459,15 @@ mod tests { 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() @@ -475,6 +475,6 @@ mod tests { let config = DatabaseSettings::from(partial_config); - assert_eq!(config.allow_statement_executions, false) + assert!(!config.allow_statement_executions) } }