Skip to content

Commit 520bb5c

Browse files
authored
feat: Do not change cursor position on select_entry (#71)
* Do not change cursor position on select_entry Consistent behavior with select_next_entry and select_prev_entry. * Create focus_entry; selects entry and focuses * Document focus_entry behavior
1 parent 5965e83 commit 520bb5c

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ for any git rev.
2424
Install the plugin with your package manager of choice.
2525

2626
```vim
27-
" Plug
27+
" Plug
2828
Plug 'sindrets/diffview.nvim'
2929
```
3030

@@ -70,7 +70,7 @@ require'diffview'.setup {
7070
-- The `view` bindings are active in the diff buffers, only when the current
7171
-- tabpage is a Diffview.
7272
view = {
73-
["<tab>"] = cb("select_next_entry"), -- Open the diff for the next file
73+
["<tab>"] = cb("select_next_entry"), -- Open the diff for the next file
7474
["<s-tab>"] = cb("select_prev_entry"), -- Open the diff for the previous file
7575
["gf"] = cb("goto_file"), -- Open the file in a new split in previous tabpage
7676
["<C-w><C-f>"] = cb("goto_file_split"), -- Open the file in a new split
@@ -139,6 +139,15 @@ that only really make sense specifically in the file panel, such as
139139
invoked from the view, these will target the file currently open in the view
140140
rather than the file under the cursor in the file panel.
141141

142+
### Available Unused Mappings
143+
144+
This section documents key-mappable functions that are not mapped by default.
145+
146+
- `focus_entry`
147+
- Like `select_entry`, but open the diff for the selected entry while
148+
focusing cursor on the current file. Available in both the file panel and
149+
the history panel.
150+
142151
## File History
143152

144153
![file-history-multi](https://user-images.githubusercontent.com/2786478/131269782-f4184640-6d73-4226-b425-feccb5002dd0.png)

doc/diffview.txt

+10-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Example configuration with default settings:
6767
-- The `view` bindings are active in the diff buffers, only when the current
6868
-- tabpage is a Diffview.
6969
view = {
70-
["<tab>"] = cb("select_next_entry"), -- Open the diff for the next file
70+
["<tab>"] = cb("select_next_entry"), -- Open the diff for the next file
7171
["<s-tab>"] = cb("select_prev_entry"), -- Open the diff for the previous file
7272
["gf"] = cb("goto_file"), -- Open the file in a new split in previous tabpage
7373
["<C-w><C-f>"] = cb("goto_file_split"), -- Open the file in a new split
@@ -131,6 +131,15 @@ enhanced_diff_hl *diffview-config-enhanced_diff_hl*
131131
in the left diff buffer will be highlighted as |hl-DiffDelete|, and
132132
the delete fill-chars will be highlighted more subtly.
133133

134+
AVAILABLE UNUSED MAPPINGS *diffview-available-unused-mappings*
135+
136+
This section documents key-mappable functions that are not mapped by default.
137+
138+
focus_entry *diffview-config-focus-entry*
139+
Like select_entry, but open the diff for the selected entry while
140+
focusing cursor on the current file. Available in both the file panel
141+
and the history panel.
142+
134143
LAYOUT *diffview-layout*
135144

136145
The diff windows can be aligned either with a horizontal split or a vertical

lua/diffview/views/diff/listeners.lua

+8
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ return function(view)
6262
view.panel:highlight_prev_file()
6363
end,
6464
select_entry = function()
65+
if view.panel:is_open() then
66+
local file = view.panel:get_file_at_cursor()
67+
if file then
68+
view:set_file(file, false)
69+
end
70+
end
71+
end,
72+
focus_entry = function()
6573
if view.panel:is_open() then
6674
local file = view.panel:get_file_at_cursor()
6775
if file then

lua/diffview/views/file_history/listeners.lua

+17
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@ return function(view)
8989
view.panel:highlight_prev_item()
9090
end,
9191
select_entry = function()
92+
if view.panel:is_cur_win() then
93+
local item = view.panel:get_item_at_cursor()
94+
if item then
95+
-- print(vim.inspect(item))
96+
if item.files then
97+
if view.panel.single_file then
98+
view:set_file(item.files[1], false)
99+
else
100+
view.panel:toggle_entry_fold(item)
101+
end
102+
else
103+
view:set_file(item, false)
104+
end
105+
end
106+
end
107+
end,
108+
focus_entry = function()
92109
if view.panel:is_cur_win() then
93110
local item = view.panel:get_item_at_cursor()
94111
if item then

0 commit comments

Comments
 (0)