From e3a2af264a6007edee26547aec10eac5f5dd1d7d Mon Sep 17 00:00:00 2001 From: Nick deLannoy Date: Thu, 27 Jun 2024 12:28:21 -0500 Subject: [PATCH 1/2] add toggle command --- lua/diffview/init.lua | 11 +++++++++++ plugin/diffview.lua | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/lua/diffview/init.lua b/lua/diffview/init.lua index 4063f71e..0055d650 100644 --- a/lua/diffview/init.lua +++ b/lua/diffview/init.lua @@ -133,6 +133,7 @@ function M.open(args) end end + ---@param range? { [1]: integer, [2]: integer } ---@param args string[] function M.file_history(range, args) @@ -157,6 +158,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 From baa110f3a6c2fbf62fe778d8a5d1821a8468c1c3 Mon Sep 17 00:00:00 2001 From: Nick deLannoy Date: Thu, 27 Jun 2024 12:48:26 -0500 Subject: [PATCH 2/2] add docs for toggle command --- doc/diffview.txt | 13 ++++++++++--- lua/diffview/init.lua | 1 - 2 files changed, 10 insertions(+), 4 deletions(-) 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 0055d650..938f4e8f 100644 --- a/lua/diffview/init.lua +++ b/lua/diffview/init.lua @@ -133,7 +133,6 @@ function M.open(args) end end - ---@param range? { [1]: integer, [2]: integer } ---@param args string[] function M.file_history(range, args)