Skip to content

Commit d8e8517

Browse files
TimelordUKclaude
andcommitted
fix(nvim): Add WEB CTE support to query generation phase
Fixed both cursor detection and query generation to handle WEB CTEs: - Search phase: Case-insensitive matching for 'name AS (' - Generation phase: Added pattern for 'WEB name AS (' lines - Handles both 'WITH WEB name AS (' and standalone 'WEB name AS (' This should fix the issue where WEB CTEs were skipped, causing enriched to be detected as CTE #1 instead of CTE #2. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 029cee7 commit d8e8517

1 file changed

Lines changed: 26 additions & 7 deletions

File tree

nvim-plugin/lua/sql-cli/cte_tester_v2.lua

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,14 @@ function M.test_cte_at_cursor(config, state)
6161
-- WITH trades AS (
6262
-- WITH WEB trades AS (
6363
-- ,\n trades AS (
64-
local pattern = "%f[%w_]" .. cte_name .. "%s+AS%s*%("
65-
if line:match(pattern) then
64+
-- WEB trades AS (
65+
66+
-- Try multiple patterns to be more flexible
67+
local upper_line = line:upper()
68+
local upper_name = cte_name:upper()
69+
70+
-- Check if this line contains the CTE name followed by AS (case-insensitive)
71+
if upper_line:match("%f[%w_]" .. upper_name .. "%s+AS%s*%(") then
6672
table.insert(cte_start_lines, {
6773
index = idx,
6874
line = i,
@@ -194,15 +200,28 @@ function M.generate_simple_test_query(query_lines, target_cte, all_ctes, target_
194200
if upper:match("WITH%s") or upper:match("WITH$") then
195201
with_found = true
196202
vim.notify("Found WITH clause", vim.log.levels.DEBUG)
197-
-- Check if first CTE is on same line
198-
if line:match("WITH%s+([%w_]+)%s+AS%s*%(") then
203+
-- Check if first CTE is on same line (handles both regular and WEB CTEs)
204+
local cte_on_with_line = line:match("WITH%s+([%w_]+)%s+AS%s*%(") -- WITH name AS (
205+
if not cte_on_with_line then
206+
cte_on_with_line = line:match("WITH%s+WEB%s+([%w_]+)%s+AS%s*%(") -- WITH WEB name AS (
207+
end
208+
209+
if cte_on_with_line then
199210
current_cte_idx = 1
200-
vim.notify("First CTE on same line as WITH", vim.log.levels.DEBUG)
211+
vim.notify(string.format("First CTE '%s' on same line as WITH", cte_on_with_line), vim.log.levels.DEBUG)
201212
end
202213
end
203214
else
204-
-- Check for new CTE definitions (not on WITH line)
205-
local cte_name = line:match("^%s*([%w_]+)%s+AS%s*%(")
215+
-- Check for new CTE definitions (including WEB CTEs)
216+
-- Pattern: line contains "name AS (" where name is a CTE from our list
217+
local cte_name = line:match("^%s*([%w_]+)%s+AS%s*%(") -- Regular CTE: " name AS ("
218+
219+
-- Also check for WEB CTE on continuation line after WITH
220+
-- In this case, line looks like: "WEB name AS (" or " WEB name AS ("
221+
if not cte_name then
222+
cte_name = line:match("^%s*WEB%s+([%w_]+)%s+AS%s*%(")
223+
end
224+
206225
if cte_name then
207226
current_cte_idx = current_cte_idx + 1
208227
vim.notify(string.format("Found CTE %d: %s (target=%d)", current_cte_idx, cte_name, target_position), vim.log.levels.INFO)

0 commit comments

Comments
 (0)