diff --git a/crates/pgt_lsp/tests/server.rs b/crates/pgt_lsp/tests/server.rs index 4a5655e6..8e40c097 100644 --- a/crates/pgt_lsp/tests/server.rs +++ b/crates/pgt_lsp/tests/server.rs @@ -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(()) +} 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) } } 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), + ), } }