Skip to content

Commit

Permalink
feat: added autocmd to trigger reload and user command Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Sep 8, 2022
1 parent 24637dd commit 0f14609
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
59 changes: 59 additions & 0 deletions lua/lsp-settings/commands.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
local Util = require("lsp-settings.util")
local Config = require("lsp-settings.config")

local M = {}

function M.setup()
vim.api.nvim_create_user_command("Settings", function(args)
if args.args == "show" then
require("lsp-settings.view").show_settings()
else
M.edit()
end
end, { nargs = "?", desc = "Show the settings of the attached lsp servers" })

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)),
group = group,
callback = function(event)
require("lsp-settings.plugins").fire("on_update", event.match)
end,
})
end

function M.edit()
local buf = vim.api.nvim_get_current_buf()

local items = {}

Util.for_each_global(function(file)
table.insert(items, { file = file, is_global = true })
end)

for _, client in ipairs(vim.lsp.get_active_clients({ bufnr = buf })) do
Util.for_each_local(function(f)
items[f] = { file = f }
end, client.config.root_dir)
end

items = vim.tbl_values(items)

vim.ui.select(items, {
prompt = "Select settings file to create/edit",
format_item = function(item)
local line = Util.exists(item.file) and " edit " or " create "
line = line .. vim.fn.fnamemodify(item.file, ":~")
if item.is_global then
line = line .. ""
end
return line
end,
}, function(choice)
if choice then
vim.cmd("edit " .. choice.file)
end
end)
end

return M
8 changes: 2 additions & 6 deletions lua/lsp-settings/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ local M = {}

function M.setup(opts)
require("lsp-settings.config").setup(opts)

vim.api.nvim_create_user_command("LspSettings", function()
require("lsp-settings.view").show_settings()
end, { desc = "Show the settings of the attached lsp servers" })

require("lsp-settings.lsp").setup()
require("lsp-settings.commands").setup()
require("lsp-settings.plugins").setup()
end

return M

0 comments on commit 0f14609

Please sign in to comment.