Skip to content

Commit 54f9946

Browse files
committed
feat: column navigate feature in result view
1 parent 1a55d17 commit 54f9946

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

lua/sqlflick/display.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ function M.create_display_window()
5454
vim.api.nvim_win_close(win, true)
5555
end, opts)
5656

57+
-- vim.keymap.set("n", "c", M.map_column_navigator)
58+
5759
vim.keymap.set("n", "W", function()
5860
local query = require("sqlflick.query")
5961
local column_index = query.get_column_under_cursor()
@@ -107,6 +109,20 @@ function M.create_display_window()
107109
return buf, win
108110
end
109111

112+
function M.map_column_navigator()
113+
local query = require("sqlflick.query")
114+
local col_num = query.get_column_number()
115+
116+
for i = 1, col_num do
117+
vim.keymap.set("n", "c" .. i, function()
118+
local column_start_pos = query.get_column_start_pos(i)
119+
local cursor_pos = vim.api.nvim_win_get_cursor(0)
120+
local line_num = cursor_pos[1]
121+
vim.fn.cursor(line_num, column_start_pos)
122+
end)
123+
end
124+
end
125+
110126
-- Display query results in display window
111127
function M.display_results(buf, _, error, query, results)
112128
-- Set buffer content

lua/sqlflick/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ function M.setup(opts)
300300
-- Format and display results
301301
local lines = query.format_query_results(result)
302302
local error = result.error ~= nil and true or false
303+
display.map_column_navigator()
303304
display.display_results(M.display_buf, M.display_win, error, query_text, lines)
304305
end
305306
end, { nargs = 1 })
@@ -376,6 +377,7 @@ function M.setup(opts)
376377
-- Format and display results
377378
local lines = query.format_query_results(result)
378379
local error = result.error ~= nil and true or false
380+
display.map_column_navigator()
379381
display.display_results(M.display_buf, M.display_win, error, query_text, lines)
380382
end
381383
end, {})

lua/sqlflick/query.lua

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
local M = {}
22

33
-- Store table data for manipulation (word wrapping, etc.)
4+
---@class TableData
5+
---@field rows table
6+
---@field columns table
7+
---@field column_start_pos table
8+
---@field wrapped_columns table
49
M.table_data = nil
510

611
M.MAX_COLUMN_WIDTH = 200 -- Maximum width for any column
@@ -97,6 +102,7 @@ function M.format_query_results(result)
97102
columns = result.columns,
98103
rows = result.rows,
99104
wrapped_columns = {}, -- Track which columns are wrapped
105+
column_start_pos = {},
100106
}
101107

102108
local col_widths = {}
@@ -137,6 +143,14 @@ function M.format_query_results(result)
137143
.. truncated_col
138144
.. string.rep(" ", col_widths[i] - vim.fn.strdisplaywidth(truncated_col) - 1)
139145
header = header .. padded_col .. ""
146+
147+
local prev_pos = 1
148+
local prev_col_width = 0
149+
if i > 1 then
150+
prev_pos = M.table_data.column_start_pos[i - 1]
151+
prev_col_width = col_widths[i - 1]
152+
end
153+
M.table_data.column_start_pos[i] = prev_pos + prev_col_width + 2 + 1
140154
end
141155

142156
table.insert(lines, top)
@@ -161,6 +175,23 @@ function M.format_query_results(result)
161175
return lines
162176
end
163177

178+
function M.get_column_number()
179+
if not M.table_data then
180+
return nil
181+
end
182+
183+
return #M.table_data.columns
184+
end
185+
186+
---@param i integer
187+
function M.get_column_start_pos(i)
188+
if not M.table_data then
189+
return nil
190+
end
191+
192+
return M.table_data.column_start_pos[i]
193+
end
194+
164195
function M.get_column_under_cursor()
165196
if not M.table_data then
166197
return nil
@@ -384,6 +415,14 @@ function M.format_query_results_with_wrapping(result)
384415
local text = row_lines[i][line_idx] or ""
385416
local padded_text = " " .. text .. string.rep(" ", col_widths[i] - vim.fn.strdisplaywidth(text) - 1)
386417
line = line .. padded_text .. ""
418+
419+
local prev_pos = 1
420+
local prev_col_width = 0
421+
if i > 1 then
422+
prev_pos = M.table_data.column_start_pos[i - 1]
423+
prev_col_width = col_widths[i - 1]
424+
end
425+
M.table_data.column_start_pos[i] = prev_pos + prev_col_width + 2 + 1
387426
end
388427
table.insert(lines, line)
389428
end

0 commit comments

Comments
 (0)