Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: consolidate logic to single file #53

Merged
merged 3 commits into from
Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 62 additions & 42 deletions lua/package-info/config.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
-- FILE DESCRIPTION: User passed config options
-- DESCRIPTION: sets up the user given config, and plugin config

local constants = require("package-info.constants")
local globals = require("package-info.globals")

----------------------------------------------------------------------------
---------------------------------- HELPERS ---------------------------------
---------------------------------- MODULE ----------------------------------
----------------------------------------------------------------------------

local DEFAULT_OPTIONS = {
local M = {}

--- Default options
M.options = {
colors = {
up_to_date = "#3C4048",
outdated = "#d19a66",
Expand All @@ -20,32 +22,31 @@ local DEFAULT_OPTIONS = {
},
},
autostart = true,

__highlight_params = {
fg = "guifg",
},
}
local highlight_param = "guifg"
if not vim.o.termguicolors then
highlight_param = "ctermfg"
DEFAULT_OPTIONS.colors = {
up_to_date = "237",
outdated = "173",
}
end

local register_highlight_group = function(group, color)
vim.cmd("highlight " .. group .. " " .. highlight_param .. "=" .. color)
end
M.__namespace = {
id = "",
register = function()
M.__namespace.id = vim.api.nvim_create_namespace("package-ui")
end,
}

M.__state = {
displayed = M.options.autostart or false,
}

local register_colorscheme_autocmd = function()
vim.cmd([[
augroup PackageInfoHighlight
autocmd!
autocmd ColorScheme * lua require('package-info.config').register_highlight_groups()
augroup END
]])
--- Clone options and replace empty ones with default ones
M.__register_user_options = function(user_options)
return vim.tbl_deep_extend("force", {}, M.options, user_options or {})
end

-- Register autocommand for auto-starting plugin
local register_autostart = function(should_autostart)
if should_autostart then
--- Register autocommand for auto-starting plugin
M.__register_autostart = function()
if M.options.autostart then
vim.api.nvim_exec(
[[augroup PackageUI
autocmd!
Expand All @@ -56,27 +57,46 @@ local register_autostart = function(should_autostart)
end
end

-- Clone options and replace empty ones with default ones
local register_user_options = function(options)
return vim.tbl_deep_extend("force", {}, DEFAULT_OPTIONS, options or {})
end
--- If terminal doesn't support true color, fallback to 256 config
M.__register_256color_support = function()
if not vim.o.termguicolors then
vim.cmd([[
augroup PackageUIHighlight
autocmd!
autocmd ColorScheme * lua require('package-info.config').__register_highlight_groups()
augroup END
]])

----------------------------------------------------------------------------
---------------------------------- MODULE ----------------------------------
----------------------------------------------------------------------------
M.options.colors = {
up_to_date = "237",
outdated = "173",
}

local M = {}
M.register_highlight_groups = function()
register_highlight_group(constants.HIGHLIGHT_GROUPS.outdated, M.options.colors.outdated)
register_highlight_group(constants.HIGHLIGHT_GROUPS.up_to_date, M.options.colors.up_to_date)
M.options.__highlight_params.fg = "ctermfg"
end
end

M.setup = function(options)
M.options = register_user_options(options)
--- Register given highlight group
-- @param group - highlight group
-- @param color - color to use with the highlight group
M.__register_highlight_group = function(group, color)
vim.cmd("highlight " .. group .. " " .. M.options.__highlight_params.fg .. "=" .. color)
end

register_autostart(M.options.autostart)
register_colorscheme_autocmd()
M.register_highlight_groups()
globals.namespace.register()
--- Register all highlight groups
M.__register_highlight_groups = function()
M.__register_highlight_group(constants.HIGHLIGHT_GROUPS.outdated, M.options.colors.outdated)
M.__register_highlight_group(constants.HIGHLIGHT_GROUPS.up_to_date, M.options.colors.up_to_date)
end

--- Take all user options and setup the config
-- @param user_options - all the options user can provide in the plugin config // See M.options for defaults
M.setup = function(user_options)
M.__register_user_options(user_options)
M.__register_autostart()
M.__register_256color_support()
M.__register_highlight_groups()
M.__namespace.register()
end

return M
5 changes: 5 additions & 0 deletions lua/package-info/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ M.HIGHLIGHT_GROUPS = {
up_to_date = "PackageInfoUpToDateVersion",
}

M.PACKAGE_MANAGERS = {
yarn = "yarn",
npm = "npm",
}

return M
18 changes: 0 additions & 18 deletions lua/package-info/globals.lua

This file was deleted.

7 changes: 5 additions & 2 deletions lua/package-info/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

local config = require("package-info.config")

local manager_module = require("package-info.modules.manager")
local core_module = require("package-info.modules.core")

local M = {}
Expand All @@ -20,7 +19,11 @@ M.hide = function()
end

M.delete = function()
manager_module.delete()
core_module.delete()
end

M.update = function()
core_module.update()
end

return M
Loading