Skip to content

Commit ef660f2

Browse files
authored
Merge pull request #1083 from NeogitOrg/editor/next-previous
2 parents e8abdee + 5d2ad30 commit ef660f2

File tree

6 files changed

+122
-23
lines changed

6 files changed

+122
-23
lines changed

doc/neogit.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ CONTENTS *neogit_contents*
5656

5757
7. Buffers |neogit_buffers|
5858
• Status |neogit_status_buffer|
59+
• Editor |neogit_editor_buffer|
5960
• Log |neogit_log_buffer|
6061
• Reflog |neogit_reflog_buffer|
6162
• Commit |neogit_commit_buffer|
@@ -1428,9 +1429,11 @@ Actions: *neogit_worktree_popup_actions*
14281429
7. Buffers *neogit_buffers*
14291430

14301431
• Status |neogit_status_buffer|
1432+
• Editor |neogit_editor_buffer|
14311433
• Log |neogit_log_buffer|
14321434
• Reflog |neogit_reflog_buffer|
14331435
• Commit |neogit_commit_buffer|
1436+
• Rebase Todo |neogit_rebase_todo_buffer|
14341437

14351438
==============================================================================
14361439
Status Buffer *neogit_status_buffer*
@@ -1441,6 +1444,47 @@ Untracked Files *neogit_status_buffer_untracked*
14411444
untracked files entirely, "normal" to show files and directories (default),
14421445
or "all" to show all files in all directories.
14431446

1447+
==============================================================================
1448+
Editor Buffer *neogit_editor_buffer*
1449+
1450+
Commands: *neogit_editor_commands*
1451+
• Close *neogit_editor_close*
1452+
Default key: `q`
1453+
1454+
Closes the editor buffer. If there are unsaved changes, user will be
1455+
prompted to save them. Discarding the changes will abort.
1456+
1457+
• Submit *neogit_editor_submit*
1458+
Default key: `<c-c><c-c>`
1459+
1460+
Writes and closes the editor.
1461+
1462+
• Abort *neogit_editor_abort*
1463+
Default key: `<c-c><c-k>`
1464+
1465+
Closes the editor buffer, discarding any changes that might have been
1466+
written.
1467+
1468+
• PrevMessage *neogit_editor_prev_message*
1469+
Default key: `<m-p>`
1470+
1471+
Replaces the current commit message with the previously used one, via
1472+
reflog. Messages that have been discarded via reset will be included as
1473+
well. Any changes a user makes to a message will be stored while the
1474+
editor is open.
1475+
1476+
• NextMessage *neogit_editor_next_message*
1477+
Default key: `<m-n>`
1478+
1479+
Replaces the current commit message with the next used one, via
1480+
reflog. Same semantics as |neogit_editor_prev_message| apply.
1481+
1482+
• ResetMessage *neogit_editor_reset_message*
1483+
Default key: `<m-r>`
1484+
1485+
If a user has changed a message that was found via reflog, reset it to how
1486+
it appeared in reflog.
1487+
14441488
==============================================================================
14451489
Log Buffer *neogit_log_buffer*
14461490
(TODO)

lua/neogit/buffers/editor/init.lua

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local Buffer = require("neogit.lib.buffer")
22
local config = require("neogit.config")
33
local input = require("neogit.lib.input")
44
local util = require("neogit.lib.util")
5+
local git = require("neogit.lib.git")
56

67
local pad = util.pad_right
78

@@ -42,6 +43,25 @@ function M:open(kind)
4243
local mapping = config.get_reversed_commit_editor_maps()
4344
local aborted = false
4445

