Skip to content

Commit

Permalink
refactor!: config structure
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Sep 23, 2023
1 parent 0f8dbbc commit 51c024d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 49 deletions.
38 changes: 18 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,35 +105,33 @@ The `setup` call is optional. These are the default settings:

```lua
local defaultConfig = {
-- Why 50/72 is recommended: https://stackoverflow.com/q/2290016/22114136
commitMaxLen = 72,
smallCommitMaxLen = 50,

-- When conforming the commit message popup with an empty message, fill in this message.
-- Set to `false` to disallow empty commit messages.
emptyCommitMsgFillIn = "squash", -- string|false

-- disallow commit messages without a conventinal commit keyword
enforceConvCommits = {
enabled = true,
keywords = {
"chore", "build", "test", "fix", "feat", "refactor", "perf",
"style", "revert", "ci", "docs", "break", "improv",
commitMsg = {
-- Why 50/72 is recommended: https://stackoverflow.com/q/2290016/22114136
maxLen = 72,
mediumLen = 50,

-- When conforming the commit message popup with an empty message, fill
-- in this message. `false` to disallow empty commit messages.
emptyFillIn = "chore", ---@type string|false

-- disallow commit messages without a conventinal commit keyword
enforceConvCommits = {
enabled = true,
-- stylua: ignore
keywords = {
"chore", "build", "test", "fix", "feat", "refactor", "perf",
"style", "revert", "ci", "docs", "break", "improv",
},
},
},

-- icons for the issue/PR search
asyncOpConfirmationSound = true, -- currently macOS only
issueIcons = {
closedIssue = "🟣",
openIssue = "🟢",
openPR = "🟦",
mergedPR = "🟨",
closedPR = "🟥",
},

-- Confirmation sound on finished async operations like push.
-- Currently macOS only.
asyncOpConfirmationSound = true,
}
```

Expand Down
67 changes: 38 additions & 29 deletions lua/tinygit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@ local M = {}
local fn = vim.fn

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

