Skip to content

Commit

Permalink
refactor: minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Jul 21, 2024
1 parent 3fabffb commit 02cff46
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 37 deletions.
22 changes: 11 additions & 11 deletions lua/tinygit/commands/commit-and-amend.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ local u = require("tinygit.shared.utils")
local push = require("tinygit.commands.push-pull").push
local updateStatusline = require("tinygit.statusline").updateAllComponents
local highlight = require("tinygit.shared.highlights")
local fn = vim.fn

--------------------------------------------------------------------------------

M.state = {
Expand All @@ -16,6 +14,8 @@ M.state = {
issueNotif = nil,
}

--------------------------------------------------------------------------------

---@nodiscard
---@return boolean
local function hasNoStagedChanges()
Expand Down Expand Up @@ -152,7 +152,7 @@ local function setupInputField(commitType)
vim.opt_local.formatoptions:remove("t") -- prevent auto-wrapping due "gitcommit" filetype

-- overlength
fn.matchadd("ErrorMsg", ([[.\{%s}\zs.*]]):format(commitMaxLen))
vim.fn.matchadd("ErrorMsg", ([[.\{%s}\zs.*]]):format(commitMaxLen))

-- treesitter parser makes first line bold, but since we have only one
-- line, we do not need to bold everything in it
Expand Down Expand Up @@ -250,8 +250,8 @@ local function postCommitNotif(title, stagedAllChanges, commitMsg, extraInfo)
-- commit msg custom highlights
vim.api.nvim_buf_call(bufnr, function()
highlight.commitMsg()
if stagedAllChanges then fn.matchadd("Comment", stageAllText) end
if extraInfo then fn.matchadd("Comment", extraInfo) end
if stagedAllChanges then vim.fn.matchadd("Comment", stageAllText) end
if extraInfo then vim.fn.matchadd("Comment", extraInfo) end
end)
end,
})
Expand Down Expand Up @@ -312,15 +312,15 @@ local function showCommitPreview()
on_open = function(win)
local bufnr = vim.api.nvim_win_get_buf(win)
vim.api.nvim_buf_call(bufnr, function()
fn.matchadd("diffAdded", [[ \zs+\+]]) -- color the plus/minus like in the terminal
fn.matchadd("diffRemoved", [[-\+\ze\s*$]])
fn.matchadd("Keyword", [[(new.*)]])
fn.matchadd("Keyword", [[(gone.*)]])
fn.matchadd("Comment", "")
vim.fn.matchadd("diffAdded", [[ \zs+\+]]) -- color the plus/minus like in the terminal
vim.fn.matchadd("diffRemoved", [[-\+\ze\s*$]])
vim.fn.matchadd("Keyword", [[(new.*)]])
vim.fn.matchadd("Keyword", [[(gone.*)]])
vim.fn.matchadd("Comment", "")

if not willStageAllChanges then
-- `\_.` matches any char, including newline
fn.matchadd("Comment", specialWhitespace .. [[\_.*]])
vim.fn.matchadd("Comment", specialWhitespace .. [[\_.*]])
end
end)
end,
Expand Down
36 changes: 16 additions & 20 deletions lua/tinygit/commands/history.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
local M = {}
local fn = vim.fn
local a = vim.api
local basename = vim.fs.basename

local highlight = require("tinygit.shared.highlights")
local u = require("tinygit.shared.utils")
local backdrop = require("tinygit.shared.backdrop")
local highlight = require("tinygit.shared.highlights")
local selectCommit = require("tinygit.shared.select-commit")
local u = require("tinygit.shared.utils")
--------------------------------------------------------------------------------

