Skip to content

Commit

Permalink
feat: easier way to work with setting patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Sep 10, 2022
1 parent 79a3cf0 commit a8e496f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 19 deletions.
6 changes: 3 additions & 3 deletions lua/settings/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function M.setup()

local group = vim.api.nvim_create_augroup("Settings", { clear = true })
vim.api.nvim_create_autocmd("BufWritePost", {
pattern = Util.merge({}, Config.options.global_settings, vim.tbl_values(Config.options.local_settings)),
pattern = Util.file_patterns(),
group = group,
callback = function(event)
local fname = Util.fqn(event.match)
Expand All @@ -61,11 +61,11 @@ function M.get_files(opts)
if not opts.global then
local root_dir = require("settings.workspace").find_root({ lsp = true })
Util.for_each_local(function(f)
items[f] = { file = f }
table.insert(items, { file = f })
end, root_dir)
end

return vim.tbl_values(items)
return items
end

function M.edit(opts)
Expand Down
39 changes: 36 additions & 3 deletions lua/settings/config.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local M = {}

--- @class Config
---@class Config
M.defaults = {
local_settings = { ".nvim.settings.json", vscode = ".vscode/settings.json" },
global_settings = { "nvim.settings.json" },
local_settings = ".nvim.settings.json",
global_settings = "nvim.settings.json",
plugins = {
lspconfig = {
enabled = true,
Expand All @@ -26,8 +26,41 @@ M.defaults = {
--- @type Config
M.options = {}

---@class SettingsPattern
---@field pattern string
---@field key string|nil|fun(string):string

---@type SettingsPattern[]
M.local_patterns = {}

---@type SettingsPattern[]
M.global_patterns = {}

function M.setup(options)
M.options = vim.tbl_deep_extend("force", {}, M.defaults, options or {})

local util = require("settings.util")

M.local_patterns = {}
M.global_patterns = {}

if M.options.import.vscode then
table.insert(M.local_patterns, { pattern = ".vscode/settings.json", key = "vscode" })
end
if M.options.import.coc then
table.insert(M.local_patterns, { pattern = "coc-settings.json", key = "coc" })
table.insert(M.global_patterns, { pattern = "coc-settings.json", key = "coc" })
end
if M.options.import.nlsp then
local function nlsp_key(file)
return "nlsp." .. vim.fn.fnamemodify(file, ":t:r")
end
table.insert(M.local_patterns, { pattern = ".nlsp-settings/*.json", key = nlsp_key })
table.insert(M.global_patterns, { pattern = "nlsp-settings/*.json", key = nlsp_key })
end

vim.list_extend(M.local_patterns, util.expand(M.options.local_settings))
vim.list_extend(M.global_patterns, util.expand(M.options.global_settings))
end

---@return Config
Expand Down
2 changes: 1 addition & 1 deletion lua/settings/plugins/jsonls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function M.on_new_config(config, root_dir)
},
type = "object",
},
fileMatch = { table.unpack(Config.options.global_settings), table.unpack(Config.options.local_settings) },
fileMatch = Util.file_patterns(),
}

for _, plugin in ipairs(require("settings.plugins").plugins) do
Expand Down
2 changes: 1 addition & 1 deletion lua/settings/plugins/lspconfig.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function M.setup()
local lsputil = require("lspconfig.util")
local hook = lsputil.add_hook_before

local settings_root = lsputil.root_pattern(unpack(vim.tbl_values(Config.options.local_settings)))
local settings_root = lsputil.root_pattern(unpack(Util.file_patterns({ global = false })))

lsputil.on_setup = hook(lsputil.on_setup, function(config)
config.on_new_config = hook(config.on_new_config, Util.protect(M.on_new_config, "Failed to setup lspconfig"))
Expand Down
50 changes: 41 additions & 9 deletions lua/settings/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ function M.merge(...)
return ret
end

function M.file_patterns(opts)
opts = M.merge({ ["local"] = true, ["global"] = true }, opts)
local ret = {}

if opts["local"] then
for _, p in ipairs(Config.local_patterns) do
table.insert(ret, p.pattern)
end
end

if opts["global"] then
for _, p in ipairs(Config.global_patterns) do
table.insert(ret, p.pattern)
end
end

return ret
end

function M.path(str)
local f = debug.getinfo(1, "S").source:sub(2)
return M.fqn(vim.fn.fnamemodify(f, ":h:h:h") .. "/" .. (str or ""))
Expand Down Expand Up @@ -87,24 +106,37 @@ function M.is_nvim_config(path)
return M.has_file(M.fqn(path), M.config_path())
end

---@param patterns table
---@param fn fun(file: string|nil, key:string|nil, pattern:string)
---@param patterns SettingsPattern[]
---@param fn fun(file: string, key:string|nil, pattern:string)
---@param root_dir string
function M.for_each(patterns, fn, root_dir)
for key, pattern in pairs(patterns) do
if type(key) == "number" then
key = nil
for _, p in ipairs(patterns) do
local file = root_dir .. "/" .. p.pattern
---@diagnostic disable-next-line: param-type-mismatch
for _, f in pairs(vim.fn.expand(file, false, true)) do
---@diagnostic disable-next-line: param-type-mismatch
fn(f, type(p.key) == "function" and p.key(f) or p.key, p.pattern)
end
local file = root_dir and root_dir .. "/" .. pattern
fn(file, key, pattern)
end
end

---@param patterns string|(string|SettingsPattern)[]
---@return SettingsPattern[]
function M.expand(patterns)
return vim.tbl_map(function(p)
return type(p) == "string" and { pattern = p } or p
end, type(patterns) == "table" and patterns or { patterns })
end

---@param root_dir string
---@param fn fun(file: string, key:string|nil, pattern:string)
function M.for_each_local(fn, root_dir)
M.for_each(Config.options.local_settings, fn, root_dir)
M.for_each(Config.local_patterns, fn, root_dir)
end

---@param fn fun(file: string, key:string|nil, pattern:string)
function M.for_each_global(fn)
M.for_each(Config.options.global_settings, fn, vim.fn.stdpath("config"))
M.for_each(Config.global_patterns, fn, vim.fn.stdpath("config"))
end

function M.is_global(file)
Expand Down
3 changes: 1 addition & 2 deletions lua/settings/workspace.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local Util = require("settings.util")
local Config = require("settings.config")
local Settings = require("settings.settings")
local lsputil = require("lspconfig.util")

Expand All @@ -18,7 +17,7 @@ function M.find_root(opts)
local fname = Util.fqn(opts.file or vim.api.nvim_buf_get_name(buf))

-- find the root dir that contains a local settings file
local root_dir = lsputil.root_pattern(unpack(vim.tbl_values(Config.options.local_settings)))(fname)
local root_dir = lsputil.root_pattern(unpack(Util.file_patterns({ global = false })))(fname)

-- fallback to lsp root_dir detection if in options
if not root_dir and opts.lsp then
Expand Down

0 comments on commit a8e496f

Please sign in to comment.