Skip to content

Commit e5284d1

Browse files
TimelordUKclaude
andcommitted
feat: Add raw query analysis JSON viewer (\sJ)
**New Feature**: Raw JSON analysis viewer for debugging and understanding query structure **Added**: 1. **New keymap `\sJ`**: Show raw `--analyze-query` JSON output - Mapped to `<leader>sJ` (J for JSON) - Command: `:SqlCliAnalyzeQuery` 2. **New function `cli_analyzer.show_raw_analysis_popup()`**: - Displays raw JSON from `--analyze-query` - Pretty-formatted with syntax highlighting - Press 'y' to yank JSON to clipboard - Press 'q' or Esc to close **Existing Feature Clarification**: - `\sA` (`<leader>sA`) - User-friendly CTE analysis (formatted report) - `\sJ` (`<leader>sJ`) - Raw JSON analysis (for debugging/inspection) **Use Cases**: - **\sA**: Quick overview of CTEs, dependencies, and structure - **\sJ**: Detailed inspection of what CLI returns, useful for: - Understanding query structure - Debugging plugin issues - Verifying CTE analysis correctness - Sharing analysis output with developers **Example Output** (\sJ): ```json { "ctes": [ {"name": "trades", "cte_type": "WEB", "columns": [...], "references": []}, {"name": "filtered", "cte_type": "Standard", "columns": [...], "references": ["trades"]} ], "tables": ["trades"], "has_star": true } ``` 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent a920422 commit e5284d1

3 files changed

Lines changed: 121 additions & 1 deletion

File tree

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,87 @@ function M.is_web_cte(cte)
293293
return cte.cte_type == 'WEB'
294294
end
295295

