Skip to content

feat: Add DiffviewToggle command #517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions doc/diffview.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ for any git rev.

USAGE *diffview-usage*

Quick-start: `:DiffviewOpen` to open a Diffview that compares against the
Quick-start: `:DiffviewOpen` or `:DiffviewToggle` to open a Diffview that compares against the
index.

You can have multiple Diffviews open, tracking different git revs. Each Diffview
opens in its own tabpage. To close and dispose of a Diffview, call either
`:DiffviewClose` or `:tabclose` while a Diffview is the current tabpage.
opens in its own tabpage. To close and dispose of a Diffview, call
`:DiffviewClose`, `:DiffviewToggle` or `:tabclose` while a Diffview is the current tabpage.
Diffviews are automatically updated:

• Every time you enter a Diffview
Expand Down Expand Up @@ -370,6 +370,13 @@ COMMANDS *diffview-commands*
*:DiffviewClose*
:DiffviewClose Close the active Diffview.

*:DiffviewToggle*
:DiffviewToggle [git-rev] [options] [ -- {paths...}]

Alias for `:DiffviewOpen` when the current tabpage is not a Diffview,
otherwise acts as an alias for `:DiffviewClose`. Accepts the same
arguments as `:DiffviewOpen`.

*:DiffviewToggleFiles*
:DiffviewToggleFiles Toggles the file panel.

Expand Down
10 changes: 10 additions & 0 deletions lua/diffview/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ function M.close(tabpage)
end
end

-- @param args string[]
function M.toggle(args)
local view = lib.get_current_view()
if view then
M.close()
else
M.open(args)
end
end

function M.completion(_, cmd_line, cur_pos)
local ctx = arg_parser.scan(cmd_line, { cur_pos = cur_pos, allow_ex_range = true })
local cmd = ctx.args[1]
Expand Down
4 changes: 4 additions & 0 deletions plugin/diffview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ command("DiffviewOpen", function(ctx)
diffview.open(arg_parser.scan(ctx.args).args)
end, { nargs = "*", complete = completion })

command("DiffviewToggle", function(ctx)
diffview.toggle(arg_parser.scan(ctx.args).args)
end, { nargs = "*", complete = completion })

command("DiffviewFileHistory", function(ctx)
local range

Expand Down