Skip to content

Commit 9bf418a

Browse files
committed
feat!: lazy auto setup
1 parent ac9fa49 commit 9bf418a

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

lua/crates/config/init.lua

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,35 +1993,26 @@ local function setup_neoconf(config)
19931993
end
19941994

19951995
---@param schema table<string,SchemaElement>|SchemaElement[]
1996-
---@param user_config table<string,any>
19971996
---@return table
1998-
local function build_config(schema, user_config)
1997+
local function build_config(schema)
19991998
---@type table<string,any>
20001999
local config = {}
20012000

20022001
for _, elem in ipairs(schema) do
20032002
local key = elem.name
2004-
local user_value = user_config[key]
2005-
local value_type = type(user_value)
20062003

20072004
if elem.type.config_type == "section" then
2008-
if value_type == "table" then
2009-
config[key] = build_config(elem.fields, user_value)
2010-
else
2011-
config[key] = build_config(elem.fields, {})
2012-
end
2005+
config[key] = build_config(elem.fields)
20132006
else
2014-
if matches_type(value_type, elem.type) then
2015-
config[key] = user_value
2016-
else
2017-
config[key] = elem.default
2018-
end
2007+
config[key] = elem.default
20192008
end
20202009
end
20212010

20222011
return config
20232012
end
20242013

2014+
local current_config = build_config(M.schema)
2015+
20252016
---comment
20262017
---@param user_config table<string,any>?
20272018
---@return Config
@@ -2035,11 +2026,11 @@ function M.build(user_config)
20352026

20362027
handle_deprecated({}, M.schema, user_config, user_config)
20372028
validate_schema({}, M.schema, user_config)
2038-
local config = build_config(M.schema, user_config)
2039-
if config.neoconf.enabled then
2040-
return setup_neoconf(config)
2029+
current_config = vim.tbl_deep_extend("force", current_config, user_config)
2030+
if current_config.neoconf.enabled then
2031+
return setup_neoconf(current_config)
20412032
else
2042-
return config
2033+
return current_config
20432034
end
20442035
end
20452036

lua/crates/init.lua

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
local actions = require("crates.actions")
22
local async = require("crates.async")
3-
local command = require("crates.command")
4-
local config = require("crates.config")
53
local core = require("crates.core")
6-
local highlight = require("crates.highlight")
74
local popup = require("crates.popup")
85
local state = require("crates.state")
9-
local util = require("crates.util")
106

117
local function attach()
128
if state.cfg.completion.cmp.enabled then
@@ -18,15 +14,12 @@ local function attach()
1814
end
1915

2016
core.update()
21-
state.cfg.on_attach(util.current_buf())
17+
state.cfg.on_attach(require("crates.util").current_buf())
2218
end
2319

2420
---@param cfg crates.UserConfig
2521
local function setup(cfg)
26-
state.cfg = config.build(cfg)
27-
28-
command.register()
29-
highlight.define()
22+
state.cfg = require("crates.config").build(cfg)
3023

3124
---@type integer
3225
local group = vim.api.nvim_create_augroup("Crates", {})

plugin/crates.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require("crates.command").register()
2+
require("crates.highlight").define()
3+
4+
vim.api.nvim_create_autocmd("BufRead", {
5+
pattern = "Cargo.toml",
6+
once = true,
7+
callback = function()
8+
require("crates").setup({})
9+
end,
10+
})

0 commit comments

Comments
 (0)