296+
--- Show raw JSON analysis in a modal popup
297+
--- @param query string SQL query to analyze
298+
function M.show_raw_analysis_popup(query)
299+
local log = get_logger()
300+
if log then
301+
log.info('cli_analyzer', 'show_raw_analysis_popup called')
302+
end
303+
304+
-- Analyze query
305+
local analysis, err = M.analyze_query(query)
306+
307+
if err then
308+
vim.notify("Failed to analyze query: " .. err, vim.log.levels.ERROR)
309+
return
310+
end
311+
312+
-- Convert analysis to pretty JSON
313+
local json_str = vim.fn.json_encode(analysis)
314+
local ok, pretty_json = pcall(vim.fn.json_decode, json_str)
315+
if ok then
316+
json_str = vim.fn.json_encode(pretty_json)
317+
end
318+
319+
-- Format JSON with indentation
320+
local lines = {}
321+
for line in json_str:gmatch('[^\n]+') do
322+
table.insert(lines, line)
323+
end
324+
325+
-- Add header and footer
326+
table.insert(lines, 1, "")
327+
table.insert(lines, 1, "Raw CLI Analysis Output (--analyze-query)")
328+
table.insert(lines, 1, "═══════════════════════════════════════════")
329+
table.insert(lines, "")
330+
table.insert(lines, "═══════════════════════════════════════════")
331+
table.insert(lines, "Press 'y' to yank | 'q' to close")
332+
333+
-- Create buffer
334+
local buf = vim.api.nvim_create_buf(false, true)
335+
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
336+
vim.api.nvim_buf_set_option(buf, 'modifiable', false)
337+
vim.api.nvim_buf_set_option(buf, 'filetype', 'json')
338+
339+
-- Calculate window size
340+
local width = math.min(80, vim.o.columns - 10)
341+
local height = math.min(#lines + 2, vim.o.lines - 5)
342+
343+
-- Create window
344+
local win_opts = {
345+
relative = 'editor',
346+
width = width,
347+
height = height,
348+
col = math.floor((vim.o.columns - width) / 2),
349+
row = math.floor((vim.o.lines - height) / 2),
350+
style = 'minimal',
351+
border = 'rounded',
352+
title = ' Query Analysis (JSON) ',
353+
title_pos = 'center',
354+
}
355+
356+
local win = vim.api.nvim_open_win(buf, true, win_opts)
357+
358+
-- Set keymaps
359+
local opts = { noremap = true, silent = true, buffer = buf }
360+
361+
vim.keymap.set('n', 'q', function()
362+
vim.api.nvim_win_close(win, true)
363+
end, opts)
364+
365+
vim.keymap.set('n', '<Esc>', function()
366+
vim.api.nvim_win_close(win, true)
367+
end, opts)
368+
369+
vim.keymap.set('n', 'y', function()
370+
vim.fn.setreg('+', json_str)
371+
vim.notify("Analysis JSON yanked to clipboard", vim.log.levels.INFO)
372+
end, opts)
373+
374+
if log then
375+
log.info('cli_analyzer', 'Displayed raw analysis popup')
376+
end
377+
end
378+
296379
return M

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,11 @@ M.defaults = {
101101
-- Export keymaps (when in table navigation mode)
102102
export_menu = "<leader>se", -- Export menu (all formats)
103103
export_browser = nil, -- Removed - use export menu instead
104-
-- CTE testing
104+
-- CTE testing and analysis
105105
test_cte = "<leader>sC", -- Test CTE at cursor (C for CTE)
106106
test_cte_new = "<leader>sN", -- Test CTE in new buffer (N for new)
107107
cte_analysis = "<leader>sA", -- Show CTE analysis popup (A for Analysis)
108+
analyze_query = "<leader>sJ", -- Show raw query analysis JSON (J for JSON)
108109
export_markdown = "<leader>sm", -- Export as Markdown
109110
export_tsv = "<leader>sT", -- Export as Tab-separated (Excel) - capital T to avoid template conflict
110111
-- Chart visualizations (execute query at cursor and visualize)

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ function M.create_commands()
185185
cte_parser_cli.show_cte_analysis_popup()
186186
end, { desc = "Show CTE analysis popup" })
187187

188+
vim.api.nvim_create_user_command("SqlCliAnalyzeQuery", function()
189+
M.analyze_query_raw()
190+
end, { desc = "Show raw query analysis JSON" })
191+
188192
vim.api.nvim_create_user_command("SqlCliResultsToBuffer", function()
189193
results.results_to_buffer(M.state)
190194
end, { desc = "Open query results in new buffer" })
@@ -465,6 +469,12 @@ function M.setup_keymaps()
465469
end, { desc = "Show CTE analysis", silent = true })
466470
end
467471

472+
if keymaps.analyze_query then
473+
vim.keymap.set("n", keymaps.analyze_query, function()
474+
M.analyze_query_raw()
475+
end, { desc = "Show raw query analysis JSON", silent = true })
476+
end
477+
468478
if keymaps.function_help then
469479
vim.keymap.set("n", keymaps.function_help, function()
470480
functions.function_help_at_cursor(M.config)
@@ -874,6 +884,32 @@ function M.test_formatter()
874884
formatter.test_formatter(M.config)
875885
end
876886

887+
-- Show raw query analysis JSON
888+
function M.analyze_query_raw()
889+
local bufnr = vim.api.nvim_get_current_buf()
890+
local cursor_line = vim.fn.line('.')
891+
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
892+
893+
-- Find query boundaries
894+
local start_line, end_line = utils.find_query_at_cursor(lines, cursor_line)
895+
896+
if not start_line then
897+
vim.notify("No SQL query found at cursor", vim.log.levels.WARN)
898+
return
899+
end
900+
901+
-- Extract query
902+
local query_lines = {}
903+
for i = start_line, end_line do
904+
table.insert(query_lines, lines[i])
905+
end
906+
local query = table.concat(query_lines, '\n')
907+
908+
-- Show raw analysis
909+
local cli_analyzer = require('sql-cli.cli_analyzer')
910+
cli_analyzer.show_raw_analysis_popup(query)
911+
end
912+
877913
-- Expose logger module for use by other modules
878914
M.logger = logger
879915

0 commit comments

Comments
 (0)