Files
gitlab.nvim/lua/gitlab/utils/init.lua
Harrison Cramer bf37b1eae7 Rebased all commits, v0.0.1
added a new file

First major commit

Successful POST of new comment 🚀

Updated README

Updated README 📕

Added more infrastructure

Creating async job

More setup

Getting arguments from Neovim -> Lua -> Golang

Moved commands

Added getProjectInfo command

Adding make comment command

Setting up arguments for MakeComment command

Removed extraneous comments

Setup basic function for adding comments

Lint fixes

Handling bad requests correctly

Better formatting

Printing success message

Adding utility table print

Set comment from popup UI

Added mappings for closing and sending text

Moved popup into separate file

Added comment

Cleaned up code and added approve command

Initialize project information

Removed extraneous import

Don't initialize project in non-gitlab directories

Setup approve command

Set up revoke and approve commands correctly

Cleaned up redundant code

Moved get current branch command

Reorganization of the code

First attempt to add step installing Go binary

Adjusted path to binary

Added install bin check

Do Lua method

Fixed install step

Tweaked binPath + bin

Added basic readme information 📗

Removed .luarc.json file

Adding diffview command

Added string_starts function

Made base branch configurable

Added note to readme

Fixed readme

Added diffview dep to readme

Update README.md

Update README.md

Update README.md

Update README.md

Renamed files

Set up developer workflow

Updated README

Removed dev note

Refactor and moving around files

Fixed ft/after mappings

Setup read command

Added read summary command

Got rid of filetype bindings and set up commands

Set correct filetype for comment buffer

Added read() command to README

Updated review -> summary

Fixed issue with diffview buffers

Added command for getting and showing all comments (out of order)

Better error message

Adding more code to handle showing comments

Added ability to jump from comment to specific changed buffer line

Initial refactor

Added simple comment action

Fixed error message

More cleanup

Fixed bug with M.PROJECT_ID

Leaving comment refactor

Fixed comment

Cleaned up old code

Added missing exit command

Check gitlab repo status before initialization

Better help strings

Added ListDiscussions command

Added Go code

Darkened metadata, filtered out non-real discussions

Removed dummy log

Sort the discussions by most recent activity

Grab hash of current discussion

Wired up reply action in Lua code

Moved to NUI Table

Adding basic jump-to-file ability

More tweaks

Allow jump anywhere in the tree

Ability to reply directly in the buffer window

Jump to location in file

Don't jump if no refresh is set

Cleaned up mappings + other code

Get rid of gitlab CLI dependency

Fixed discussions bug

Don't initialize client on main/master branches

Moved comment into separate module

Moved lua modules into separate files

Modularized library and state

Slightly better error/exception handling

Added license file

Updated readme

Moved into todo.md file

Added todo file

Standardized naming conventions (snake_case in Lua, camelCase in Go)

Moved common popup state into utils folder

Cleaned up keymapping functions

Note on install

Changing bin path

Updated README

Chnaged from success to info

Redirect output to /dev/null on build

Checking install code

Removed print statement

Slight reorganization

Setting up delete comment

Set up confirmation modal

Passing in node ID to delete_comment

Functioning comment deletion

Added delete_comment command

Updated README

Furhter modularized discussion code

Cleaned up and refactored reply code

Update README.md

Added ability to edit comments

Updated todos

Fixed main/master base branch issue

Set up keybinding rules

Updated todo.md

Removed diffview dependency

Slight cleanup 🧹

Trying something out...

Trying something for the binary...

Trying again

Fixed install for non-lazy users

Update README.md

Update README.md

Update README.md

Update README.md

Update README.md
2023-04-21 19:11:53 -04:00

227 lines
5.7 KiB
Lua

local state = require("gitlab.state")
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()
return vim.api.nvim_call_function('line', { '.' })
end
function P(...)
local objects = {}
for i = 1, select("#", ...) do
local v = select(i, ...)
table.insert(objects, vim.inspect(v))
end
print(table.concat(objects, "\n"))
return ...
end
local function get_buffer_text(bufnr)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local text = table.concat(lines, "\n")
return text
end
local feature_branch_exists = function(base_branch)
local is_git_branch = io.popen("git rev-parse --is-inside-work-tree 2>/dev/null"):read("*a")
if is_git_branch == "true\n" then
for line in io.popen("git branch 2>/dev/null"):lines() do
line = line:gsub("%s+", "")
if line == base_branch then
return true
end
end
end
return false
end
local string_starts = function(str, start)
return str:sub(1, #start) == start
end
local press_enter = function()
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<CR>", false, true, true), "n", false)
end
local base_invalid = function()
local current_branch_raw = io.popen("git rev-parse --abbrev-ref HEAD"):read("*a")
local current_branch = string.gsub(current_branch_raw, "\n", "")
if current_branch == "main" or current_branch == "master" then
require("notify")('On ' .. current_branch .. ' branch, no MRs available', "error")
return true
end
local base = state.BASE_BRANCH
local hasBaseBranch = feature_branch_exists(base)
if not hasBaseBranch then
require("notify")('No base branch. If this is a Gitlab repository, please check your setup function!', "error")
return true
end
end
local format_date = function(date_string)
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 })
-- Format date into human-readable string without leading zeros
local formatted_date = os.date("%A, %B %e at %l:%M %p", date)
return formatted_date
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 is_gitlab_repo()
local current_dir = vim.fn.getcwd()
-- check if it contains a .git folder
local git_dir = current_dir .. "/.git"
if vim.fn.isdirectory(git_dir) == 0 then
return false
end
local git_cmd = 'git remote get-url origin'
local handle = io.popen(git_cmd)
local result = handle:read("*a")
handle:close()
-- check if the remote URL is a Gitlab URL
if string.match(result, "gitlab%.com") then
return true
else
return false
end
end
local function jump_to_file(filename, line_number)
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 })
return
end
-- If buffer is not already open, open it
vim.cmd("edit " .. filename)
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
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
require("notify")(line, "info")
end
end
local function print_error(_, line)
if line ~= nil and line ~= "" then
require("notify")(line, "error")
end
end
local function exit(popup)
popup:unmount()
end
local create_popup_state = function(title, width, height)
return {
buf_options = {
filetype = 'markdown'
},
enter = true,
focusable = true,
border = {
style = "rounded",
text = {
top = title
},
},
position = "50%",
size = {
width = width,
height = height,
},
}
end
local M = {}
M.merge_tables = 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 {})
else
result[key] = overrides[key] or value
end
end
return result
end
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.feature_branch_exists = feature_branch_exists
M.press_enter = press_enter
M.string_starts = string_starts
M.base_invalid = base_invalid
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.is_gitlab_repo = is_gitlab_repo
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.P = P
return M