Skip to content

Commit

Permalink
feat: Rework configuration setup to add config validation
Browse files Browse the repository at this point in the history
This commit reworks the config setup structure and adds Config typing, validations and tests.
On config setup the user's configuration will be validated against. Any
type errors raised by invalid settings will result in a user-friendly
error message being shown to the user.

Signed-off-by: Liam Jarvis <[email protected]>
  • Loading branch information
jarviliam committed Dec 27, 2023
1 parent 4a60f50 commit e333d44
Show file tree
Hide file tree
Showing 21 changed files with 612 additions and 241 deletions.
4 changes: 2 additions & 2 deletions lua/octo/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ function M.octo(object, action, ...)
end

if not object then
if config.get_config().enable_builtin then
if config.values.enable_builtin then
M.commands.actions()
else
utils.error "Missing arguments"
Expand Down Expand Up @@ -759,7 +759,7 @@ end

function M.create_pr(is_draft)
is_draft = "draft" == is_draft and true or false
local conf = config.get_config()
local conf = config.values
local select = conf.pull_requests.always_select_remote_on_create or false

local repo
Expand Down
611 changes: 418 additions & 193 deletions lua/octo/config.lua

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lua/octo/gh/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ local env_vars = {

local function get_env()
local env = env_vars
local gh_env = config.get_config().gh_env
local gh_env = config.values.gh_env
if type(gh_env) == "function" then
local computed_env = gh_env()
if type(computed_env) == "table" then
Expand Down Expand Up @@ -79,7 +79,7 @@ function M.run(opts)
end

opts = opts or {}
local conf = config.get_config()
local conf = config.values
local mode = opts.mode or "async"
local hostname = ""
if opts.args[1] == "api" then
Expand Down
4 changes: 2 additions & 2 deletions lua/octo/model/octo-buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ end
function OctoBuffer:configure()
-- configure buffer
vim.api.nvim_buf_call(self.bufnr, function()
local use_signcolumn = config.get_config().ui.use_signcolumn
local use_signcolumn = config.values.ui.use_signcolumn
vim.cmd [[setlocal filetype=octo]]
vim.cmd [[setlocal buftype=acwrite]]
vim.cmd [[setlocal omnifunc=v:lua.octo_omnifunc]]
Expand Down Expand Up @@ -799,7 +799,7 @@ end

---Renders the signcolumn
function OctoBuffer:render_signcolumn()
local use_signcolumn = config.get_config().ui.use_signcolumn
local use_signcolumn = config.values.ui.use_signcolumn
if not use_signcolumn or not self.ready then
return
end
Expand Down
2 changes: 1 addition & 1 deletion lua/octo/picker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local utils = require "octo.utils"
local M = {}

function M.setup()
local provider_name = config.get_config().picker
local provider_name = config.values.picker
if utils.is_blank(provider_name) then
provider_name = "telescope"
end
Expand Down
2 changes: 1 addition & 1 deletion lua/octo/pickers/fzf-lua/pickers/fzf_actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ M.common_buffer_actions = function(formatted_items)
end

M.common_open_actions = function(formatted_items)
local cfg = octo_config.get_config()
local cfg = octo_config.values
return vim.tbl_extend("force", M.common_buffer_actions(formatted_items), {
[utils.convert_vim_mapping_to_fzf(cfg.picker_config.mappings.open_in_browser.lhs)] = function(selected)
picker_utils.open_in_browser(formatted_items[selected[1]])
Expand Down
2 changes: 1 addition & 1 deletion lua/octo/pickers/fzf-lua/pickers/issues.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ return function(opts)
end

local owner, name = utils.split_repo(opts.repo)
local cfg = octo_config.get_config()
local cfg = octo_config.values
local order_by = cfg.issues.order_by

local query = graphql("issues_query", owner, name, filter, order_by.field, order_by.direction, { escape = false })
Expand Down
2 changes: 1 addition & 1 deletion lua/octo/pickers/fzf-lua/pickers/prs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ return function(opts)
end

local owner, name = utils.split_repo(opts.repo)
local cfg = octo_config.get_config()
local cfg = octo_config.values
local order_by = cfg.pull_requests.order_by

local query =
Expand Down
2 changes: 1 addition & 1 deletion lua/octo/pickers/fzf-lua/previewers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ M.repo = function(formatted_repos)
self:set_preview_buf(tmpbuf)

local stargazer, fork
if config.get_config().picker_config.use_emojis then
if config.values.picker_config.use_emojis then
stargazer = string.format("💫: %s", entry.repo.stargazerCount)
fork = string.format("🔱: %s", entry.repo.forkCount)
else
Expand Down
8 changes: 4 additions & 4 deletions lua/octo/pickers/telescope/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function M.issues(opts)
end

local owner, name = utils.split_repo(opts.repo)
local cfg = octo_config.get_config()
local cfg = octo_config.values
local order_by = cfg.issues.order_by
local query = graphql("issues_query", owner, name, filter, order_by.field, order_by.direction, { escape = false })
utils.info "Fetching issues (this may take a while) ..."
Expand Down Expand Up @@ -277,7 +277,7 @@ function M.pull_requests(opts)
end

local owner, name = utils.split_repo(opts.repo)
local cfg = octo_config.get_config()
local cfg = octo_config.values
local order_by = cfg.pull_requests.order_by
local query =
graphql("pull_requests_query", owner, name, filter, order_by.field, order_by.direction, { escape = false })
Expand Down Expand Up @@ -479,7 +479,7 @@ end
---
function M.search(opts)
opts = opts or {}
local cfg = octo_config.get_config()
local cfg = octo_config.values
local requester = function()
return function(prompt)
if not opts.prompt and utils.is_blank(prompt) then
Expand Down Expand Up @@ -924,7 +924,7 @@ end
--
function M.repos(opts)
opts = opts or {}
local cfg = octo_config.get_config()
local cfg = octo_config.values
if not opts.login then
if vim.g.octo_viewer then
opts.login = vim.g.octo_viewer
Expand Down
8 changes: 4 additions & 4 deletions lua/octo/reviews/file-entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function FileEntry:fetch()
local left_sha = current_review.layout.left.commit
local right_abbrev = current_review.layout.right:abbrev()
local left_abbrev = current_review.layout.left:abbrev()
local conf = config.get_config()
local conf = config.values

-- handle renamed files
if self.status == "R" and self.previous_path then
Expand Down Expand Up @@ -236,7 +236,7 @@ function FileEntry:load_buffers(left_winid, right_winid)
-- configure diff buffers
for _, split in ipairs(splits) do
if not split.bufid or not vim.api.nvim_buf_is_loaded(split.bufid) then
local conf = config.get_config()
local conf = config.values
local use_local = conf.use_local_fs and split.pos == "right" and utils.in_pr_branch(self.pull_request.bufnr)

-- create buffer
Expand Down Expand Up @@ -469,15 +469,15 @@ end

function M._configure_buffer(bufid)
utils.apply_mappings("review_diff", bufid)
-- local conf = config.get_config()
-- local conf = config.values
-- vim.cmd(string.format("nnoremap %s :OctoAddReviewComment<CR>", conf.mappings.review_thread.add_comment))
-- vim.cmd(string.format("vnoremap %s :OctoAddReviewComment<CR>", conf.mappings.review_thread.add_comment))
-- vim.cmd(string.format("nnoremap %s :OctoAddReviewSuggestion<CR>", conf.mappings.review_thread.add_suggestion))
-- vim.cmd(string.format("vnoremap %s :OctoAddReviewSuggestion<CR>", conf.mappings.review_thread.add_suggestion))
end

function M._detach_buffer(bufid)
local conf = config.get_config()
local conf = config.values
for _, lhs in pairs(conf.mappings.review_diff) do
pcall(vim.keymap.del, "n", lhs, { buffer = bufid })
end
Expand Down
6 changes: 3 additions & 3 deletions lua/octo/reviews/file-panel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ FilePanel.bufopts = {
---@param files FileEntry[]
---@return FilePanel
function FilePanel:new(files)
local conf = config.get_config()
local conf = config.values
local this = {
files = files,
size = conf.file_panel.size,
Expand Down Expand Up @@ -96,7 +96,7 @@ function FilePanel:open()
return
end

local conf = config.get_config()
local conf = config.values
self.size = conf.file_panel.size
--vim.cmd("wincmd H")
--vim.cmd("vsp")
Expand Down Expand Up @@ -233,7 +233,7 @@ function FilePanel:render()
end

local current_review = require("octo.reviews").get_current_review()
local conf = config.get_config()
local conf = config.values
local strlen = vim.fn.strlen
local s = "Files changed"
add_hl("OctoFilePanelTitle", line_idx, 0, #s)
Expand Down
4 changes: 2 additions & 2 deletions lua/octo/reviews/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ end
function Review:initiate(opts)
opts = opts or {}
local pr = self.pull_request
local conf = config.get_config()
local conf = config.values
if conf.use_local_fs and not utils.in_pr_branch(pr.bufnr) then
local choice = vim.fn.confirm("Currently not in PR branch, would you like to checkout?", "&Yes\n&No", 2)
if choice == 1 then
Expand Down Expand Up @@ -224,7 +224,7 @@ function Review:collect_submit_info()
return
end

local conf = config.get_config()
local conf = config.values
local winid, bufnr = window.create_centered_float {
header = string.format(
"Press %s to approve, %s to comment or %s to request changes",
Expand Down
2 changes: 1 addition & 1 deletion lua/octo/reviews/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function M.get_git_hl(status)
end

function M.get_file_icon(name, ext, render_data, line_idx, offset)
local use_icons = config.get_config().file_panel.use_icons
local use_icons = config.values.file_panel.use_icons
if not use_icons then
return " "
end
Expand Down
6 changes: 3 additions & 3 deletions lua/octo/ui/bubbles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local colors = require "octo.ui.colors"

local function make_bubble(content, highlight_group, opts)
opts = opts or {}
local conf = config.get_config()
local conf = config.values
local margin = string.rep(" ", opts.margin_width or 0)
local right_margin = string.rep(" ", opts.right_margin_width or 0)
local left_margin = string.rep(" ", opts.left_margin_width or 0)
Expand Down Expand Up @@ -38,7 +38,7 @@ end

local function make_user_bubble(name, is_viewer, opts)
opts = opts or {}
local conf = config.get_config()
local conf = config.values
local highlight = is_viewer and "OctoUserViewer" or "OctoUser"
local icon_position = opts.icon_position or "left"
local icon = conf.user_icon
Expand All @@ -53,7 +53,7 @@ local function make_user_bubble(name, is_viewer, opts)
end

local function make_reaction_bubble(icon, includes_viewer, opts)
local conf = config.get_config()
local conf = config.values
local highlight = includes_viewer and "OctoReactionViewer" or "OctoReaction"
local hint_for_viewer = includes_viewer and conf.reaction_viewer_hint_icon or ""
local content = icon .. hint_for_viewer
Expand Down
2 changes: 1 addition & 1 deletion lua/octo/ui/colors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ local function get_fg(hl_group_name)
end

local function get_colors()
local conf = config.get_config()
local conf = config.values
return conf.colors
end

Expand Down
2 changes: 1 addition & 1 deletion lua/octo/ui/signs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local config = require "octo.config"
local M = {}

function M.setup()
local conf = config.get_config()
local conf = config.values

vim.cmd(string.format("sign define octo_thread text=%s texthl=OctoBlue", conf.comment_icon))
vim.cmd(string.format("sign define octo_thread_resolved text=%s texthl=OctoGreen", conf.comment_icon))
Expand Down
26 changes: 13 additions & 13 deletions lua/octo/ui/writers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ function M.write_comment(bufnr, comment, kind, line)
---- PullRequestComment (regular comment (not associated to any review) to a PR review comment)

local buffer = octo_buffers[bufnr]
local conf = config.get_config()
local conf = config.values

-- heading
line = line or vim.api.nvim_buf_line_count(bufnr) + 1
Expand Down Expand Up @@ -598,7 +598,7 @@ function M.write_comment(bufnr, comment, kind, line)
end

local function find_snippet_range(diffhunk_lines)
local conf = config.get_config()
local conf = config.values
local context_lines = conf.snippet_context_lines or 4
local snippet_start
local count = 0
Expand Down Expand Up @@ -818,7 +818,7 @@ end
function M.write_review_thread_header(bufnr, opts, line)
line = line or vim.api.nvim_buf_line_count(bufnr) - 1

local conf = config.get_config()
local conf = config.values

-- clear virtual texts
vim.api.nvim_buf_clear_namespace(bufnr, constants.OCTO_THREAD_HEADER_VT_NS, line, line + 2)
Expand Down Expand Up @@ -1018,7 +1018,7 @@ end

function M.write_issue_summary(bufnr, issue, opts)
opts = opts or {}
local conf = config.get_config()
local conf = config.values
local max_length = opts.max_length or 80
local chunks = {}

Expand Down Expand Up @@ -1101,7 +1101,7 @@ function M.write_assigned_event(bufnr, item)
-- item.actor.login == vim.g.octo_viewer
-- )
local vt = {}
local conf = config.get_config()
local conf = config.values
table.insert(vt, { conf.timeline_marker .. " ", "OctoTimelineMarker" })
table.insert(vt, { "EVENT: ", "OctoTimelineItemHeading" })
--vim.list_extend(vt, actor_bubble)
Expand All @@ -1114,7 +1114,7 @@ end

function M.write_commit_event(bufnr, item)
local vt = {}
local conf = config.get_config()
local conf = config.values
table.insert(vt, { conf.timeline_marker .. " ", "OctoTimelineMarker" })
table.insert(vt, { "EVENT: ", "OctoTimelineItemHeading" })
if item.commit.committer.user ~= vim.NIL then
Expand Down Expand Up @@ -1142,7 +1142,7 @@ function M.write_merged_event(bufnr, item)
-- item.actor.login == vim.g.octo_viewer
-- )
local vt = {}
local conf = config.get_config()
local conf = config.values
table.insert(vt, { conf.timeline_marker .. " ", "OctoTimelineMarker" })
table.insert(vt, { "EVENT: ", "OctoTimelineItemHeading" })
--vim.list_extend(vt, actor_bubble)
Expand All @@ -1161,7 +1161,7 @@ function M.write_closed_event(bufnr, item)
-- item.actor.login == vim.g.octo_viewer
-- )
local vt = {}
local conf = config.get_config()
local conf = config.values
table.insert(vt, { conf.timeline_marker .. " ", "OctoTimelineMarker" })
table.insert(vt, { "EVENT: ", "OctoTimelineItemHeading" })
--vim.list_extend(vt, actor_bubble)
Expand All @@ -1186,7 +1186,7 @@ function M.write_labeled_events(bufnr, items, action)

for _, actor in ipairs(vim.tbl_keys(labels_by_actor)) do
local vt = {}
local conf = config.get_config()
local conf = config.values
table.insert(vt, { conf.timeline_marker .. " ", "OctoTimelineMarker" })
table.insert(vt, { "EVENT: ", "OctoTimelineItemHeading" })
--vim.list_extend(vt, actor_bubble)
Expand All @@ -1212,7 +1212,7 @@ function M.write_reopened_event(bufnr, item)
-- item.actor.login == vim.g.octo_viewer
-- )
local vt = {}
local conf = config.get_config()
local conf = config.values
table.insert(vt, { conf.timeline_marker .. " ", "OctoTimelineMarker" })
table.insert(vt, { "EVENT: ", "OctoTimelineItemHeading" })
--vim.list_extend(vt, actor_bubble)
Expand All @@ -1229,7 +1229,7 @@ function M.write_review_requested_event(bufnr, item)
-- )

local vt = {}
local conf = config.get_config()
local conf = config.values
table.insert(vt, { conf.timeline_marker .. " ", "OctoTimelineMarker" })
table.insert(vt, { "EVENT: ", "OctoTimelineItemHeading" })
--vim.list_extend(vt, actor_bubble)
Expand All @@ -1250,7 +1250,7 @@ function M.write_review_request_removed_event(bufnr, item)
-- item.actor.login == vim.g.octo_viewer
-- )
local vt = {}
local conf = config.get_config()
local conf = config.values
table.insert(vt, { conf.timeline_marker .. " ", "OctoTimelineMarker" })
table.insert(vt, { "EVENT: ", "OctoTimelineItemHeading" })
--vim.list_extend(vt, actor_bubble)
Expand All @@ -1271,7 +1271,7 @@ function M.write_review_dismissed_event(bufnr, item)
-- item.actor.login == vim.g.octo_viewer
-- )
local vt = {}
local conf = config.get_config()
local conf = config.values
table.insert(vt, { conf.timeline_marker .. " ", "OctoTimelineMarker" })
table.insert(vt, { "EVENT: ", "OctoTimelineItemHeading" })
--vim.list_extend(vt, actor_bubble)
Expand Down
Loading

0 comments on commit e333d44

Please sign in to comment.