Skip to content

Commit 0f0f437

Browse files
authored
Merge pull request #97 from mrjones2014/mrj/push-prykyxnwmrxw
feat(docs): Generate config defaults from schema
2 parents b4ed34e + 552ee50 commit 0f0f437

File tree

4 files changed

+173
-36
lines changed

4 files changed

+173
-36
lines changed

README.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,49 @@ or to configure `codesettings.nvim` itself with local files.
2323

2424
- lazy.nvim (recommended)
2525

26+
<!-- GENERATED:CONFIG:START -->
27+
2628
```lua
2729
return {
2830
'mrjones2014/codesettings.nvim',
2931
-- these are the default settings just set `opts = {}` to use defaults
3032
opts = {
3133
---Look for these config files
32-
config_file_paths = { '.vscode/settings.json', 'codesettings.json', 'lspsettings.json' },
34+
config_file_paths = { ".vscode/settings.json", "codesettings.json", "lspsettings.json" },
35+
---Set filetype to jsonc when opening a file specified by `config_file_paths`,
36+
---make sure you have the json tree-sitter parser installed for highlighting
37+
jsonc_filetype = true,
3338
---Integrate with jsonls to provide LSP completion for LSP settings based on schemas
3439
jsonls_integration = true,
35-
---Set up library paths for lua_ls automatically to pick up the generated type
40+
---Enable live reloading of settings when config files change; for servers that support it,
41+
---this is done via the `workspace/didChangeConfiguration` notification, otherwise the
42+
---server is restarted
43+
live_reload = false,
44+
---List of loader extensions to use when loading settings; `string` values will be `require`d
45+
loader_extensions = {},
46+
---Set up library paths for `lua_ls` automatically to pick up the generated type
3647
---annotations provided by codesettings.nvim; to enable for only your nvim config,
3748
---you can also do something like:
3849
---lua_ls_integration = function()
3950
--- return vim.uv.cwd() == ('%s/.config/nvim'):format(vim.env.HOME)
4051
---end,
4152
---This integration also works for emmylua_ls
4253
lua_ls_integration = true,
43-
---Set filetype to jsonc when opening a file specified by `config_file_paths`,
44-
---make sure you have the json tree-sitter parser installed for highlighting
45-
jsonc_filetype = true,
46-
---Enable live reloading of settings when config files change; for servers that support it,
47-
---this is done via the `workspace/didChangeConfiguration` notification, otherwise the
48-
---server is restarted
49-
live_reload = false,
54+
---How to merge lists; 'append' (default), 'prepend' or 'replace'
55+
merge_lists = "append",
5056
---Provide your own root dir; can be a string or function returning a string.
5157
---It should be/return the full absolute path to the root directory.
5258
---If not set, defaults to `require('codesettings.util').get_root()`
5359
root_dir = nil,
54-
--- How to merge lists; 'append' (default), 'prepend' or 'replace'
55-
merge_lists = 'append',
5660
},
5761
-- I recommend loading on these filetype so that the
5862
-- jsonls integration, lua_ls integration, and jsonc filetype setup works
5963
ft = { 'json', 'jsonc', 'lua' },
6064
}
6165
```
6266

67+
<!-- GENERATED:CONFIG:END -->
68+
6369
`codesettings.nvim` can also be specified in the local JSON configuration files by using a top-level `codesettings`
6470
object key, and these _override the global plugin configuration._ For example:
6571

@@ -369,7 +375,7 @@ This project would not exist without the hard work of some other open source pro
369375

370376
## Supported LSP Servers
371377

372-
<!-- GENERATED -->
378+
<!-- GENERATED:SERVERS:START -->
373379

