Skip to content
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

Use Native Folding (nvim 0.10) #959

Merged
merged 21 commits into from
Nov 28, 2023
Merged
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
15 changes: 0 additions & 15 deletions ftplugin/NeogitGitCommandHistory.vim

This file was deleted.

2 changes: 2 additions & 0 deletions lua/neogit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function M.setup(opts)
local hl = require("neogit.lib.hl")
local state = require("neogit.lib.state")
local logger = require("neogit.logger")
local folds = require("neogit.lib.folds")

if did_setup then
logger.debug("Already did setup!")
Expand Down Expand Up @@ -40,6 +41,7 @@ function M.setup(opts)
signs.setup()
state.setup()
autocmds.setup()
folds.setup()
end

---@alias Popup "cherry_pick" | "commit" | "branch" | "diff" | "fetch" | "log" | "merge" | "remote" | "pull" | "push" | "rebase" | "revert" | "reset" | "stash"
Expand Down
21 changes: 4 additions & 17 deletions lua/neogit/buffers/commit_view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,30 +173,17 @@ function M:open()
["q"] = function()
self:close()
end,
["<F10>"] = function()
self.buffer.ui:print_layout_tree { collapse_hidden_components = true }
end,
["<tab>"] = function()
local c = self.buffer.ui:get_component_under_cursor()

if c then
local c = c.parent
if c.options.tag == "HunkContent" then
c = c.parent
end
if vim.tbl_contains({ "Diff", "Hunk" }, c.options.tag) then
local first, _ = c:row_range_abs()
c.children[2]:toggle_hidden()
self.buffer.ui:update()
api.nvim_win_set_cursor(0, { first, 0 })
end
end
pcall(vim.cmd, "normal! za")
end,
},
},
render = function()
return ui.CommitView(self.commit_info, self.commit_overview, self.commit_signature)
end,
after = function()
vim.cmd("normal! zR")
end,
}
end

Expand Down
13 changes: 9 additions & 4 deletions lua/neogit/buffers/commit_view/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,21 @@ function M.CommitHeader(info)
}
end

function M.CommitView(info, overview, signature_block)
local hide_signature = vim.tbl_isempty(signature_block)
function M.SignatureBlock(signature_block)
if vim.tbl_isempty(signature_block or {}) then
return text("")
end

return col(util.merge(map(signature_block, text), { text("") }), { tag = "Signature" })
end

function M.CommitView(info, overview, signature_block)
return {
M.CommitHeader(info),
text(""),
col(map(info.description, text), { sign = "NeogitCommitViewDescription", tag = "Description" }),
text(""),
col(map(signature_block or {}, text), { tag = "Signature", hidden = hide_signature }),
text("", { hidden = hide_signature }),
M.SignatureBlock(signature_block),
text(overview.summary),
col(map(overview.files, M.OverviewFile), { tag = "OverviewFileList" }),
text(""),
Expand Down
24 changes: 8 additions & 16 deletions lua/neogit/buffers/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ M.Diff = Component.new(function(diff)
}
end)

return col.tag("Diff") {
return col.tag("Diff")({
text(string.format("%s %s", diff.kind, diff.file), { sign = "NeogitDiffHeader" }),
col.tag("DiffContent") {
col.tag("DiffInfo")(map(diff.info, text)),
col.tag("HunkList")(map(hunk_props, M.Hunk)),
},
}
}, { foldable = true })
end)