46+
local message_index = 1
47+
local message_buffer = { { "" } }
48+
local footer
49+
50+
local function reflog_message(index)
51+
return git.log.reflog_message(index - 2)
52+
end
53+
54+
local function commit_message()
55+
return message_buffer[message_index] or reflog_message(message_index)
56+
end
57+
58+
local function current_message(buffer)
59+
local message = buffer:get_lines(0, -1)
60+
message = util.slice(message, 1, math.max(1, #message - #footer))
61+
62+
return message
63+
end
64+
4565
self.buffer = Buffer.create {
4666
name = self.filename,
4767
filetype = filetypes[self.filename:match("[%u_]+$")] or "NeogitEditor",
@@ -64,6 +84,9 @@ function M:open(kind)
6484
string.format("# %s Close", pad_mapping("Close")),
6585
string.format("# %s Submit", pad_mapping("Submit")),
6686
string.format("# %s Abort", pad_mapping("Abort")),
87+
string.format("# %s Previous Message", pad_mapping("PrevMessage")),
88+
string.format("# %s Next Message", pad_mapping("NextMessage")),
89+
string.format("# %s Reset Message", pad_mapping("ResetMessage")),
6790
}
6891

6992
help_lines = util.filter_map(help_lines, function(line)
@@ -77,6 +100,8 @@ function M:open(kind)
77100
buffer:write()
78101
buffer:move_cursor(1)
79102

103+
footer = buffer:get_lines(1, -1)
104+
80105
-- Start insert mode if user has configured it
81106
local disable_insert = config.values.disable_insert_on_commit
82107
if
@@ -127,6 +152,31 @@ function M:open(kind)
127152
buffer:write()
128153
buffer:close(true)
129154
end,
155+
[mapping["PrevMessage"]] = function(buffer)
156+
local message = current_message(buffer)
157+
message_buffer[message_index] = message
158+
159+
message_index = message_index + 1
160+
161+
buffer:set_lines(0, #message, false, commit_message())
162+
buffer:move_cursor(1)
163+
end,
164+
[mapping["NextMessage"]] = function(buffer)
165+
local message = current_message(buffer)
166+
167+
if message_index > 1 then
168+
message_buffer[message_index] = message
169+
message_index = message_index - 1
170+
end
171+
172+
buffer:set_lines(0, #message, false, commit_message())
173+
buffer:move_cursor(1)
174+
end,
175+
[mapping["ResetMessage"]] = function(buffer)
176+
local message = current_message(buffer)
177+
buffer:set_lines(0, #message, false, reflog_message(message_index))
178+
buffer:move_cursor(1)
179+
end,
130180
},
131181
},
132182
}

lua/neogit/config.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ end
114114

115115
---@alias NeogitConfigMappingsRebaseEditor "Pick" | "Reword" | "Edit" | "Squash" | "Fixup" | "Execute" | "Drop" | "Break" | "MoveUp" | "MoveDown" | "Close" | "OpenCommit" | "Submit" | "Abort" | false | fun()
116116
---
117-
---@alias NeogitConfigMappingsCommitEditor "Close" | "Submit" | "Abort" | false | fun()
117+
---@alias NeogitConfigMappingsCommitEditor "Close" | "Submit" | "Abort" | "PrevMessage" | "ResetMessage" | "NextMessage" | false | fun()
118118

119119
---@class NeogitConfigMappings Consult the config file or documentation for values
120120
---@field finder? { [string]: NeogitConfigMappingsFinder } A dictionary that uses finder commands to set multiple keybinds
@@ -301,6 +301,9 @@ function M.get_default_values()
301301
["q"] = "Close",
302302
["<c-c><c-c>"] = "Submit",
303303
["<c-c><c-k>"] = "Abort",
304+
["<m-p>"] = "PrevMessage",
305+
["<m-n>"] = "NextMessage",
306+
["<m-r>"] = "ResetMessage",
304307
},
305308
rebase_editor = {
306309
["p"] = "Pick",

lua/neogit/lib/buffer.lua

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function Buffer:write()
8181
end
8282

8383
function Buffer:get_lines(first, last, strict)
84-
return api.nvim_buf_get_lines(self.handle, first, last, strict)
84+
return api.nvim_buf_get_lines(self.handle, first, last, strict or false)
8585
end
8686

8787
function Buffer:get_line(line)
@@ -148,13 +148,7 @@ function Buffer:set_text(first_line, last_line, first_col, last_col, lines)
148148
end
149149

150150
function Buffer:move_cursor(line)
151-
if line < 0 then
152-
self:focus()
153-
vim.cmd("norm! G")
154-
else
155-
self:focus()
156-
vim.cmd("norm! " .. line .. "G")
157-
end
151+
api.nvim_win_set_cursor(0, { line, 0 })
158152
end
159153

160154
function Buffer:close(force)

lua/neogit/lib/git/log.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,4 +535,12 @@ M.branch_info = util.memoize(function(ref, remotes)
535535
return result
536536
end)
537537

538+
function M.reflog_message(skip)
539+
return cli.log
540+
.format("%B")
541+
.max_count(1)
542+
.args("--reflog", "--no-merges", "--skip=" .. tostring(skip))
543+
.call_sync({ ignore_error = true }).stdout
544+
end
545+
538546
return M

lua/neogit/lib/util.lua

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -193,20 +193,20 @@ function M.str_min_width(str, len, sep)
193193
return str .. string.rep(sep or " ", len - length)
194194
end
195195

196-
-- function M.slice(tbl, s, e)
197-
-- local pos, new = 1, {}
198-
--
199-
-- if e == nil then
200-
-- e = #tbl
201-
-- end
202-
--
203-
-- for i = s, e do
204-
-- new[pos] = tbl[i]
205-
-- pos = pos + 1
206-
-- end
207-
--
208-
-- return new
209-
-- end
196+
function M.slice(tbl, s, e)
197+
local pos, new = 1, {}
198+
199+
if e == nil then
200+
e = #tbl
201+
end
202+
203+
for i = s, e do
204+
new[pos] = tbl[i]
205+
pos = pos + 1
206+
end
207+
208+
return new
209+
end
210210

211211
-- function M.str_count(str, target)
212212
-- local count = 0

0 commit comments

Comments
 (0)