374380
- [x] [als](https://github.com/AdaCore/ada_language_server/tree/master/integration/vscode/ada/package.json)
375381
- [x] [angularls](https://github.com/angular/vscode-ng-language-service/refs/heads/tree/main/package.json)
@@ -444,3 +450,4 @@ This project would not exist without the hard work of some other open source pro
444450
- [x] [zeta_note](https://github.com/artempyanykh/zeta-note-vscode/tree/main/package.json)
445451
- [x] [zls](https://github.com/zigtools/zls-vscode/tree/master/package.json)
446452

453+
<!-- GENERATED:SERVERS:END -->

lua/codesettings/build/doc.lua

Lines changed: 113 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
local ConfigSchema = require('codesettings.config.schema')
12
local Schemas = require('codesettings.build.schemas')
23
local Util = require('codesettings.util')
34

45
local M = {}
56

6-
function M.build()
7-
if #arg == 0 then
8-
error('This function is part of a build tool and should not be called directly!')
9-
end
10-
print('Generating list of supported LSP servers in README.md...')
7+
---@class DocSection
8+
---@field start_marker string The start marker to find in the README (without <!-- -->)
9+
---@field end_marker string The end marker to find in the README (without <!-- -->)
10+
---@field generator fun(): string Function that generates the content for this section
11+
12+
---Generate the LSP servers list section
13+
---@return string
14+
local function generate_lsp_servers()
1115
local lines = {}
1216
local schemas = Schemas.get_schemas()
1317
local lsp_names = vim.tbl_keys(schemas)
@@ -24,10 +28,112 @@ function M.build()
2428
end
2529
table.insert(lines, ('- [x] [%s](%s)'):format(name, url))
2630
end
27-
local generated_doc = '<!-- GENERATED -->\n\n' .. table.concat(lines, '\n') .. '\n'
31+
return table.concat(lines, '\n')
32+
end
33+
34+
---Format a value for Lua code
35+
---@param value any
36+
---@return string
37+
local function format_value(value)
38+
if value == nil or value == vim.NIL then
39+
return 'nil'
40+
elseif type(value) == 'string' then
41+
return vim.inspect(value)
42+
elseif type(value) == 'table' and vim.tbl_isempty(value) then
43+
return '{}'
44+
else
45+
return vim.inspect(value)
46+
end
47+
end
48+
49+
---Generate the default config section
50+
---@return string
51+
local function generate_default_config()
52+
local lines = {}
53+
table.insert(lines, '```lua')
54+
table.insert(lines, 'return {')
55+
table.insert(lines, " 'mrjones2014/codesettings.nvim',")
56+
table.insert(lines, ' -- these are the default settings just set `opts = {}` to use defaults')
57+
table.insert(lines, ' opts = {')
58+
59+
-- Get sorted property names
60+
local props = vim.tbl_keys(ConfigSchema.properties)
61+
table.sort(props)
62+
63+
for _, name in ipairs(props) do
64+
local prop = ConfigSchema.properties[name]
65+
66+
-- Add description as comment
67+
if prop.description then
68+
-- Handle multi-line descriptions
69+
for line in prop.description:gmatch('[^\n]+') do
70+
table.insert(lines, ' ---' .. line)
71+
end
72+
end
73+
74+
-- Add the field with default value
75+
local default_val = prop.default
76+
if default_val == vim.NIL then
77+
default_val = nil
78+
end
79+
80+
local formatted_value = format_value(default_val)
81+
table.insert(lines, ' ' .. name .. ' = ' .. formatted_value .. ',')
82+
end
83+
84+
table.insert(lines, ' },')
85+
table.insert(lines, ' -- I recommend loading on these filetype so that the')
86+
table.insert(lines, ' -- jsonls integration, lua_ls integration, and jsonc filetype setup works')
87+
table.insert(lines, " ft = { 'json', 'jsonc', 'lua' },")
88+
table.insert(lines, '}')
89+
table.insert(lines, '```')
90+
91+
return table.concat(lines, '\n')
92+
end
93+
94+
---@type DocSection[]
95+
local sections = {
96+
{
97+
start_marker = 'GENERATED:CONFIG:START',
98+
end_marker = 'GENERATED:CONFIG:END',
99+
generator = generate_default_config,
100+
},
101+
{
102+
start_marker = 'GENERATED:SERVERS:START',
103+
end_marker = 'GENERATED:SERVERS:END',
104+
generator = generate_lsp_servers,
105+
},
106+
}
107+
108+
function M.build()
109+
if #arg == 0 then
110+
error('This function is part of a build tool and should not be called directly!')
111+
end
112+
113+
print('Generating documentation sections in README.md...')
28114
local readme = Util.read_file('README.md')
29-
readme = readme:gsub('<!%-%- GENERATED %-%->.*', generated_doc) .. '\n'
115+
116+
-- Process each section
117+
for _, section in ipairs(sections) do
118+
local content = section.generator()
119+
local start_pattern = '<!%-%- ' .. section.start_marker:gsub('%-', '%%-') .. ' %-%->'
120+
local end_pattern = '<!%-%- ' .. section.end_marker:gsub('%-', '%%-') .. ' %-%->'
121+
122+
-- Replace content between start and end markers
123+
local pattern = '(' .. start_pattern .. ').-(' .. end_pattern .. ')'
124+
local replacement = '%1\n\n' .. content .. '\n\n%2'
125+
126+
local new_readme, count = readme:gsub(pattern, replacement)
127+
if count > 0 then
128+
readme = new_readme
129+
print(' Generated section: ' .. section.start_marker:gsub(':START', ''))
130+
else
131+
print(' Warning: Markers not found: ' .. section.start_marker .. ' / ' .. section.end_marker)
132+
end
133+
end
134+
30135
Util.write_file('README.md', readme)
136+
print('Documentation generation complete!')
31137
end
32138

33139
return M

lua/codesettings/config/schema.lua

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,22 @@ M.properties = {
3333
config_file_paths = {
3434
type = 'array',
3535
items = { type = 'string', description = 'List of relative config file paths to look for' },
36-
description = 'List of config file paths to look for',
36+
description = 'Look for these config files',
3737
default = { '.vscode/settings.json', 'codesettings.json', 'lspsettings.json' },
3838
overridable = true,
3939
},
4040
merge_lists = {
4141
type = 'CodesettingsMergeListsBehavior',
42-
description = 'How to merge list fields when combining settings from multiple sources',
42+
description = [[How to merge lists; 'append' (default), 'prepend' or 'replace']],
4343
enum = { 'replace', 'append', 'prepend' },
4444
default = 'append',
4545
overridable = true,
4646
},
4747
root_dir = {
4848
type = { 'string', { args = {}, ret = 'string' }, 'null' },
49-
description = "Function or string to determine the project root directory; defaults to `require('codesettings.util').get_root()`",
49+
description = [[Provide your own root dir; can be a string or function returning a string.
50+
It should be/return the full absolute path to the root directory.
51+
If not set, defaults to `require('codesettings.util').get_root()`]],
5052
default = vim.NIL,
5153
overridable = true,
5254
},
@@ -62,22 +64,31 @@ M.properties = {
6264
},
6365
jsonls_integration = {
6466
type = 'boolean',
65-
description = 'Integrate with jsonls for LSP settings completion',
67+
description = 'Integrate with jsonls to provide LSP completion for LSP settings based on schemas',
6668
default = true,
6769
},
6870
lua_ls_integration = {
6971
type = { 'boolean', { args = {}, ret = 'boolean' } },
70-
description = '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',
72+
description = [[Set up library paths for `lua_ls` automatically to pick up the generated type
73+
annotations provided by codesettings.nvim; to enable for only your nvim config,
74+
you can also do something like:
75+
lua_ls_integration = function()
76+
return vim.uv.cwd() == ('%%s/.config/nvim'):format(vim.env.HOME)
77+
end,
78+
This integration also works for emmylua_ls]],
7179
default = true,
7280
},
7381
jsonc_filetype = {
7482
type = 'boolean',
75-
description = 'Set filetype to jsonc for config files',
83+
description = [[Set filetype to jsonc when opening a file specified by `config_file_paths`,
84+
make sure you have the json tree-sitter parser installed for highlighting]],
7685
default = true,
7786
},
7887
live_reload = {
7988
type = 'boolean',
80-
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',
89+
description = [[Enable live reloading of settings when config files change; for servers that support it,
90+
this is done via the `workspace/didChangeConfiguration` notification, otherwise the
91+
server is restarted]],
8192
default = false,
8293
},
8394
}

lua/codesettings/generated/codesettings-config-schema.lua

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
---Options which can be passed on a per-load basis (i.e. can override global config)
1010
---@class CodesettingsOverridableConfig
11-
---List of config file paths to look for
11+
---Look for these config files
1212
---
1313
---```lua
1414
---default = { ".vscode/settings.json", "codesettings.json", "lspsettings.json" }
@@ -20,13 +20,15 @@
2020
---default = {}
2121
---```
2222
---@field loader_extensions (string|CodesettingsLoaderExtension)[]?
23-
---How to merge list fields when combining settings from multiple sources
23+
---How to merge lists; 'append' (default), 'prepend' or 'replace'
2424
---
2525
---```lua
2626
---default = "append"
2727
---```
2828
---@field merge_lists CodesettingsMergeListsBehavior?
29-
---Function or string to determine the project root directory; defaults to `require('codesettings.util').get_root()`
29+
---Provide your own root dir; can be a string or function returning a string.
30+
---It should be/return the full absolute path to the root directory.
31+
---If not set, defaults to `require('codesettings.util').get_root()`
3032
---
3133
---```lua
3234
---default = nil
@@ -35,21 +37,30 @@
3537

3638
---Main configuration class
3739
---@class CodesettingsConfig: CodesettingsOverridableConfig
38-
---Set filetype to jsonc for config files
40+
---Set filetype to jsonc when opening a file specified by `config_file_paths`,
41+
---make sure you have the json tree-sitter parser installed for highlighting
3942
---
4043
---```lua
4144
---default = true
4245
---```
4346
---@field jsonc_filetype boolean
44-
---Integrate with jsonls for LSP settings completion
47+
---Integrate with jsonls to provide LSP completion for LSP settings based on schemas
4548
---
4649
---```lua
4750
---default = true
4851
---```
4952
---@field jsonls_integration boolean
50-
---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
53+
---Enable live reloading of settings when config files change; for servers that support it,
54+
---this is done via the `workspace/didChangeConfiguration` notification, otherwise the
55+
---server is restarted
5156
---@field live_reload boolean
52-
---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
57+
---Set up library paths for `lua_ls` automatically to pick up the generated type
58+
---annotations provided by codesettings.nvim; to enable for only your nvim config,
59+
---you can also do something like:
60+
---lua_ls_integration = function()
61+
--- return vim.uv.cwd() == ('%%s/.config/nvim'):format(vim.env.HOME)
62+
---end,
63+
---This integration also works for emmylua_ls
5364
---
5465
---```lua
5566
---default = true
@@ -62,7 +73,7 @@
6273
---Each overridable config property gets a corresponding setter method.
6374
---@class CodesettingsConfigBuilder
6475
---@field private _config CodesettingsOverridableConfig
65-
---List of config file paths to look for
76+
---Look for these config files
6677
---
6778
---```lua
6879
---default = { ".vscode/settings.json", "codesettings.json", "lspsettings.json" }
@@ -74,13 +85,15 @@
7485
---default = {}
7586
---```
7687
---@field loader_extensions fun(self: CodesettingsConfigBuilder, value: (string|CodesettingsLoaderExtension)[]): CodesettingsConfigBuilder
77-
---How to merge list fields when combining settings from multiple sources
88+
---How to merge lists; 'append' (default), 'prepend' or 'replace'
7889
---
7990
---```lua
8091
---default = "append"
8192
---```
8293
---@field merge_lists fun(self: CodesettingsConfigBuilder, value: CodesettingsMergeListsBehavior): CodesettingsConfigBuilder
83-
---Function or string to determine the project root directory; defaults to `require('codesettings.util').get_root()`
94+
---Provide your own root dir; can be a string or function returning a string.
95+
---It should be/return the full absolute path to the root directory.
96+
---If not set, defaults to `require('codesettings.util').get_root()`
8497
---
8598
---```lua
8699
---default = nil

0 commit comments

Comments
 (0)