Skip to content

Commit 2cc8ff1

Browse files
committed
chore: backward/forward navigating keymapping for column
1 parent 7b9c1ed commit 2cc8ff1

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,13 @@ The following mappings are available for SQL-related file types (e.g., `.sql`, `
131131
| `SQLFlickExecute` | v | `<leader>sq` | Execute selected query |
132132

133133
The following mappings are available in specific pages. (not user command)
134-
| name | mode |Key binding | runnable view | description |
135-
| ------------------ | ----|- | ------------- | ----------------------------------------------------- |
136-
| Toggle column wrap | n | `W`| result view | Place your cursor to long data column, to toggle wrap |
137-
| Navigate column | n | `c{i}` | result view | Navigate cursor to specified column number |
134+
135+
| name | mode | Key binding | runnable view | description |
136+
| ------------------ | ---- | ----------- | ------------- | ----------------------------------------------------- |
137+
| Toggle column wrap | n | `W` | result view | Place your cursor to long data column, to toggle wrap |
138+
| Navigate column | n | `c{i}` | result view | Navigate cursor to specified column number |
139+
| Previous column | n | `[c` | result view | Navigate cursor to previous column |
140+
| Next column | n | `]c` | result view | Navigate cursor to next column |
138141

139142
## Supported Database
140143

lua/sqlflick/display.lua

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,38 @@ function M.create_display_window()
5656

5757
-- vim.keymap.set("n", "c", M.map_column_navigator)
5858

59+
vim.keymap.set("n", "[c", function()
60+
local query = require("sqlflick.query")
61+
local cursor_column = query.get_column_under_cursor()
62+
local cursor_pos = vim.api.nvim_win_get_cursor(0)
63+
local line_num = cursor_pos[1]
64+
if nil == cursor_column then
65+
if cursor_pos[2] == 0 then
66+
cursor_column = 1
67+
else
68+
cursor_column = query.get_column_number()
69+
end
70+
end
71+
local column_start_pos = query.get_column_start_pos(cursor_column - 1)
72+
vim.fn.cursor(line_num, column_start_pos)
73+
end, { desc = "Move cursor to previous column" })
74+
75+
vim.keymap.set("n", "]c", function()
76+
local query = require("sqlflick.query")
77+
local cursor_pos = vim.api.nvim_win_get_cursor(0)
78+
local line_num = cursor_pos[1]
79+
local cursor_column = query.get_column_under_cursor()
80+
if nil == cursor_column then
81+
if cursor_pos[2] == 0 then
82+
cursor_column = 1
83+
else
84+
cursor_column = query.get_column_number()
85+
end
86+
end
87+
local column_start_pos = query.get_column_start_pos(cursor_column + 1)
88+
vim.fn.cursor(line_num, column_start_pos)
89+
end, { desc = "Move cursor to next column" })
90+
5991
vim.keymap.set("n", "W", function()
6092
local query = require("sqlflick.query")
6193
local column_index = query.get_column_under_cursor()
@@ -119,7 +151,7 @@ function M.map_column_navigator()
119151
local cursor_pos = vim.api.nvim_win_get_cursor(0)
120152
local line_num = cursor_pos[1]
121153
vim.fn.cursor(line_num, column_start_pos)
122-
end)
154+
end, { desc = "Navigate cursor to column" })
123155
end
124156
end
125157

lua/sqlflick/query.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ end
177177

178178
function M.get_column_number()
179179
if not M.table_data then
180-
return nil
180+
return 0
181181
end
182182

183183
return #M.table_data.columns
@@ -186,7 +186,11 @@ end
186186
---@param i integer
187187
function M.get_column_start_pos(i)
188188
if not M.table_data then
189-
return nil
189+
return 0
190+
end
191+
192+
if #M.table_data.column_start_pos < i or i < 1 then
193+
return 0
190194
end
191195

192196
return M.table_data.column_start_pos[i]

0 commit comments

Comments
 (0)