Skip to content

Commit 03f34e6

Browse files
committed
feat: cmds without selection (issue: #194)
- without selection use current line instead for rewrite/append/prepend - improve logic for choosing default command template
1 parent 1c22349 commit 03f34e6

File tree

1 file changed

+38
-52
lines changed

1 file changed

+38
-52
lines changed

lua/gp/init.lua

Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -330,18 +330,18 @@ M.prepare_commands = function()
330330

331331
-- template is chosen dynamically based on mode in which the command is called
332332
local template = M.config.template_command
333-
if params.range == 2 then
333+
if params.range > 0 then
334334
template = M.config.template_selection
335-
-- rewrite needs custom template
336-
if target == M.Target.rewrite then
337-
template = M.config.template_rewrite
338-
end
339-
if target == M.Target.append then
340-
template = M.config.template_append
341-
end
342-
if target == M.Target.prepend then
343-
template = M.config.template_prepend
344-
end
335+
end
336+
-- rewrite needs custom template
337+
if target == M.Target.rewrite then
338+
template = M.config.template_rewrite
339+
end
340+
if target == M.Target.append then
341+
template = M.config.template_append
342+
end
343+
if target == M.Target.prepend then
344+
template = M.config.template_prepend
345345
end
346346
if agent then
347347
M.Prompt(params, target, agent, template, agent.cmd_prefix, whisper)
@@ -1696,52 +1696,38 @@ M.Prompt = function(params, target, agent, template, prompt, whisper, callback)
16961696
return
16971697
end
16981698

1699-
-- defaults to normal mode
1700-
local selection = nil
1701-
local prefix = ""
1702-
local start_line = vim.api.nvim_win_get_cursor(0)[1]
1703-
local end_line = start_line
1704-
1705-
-- handle range
1706-
if params.range == 2 then
1707-
start_line = params.line1
1708-
end_line = params.line2
1709-
local lines = vim.api.nvim_buf_get_lines(buf, start_line - 1, end_line, false)
1710-
1711-
local min_indent = nil
1712-
local use_tabs = false
1713-
-- measure minimal common indentation for lines with content
1714-
for i, line in ipairs(lines) do
1715-
lines[i] = line
1716-
-- skip whitespace only lines
1717-
if not line:match("^%s*$") then
1718-
local indent = line:match("^%s*")
1719-
-- contains tabs
1720-
if indent:match("\t") then
1721-
use_tabs = true
1722-
end
1723-
if min_indent == nil or #indent < min_indent then
1724-
min_indent = #indent
1725-
end
1699+
local start_line = params.line1
1700+
local end_line = params.line2
1701+
local lines = vim.api.nvim_buf_get_lines(buf, start_line - 1, end_line, false)
1702+
1703+
local min_indent = nil
1704+
local use_tabs = false
1705+
-- measure minimal common indentation for lines with content
1706+
for i, line in ipairs(lines) do
1707+
lines[i] = line
1708+
-- skip whitespace only lines
1709+
if not line:match("^%s*$") then
1710+
local indent = line:match("^%s*")
1711+
-- contains tabs
1712+
if indent:match("\t") then
1713+
use_tabs = true
1714+
end
1715+
if min_indent == nil or #indent < min_indent then
1716+
min_indent = #indent
17261717
end
17271718
end
1728-
if min_indent == nil then
1729-
min_indent = 0
1730-
end
1731-
prefix = string.rep(use_tabs and "\t" or " ", min_indent)
1732-
1733-
for i, line in ipairs(lines) do
1734-
lines[i] = line:sub(min_indent + 1)
1735-
end
1736-
1737-
selection = table.concat(lines, "\n")
1719+
end
1720+
if min_indent == nil then
1721+
min_indent = 0
1722+
end
1723+
local prefix = string.rep(use_tabs and "\t" or " ", min_indent)
17381724

1739-
if selection == "" then
1740-
M.logger.warning("Please select some text to rewrite")
1741-
return
1742-
end
1725+
for i, line in ipairs(lines) do
1726+
lines[i] = line:sub(min_indent + 1)
17431727
end
17441728

1729+
local selection = table.concat(lines, "\n")
1730+
17451731
M._selection_first_line = start_line
17461732
M._selection_last_line = end_line
17471733

0 commit comments

Comments
 (0)