Skip to content

Commit

Permalink
refactor(configs)!: use list configs instead of hash map! (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
linrongbin16 committed Mar 6, 2024
1 parent e9b0096 commit d3e2bc1
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 78 deletions.
102 changes: 62 additions & 40 deletions lua/gentags/configs.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
local tbl = require("gentags.commons.tbl")
local str = require("gentags.commons.str")

local M = {}

--- @alias gentags.Options table<any, any>
Expand All @@ -8,65 +11,65 @@ local Defaults = {

-- ctags options
ctags = {
["--tag-relative=never"] = true,
"--tag-relative=never",

-- Recommended Options:

-- -- exclude logs
-- ["--exclude=*.log"] = true,
-- "--exclude=*.log",
--
-- -- exclude vcs
-- ["--exclude=*.git"] = true,
-- ["--exclude=*.svg"] = true,
-- ["--exclude=*.hg"] = true,
-- "--exclude=*.git",
-- "--exclude=*.svg",
-- "--exclude=*.hg",
--
-- -- exclude nodejs
-- ["--exclude=node_modules"] = true,
-- "--exclude=node_modules",
--
-- -- exclude tags/cscope
-- ["--exclude=*tags*"] = true,
-- ["--exclude=*cscope.*"] = true,
-- "--exclude=*tags*",
-- "--exclude=*cscope.*",
--
-- -- exclude python
-- ["--exclude=*.pyc"] = true,
-- "--exclude=*.pyc",
--
-- -- exclude jvm class
-- ["--exclude=*.class"] = true,
-- "--exclude=*.class",
--
-- -- exclude VS project generated
-- ["--exclude=*.pdb"] = true,
-- ["--exclude=*.sln"] = true,
-- ["--exclude=*.csproj"] = true,
-- ["--exclude=*.csproj.user"] = true,
-- "--exclude=*.pdb",
-- "--exclude=*.sln",
-- "--exclude=*.csproj",
-- "--exclude=*.csproj.user",
--
-- -- exclude blobs
-- ["--exclude=*.exe"] = true,
-- ["--exclude=*.dll"] = true,
-- ["--exclude=*.mp3"] = true,
-- ["--exclude=*.ogg"] = true,
-- ["--exclude=*.flac"] = true,
-- ["--exclude=*.swp"] = true,
-- ["--exclude=*.swo"] = true,
-- ["--exclude=*.bmp"] = true,
-- ["--exclude=*.gif"] = true,
-- ["--exclude=*.ico"] = true,
-- ["--exclude=*.jpg"] = true,
-- ["--exclude=*.png"] = true,
-- ["--exclude=*.rar"] = true,
-- ["--exclude=*.zip"] = true,
-- ["--exclude=*.tar"] = true,
-- ["--exclude=*.tar.gz"] = true,
-- ["--exclude=*.tar.xz"] = true,
-- ["--exclude=*.tar.bz2"] = true,
-- ["--exclude=*.pdf"] = true,
-- ["--exclude=*.doc"] = true,
-- ["--exclude=*.docx"] = true,
-- ["--exclude=*.ppt"] = true,
-- ["--exclude=*.pptx"] = true,
-- "--exclude=*.exe",
-- "--exclude=*.dll",
-- "--exclude=*.mp3",
-- "--exclude=*.ogg",
-- "--exclude=*.flac",
-- "--exclude=*.swp",
-- "--exclude=*.swo",
-- "--exclude=*.bmp",
-- "--exclude=*.gif",
-- "--exclude=*.ico",
-- "--exclude=*.jpg",
-- "--exclude=*.png",
-- "--exclude=*.rar",
-- "--exclude=*.zip",
-- "--exclude=*.tar",
-- "--exclude=*.tar.gz",
-- "--exclude=*.tar.xz",
-- "--exclude=*.tar.bz2",
-- "--exclude=*.pdf",
-- "--exclude=*.doc",
-- "--exclude=*.docx",
-- "--exclude=*.ppt",
-- "--exclude=*.pptx",
},

-- workspace detection
workspace = { [".git"] = true, [".svn"] = true, [".hg"] = true },
workspace = { ".git", ".svn", ".hg" },

-- excluded workspace
disabled_workspaces = {},
Expand All @@ -76,8 +79,8 @@ local Defaults = {

-- Recommended Options:

-- ["neo-tree"] = true,
-- ["NvimTree"] = true,
-- "neo-tree",
-- "NvimTree",
},

-- excluded filenames
Expand Down Expand Up @@ -135,7 +138,26 @@ end
--- @param opts gentags.Options?
--- @return gentags.Options
M.setup = function(opts)
local user_ctags = tbl.tbl_get(opts, "ctags") or {}
local ctags_opts = vim.deepcopy(Defaults.ctags)
for _, o in ipairs(user_ctags) do
if str.not_empty(o) then
table.insert(ctags_opts, o)
end
end

local user_workspace = tbl.tbl_get(opts, "workspace") or {}
local workspace_opts = vim.deepcopy(Defaults.workspace)
for _, o in ipairs(user_workspace) do
if str.not_empty(o) then
table.insert(workspace_opts, o)
end
end

Configs = vim.tbl_deep_extend("force", vim.deepcopy(Defaults), opts or {})
Configs.ctags = ctags_opts
Configs.workspace = workspace_opts

return Configs
end

Expand Down
16 changes: 2 additions & 14 deletions lua/gentags/ctags.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,7 @@ M._write = function(ctx, on_exit)
end

local cfg = configs.get()
local opts_table = vim.deepcopy(tbl.tbl_get(cfg, "ctags") or {})
local opts = {}
for o, v in ipairs(opts_table) do
if str.not_empty(o) and v then
table.insert(opts, o)
end
end
local opts = vim.deepcopy(tbl.tbl_get(cfg, "ctags") or {})

local cwd = nil
if ctx.mode == "workspace" then
Expand Down Expand Up @@ -231,13 +225,7 @@ M._append = function(ctx, on_exit)
end

local cfg = configs.get()
local opts_table = vim.deepcopy(tbl.tbl_get(cfg, "ctags") or {})
local opts = {}
for o, v in pairs(opts_table) do
if str.not_empty(o) and v then
table.insert(opts, o)
end
end
local opts = vim.deepcopy(tbl.tbl_get(cfg, "ctags") or {})

-- append mode
table.insert(opts, "--append=yes")
Expand Down
6 changes: 3 additions & 3 deletions lua/gentags/dispatcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ M.enabled = function()
local filename = utils.get_filename()
local filetype = utils.get_filetype()

if cfg.disabled_filetypes[filetype] then
if tbl.list_contains(cfg.disabled_filetypes, filetype) then
return false
end
if cfg.disabled_filenames[filename] then
if tbl.list_contains(cfg.disabled_filenames, filename) then
return false
end

Expand All @@ -99,7 +99,7 @@ M.enabled = function()
filedir = path.parent(filename)
end
local workspace = utils.get_workspace(filedir)
if cfg.disabled_workspaces[workspace] then
if tbl.list_contains(cfg.disabled_workspaces, workspace) then
return false
end

Expand Down
40 changes: 19 additions & 21 deletions lua/gentags/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,25 @@ M.get_workspace = function(cwd)
cwd = cwd or vim.fn.getcwd()
while true do
-- logger:debug("|get_workspace| 0-cwd:%s", vim.inspect(cwd))
for pattern, value in pairs(configs.get().workspace) do
if value then
local target = path.join(cwd, pattern)
-- logger:debug(
-- "|get_workspace| 1-cwd:%s, target:%s",
-- vim.inspect(cwd),
-- vim.inspect(target)
-- )
target =
path.normalize(target, { double_backslash = true, expand = true })
local stat_result, stat_err = uv.fs_stat(target)
-- logger:debug(
-- "|get_workspace| 2-cwd:%s, target:%s, stat result:%s, stat err:%s",
-- vim.inspect(cwd),
-- vim.inspect(target),
-- vim.inspect(stat_result),
-- vim.inspect(stat_err)
-- )
if stat_result then
return cwd
end
for _, pattern in ipairs(configs.get().workspace) do
local target = path.join(cwd, pattern)
-- logger:debug(
-- "|get_workspace| 1-cwd:%s, target:%s",
-- vim.inspect(cwd),
-- vim.inspect(target)
-- )
target =
path.normalize(target, { double_backslash = true, expand = true })
local stat_result, stat_err = uv.fs_stat(target)
-- logger:debug(
-- "|get_workspace| 2-cwd:%s, target:%s, stat result:%s, stat err:%s",
-- vim.inspect(cwd),
-- vim.inspect(target),
-- vim.inspect(stat_result),
-- vim.inspect(stat_err)
-- )
if stat_result then
return cwd
end
end
local parent = path.parent(cwd)
Expand Down

0 comments on commit d3e2bc1

Please sign in to comment.