local HunkLine = Component.new(function(line)
Expand All @@ -55,10 +55,10 @@ local HunkLine = Component.new(function(line)
end)

M.Hunk = Component.new(function(props)
return col.tag("Hunk") {
return col.tag("Hunk")({
text.sign("NeogitHunkHeader")(props.header),
col.tag("HunkContent")(map(props.content, HunkLine)),
}
}, { foldable = true })
end)

M.List = Component.new(function(props)
Expand Down Expand Up @@ -149,7 +149,7 @@ M.CommitEntry = Component.new(function(commit, args)

local details
if args.details then
details = col.hidden(true).padding_left(8) {
details = col.padding_left(8) {
row(util.merge(graph, {
text(" "),
text("Author: ", { highlight = "NeogitGraphAuthor" }),
Expand Down Expand Up @@ -196,7 +196,7 @@ M.CommitEntry = Component.new(function(commit, args)
}
end

return col({
return col.tag("commit")({
row(
util.merge({
text(commit.oid:sub(1, 7), {
Expand All @@ -216,11 +216,11 @@ M.CommitEntry = Component.new(function(commit, args)
}
),
details,
}, { oid = commit.oid })
}, { oid = commit.oid, foldable = args.details == true })
end)

M.CommitGraph = Component.new(function(commit, _)
return col.padding_left(8) { row(build_graph(commit.graph)) }
return col.tag("graph").padding_left(8) { row(build_graph(commit.graph)) }
end)

M.Grid = Component.new(function(props)
Expand Down Expand Up @@ -260,14 +260,6 @@ M.Grid = Component.new(function(props)
for i = 1, #props.items do
local children = {}

-- TODO: seems to be a leftover from when the grid was column major
-- if i ~= 1 then
-- children = map(range(props.gap), function()
-- return text("")
-- end)
-- end

-- current row
local r = props.items[i]

for j = 1, #r do
Expand Down
33 changes: 24 additions & 9 deletions lua/neogit/buffers/git_command_history.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,30 @@ function M:show()
["<esc>"] = function()
self:close()
end,
["<tab>"] = function()
local stack = self.buffer.ui:get_component_stack_under_cursor()
local c = stack[#stack]
["<c-k>"] = function()
vim.cmd("normal! zc")

vim.cmd("normal! k")
while vim.fn.foldlevel(".") == 0 do
vim.cmd("normal! k")
end

vim.cmd("normal! zo")
vim.cmd("normal! zz")
end,
["<c-j>"] = function()
vim.cmd("normal! zc")

if c then
c.children[2]:toggle_hidden()
self.buffer.ui:update()
vim.cmd("normal! j")
while vim.fn.foldlevel(".") == 0 do
vim.cmd("normal! j")
end

vim.cmd("normal! zo")
vim.cmd("normal! zz")
end,
["<tab>"] = function()
pcall(vim.cmd, "normal! za")
end,
},
},
Expand All @@ -77,7 +93,7 @@ function M:show()

local spacing = string.rep(" ", win_width - #code - #command - #time - #stdio - 6)

return col {
return col({
row {
text.highlight(highlight_code)(code),
text(" "),
Expand All @@ -88,10 +104,9 @@ function M:show()
text.highlight("NeogitCommandTime")(stdio),
},
col
.hidden(true)
.padding_left(" | ")
.highlight("NeogitCommandText")(map(is_err and item.stderr or item.stdout, text)),
}
}, { foldable = true })
end)
end,
}
Expand Down
59 changes: 24 additions & 35 deletions lua/neogit/buffers/log_view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,49 +112,38 @@ function M:open()
["<enter>"] = function()
CommitViewBuffer.new(self.buffer.ui:get_commit_under_cursor()):open()
end,
["<c-k>"] = function(buffer)
local stack = self.buffer.ui:get_component_stack_under_cursor()
local c = stack[#stack]
c.children[2].options.hidden = true

local t_idx = math.max(c.index - 1, 1)
local target = c.parent.children[t_idx]
while not target.children[2] do
t_idx = t_idx - 1
target = c.parent.children[t_idx]
end
["<c-k>"] = function()
pcall(vim.cmd, "normal! zc")

target.children[2].options.hidden = false
vim.cmd("normal! k")
for _ = vim.fn.line("."), 0, -1 do
if vim.fn.foldlevel(".") > 0 then
break
end

buffer.ui:update()
self.buffer:move_cursor(target.position.row_start)
end,
["<c-j>"] = function(buffer)
local stack = self.buffer.ui:get_component_stack_under_cursor()
local c = stack[#stack]
c.children[2].options.hidden = true

local t_idx = math.min(c.index + 1, #c.parent.children)
local target = c.parent.children[t_idx]
while not target.children[2] do
t_idx = t_idx + 1
target = c.parent.children[t_idx]
vim.cmd("normal! k")
end

target.children[2].options.hidden = false

buffer.ui:update()
buffer:move_cursor(target.position.row_start)
pcall(vim.cmd, "normal! zo")
vim.cmd("normal! zz")
end,
["<tab>"] = function()
local stack = self.buffer.ui:get_component_stack_under_cursor()
local c = stack[#stack]
["<c-j>"] = function()
pcall(vim.cmd, "normal! zc")

vim.cmd("normal! j")
for _ = vim.fn.line("."), vim.fn.line("$"), 1 do
if vim.fn.foldlevel(".") > 0 then
break
end

if c.children[2] then
c.children[2]:toggle_hidden()
self.buffer.ui:update()
vim.cmd("normal! j")
end

pcall(vim.cmd, "normal! zo")
vim.cmd("normal! zz")
end,
["<tab>"] = function()
pcall(vim.cmd, "normal! za")
end,
["d"] = function()
if not config.check_integration("diffview") then
Expand Down
28 changes: 21 additions & 7 deletions lua/neogit/lib/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function Buffer:new(handle)
hl_buffer = {},
sign_buffer = {},
ext_buffer = {},
fold_buffer = {},
}

this.ui = Ui.new(this)
Expand Down Expand Up @@ -106,6 +107,10 @@ function Buffer:buffered_set_extmark(...)
table.insert(self.ext_buffer, { ... })
end

function Buffer:buffered_create_fold(...)
table.insert(self.fold_buffer, { ... })
end

function Buffer:resize(length)
api.nvim_buf_set_lines(self.handle, length, -1, false, {})
end
Expand All @@ -130,6 +135,11 @@ function Buffer:flush_buffers()
self:set_extmark(unpack(ext))
end
self.ext_buffer = {}

for _, fold in ipairs(self.fold_buffer) do
self:create_fold(unpack(fold))
end
self.fold_buffer = {}
end

function Buffer:set_text(first_line, last_line, first_col, last_col, lines)
Expand Down Expand Up @@ -270,7 +280,7 @@ function Buffer:put(lines, after, follow)
end

function Buffer:create_fold(first, last)
vim.cmd(string.format(self.handle .. "bufdo %d,%dfold", first, last))
vim.cmd(string.format("%d,%dfold", first, last))
end

function Buffer:unlock()
Expand All @@ -290,10 +300,6 @@ function Buffer:set_name(name)
api.nvim_buf_set_name(self.handle, name)
end

function Buffer:set_foldlevel(level)
vim.cmd("setlocal foldlevel=" .. level)
end

function Buffer:replace_content_with(lines)
api.nvim_buf_set_lines(self.handle, 0, -1, false, lines)
end
Expand Down Expand Up @@ -382,8 +388,8 @@ function Buffer:set_extmark(...)
return api.nvim_buf_set_extmark(self.handle, ...)
end

function Buffer:get_extmark(ns, id)
return api.nvim_buf_get_extmark_by_id(self.handle, ns, id, { details = true })
function Buffer:get_extmark(id, ns)
return api.nvim_buf_get_extmark_by_id(self.handle, ns or self.namespace, id, { details = true })
end

function Buffer:del_extmark(ns, id)
Expand Down Expand Up @@ -438,6 +444,13 @@ function Buffer.create(config)
buffer:set_option("buftype", config.buftype or "nofile")
buffer:set_option("swapfile", false)

if win then
vim.api.nvim_set_option_value("foldenable", true, { win = win })
vim.api.nvim_set_option_value("foldlevel", 99, { win = win })
vim.api.nvim_set_option_value("foldminlines", 0, { win = win })
vim.api.nvim_set_option_value("foldtext", "v:lua.NeogitBufferFoldText()", { win = win })
end

if config.filetype then
buffer:set_filetype(config.filetype)
end
Expand Down Expand Up @@ -493,6 +506,7 @@ function Buffer.create(config)
buffer:call(function()
-- Set fold styling for Neogit windows while preserving user styling
vim.opt_local.winhl:append("Folded:NeogitFold")
vim.opt_local.fillchars:append("fold: ")

-- Set signcolumn unless disabled by user settings
if not config.disable_signs then
Expand Down
Loading
Loading