local defaultConfig = {
commitMaxLen = 72,
smallCommitMaxLen = 50, -- https://stackoverflow.com/q/2290016/22114136
emptyCommitMsgFillIn = "chore",
enforceConvCommits = {
enabled = true,
-- stylua: ignore
keywords = {
"chore", "build", "test", "fix", "feat", "refactor", "perf",
"style", "revert", "ci", "docs", "break", "improv",
commitMsg = {
-- Why 50/72 is recommended: https://stackoverflow.com/q/2290016/22114136
maxLen = 72,
mediumLen = 50,

-- When conforming the commit message popup with an empty message, fill in
-- this message. Set to `false` to disallow empty commit messages.
emptyFillIn = "chore", ---@type string|false

-- disallow commit messages without a conventinal commit keyword
enforceConvCommits = {
enabled = true,
-- stylua: ignore
keywords = {
"chore", "build", "test", "fix", "feat", "refactor", "perf",
"style", "revert", "ci", "docs", "break", "improv",
},
},
},
asyncOpConfirmationSound = true, -- currently macOS only
issueIcons = {
closedIssue = "🟣",
openIssue = "🟢",
openPR = "🟦",
mergedPR = "🟨",
closedPR = "🟥",
},
asyncOpConfirmationSound = true, -- Currently macOS only
}

-- set values if setup call is not run
Expand Down Expand Up @@ -96,23 +105,26 @@ end
---@return string the (modified) commit message
local function processCommitMsg(commitMsg)
commitMsg = vim.trim(commitMsg)
if #commitMsg > config.commitMaxLen then
local conf = config.commitMsg

if #commitMsg > conf.maxLen then
notify("Commit Message too long.", "warn")
local shortenedMsg = commitMsg:sub(1, config.commitMaxLen)
local shortenedMsg = commitMsg:sub(1, conf.maxLen)
return false, shortenedMsg
elseif commitMsg == "" then
if not config.emptyCommitMsgFillIn then
if not conf.emptyFillIn then
notify("Commit Message empty.", "warn")
return false, ""
else
return true, config.emptyCommitMsgFillIn
---@diagnostic disable-next-line: return-type-mismatch -- checked above
return true, conf.emptyFillIn
end
end

if config.enforceConvCommits then
if conf.enforceConvCommits.enabled then
-- stylua: ignore
local firstWord = commitMsg:match("^%w+")
if not vim.tbl_contains(config.enforceConvCommits.keywords, firstWord) then
if not vim.tbl_contains(conf.enforceConvCommits.keywords, firstWord) then
notify("Not using a Conventional Commits keyword.", "warn")
return false, commitMsg
end
Expand All @@ -129,34 +141,31 @@ local function setGitCommitAppearance()
pattern = "DressingInput",
once = true, -- do not affect other DressingInputs
callback = function()
local conf = config.commitMsg
local winNs = 1

vim.api.nvim_win_set_hl_ns(0, winNs)

-- \zs = start of match
-- \ze = end of match
-- \ze = end of match, \zs = start of match
fn.matchadd(
"almostLong",
([[.\{%s}\zs.\{1,%s}\ze]]):format(
config.smallCommitMaxLen - 1,
config.commitMaxLen - config.smallCommitMaxLen
)
([[.\{%s}\zs.\{1,%s}\ze]]):format(conf.mediumLen - 1, conf.maxLen - conf.mediumLen)
)
vim.api.nvim_set_hl(winNs, "almostLong", { link = "WarningMsg" })

fn.matchadd("tooLong", ([[.\{%s}\zs.*\ze]]):format(config.commitMaxLen - 1))
fn.matchadd("tooLong", ([[.\{%s}\zs.*\ze]]):format(conf.maxLen - 1))
vim.api.nvim_set_hl(winNs, "tooLong", { link = "ErrorMsg" })

-- for treesitter highlighting
vim.bo.filetype = "gitcommit" ---@diagnostic disable-line: inject-field
vim.bo.filetype = "gitcommit"
vim.api.nvim_set_hl(winNs, "Title", { link = "Normal" })

-- fix confirming input field (not working in insert mode due to filetype change)
vim.keymap.set("i", "<CR>", "<Esc><CR>", { buffer = true, remap = true })

vim.api.nvim_buf_set_name(0, "COMMIT_EDITMSG") -- for statusline plugins

---@diagnostic disable-next-line: inject-field
vim.opt_local.colorcolumn = { config.smallCommitMaxLen, config.commitMaxLen }
vim.opt_local.colorcolumn = { conf.mediumLen, conf.maxLen }
end,
})
end
Expand All @@ -181,7 +190,7 @@ function M.amendNoEdit(opts)
if nonZeroExit(stderr) then return end

local body = ('"%s"'):format(lastCommitMsg)
if opts.forcePush then body = body .. "\nForce Pushing…" end
if opts.forcePush then body = body .. "\n➤ Force Pushing…" end
notify(body, "info", "Amend-No-edit")

if opts.forcePush then M.push { force = true } end
Expand Down Expand Up @@ -212,7 +221,7 @@ function M.amendOnlyMsg(opts, prefillMsg)
if nonZeroExit(stderr) then return end

local body = ('"%s"'):format(newMsg)
if opts.forcePush then body = body .. "\nForce Pushing…" end
if opts.forcePush then body = body .. "\n➤ Force Pushing…" end
notify(body, "info", "Amend-Only-Msg")

if opts.forcePush then M.push { force = true } end
Expand Down Expand Up @@ -250,7 +259,7 @@ function M.smartCommit(opts, prefillMsg)
if nonZeroExit(stderr) then return end

local body = ('"%s"'):format(newMsg)
if opts.push then body = body .. "\nPushing" end
if opts.push then body = body .. "\n➤ Pushing" end
notify(body, "info", "Smart-Commit")

if opts.push then M.push { pullBefore = true } end
Expand Down

0 comments on commit 51c024d

Please sign in to comment.