-
-
Notifications
You must be signed in to change notification settings - Fork 43
feat!: lazy auto setup #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1993,35 +1993,26 @@ local function setup_neoconf(config) | |
end | ||
|
||
---@param schema table<string,SchemaElement>|SchemaElement[] | ||
---@param user_config table<string,any> | ||
---@return table | ||
local function build_config(schema, user_config) | ||
local function build_config(schema) | ||
---@type table<string,any> | ||
local config = {} | ||
|
||
for _, elem in ipairs(schema) do | ||
local key = elem.name | ||
local user_value = user_config[key] | ||
local value_type = type(user_value) | ||
|
||
if elem.type.config_type == "section" then | ||
if value_type == "table" then | ||
config[key] = build_config(elem.fields, user_value) | ||
else | ||
config[key] = build_config(elem.fields, {}) | ||
end | ||
config[key] = build_config(elem.fields) | ||
else | ||
if matches_type(value_type, elem.type) then | ||
config[key] = user_value | ||
else | ||
config[key] = elem.default | ||
end | ||
config[key] = elem.default | ||
end | ||
end | ||
|
||
return config | ||
end | ||
|
||
local current_config = build_config(M.schema) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Build the config once to be able to just |
||
|
||
---comment | ||
---@param user_config table<string,any>? | ||
---@return Config | ||
|
@@ -2035,11 +2026,11 @@ function M.build(user_config) | |
|
||
handle_deprecated({}, M.schema, user_config, user_config) | ||
validate_schema({}, M.schema, user_config) | ||
local config = build_config(M.schema, user_config) | ||
if config.neoconf.enabled then | ||
return setup_neoconf(config) | ||
current_config = vim.tbl_deep_extend("force", current_config, user_config) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will fail in some cases. For example if a scalar is provided by the user instead of a table, the config will now be invalid, because the default table will be overwritten. Also unless I'm forgetting something the user config was never mutated to begin with, or did you hit some case where calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
A few lines above it ensures that the
The reason I changed it was because if calling setup once with for example There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I was talking about a nested table so for example
Oh, I see. Maybe we could just extend the previous config before passing it into the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hmm good point. It should still use the default value in this case like the warning is saying that it does? Another option (maybe a bit hard) would be to just error instead of giving a warning
Yeah I was thinking about that as well, but that would give a lot of warning about the user using deprecated options etc since the user_config is now the full config instead of only what the user provided🤔 |
||
if current_config.neoconf.enabled then | ||
return setup_neoconf(current_config) | ||
else | ||
return config | ||
return current_config | ||
end | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
local actions = require("crates.actions") | ||
local async = require("crates.async") | ||
local command = require("crates.command") | ||
local config = require("crates.config") | ||
local core = require("crates.core") | ||
local highlight = require("crates.highlight") | ||
local popup = require("crates.popup") | ||
local state = require("crates.state") | ||
local util = require("crates.util") | ||
|
||
local function attach() | ||
if state.cfg.completion.cmp.enabled then | ||
|
@@ -18,15 +14,12 @@ local function attach() | |
end | ||
|
||
core.update() | ||
state.cfg.on_attach(util.current_buf()) | ||
state.cfg.on_attach(require("crates.util").current_buf()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lazy require the |
||
end | ||
|
||
---@param cfg crates.UserConfig | ||
local function setup(cfg) | ||
state.cfg = config.build(cfg) | ||
|
||
command.register() | ||
highlight.define() | ||
state.cfg = require("crates.config").build(cfg) | ||
|
||
---@type integer | ||
local group = vim.api.nvim_create_augroup("Crates", {}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require("crates.command").register() | ||
require("crates.highlight").define() | ||
|
||
vim.api.nvim_create_autocmd("BufRead", { | ||
pattern = "Cargo.toml", | ||
once = true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Create an autocmd that is fired once upon the first entry in a |
||
callback = function() | ||
require("crates").setup({}) | ||
end, | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inline these wrapped around a function to lazily require the modules😅 This seems to be improve the setup time from around 3ms to 0.5 ish for me