BREAKING CHANGE: Delta Pager + Large Refactor (#43)

BREAKING CHANGE: This MR addresses an underlying issue with the original implementation in regards to detecting line numbers for comments. 

As such, this is a major breaking change. The setup function signature has changed, please review the `README.md` for the new arguments. The delta pager has also been added as a dependency: https://github.com/dandavison/delta

There will be future work to implement a native solution for parsing changes and line numbers.
This commit is contained in:
Harrison (Harry) Cramer
2023-08-27 17:26:54 -04:00
committed by GitHub
parent ed67a03f8f
commit 19468a3d2d
19 changed files with 1273 additions and 967 deletions

View File

@@ -1,29 +1,14 @@
local state = require("gitlab.state")
local M = {}
local function get_git_root()
local output = vim.fn.system('git rev-parse --show-toplevel 2>/dev/null')
if vim.v.shell_error == 0 then
return vim.fn.substitute(output, '\n', '', '')
else
return nil
end
end
local function get_relative_file_path()
local git_root = get_git_root()
if git_root ~= nil then
local current_file = vim.fn.expand('%:p')
return vim.fn.substitute(current_file, git_root .. '/', '', '')
else
return nil
end
end
local get_current_line_number = function()
M.get_current_line_number = function()
return vim.api.nvim_call_function('line', { '.' })
end
function P(...)
M.has_delta = function()
return vim.fn.executable("delta") == 1
end
M.P = function(...)
local objects = {}
for i = 1, select("#", ...) do
local v = select(i, ...)
@@ -34,21 +19,21 @@ function P(...)
return ...
end
local function get_buffer_text(bufnr)
M.get_buffer_text = function(bufnr)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local text = table.concat(lines, "\n")
return text
end
local string_starts = function(str, start)
M.string_starts = function(str, start)
return str:sub(1, #start) == start
end
local press_enter = function()
M.press_enter = function()
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<CR>", false, true, true), "n", false)
end
local format_date = function(date_string)
M.format_date = function(date_string)
local date_table = os.date("!*t")
local year, month, day, hour, min, sec = date_string:match("(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)")
local date = os.time({ year = year, month = month, day = day, hour = hour, min = min, sec = sec })
@@ -78,20 +63,11 @@ local format_date = function(date_string)
end
end
local add_comment_sign = function(line_number)
local bufnr = vim.api.nvim_get_current_buf()
vim.cmd("sign define piet text= texthl=Substitute")
vim.fn.sign_place(0, "piet", "piet", bufnr, { lnum = line_number })
end
local function jump_to_file(filename, line_number)
M.jump_to_file = function(filename, line_number)
if line_number == nil then line_number = 1 end
vim.api.nvim_command("wincmd l")
local bufnr = vim.fn.bufnr(filename)
if bufnr ~= -1 then
-- Buffer is already open, switch to it
vim.cmd("buffer " .. bufnr)
vim.api.nvim_win_set_cursor(0, { line_number, 0 })
M.jump_to_buffer(bufnr, line_number)
return
end
@@ -100,43 +76,12 @@ local function jump_to_file(filename, line_number)
vim.api.nvim_win_set_cursor(0, { line_number, 0 })
end
local function find_value_by_id(tbl, id)
for i = 1, #tbl do
if tbl[i].id == tonumber(id) then
return tbl[i]
end
end
return nil
M.jump_to_buffer = function(bufnr, line_number)
vim.cmd("buffer " .. bufnr)
vim.api.nvim_win_set_cursor(0, { line_number, 0 })
end
vim.cmd("highlight Gray guifg=#888888")
local function darken_metadata(bufnr, regex)
local num_lines = vim.api.nvim_buf_line_count(bufnr)
for i = 0, num_lines - 1 do
local line = vim.api.nvim_buf_get_lines(bufnr, i, i + 1, false)[1]
if string.match(line, regex) then
vim.api.nvim_buf_add_highlight(bufnr, -1, 'Gray', i, 0, -1)
end
end
end
local function print_success(_, line)
if line ~= nil and line ~= "" then
vim.notify(line, vim.log.levels.INFO)
end
end
local function print_error(_, line)
if line ~= nil and line ~= "" then
vim.notify(line, vim.log.levels.ERROR)
end
end
local function exit(popup)
popup:unmount()
end
local create_popup_state = function(title, width, height)
M.create_popup_state = function(title, width, height)
return {
buf_options = {
filetype = 'markdown'
@@ -158,13 +103,20 @@ local create_popup_state = function(title, width, height)
}
end
local M = {}
M.merge_tables = function(defaults, overrides)
M.merge = function(defaults, overrides)
local result = {}
for key, value in pairs(defaults) do
if type(value) == "table" then
result[key] = M.merge_tables(value, overrides[key] or {})
result[key] = M.merge(value, overrides[key] or {})
else
result[key] = overrides[key] or value
end
end
for key, value in pairs(overrides) do
if type(value) == "table" then
result[key] = M.merge(value, overrides[key] or {})
else
result[key] = overrides[key] or value
end
@@ -173,7 +125,23 @@ M.merge_tables = function(defaults, overrides)
return result
end
local read_file = function(file_path)
M.join = function(tbl, separator)
separator = separator or " "
local result = ""
for _, value in pairs(tbl) do
result = result .. tostring(value) .. separator
end
-- Remove the trailing separator
if separator ~= "" then
result = result:sub(1, - #separator - 1)
end
return result
end
M.read_file = function(file_path)
local file = io.open(file_path, "r")
if file == nil then
return nil
@@ -184,22 +152,13 @@ local read_file = function(file_path)
return file_contents
end
local split_diff_view_filename = function(filename)
local hash, path = filename:match("://%.git/(/?[0-9a-f]+)(/.*)$")
if hash and path then
path = path:gsub("%.git/", ""):gsub("^/", "")
hash = hash:gsub("^/", "")
end
return hash, path
end
local current_file_path = function()
M.current_file_path = function()
local path = debug.getinfo(1, 'S').source:sub(2)
return vim.fn.fnamemodify(path, ':p')
end
local random = math.random
local function uuid()
M.uuid = function()
local template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
return string.gsub(template, '[xy]', function(c)
local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
@@ -207,11 +166,7 @@ local function uuid()
end)
end
local attach_uuid = function(str)
return { text = str, id = uuid() }
end
local join_tables = function(table1, table2)
M.join_tables = function(table1, table2)
for _, value in ipairs(table2) do
table.insert(table1, value)
end
@@ -219,7 +174,7 @@ local join_tables = function(table1, table2)
return table1
end
local contains = function(array, search_value)
M.contains = function(array, search_value)
for _, value in ipairs(array) do
if value == search_value then
return true
@@ -228,7 +183,7 @@ local contains = function(array, search_value)
return false
end
local extract = function(t, property)
M.extract = function(t, property)
local resultTable = {}
for _, value in ipairs(t) do
if value[property] then
@@ -238,7 +193,7 @@ local extract = function(t, property)
return resultTable
end
local remove_last_chunk = function(sentence)
M.remove_last_chunk = function(sentence)
local words = {}
for word in sentence:gmatch("%S+") do
table.insert(words, word)
@@ -248,27 +203,45 @@ local remove_last_chunk = function(sentence)
return sentence_without_last
end
M.remove_last_chunk = remove_last_chunk
M.extract = extract
M.contains = contains
M.attach_uuid = attach_uuid
M.join_tables = join_tables
M.get_relative_file_path = get_relative_file_path
M.get_current_line_number = get_current_line_number
M.get_buffer_text = get_buffer_text
M.press_enter = press_enter
M.string_starts = string_starts
M.format_date = format_date
M.add_comment_sign = add_comment_sign
M.jump_to_file = jump_to_file
M.find_value_by_id = find_value_by_id
M.darken_metadata = darken_metadata
M.print_success = print_success
M.print_error = print_error
M.create_popup_state = create_popup_state
M.exit = exit
M.read_file = read_file
M.split_diff_view_filename = split_diff_view_filename
M.current_file_path = current_file_path
M.P = P
M.get_first_chunk = function(sentence, divider)
local words = {}
for word in sentence:gmatch(divider or "%S+") do
table.insert(words, word)
end
return words[1]
end
M.get_last_chunk = function(sentence, divider)
local words = {}
for word in sentence:gmatch(divider or "%S+") do
table.insert(words, word)
end
return words[#words]
end
M.trim = function(s)
return s:gsub("^%s+", ""):gsub("%s+$", "")
end
M.get_line_content = function(bufnr, start)
local current_buffer = vim.api.nvim_get_current_buf()
local lines = vim.api.nvim_buf_get_lines(
bufnr ~= nil and bufnr or current_buffer,
start - 1,
start,
false)
for _, line in ipairs(lines) do
return line
end
end
M.get_win_from_buf = function(bufnr)
for _, win in ipairs(vim.api.nvim_list_wins()) do
if vim.fn.winbufnr(win) == bufnr then
return win
end
end
end
return M