Skip to content

Commit

Permalink
Add option to run bcommits_range as operator
Browse files Browse the repository at this point in the history
Starts operator-pending mode and shows commits in the range of lines
covered by the next text object or motion
  • Loading branch information
aaronkollasch committed Mar 22, 2023
1 parent 298a919 commit de717f6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
6 changes: 4 additions & 2 deletions doc/telescope.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1040,9 +1040,11 @@ builtin.git_bcommits_range({opts}) *telescope.builtin.git_bcommits_range()*
{git_command} (table) command that will be executed.
{"git","log","--pretty=oneline","--abbrev-commit","--no-patch","-L"}
{start} (number) the first line in the range
(optional in visual mode)
(optional in visual or operator mode)
{stop} (number) the last line in the range
(optional in visual mode)
(optional in visual or operator mode)
{operator} (boolean) select lines in operator-pending mode
(default: false)

builtin.git_branches({opts}) *telescope.builtin.git_branches()*
List branches for current directory, with output from `git log --oneline`
Expand Down
18 changes: 17 additions & 1 deletion lua/telescope/builtin/__git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ git.bcommits = function(opts)
:find()
end

local last_bcommits_range_opts = {}

local bcommits_range_callback = function()
last_bcommits_range_opts.operator = false
last_bcommits_range_opts.operator_callback = true
git.bcommits_range(last_bcommits_range_opts)
end

git.bcommits_range = function(opts)
opts.current_line = (opts.current_file == nil) and get_current_buf_line(opts.winnr) or nil
opts.current_file = vim.F.if_nil(opts.current_file, vim.api.nvim_buf_get_name(opts.bufnr))
Expand All @@ -207,6 +215,14 @@ git.bcommits_range = function(opts)
if visual then
line_number_start = vim.F.if_nil(line_number_start, vim.fn.line "v")
line_number_stop = vim.F.if_nil(line_number_stop, vim.fn.line ".")
elseif opts.operator then
last_bcommits_range_opts = opts
vim.o.operatorfunc = "v:lua.require'telescope.builtin.__git'.bcommits_range_callback"
vim.api.nvim_feedkeys("g@", "n", false)
return
elseif opts.operator_callback then
line_number_start = vim.fn.line "'["
line_number_stop = vim.fn.line "']"
end
local line_range =
string.format("%d,%d:%s", line_number_start, line_number_stop, Path:new(opts.current_file):make_relative(opts.cwd))
Expand Down Expand Up @@ -512,4 +528,4 @@ local function apply_checks(mod)
return mod
end

return apply_checks(git)
return vim.tbl_extend("keep", apply_checks(git), { bcommits_range_callback = bcommits_range_callback })
1 change: 1 addition & 0 deletions lua/telescope/builtin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ builtin.git_bcommits = require_on_exported_call("telescope.builtin.__git").bcomm
---@field git_command table: command that will be executed. {"git","log","--pretty=oneline","--abbrev-commit","--no-patch","-L"}
---@field start number: the first line in the range (optional in visual or operator mode)
---@field stop number: the last line in the range (optional in visual or operator mode)
---@field operator boolean: select lines in operator-pending mode (default: false)
builtin.git_bcommits_range = require_on_exported_call("telescope.builtin.__git").bcommits_range

--- List branches for current directory, with output from `git log --oneline` shown in the preview window
Expand Down

0 comments on commit de717f6

Please sign in to comment.