---@class (exact) historyState
Expand All @@ -32,7 +28,7 @@ local state = {
---@param level? "info"|"trace"|"debug"|"warn"|"error"
---@param extraOpts? { on_open?: function, timeout?: boolean|number, animate?: boolean }
local function notify(msg, level, extraOpts)
---@diagnostic disable-next-line: param-type-mismatch -- wrong diagnostic
---@diagnostic disable-next-line: param-type-mismatch diagnostic is wrong
u.notify(msg, level, "Git History", extraOpts)
end

Expand Down Expand Up @@ -151,8 +147,8 @@ local function showDiff(commitIdx)
local absWidth = math.floor(relWidth * vimWidth)

-- BUFFER
local bufnr = a.nvim_create_buf(false, true)
a.nvim_buf_set_name(bufnr, hash .. " " .. nameAtCommit)
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(bufnr, hash .. " " .. nameAtCommit)
vim.bo[bufnr].buftype = "nofile"
setDiffBuffer(bufnr, diffLines, state.ft, absWidth)

Expand All @@ -172,7 +168,7 @@ local function showDiff(commitIdx)

-- CREATE WINDOW
local historyZindex = 40 -- below nvim-notify, which has 50
local winnr = a.nvim_open_win(bufnr, true, {
local winnr = vim.api.nvim_open_win(bufnr, true, {
-- center of the editor
relative = "editor",
width = absWidth,
Expand All @@ -198,7 +194,7 @@ local function showDiff(commitIdx)
vim.o.ignorecase = true
vim.o.smartcase = false

fn.matchadd("Search", query) -- highlight, CAVEAT: is case-sensitive
vim.fn.matchadd("Search", query) -- highlight, CAVEAT: is case-sensitive
vim.fn.setreg("/", query) -- so `n` searches directly
pcall(vim.cmd.normal, { "n", bang = true }) -- move to first match
-- (pcall to prevent error when query cannot found, due to non-equivalent
Expand All @@ -210,8 +206,8 @@ local function showDiff(commitIdx)
local keymap = vim.keymap.set
local opts = { buffer = bufnr, nowait = true }
local function closePopup()
if a.nvim_win_is_valid(winnr) then a.nvim_win_close(winnr, true) end
if a.nvim_buf_is_valid(bufnr) then a.nvim_buf_delete(bufnr, { force = true }) end
if vim.api.nvim_win_is_valid(winnr) then vim.api.nvim_win_close(winnr, true) end
if vim.api.nvim_buf_is_valid(bufnr) then vim.api.nvim_buf_delete(bufnr, { force = true }) end
vim.o.ignorecase = ignoreCaseBefore
vim.o.smartcase = smartCaseBefore
end
Expand Down Expand Up @@ -273,9 +269,9 @@ local function selectFromCommits(commitList)
local oneCommitPer3Lines = vim.split(commitList, "\n")
for i = 1, #oneCommitPer3Lines, 3 do
local commitLine = oneCommitPer3Lines[i]
local nameAtCommit = basename(oneCommitPer3Lines[i + 2])
local nameAtCommit = vim.fs.basename(oneCommitPer3Lines[i + 2])
-- append name at commit only when it is not the same name as in the present
if basename(state.absPath) ~= nameAtCommit then
if vim.fs.basename(state.absPath) ~= nameAtCommit then
-- tab-separated for consistently with `--format` output
commitLine = commitLine .. "\t" .. nameAtCommit
end
Expand All @@ -296,13 +292,13 @@ local function selectFromCommits(commitList)

-- select commit
local autocmdId = selectCommit.setupAppearance()
local searchMode = state.query == "" and basename(state.absPath) or state.query
local searchMode = state.query == "" and vim.fs.basename(state.absPath) or state.query
vim.ui.select(commits, {
prompt = ('󰊢 Commits that changed "%s"'):format(searchMode),
format_item = selectCommit.selectorFormatter,
kind = "tinygit.history",
}, function(_, commitIdx)
a.nvim_del_autocmd(autocmdId)
vim.api.nvim_del_autocmd(autocmdId)
if commitIdx then showDiff(commitIdx) end
end)
end
Expand All @@ -311,7 +307,7 @@ end

function M.searchFileHistory()
if u.notInGitRepo() or repoIsShallow() then return end
state.absPath = a.nvim_buf_get_name(0)
state.absPath = vim.api.nvim_buf_get_name(0)
state.ft = vim.bo.filetype
state.type = "file"

Expand Down Expand Up @@ -385,7 +381,7 @@ function M.functionHistory()
return
end

state.absPath = a.nvim_buf_get_name(0)
state.absPath = vim.api.nvim_buf_get_name(0)
state.ft = vim.bo.filetype
state.type = "function"

Expand Down Expand Up @@ -484,7 +480,7 @@ function M.lineHistory()
state.query = "L" .. startOfVisual .. (onlyOneLine and "" or "-L" .. endOfVisual)
end

state.absPath = a.nvim_buf_get_name(0)
state.absPath = vim.api.nvim_buf_get_name(0)
state.ft = vim.bo.filetype
state.lnum = lnum
state.offset = offset
Expand Down
5 changes: 1 addition & 4 deletions lua/tinygit/commands/staging.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ local u = require("tinygit.shared.utils")
---@param msg string
---@param level? "info"|"trace"|"debug"|"warn"|"error"
---@param extraOpts? { on_open?: function, timeout?: boolean|number, animate?: boolean }
local function notify(msg, level, extraOpts)
---@diagnostic disable-next-line: param-type-mismatch -- wrong diagnostic
u.notify(msg, level, "Staging", extraOpts)
end
local function notify(msg, level, extraOpts) u.notify(msg, level, "Staging", extraOpts) end

--------------------------------------------------------------------------------

Expand Down
2 changes: 2 additions & 0 deletions lua/tinygit/shared/diff.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ local FILEMODES = {
binary = 4,
}

--------------------------------------------------------------------------------

-- remove diff header, if the input has it. checking for `@@`, as number of
-- header lines can vary (e.g., diff to new file are 5 lines, not 4)
---@param diffLines string[]
Expand Down
5 changes: 3 additions & 2 deletions lua/tinygit/shared/utils.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
local M = {}
--------------------------------------------------------------------------------

---send notification
---@param body string
---@param level? "info"|"trace"|"debug"|"warn"|"error"
---@param title? string
---@param extraOpts? { icon?: string, on_open?: function, timeout?: boolean|number, animate?: boolean }
---@param extraOpts? { on_open?: function, timeout?: boolean|number, animate?: boolean }
function M.notify(body, level, title, extraOpts)
local pluginName = "tinygit"
local notifyTitle = title and pluginName .. ": " .. title or pluginName
Expand Down Expand Up @@ -35,6 +34,7 @@ function M.notInGitRepo()
return notInRepo
end

---@nodiscard
---@return boolean
function M.inShallowRepo()
return M.syncShellCmd { "git", "rev-parse", "--is-shallow-repository" } == "true"
Expand All @@ -50,6 +50,7 @@ function M.syncShellCmd(cmd, notrim)
return vim.trim(stdout)
end

---@nodiscard
---@return string? ahead
---@return string? behind
function M.getAheadBehind()
Expand Down

0 comments on commit 02cff46

Please sign in to comment.