Skip to content
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ neogit.setup {
item = { ">", "v" },
hunk = { "", "" },
},
-- Integrations are auto-detected, and enabled if available, but can be disabled by setting to "false"
-- Each Integration is auto-detected through plugin presence. Disabled by setting to `false`
integrations = {
-- If enabled, use telescope for menu selection rather than vim.ui.select.
-- Allows multi-select and some things that vim.ui.select doesn't.
telescope = false,
telescope = nil,

-- Neogit only provides inline diffs. If you want a more traditional way to look at diffs, you can use `sindrets/diffview.nvim`.
-- The diffview integration enables the diff popup, which is a wrapper around `sindrets/diffview.nvim`.
Expand All @@ -193,7 +193,7 @@ neogit.setup {
-- }
-- }
--
diffview = false,
diffview = nil,
},
-- Setting any section to `false` will make the section not render at all
sections = {
Expand Down
6 changes: 4 additions & 2 deletions lua/neogit/buffers/log_view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ function M:open()
end
end,
["d"] = function()
if not config.ensure_integration("diffview") then
Copy link
Member

@CKolkey CKolkey Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of a boolean param, can we do an opts table, like { required = x }? Not a huge fan of naked boolean params. Thats how you wind up with a function signature like call("string", true, false, true true) :P

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Not entirely sure about the existing name for the function either.

ensure_, at least the way I've encountered and used the name refers to a function which does a check and in some way ensures a value, such as generating one if it doesn't exist, ensures a column is present in a database etc.

This is essentially a query or check function, especially when adding a { required = true }

Perhaps query_integration(name) -> bool and the have the caller do an error or fallback, with better context, such as diffview integration needs to be enabled for "diff head"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of leaving it up to the caller. That removes the need for the { required = true } param altogether, and... wait for it... we could call it... check_integration(name). Because it's a check.. for the integration. :P

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. Often great solutions come from stepping back and looking at the larger picture, rather than adding yet another bool argument

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made the change to check and provided some more descriptive error messages that show a notification.

Just fixed a call("string", true, false, true true) situation where a big async function begun with an if statement, that either allocated a scratch buffer and did a thing, or did a thing directly. Just pried it apart into two functions, as the bools were never dynamic but always hardcoded anyway

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeahhh. Thats something I remember from.. I think Uncle Bob... early on in my career. If you are controlling a functions behaviour externally with boolean flags... congrats, you have two functions :P

return
if not config.check_integration("diffview") then
require("neogit.lib.notification").create_error(
"Diffview integration must be enabled for log diff"
)
end

local stack = self.buffer.ui:get_component_stack_under_cursor()
Expand Down
5 changes: 4 additions & 1 deletion lua/neogit/buffers/reflog_view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ function M:open()
CommitViewBuffer.new(stack[#stack].options.oid):open()
end,
["d"] = function()
if not config.ensure_integration("diffview") then
if not config.check_integration("diffview") then
require("neogit.lib.notification").create_error(
"Diffview integration must be enabled for reflog diff"
)
return
end

Expand Down
27 changes: 16 additions & 11 deletions lua/neogit/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,10 @@ M.values = {
item = { ">", "v" },
section = { ">", "v" },
},
integrations = setmetatable({}, {
__index = function(_, key)
local ok, value = pcall(require, key)
return ok and value or false
end,
}),
integrations = {
Copy link
Member Author

@ten3roberts ten3roberts Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous auto integration relies on the metatable being left intact and would break if the table was replaced. Additionally, this makes the config data only and does not muddle printing or probing of the config with runtime determined fields or obscuring what the user originally said with what we found through pcall.

This functionality is now ensured by the check_integration function

telescope = nil,
diffview = nil,
},
sections = {
untracked = {
folded = false,
Expand Down Expand Up @@ -153,13 +151,20 @@ M.values = {
},
}

function M.ensure_integration(name)
if not M.values.integrations[name] then
vim.api.nvim_err_writeln(string.format("Neogit: `%s` integration is not enabled", name))
return false
---@param name string
---@return boolean
function M.check_integration(name)
local logger = require("neogit.logger")
local enabled = M.values.integrations[name]

if enabled == nil or enabled == "auto" then
local success, _ = pcall(require, name)
logger.fmt_info("[CONFIG] Found auto integration '%s = %s'", name, success)
return success
end

return true
logger.fmt_info("[CONFIG] Found explicit integration '%s' = %s", name, enabled)
return enabled
end

return M
2 changes: 1 addition & 1 deletion lua/neogit/lib/finder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ end
---Engages finder and invokes `on_select` with the item or items, or nil if aborted
---@param on_select fun(item: any|nil)
function Finder:find(on_select)
if config.ensure_integration("telescope") then
if config.check_integration("telescope") then
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local sorters = require("telescope.sorters")
Expand Down
3 changes: 3 additions & 0 deletions lua/neogit/lib/notification.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ end

return {
create = create,
error = function(msg, delay)
return create(msg, vim.log.levels.ERROR, delay)
end,
delete_all = function()
for _, n in ipairs(notifications) do
n:delete()
Expand Down
3 changes: 2 additions & 1 deletion lua/neogit/popups/diff/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ local config = require("neogit.config")
local popup = require("neogit.lib.popup")

function M.create()
if not config.ensure_integration("diffview") then
if not config.check_integration("diffview") then
require("neogit.lib.notification").create_error("Diffview integration must be enabled for diff popup")
return
end

Expand Down
4 changes: 3 additions & 1 deletion lua/neogit/status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1030,9 +1030,11 @@ local cmd_func_map = function()
-- INTEGRATIONS --

["DiffAtFile"] = function()
if not config.ensure_integration("diffview") then
if not config.check_integration("diffview") then
require("neogit.lib.notification").error("Diffview integration is not enabled")
return
end

local dv = require("neogit.integrations.diffview")
local section, item = get_current_section_item()

Expand Down