Skip to content

Commit

Permalink
Adjust column for lua handler column option
Browse files Browse the repository at this point in the history
  • Loading branch information
notomo committed Jul 29, 2024
1 parent 8218d36 commit 84cca62
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 10 deletions.
15 changes: 7 additions & 8 deletions lua/cmdbuf/core/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local _windows = {}
local Window = {}
Window.__index = Window

--- @param buffer CmdbufBuffer
function Window.open(buffer, created, open_window, reusable_window_ids, line, column)
local origin_window_id = vim.api.nvim_get_current_win()

Expand All @@ -24,8 +25,11 @@ function Window.open(buffer, created, open_window, reusable_window_ids, line, co
elseif line then
cursorlib.to_bottom(window_id)
end

if column then
cursorlib.set_column(column - 1)
local row = vim.api.nvim_win_get_cursor(window_id)[1]
local cmdline = buffer:cmdline(row)
cursorlib.set_column(column - cmdline.column - 1)
end
end

Expand Down Expand Up @@ -72,13 +76,8 @@ function Window.cmdline_expr(self)
enter_normal_mode_cmd = ""
end

local set_pos_cmd = ""
if cmdline.column == -1 then
set_pos_cmd = ([[<End>]]):format(cmdline.column + pos[2] + 1)
else
local column = cmdline.column or 0
set_pos_cmd = ([[<C-R>=setcmdpos(%d)<CR><BS>]]):format(column + pos[2] + 1)
end
local column = cmdline.column
local set_pos_cmd = ([[<C-R>=setcmdpos(%d)<CR><BS>]]):format(column + pos[2] + 1)

local close_cmd = ([[:<C-u>lua require("cmdbuf.command").close(%s)<CR>]]):format(self._window_id)
return enter_normal_mode_cmd .. close_cmd .. cmdline.str .. set_pos_cmd
Expand Down
1 change: 1 addition & 0 deletions lua/cmdbuf/handler/vim/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ end
function M.cmdline(_, line)
return {
str = ":" .. line,
column = 0,
}
end

Expand Down
5 changes: 3 additions & 2 deletions lua/cmdbuf/handler/vim/env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ end

function M.cmdline(_, line)
local key, value = extract_key_value(line)
local str = (":lua vim.env[%q] = [=[%s]=]"):format(key, value)
return {
str = (":lua vim.env[%q] = [=[%s]=]"):format(key, value),
column = -1,
str = str,
column = #str,
}
end

Expand Down
1 change: 1 addition & 0 deletions lua/cmdbuf/handler/vim/input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function M.cmdline(_, _)
-- not supported
return {
str = ":",
column = 0,
}
end

Expand Down
1 change: 1 addition & 0 deletions lua/cmdbuf/handler/vim/search/backward.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ end
function M.cmdline(_, line)
return {
str = "?" .. line,
column = 0,
}
end

Expand Down
1 change: 1 addition & 0 deletions lua/cmdbuf/handler/vim/search/forward.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ end
function M.cmdline(_, line)
return {
str = "/" .. line,
column = 0,
}
end

Expand Down
10 changes: 10 additions & 0 deletions spec/lua/cmdbuf/handler/lua/cmd_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,14 @@ describe("lua/cmd handler", function()

assert.current_line("print(8888)")
end)

it("adjusts column for lua prefixed line", function()
cmdbuf.open({
type = typ,
line = "lua vim.b.test = 1",
column = 5,
})

assert.cursor_word("vim")
end)
end)

0 comments on commit 84cca62

Please sign in to comment.