diff --git a/doc/diffview.txt b/doc/diffview.txt index 3a564c12..f76ab945 100644 --- a/doc/diffview.txt +++ b/doc/diffview.txt @@ -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 @@ -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. diff --git a/lua/diffview/init.lua b/lua/diffview/init.lua index 4063f71e..938f4e8f 100644 --- a/lua/diffview/init.lua +++ b/lua/diffview/init.lua @@ -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] diff --git a/plugin/diffview.lua b/plugin/diffview.lua index c7eeb54b..f402d57d 100644 --- a/plugin/diffview.lua +++ b/plugin/diffview.lua @@ -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