Skip to content
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ return {
---Set filetype to jsonc when opening a file specified by `config_file_paths`,
---make sure you have the jsonc tree-sitter parser installed for highlighting
jsonc_filetype = true,
---Enable live reloading of settings when config files change; for servers that support it,
---this is done via the `workspace/didChangeConfiguration` notification, otherwise the
---server is restarted
live_reload = false,
---Provide your own root dir; can be a string or function returning a string.
---It should be/return the full absolute path to the root directory.
---If not set, defaults to `require('codesettings.util').get_root()`
Expand Down Expand Up @@ -149,6 +153,7 @@ return {

- Minimal API: one function you call per server setup, or with a global hook (see example above)
- `jsonc` filetype for local config files
- Live reload: automatically reload settings when config files change (opt-in via `live_reload = true`)
- Configure the `codesettings.nvim` plugin itself in local config JSON files
- `jsonls` integration for schema-based completion of LSP settings in JSON(C) configuration files
![jsonls integration](https://github.com/user-attachments/assets/5d37f0bb-0e07-4c22-bc6b-16cf3e65e201)
Expand Down
2 changes: 1 addition & 1 deletion bench/run.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local Util = require('codesettings.util')
local Jsonls = require('codesettings.integrations.jsonls')
local Jsonls = require('codesettings.setup.jsonls')
local Settings = require('codesettings.settings')
local Benchmark = {}

Expand Down
4 changes: 3 additions & 1 deletion codesettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
"workspace": {
"library": [
"${3rd}/luassert/library",
"${3rd}/busted/library"
"${3rd}/busted/library",
"${3rd}/luv/library"
],
"checkThirdParty": false
},
"diagnostics.globals": [
"vim"
]
},
"codesettings.live_reload": true
}
2 changes: 1 addition & 1 deletion lua/codesettings/build/config-schema.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local Util = require('codesettings.util')
local ConfigSchema = require('codesettings.config.schema')
local Util = require('codesettings.util')

local relpath = 'lua/codesettings/generated/codesettings-config-schema.lua'

Expand Down
2 changes: 1 addition & 1 deletion lua/codesettings/build/doc.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local Util = require('codesettings.util')
local Schemas = require('codesettings.build.schemas')
local Util = require('codesettings.util')

local M = {}

Expand Down
2 changes: 1 addition & 1 deletion lua/codesettings/build/schemas.lua
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ function M.clean()
end
for _, f in pairs(files) do
Util.delete_file(f)
print('Deleted ' .. f)
end
print('Deleted ' .. #files .. ' schema files from schemas/*')
end

return M
10 changes: 7 additions & 3 deletions lua/codesettings/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ function Config.setup(opts)
options = vim.tbl_deep_extend('force', {}, options, plugin_config)

if options.jsonls_integration then
require('codesettings.integrations.jsonls').setup()
require('codesettings.setup.jsonls').setup()
end

if options.jsonc_filetype then
require('codesettings.integrations.jsonc-filetype').setup()
require('codesettings.setup.jsonc-filetype').setup()
end

local lua_ls_integration = options.lua_ls_integration
if lua_ls_integration == true or (type(lua_ls_integration) == 'function' and lua_ls_integration()) then
require('codesettings.integrations.lua_ls').setup()
require('codesettings.setup.lua_ls').setup()
end

if options.live_reload then
require('codesettings.setup.live-reload').setup()
end
end

Expand Down
5 changes: 5 additions & 0 deletions lua/codesettings/config/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ M.properties = {
description = 'Set filetype to jsonc for config files',
default = true,
},
live_reload = {
type = 'boolean',
description = 'Enable live reloading of settings when config files change; for servers that support it, this is done via the `workspace/didChangeConfiguration` notification, otherwise the server is restarted',
default = false,
},
}

---Extract the default values from the schema
Expand Down
27 changes: 24 additions & 3 deletions lua/codesettings/generated/annotations.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
-- vim: ft=bigfile
-- stylua: ignore
---@meta

Expand Down Expand Up @@ -2306,6 +2305,7 @@
--
-- ```lua
-- default = {
-- analyze_files = false,
-- run_tests = false
-- }
-- ```
Expand Down Expand Up @@ -3173,7 +3173,7 @@
-- An array of language ids for which the extension should probe if support is installed.
--
-- ```lua
-- default = { "astro", "civet", "javascript", "javascriptreact", "typescript", "typescriptreact", "html", "mdx", "vue", "markdown", "json", "jsonc", "css", "glimmer-js", "glimmer-ts" }
-- default = { "astro", "civet", "javascript", "javascriptreact", "typescript", "typescriptreact", "html", "mdx", "vue", "markdown", "json", "jsonc", "css", "glimmer-js", "glimmer-ts", "svelte" }
-- ```
---@field probe string[]?
---@field problems lsp.eslint.Problems?
Expand Down Expand Up @@ -10678,6 +10678,26 @@
-- ```
---@field enable boolean?

---@class lsp.intelephense.InlayHint
-- Will show inlay hints for call argument parameter names if named arguments are not already in use.
--
-- ```lua
-- default = true
-- ```
---@field parameterNames boolean?
-- Will show inlay hints for anonymous function declaration parameter types if not already declared.
--
-- ```lua
-- default = true
-- ```
---@field parameterTypes boolean?
-- Will show an inlay hint for call declaration return type if not already declared.
--
-- ```lua
-- default = true
-- ```
---@field returnTypes boolean?

-- An object that describes the format of generated class/interface/trait phpdoc. The following snippet variables are available: SYMBOL_NAME; SYMBOL_KIND; SYMBOL_TYPE; SYMBOL_NAMESPACE.
--
-- ```lua
Expand Down Expand Up @@ -10809,6 +10829,7 @@
---@field environment lsp.intelephense.Environment?
---@field files lsp.intelephense.Files?
---@field format lsp.intelephense.Format?
---@field inlayHint lsp.intelephense.InlayHint?
-- DEPRECATED. Don't use this. Go to command palette and search for enter licence key.
---@field licenceKey string?
-- Maximum memory (in MB) that the server should use. On some systems this may only have effect when runtime has been set. Minimum 256.
Expand Down Expand Up @@ -24730,4 +24751,4 @@
---@field zig_lib_path string?

---@class lsp.zls
---@field zls lsp.zls.Zls?
---@field zls lsp.zls.Zls?
2 changes: 2 additions & 0 deletions lua/codesettings/generated/codesettings-config-schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
---default = true
---```
---@field jsonls_integration boolean
---Enable live reloading of settings when config files change; for servers that support it, this is done via the `workspace/didChangeConfiguration` notification, otherwise the server is restarted
---@field live_reload boolean
---Integrate with lua_ls for LSP settings completion; can be a function so that, for example, you can enable it only if editing your nvim config
---
---```lua
Expand Down
31 changes: 29 additions & 2 deletions lua/codesettings/health.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local Config = require('codesettings.config')
local Util = require('codesettings.util')

local health_start = vim.health.start or vim.health.report_start
Expand Down Expand Up @@ -49,9 +50,35 @@ function M.check()
end

if pcall(vim.treesitter.get_string_parser, '', 'jsonc') then
ok('**jsonc** parser for tree-sitter is installed')
ok('`jsonc` parser for tree-sitter is installed')
else
warn('**jsonc** parser for tree-sitter is not installed. Jsonc highlighting might be broken')
warn('`jsonc` parser for tree-sitter is not installed. Jsonc highlighting might be broken')
end

if Config.live_reload then
ok(('Live reload is enabled for paths: %s'):format(vim.inspect(Config.config_file_paths)))
else
info(
'Live reload is disabled. Enable with `live_reload = true` to automatically reload settings when config files change'
)
end

-- check LSP integration settings
if Config.jsonls_integration then
ok('`jsonls` integration is enabled')
else
info('`jsonls` integration is disabled')
end

if Config.lua_ls_integration then
ok('`lua_ls` integration is enabled')
else
info('`lua_ls` integration is disabled')
end

-- check loader extensions
if Config.loader_extensions and #Config.loader_extensions > 0 then
ok('Loader extensions configured: %d', #Config.loader_extensions)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lua/codesettings/settings.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local Util = require('codesettings.util')
local Extensions = require('codesettings.extensions')
local TerminalObjects = require('codesettings.generated.terminal-objects')
local Util = require('codesettings.util')

local M = {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local Transformer = require('codesettings.setup.jsonls.transformer')
local Util = require('codesettings.util')
local Transformer = require('codesettings.integrations.jsonls.transformer')

local M = {}

Expand Down Expand Up @@ -75,7 +75,9 @@ function M.setup()
})

-- lazy loading; if jsonls is already active, restart it
Util.restart_lsp('jsonls')
vim.defer_fn(function()
Util.did_change_configuration('jsonls', vim.lsp.config.jsonls, true)
end, 500)
end

return M
77 changes: 77 additions & 0 deletions lua/codesettings/setup/live-reload.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
local Util = require('codesettings.util')

local M = {}

---@type number|nil
local augroup = nil

---@type {[string]: uv_timer_t}
local debounce_timers = {}

---Reload settings for all active LSP clients
---@param filepath string the config file that changed
local function reload_settings(filepath)
local Settings = require('codesettings.settings')

local settings = Settings.new():load(filepath)
if not settings then
return
end

local clients = vim.lsp.get_clients()

if #clients == 0 then
Util.info('settings file changed but no LSP clients are running')
return
end
local updated_clients = false

for _, client in ipairs(clients) do
local client_settings = settings:schema(client.name)
if client_settings and vim.tbl_count(client_settings:totable()) > 0 then
client.config.settings = vim.tbl_deep_extend('force', client.config.settings or {}, client_settings:totable())
Util.did_change_configuration(client, client.config)
updated_clients = true
end
end

if not updated_clients then
Util.info('settings file changed but no settings found for running LSP servers')
end
end

function M.setup()
if augroup then
return
end

augroup = vim.api.nvim_create_augroup('CodesettingsLiveReload', { clear = true })
local config_files = Util.get_local_configs({ only_exists = false })
vim.api.nvim_create_autocmd('BufWritePost', {
group = augroup,
pattern = config_files,
callback = function(args)
-- cancel existing timer for this file if any
if debounce_timers[args.file] then
debounce_timers[args.file]:stop()
debounce_timers[args.file]:close()
end

-- create new timer that will fire after 100ms
debounce_timers[args.file] = vim.defer_fn(function()
reload_settings(args.file)
debounce_timers[args.file] = nil
end, 500)
end,
})
end

---Teardown live reload
function M.teardown()
if augroup then
vim.api.nvim_del_augroup_by_id(augroup)
augroup = nil
end
end

return M
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ function M.setup()
})

-- lazy loading; if lua_ls is already active, restart it
Util.restart_lsp('lua_ls')
vim.defer_fn(function()
Util.did_change_configuration('lua_ls', vim.lsp.config.lua_ls, true)
end, 500)
end

return M
Loading