mirror of
https://github.com/LazyVim/LazyVim.git
synced 2026-07-25 14:01:06 +00:00
feat(format): removes hard dependency on gitsigns for format changes feature and add support for mini-diff
This commit is contained in:
parent
d5f5b6e113
commit
8506618955
6 changed files with 116 additions and 16 deletions
|
|
@ -14,6 +14,12 @@ vim.g.formatmode = "file"
|
||||||
--- for these filetypes and fallback to file
|
--- for these filetypes and fallback to file
|
||||||
vim.g.format_range_exclude_ft = { "lua" }
|
vim.g.format_range_exclude_ft = { "lua" }
|
||||||
|
|
||||||
|
-- LazyVim changes provider to use.
|
||||||
|
-- Can be one of: gitsigns, mini-diff
|
||||||
|
-- Leave it to "auto" to automatically use the provider
|
||||||
|
-- enabled with `:LazyExtras`
|
||||||
|
vim.g.lazyvim_changes_provider = "auto"
|
||||||
|
|
||||||
-- Snacks animations
|
-- Snacks animations
|
||||||
-- Set to `false` to globally disable all snacks animations
|
-- Set to `false` to globally disable all snacks animations
|
||||||
vim.g.snacks_animate = true
|
vim.g.snacks_animate = true
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,24 @@
|
||||||
|
---@type LazyChangeProvider
|
||||||
|
local gitsigns_provider = {
|
||||||
|
name = "gitsigns",
|
||||||
|
changes = function(buf)
|
||||||
|
buf = buf or vim.api.get_current_buf(buf)
|
||||||
|
local hunks = require("gitsigns").get_hunks(buf)
|
||||||
|
if hunks == nil then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
hunks = vim.tbl_filter(function(h)
|
||||||
|
return h and h.type ~= "delete"
|
||||||
|
end, hunks)
|
||||||
|
return vim.tbl_map(function(h)
|
||||||
|
local start = h.added.start
|
||||||
|
local last = start + h.added.count
|
||||||
|
local last_hunk_line = vim.api.nvim_buf_get_lines(0, last - 2, last - 1, true)[1]
|
||||||
|
return { start = { start, 0 }, ["end"] = { last - 1, last_hunk_line:len() } }
|
||||||
|
end, hunks)
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
||||||
-- search/replace in multiple files
|
-- search/replace in multiple files
|
||||||
|
|
@ -123,6 +144,9 @@ return {
|
||||||
{
|
{
|
||||||
"lewis6991/gitsigns.nvim",
|
"lewis6991/gitsigns.nvim",
|
||||||
event = "LazyFile",
|
event = "LazyFile",
|
||||||
|
init = function()
|
||||||
|
LazyVim.changes.register(gitsigns_provider)
|
||||||
|
end,
|
||||||
opts = {
|
opts = {
|
||||||
signs = {
|
signs = {
|
||||||
add = { text = "▎" },
|
add = { text = "▎" },
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,25 @@
|
||||||
|
---@type LazyChangeProvider
|
||||||
|
local mini_diff_provider = {
|
||||||
|
name = "mini-diff",
|
||||||
|
changes = function(buf)
|
||||||
|
buf = buf or vim.api.get_current_buf(buf)
|
||||||
|
local data = MiniDiff.get_buf_data(buf)
|
||||||
|
if not data or not data.hunks then
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
local hunks = data.hunks
|
||||||
|
hunks = vim.tbl_filter(function(h)
|
||||||
|
return h and h.type ~= "delete"
|
||||||
|
end, hunks)
|
||||||
|
return vim.tbl_map(function(h)
|
||||||
|
local start = h.buf_start
|
||||||
|
local last = start + h.buf_count
|
||||||
|
local last_hunk_line = vim.api.nvim_buf_get_lines(0, last - 2, last - 1, true)[1]
|
||||||
|
return { start = { start, 0 }, ["end"] = { last - 1, last_hunk_line:len() } }
|
||||||
|
end, hunks)
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
-- disable gitsigns.nvim
|
-- disable gitsigns.nvim
|
||||||
{
|
{
|
||||||
|
|
@ -9,6 +31,9 @@ return {
|
||||||
{
|
{
|
||||||
"echasnovski/mini.diff",
|
"echasnovski/mini.diff",
|
||||||
event = "VeryLazy",
|
event = "VeryLazy",
|
||||||
|
init = function()
|
||||||
|
LazyVim.changes.register(mini_diff_provider)
|
||||||
|
end,
|
||||||
keys = {
|
keys = {
|
||||||
{
|
{
|
||||||
"<leader>go",
|
"<leader>go",
|
||||||
|
|
|
||||||
56
lua/lazyvim/util/changes.lua
Normal file
56
lua/lazyvim/util/changes.lua
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
---@class lazyvim.util.changes
|
||||||
|
---@overload fun(buf:number):LazyChange[]
|
||||||
|
local M = setmetatable({}, {
|
||||||
|
__call = function(m, ...)
|
||||||
|
if m.provider and m.provider.changes then
|
||||||
|
return m.provider.changes(...)
|
||||||
|
end
|
||||||
|
LazyVim.warn("`LazyVim.changes`: provider is not set")
|
||||||
|
return {}
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
|
---@class LazyChange
|
||||||
|
---@field start number
|
||||||
|
---@field end number
|
||||||
|
|
||||||
|
---@class LazyChangeProvider
|
||||||
|
---@field name string
|
||||||
|
---@field changes fun(buf?:number):LazyChange[]?
|
||||||
|
|
||||||
|
---@type LazyChangeProvider?
|
||||||
|
M.provider = nil
|
||||||
|
|
||||||
|
---@param provider LazyChangeProvider
|
||||||
|
function M.register(provider)
|
||||||
|
-- this only happens when using :LazyExtras
|
||||||
|
-- so allow to get the full spec
|
||||||
|
if vim.v.vim_did_enter == 1 then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
if M.provider and M.provider.name ~= M.want() then
|
||||||
|
M.provider = nil
|
||||||
|
end
|
||||||
|
if M.provider and M.provider.name ~= provider.name then
|
||||||
|
LazyVim.warn(
|
||||||
|
"`LazyVim.changes`: provider already set to `"
|
||||||
|
.. M.provider.name
|
||||||
|
.. "`,\nignoring new provider `"
|
||||||
|
.. provider.name
|
||||||
|
.. "`"
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
M.provider = provider
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
---@return "gitsigns" | "mini-diff"
|
||||||
|
function M.want()
|
||||||
|
if not vim.g.lazyvim_changes_provider or vim.g.lazyvim_changes_provider == "auto" then
|
||||||
|
return LazyVim.has_extra("editor.mini-diff") and "mini-diff" or "gitsigns"
|
||||||
|
end
|
||||||
|
return vim.g.lazyvim_changes_provider
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
|
|
@ -158,22 +158,10 @@ end
|
||||||
---@param formatter LazyFormatter
|
---@param formatter LazyFormatter
|
||||||
---@param buf number
|
---@param buf number
|
||||||
function M._format_changes(formatter, buf)
|
function M._format_changes(formatter, buf)
|
||||||
if not LazyVim.has("gitsigns.nvim") then
|
local changes = LazyVim.changes(buf)
|
||||||
return
|
for i = #changes, 1, -1 do
|
||||||
end
|
local change = changes[i]
|
||||||
local hunks = require("gitsigns").get_hunks(buf)
|
formatter.format(buf, change)
|
||||||
if hunks == nil then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
for i = #hunks, 1, -1 do
|
|
||||||
local hunk = hunks[i]
|
|
||||||
if hunk ~= nil and hunk.type ~= "delete" then
|
|
||||||
local start = hunk.added.start
|
|
||||||
local last = start + hunk.added.count
|
|
||||||
local last_hunk_line = vim.api.nvim_buf_get_lines(0, last - 2, last - 1, true)[1]
|
|
||||||
local range = { start = { start, 0 }, ["end"] = { last - 1, last_hunk_line:len() } }
|
|
||||||
formatter.format(buf, range)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ local LazyUtil = require("lazy.core.util")
|
||||||
---@field inject lazyvim.util.inject
|
---@field inject lazyvim.util.inject
|
||||||
---@field news lazyvim.util.news
|
---@field news lazyvim.util.news
|
||||||
---@field json lazyvim.util.json
|
---@field json lazyvim.util.json
|
||||||
|
---@field changes lazyvim.util.changes
|
||||||
---@field lualine lazyvim.util.lualine
|
---@field lualine lazyvim.util.lualine
|
||||||
---@field mini lazyvim.util.mini
|
---@field mini lazyvim.util.mini
|
||||||
---@field pick lazyvim.util.pick
|
---@field pick lazyvim.util.pick
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue