From 871496d192855f7411d940268f53a977c647aa17 Mon Sep 17 00:00:00 2001 From: Gints Polis Date: Sun, 12 Jul 2026 09:30:07 +0300 Subject: [PATCH 1/2] add test for function ending with space fail --- crates/pgls_treesitter/src/context/ancestors.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/pgls_treesitter/src/context/ancestors.rs b/crates/pgls_treesitter/src/context/ancestors.rs index 67fc70be4..f00894cc7 100644 --- a/crates/pgls_treesitter/src/context/ancestors.rs +++ b/crates/pgls_treesitter/src/context/ancestors.rs @@ -165,6 +165,11 @@ mod tests { assert_no_panic_for_all_positions("-- a comment\nSELECT 1;"); } + #[test] + fn plpgsql_function_with_leading_space() { + assert_no_panic_for_all_positions("BEGIN END; "); + } + #[test] fn issue_704_regression() { let statements = vec![ From 27518bac85a012ee4a85da8b35a946d0ac6931a6 Mon Sep 17 00:00:00 2001 From: Gints Polis Date: Sun, 12 Jul 2026 11:37:51 +0300 Subject: [PATCH 2/2] fix(treesitter): do not fail when there is a space after procedure --- crates/pgls_treesitter/src/context/ancestors.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/pgls_treesitter/src/context/ancestors.rs b/crates/pgls_treesitter/src/context/ancestors.rs index f00894cc7..2e41babd2 100644 --- a/crates/pgls_treesitter/src/context/ancestors.rs +++ b/crates/pgls_treesitter/src/context/ancestors.rs @@ -24,13 +24,17 @@ impl ScopeTracker { } pub fn register<'a>(&mut self, node: tree_sitter::Node<'a>, position: usize) { - if SCOPE_BOUNDARIES.contains(&node.kind()) { + // The `program` root node is never registered directly, so the first node + // we see is one of its children. Usually that's a scope boundary (a + // statement, block, ...), but it can also be a stray token such as a + // trailing `;` – in which case we still need a base scope for it. + if SCOPE_BOUNDARIES.contains(&node.kind()) || self.scopes.is_empty() { self.add_new_scope(node); } self.scopes .last_mut() - .unwrap_or_else(|| panic!("No top-level grammar-rule found. Please create an issue with the entire Postgres file, noting cursor/hover position.")) + .expect("a scope was just ensured to exist") .ancestors .register(node, position); }