Rework configuration

- Use a jsonschema like object to process/validate/build the config
- Attempt to clean up the imports
This commit is contained in:
Lewis Russell 2020-11-19 16:04:25 +00:00
parent c845f02969
commit 58215b9f9b
4 changed files with 153 additions and 94 deletions

View File

@ -1,32 +1,30 @@
local Job = require('plenary/job')
local Path = require('plenary/path')
local CM = require('plenary/context_manager')
local AS = require('gitsigns/async')
local default_config = require('gitsigns/defaults')
local mk_repeatable = require('gitsigns/repeat').mk_repeatable
local DB = require('gitsigns/debounce')
local pln_cm = require('plenary/context_manager')
local with = pln_cm.with
local open = pln_cm.open
local gs_async = require('gitsigns/async')
local async = gs_async.async
local async0 = gs_async.async0
local await = gs_async.await
local await_main = gs_async.await_main
local gs_debounce = require('gitsigns/debounce')
local throttle_leading = gs_debounce.throttle_leading
local debounce_trailing = gs_debounce.debounce_trailing
local gs_popup = require('gitsigns/popup')
local sign_define = require('gitsigns/signs').sign_define
local apply_config = require('gitsigns/config')
local mk_repeatable = require('gitsigns/repeat').mk_repeatable
local apply_mappings = require('gitsigns/mappings')
local popup = require('gitsigns/popup')
local sign_define = require('gitsigns/signs').sign_define
local throttle_leading = DB.throttle_leading
local debounce_trailing = DB.debounce_trailing
local api = vim.api
local current_buf = api.nvim_get_current_buf
local async = AS.async
local async0 = AS.async0
local await = AS.await
local await_main = function()
return await(vim.schedule)
end
local with = CM.with
local open = CM.open
local config = {}
local sign_map = {
@ -198,18 +196,6 @@ local get_staged_txt = function(toplevel, relpath, callback)
}
end
local get_diff_algorithm = function()
local algo = 'myers'
for o in vim.gsplit(vim.o.diffopt, ',') do
if vim.startswith(o, 'algorithm:') then
algo = string.sub(o, 11)
end
end
return algo
end
local algo = get_diff_algorithm()
local run_diff = function(staged, text, callback)
local results = {}
run_job {
@ -243,15 +229,6 @@ local run_diff = function(staged, text, callback)
}
end
local function mk_status_txt(status)
local added, changed, removed = status.added, status.changed, status.removed
local status_txt = {}
if added > 0 then table.insert(status_txt, '+'..added ) end
if changed > 0 then table.insert(status_txt, '~'..changed) end
if removed > 0 then table.insert(status_txt, '-'..removed) end
return table.concat(status_txt, ' ')
end
local cache = {}
-- <bufnr> = {
-- file: string - Full filename
@ -761,17 +738,7 @@ local attach = throttle_leading(100, async('attach', function()
end))
local function setup(cfg)
cfg = cfg or {}
if cfg.keymaps then
default_config.keymaps = {}
end
config = vim.tbl_deep_extend("keep", cfg or {}, default_config)
if type(config.status_formatter) ~= 'function' then
config.status_formatter = mk_status_txt
end
config = apply_config(cfg)
-- Define signs
for t, sign_name in pairs(sign_map) do
@ -794,7 +761,7 @@ function preview_hunk()
return
end
local winid, bufnr = popup.create(hunk.lines, { relative = 'cursor' })
local winid, bufnr = gs_popup.create(hunk.lines, { relative = 'cursor' })
vim.fn.nvim_buf_set_option(bufnr, 'filetype', 'diff')
vim.fn.nvim_win_set_option(winid, 'number', false)

View File

@ -56,4 +56,8 @@ M.await = function(defer, ...)
return co.yield(M.awrap(defer)(...))
end
M.await_main = function()
return M.await(vim.schedule)
end
return M

128
lua/gitsigns/config.lua Normal file
View File

@ -0,0 +1,128 @@
local schema = {
signs = {
type = 'table',
deep_extend = true,
default = {
add = {hl = 'DiffAdd' , text = ''},
change = {hl = 'DiffChange', text = ''},
delete = {hl = 'DiffDelete', text = '_'},
topdelete = {hl = 'DiffDelete', text = ''},
changedelete = {hl = 'DiffChange', text = '~'},
}
},
watch_index = {
type = 'table',
default = {
interval = 1000
}
},
debug_mode = {
type = 'boolean',
default = false
},
sign_priority = {
type = 'number',
default = 6
},
diff_algorithm = {
type = 'string',
-- Get algorithm from 'diffopt'
default = function()
local algo = 'myers'
for o in vim.gsplit(vim.o.diffopt, ',') do
if vim.startswith(o, 'algorithm:') then
algo = string.sub(o, 11)
end
end
return algo
end
},
keymaps = {
type = 'table',
default = {
-- Default keymap options
noremap = true,
buffer = true,
['n ]c'] = { expr = true, "&diff ? ']c' : '<cmd>lua require\"gitsigns\".next_hunk()<CR>'"},
['n [c'] = { expr = true, "&diff ? '[c' : '<cmd>lua require\"gitsigns\".prev_hunk()<CR>'"},
['n <leader>hs'] = '<cmd>lua require"gitsigns".stage_hunk()<CR>',
['n <leader>hu'] = '<cmd>lua require"gitsigns".undo_stage_hunk()<CR>',
['n <leader>hr'] = '<cmd>lua require"gitsigns".reset_hunk()<CR>',
['n <leader>hp'] = '<cmd>lua require"gitsigns".preview_hunk()<CR>',
}
},
status_formatter = {
type = 'function',
default = function(status)
local added, changed, removed = status.added, status.changed, status.removed
local status_txt = {}
if added > 0 then table.insert(status_txt, '+'..added ) end
if changed > 0 then table.insert(status_txt, '~'..changed) end
if removed > 0 then table.insert(status_txt, '-'..removed) end
return table.concat(status_txt, ' ')
end
},
count_chars = {
type = 'table',
default = {
[1] = '1', -- '₁',
[2] = '2', -- '₂',
[3] = '3', -- '₃',
[4] = '4', -- '₄',
[5] = '5', -- '₅',
[6] = '6', -- '₆',
[7] = '7', -- '₇',
[8] = '8', -- '₈',
[9] = '9', -- '₉',
['+'] = '>', -- '₊',
}
}
}
local function validate_config(config)
for k, v in pairs(config) do
if schema[k] == nil then
print(("gitsigns: Ignoring invalid configuration field '%s'"):format(k))
else
vim.validate {
[k] = { v, schema[k].type };
}
end
end
end
return function(user_config)
user_config = user_config or {}
validate_config(user_config)
local config = {}
for k, v in pairs(schema) do
if user_config[k] ~= nil then
if v.deep_extend then
config[k] = vim.tbl_deep_extend('force', v.default, user_config[k])
else
config[k] = user_config[k]
end
else
if type(v.default) == 'function' and v.type ~= 'function' then
config[k] = v.default()
else
config[k] = v.default
end
end
end
return config
end

View File

@ -1,40 +0,0 @@
return {
signs = {
add = {hl = 'DiffAdd' , text = ''},
change = {hl = 'DiffChange', text = ''},
delete = {hl = 'DiffDelete', text = '_'},
topdelete = {hl = 'DiffDelete', text = ''},
changedelete = {hl = 'DiffChange', text = '~'},
},
count_chars = {
[1] = '1', -- '₁',
[2] = '2', -- '₂',
[3] = '3', -- '₃',
[4] = '4', -- '₄',
[5] = '5', -- '₅',
[6] = '6', -- '₆',
[7] = '7', -- '₇',
[8] = '8', -- '₈',
[9] = '9', -- '₉',
['+'] = '>', -- '₊',
},
keymaps = {
-- Default keymap options
noremap = true,
buffer = true,
['n ]c'] = { expr = true, "&diff ? ']c' : '<cmd>lua require\"gitsigns\".next_hunk()<CR>'"},
['n [c'] = { expr = true, "&diff ? '[c' : '<cmd>lua require\"gitsigns\".prev_hunk()<CR>'"},
['n <leader>hs'] = '<cmd>lua require"gitsigns".stage_hunk()<CR>',
['n <leader>hu'] = '<cmd>lua require"gitsigns".undo_stage_hunk()<CR>',
['n <leader>hr'] = '<cmd>lua require"gitsigns".reset_hunk()<CR>',
['n <leader>hp'] = '<cmd>lua require"gitsigns".preview_hunk()<CR>',
},
watch_index = {
interval = 1000
},
debug_mode = false,
sign_priority = 6,
status_formatter = nil, -- Use default
}