From 218c5be07ebba96f3039180857ccef5a5d2c4d6a Mon Sep 17 00:00:00 2001 From: hjalmar jakobsson Date: Mon, 16 Jan 2023 10:45:03 -0500 Subject: [PATCH 1/9] add traverser and utils/modules --- lua/doom/services/traverser.lua | 75 ++++++++++++++++++++++++++++++++ lua/doom/utils/modules.lua | 76 +++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 lua/doom/services/traverser.lua create mode 100644 lua/doom/utils/modules.lua diff --git a/lua/doom/services/traverser.lua b/lua/doom/services/traverser.lua new file mode 100644 index 00000000..b544f9a6 --- /dev/null +++ b/lua/doom/services/traverser.lua @@ -0,0 +1,75 @@ +-- +-- TRAVERSER +-- + +-- TODO: More documentation + +-- Default debugger print +local default_debug_node = function(node, stack) + local parent = stack[#stack] + local indent_str = string.rep("--", #stack) + local indent_cap = type(node) == "table" and "+" or ">" + print( + ("default: %s%s %s"):format( + indent_str, + indent_cap, + type(node) == "table" and parent.key or node + ) + ) +end + +-- Default debug levels +local default_log_levels = + { debug = doom.settings.logging == "trace" or doom.settings.logging == "debug" } + +local tree_traverser = { + build = function(builder_opts) + local traverser = builder_opts.traverser + local debug_node = builder_opts.debug_node or default_debug_node + local stack = {} + local result = {} + + -- Traverse out, pops from stack, adds to result + local traverse_out = function() + table.remove(stack, #stack) + end + + -- Error does not add to result or anything + local err = function(message) + table.remove(stack, #stack) + local path = vim.tbl_map(function(stack_node) + return "[" .. vim.inspect(stack_node.key) .. "]" + end, stack) + print(("%s\n Occursed at key `%s`."):format(message, table.concat(path, ""))) + table.remove(result, #result) + end + + local traverse_in + traverse_in = function(key, node) + table.insert(stack, { key = key, node = node }) + table.insert(result, { node = node, stack = vim.deepcopy(stack) }) + traverser(node, stack, traverse_in, traverse_out, err) + end + + return function(tree, handler, opts) + result = {} -- Reset result + if opts == nil then + opts = default_log_levels + end + + traverser(tree, stack, traverse_in, traverse_out, err) + + if opts.debug and debug_node then + for _, value in ipairs(result) do + debug_node(value.node, value.stack) + end + end + + for _, value in ipairs(result) do + handler(value.node, value.stack) + end + end + end, +} + +return tree_traverser diff --git a/lua/doom/utils/modules.lua b/lua/doom/utils/modules.lua new file mode 100644 index 00000000..e41e80df --- /dev/null +++ b/lua/doom/utils/modules.lua @@ -0,0 +1,76 @@ +local traverser = require("doom.services.traverser") + +-- +-- This file hosts some common recurring traversers in doom. +-- + +local M = {} + +-- Designed to travers `modules.lua` file +M.traverse_enabled = traverser.build({ + -- Builds the traversal function defining how we should move through the tree + -- @param node any The node itself + -- @param next function(node: any) Traverse into the traverse_in node, adding the node to the stack + -- @param traverse_out function() Traverses back a depth, pops the value from the stack + -- @param err function(message: string) Traverses back a depth, this value is skipped by the handler (see below) + traverser = function(node, stack, traverse_in, traverse_out, err) + local parent = stack[#stack] + local parent_is_section = ( + parent == nil or (type(parent.key) == "string" and type(parent.node) == "table") + ) + if type(node) == "table" and parent_is_section then + if vim.tbl_count(node) == 0 then + traverse_out() -- Handle case if a table is empty. + else + for key, value in pairs(node) do + traverse_in(key, value) -- Traverse into next layer. + end + traverse_out() -- Travel back up when a sub table has been completed. + end + elseif type(node) == "string" and not parent_is_section then + traverse_out() -- This is a leaf, traverse back a layer. + else + err( + ("doom-nvim: Error traversing doom modules in `modules.lua`, unexpected value `%s`."):format( + vim.inspect(node) + ) + ) -- Traverse back a layer but do not pass this value to the handler function. + end + end, + -- -- Optional debugging function that can be used to + -- debug_node = function(node, stack) + -- local parent = stack[#stack] + -- local indent_str = string.rep("--", #stack) + -- local indent_cap = type(node) == "table" and "+" or ">" + -- print(("%s%s %s"):format(indent_str, indent_cap, type(node) == "table" and parent.key or node)) + -- end, +}) + +M.traverse_loaded = traverser.build({ + traverser = function(node, stack, traverse_in, traverse_out, err) + if node.type == "doom_module_single" then + traverse_out() + else + for key, value in pairs(node) do + traverse_in(key, value) -- Traverse into next layer. + end + traverse_out() -- Travel back up when a sub table has been completed. + end + -- else + -- err( + -- ("doom-nvim: Error traversing `doom.modules`, unexpected value `%s`."):format( + -- vim.inspect(node) + -- ) + -- ) -- Traverse back a layer but do not pass this value to the handler function. + -- end + end, + -- Optional debugging function that can be used to + -- debug_node = function(node, stack) + -- local parent = stack[#stack] + -- local indent_str = string.rep("--", #stack) + -- local indent_cap = type(node) == "table" and "+" or ">" + -- print(("%s%s %s"):format(indent_str, indent_cap, type(node) == "table" and parent.key or node)) + -- end, +}) + +return M From d4859b71da97a22f2c26dbec3e06f38e8df31a9e Mon Sep 17 00:00:00 2001 From: hjalmar jakobsson Date: Mon, 16 Jan 2023 10:51:03 -0500 Subject: [PATCH 2/9] get set table path util --- lua/doom/utils/init.lua | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/lua/doom/utils/init.lua b/lua/doom/utils/init.lua index 13dcf2b2..ee9a769c 100644 --- a/lua/doom/utils/init.lua +++ b/lua/doom/utils/init.lua @@ -231,5 +231,45 @@ utils.left_pad = function(str, length, char) return res, res ~= str end +-- Get or Set a table path list. +-- +-- Used with recursive module structures so that you can check if eg. a deep +-- path exists or if you want to create/set data to a deep path. +-- +-- if no data supplies -> returns table path node or false if not exists +-- +---@param head table The table to which you want target +---@param tp table Path that you wish to check in head +---@param data any If supplied, attaches this data to tp tip, eg. `{"a", "b"} -> b = data` +utils.get_set_table_path = function(head, tp, data) + if not head or not tp then + return false + end + local last = #tp + for i, p in ipairs(tp) do + if i ~= last then + if head[p] == nil then + if not data then + -- if a nil occurs, this means the path does no exist >> return + return false + end + head[p] = {} + end + head = head[p] + else + if data then + if type(data) == "function" then + data(head[p]) + else + head[p] = data + end + else + -- print(vim.inspect(head), p) + return head[p] + end + end + end +end + return utils From 87bb81022296dafd3b9a7e8342d4adbbe164673f Mon Sep 17 00:00:00 2001 From: hjalmar jakobsson Date: Mon, 16 Jan 2023 10:54:39 -0500 Subject: [PATCH 3/9] traverser remove since it doesnt exist --- lua/doom/services/traverser.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/doom/services/traverser.lua b/lua/doom/services/traverser.lua index b544f9a6..2f04cc86 100644 --- a/lua/doom/services/traverser.lua +++ b/lua/doom/services/traverser.lua @@ -20,7 +20,7 @@ end -- Default debug levels local default_log_levels = - { debug = doom.settings.logging == "trace" or doom.settings.logging == "debug" } + { debug = doom.logging == "trace" or doom.logging == "debug" } local tree_traverser = { build = function(builder_opts) From f6b37e7748e24cf14f9db3acb4b4adbf3f83ad59 Mon Sep 17 00:00:00 2001 From: hjalmar jakobsson Date: Mon, 16 Jan 2023 10:55:00 -0500 Subject: [PATCH 4/9] replaces core/config with traverser --- lua/doom/core/config.lua | 115 +++++++++++++++++++++++++++++---------- 1 file changed, 85 insertions(+), 30 deletions(-) diff --git a/lua/doom/core/config.lua b/lua/doom/core/config.lua index 71af2646..61042c5a 100644 --- a/lua/doom/core/config.lua +++ b/lua/doom/core/config.lua @@ -63,40 +63,95 @@ config.load = function() local enabled_modules = require("doom.core.modules").enabled_modules profiler.start("framework|import modules") - -- Iterate over each module and save it to the doom global object - for section_name, section_modules in pairs(enabled_modules) do - for _, module_name in pairs(section_modules) do - -- If the section is `user` resolves from `lua/user/modules` - local profiler_message = ("modules|import `%s.%s`"):format(section_name, module_name) - profiler.start(profiler_message) - local search_paths = { - ("user.modules.%s.%s"):format(section_name, module_name), - ("doom.modules.%s.%s"):format(section_name, module_name), - } - - local ok, result - for _, path in ipairs(search_paths) do - ok, result = xpcall(require, debug.traceback, path) - if ok then - break + + -- -- Iterate over each module and save it to the doom global object + -- for section_name, section_modules in pairs(enabled_modules) do + -- for _, module_name in pairs(section_modules) do + -- -- If the section is `user` resolves from `lua/user/modules` + -- local profiler_message = ("modules|import `%s.%s`"):format(section_name, module_name) + -- profiler.start(profiler_message) + -- local search_paths = { + -- ("user.modules.%s.%s"):format(section_name, module_name), + -- ("doom.modules.%s.%s"):format(section_name, module_name), + -- } + -- + -- local ok, result + -- for _, path in ipairs(search_paths) do + -- ok, result = xpcall(require, debug.traceback, path) + -- if ok then + -- break + -- end + -- end + -- if ok then + -- doom[section_name][module_name] = result + -- else + -- local log = require("doom.utils.logging") + -- log.error( + -- string.format( + -- "There was an error loading module '%s.%s'. Traceback:\n%s", + -- section_name, + -- module_name, + -- result + -- ) + -- ) + -- end + -- profiler.stop(profiler_message) + -- end + -- end + + + -- Combine enabled modules (`modules.lua`) with core modules. + require("doom.utils.modules").traverse_enabled( + enabled_modules, + function(node, stack) + if type(node) == "string" then + local t_path = vim.tbl_map(function(stack_node) + return type(stack_node.key) == "string" and stack_node.key or stack_node.node + end, stack) + + local path_module = table.concat(t_path, ".") + + local profiler_message = ("modules|import `%s`"):format(path_module) + profiler.start(profiler_message) + + -- If the section is `user` resolves from `lua/user/modules` + local search_paths = { + ("user.modules.%s"):format(path_module), + ("doom.modules.%s"):format(path_module), + } + + local ok, result + for _, path in ipairs(search_paths) do + ok, result = xpcall(require, debug.traceback, path) + if ok then + break + end end - end - if ok then - doom[section_name][module_name] = result - else - local log = require("doom.utils.logging") - log.error( - string.format( - "There was an error loading module '%s.%s'. Traceback:\n%s", - section_name, - module_name, - result + + if ok then + -- Add string tag so that we can easilly target modules with more + -- traversers, ie. in `core/modules` when traversing `doom.modules` + result.type = "doom_module_single" + utils.get_set_table_path(doom.modules, t_path, result) + else + local log = require("doom.utils.logging") + log.error( + string.format( + "There was an error loading module '%s'. Traceback:\n%s", + path_module, + result + ) ) - ) + end + + profiler.stop(profiler_message) end - profiler.stop(profiler_message) end - end + , { debug = doom.logging == "trace" or doom.logging == "debug" } + ) + + + profiler.stop("framework|import modules") profiler.start("framework|config.lua (user)") From 2dd3cd399d3040dff1b2415149bd68e7c81934f6 Mon Sep 17 00:00:00 2001 From: hjalmar jakobsson Date: Mon, 16 Jan 2023 10:57:30 -0500 Subject: [PATCH 5/9] replaces core/modules with traverser --- lua/doom/core/modules.lua | 121 ++++++++++++++++++++++++++++++++------ 1 file changed, 103 insertions(+), 18 deletions(-) diff --git a/lua/doom/core/modules.lua b/lua/doom/core/modules.lua index 0bb24e68..38722c58 100644 --- a/lua/doom/core/modules.lua +++ b/lua/doom/core/modules.lua @@ -38,13 +38,99 @@ local autocmds_service = require("doom.services.autocommands") --- Applies commands, autocommands, packages from enabled modules (`modules.lua`). modules.load_modules = function() local logger = require("doom.utils.logging") + + -- -- Handle the Modules + -- for section_name, _ in pairs(doom.modules) do + -- for module_name, module in pairs(doom.modules[section_name]) do + -- if type(module) ~= "table" then + -- print(("Error on module %s type is %s val is %s"):format(module_name, type(module), module)) + -- end + -- local profile_msg = ("modules|init `%s.%s`"):format(section_name, module_name) + -- profiler.start(profile_msg) + -- + -- -- Flag to continue enabling module + -- local should_enable_module = true + -- + -- -- Check module has necessary dependencies + -- if module.requires_modules then + -- for _, dependent_module in ipairs(module.requires_modules) do + -- local dep_section_name, dep_module_name = unpack(vim.split(dependent_module, "%.")) + -- + -- if not doom.modules[dep_section_name][dep_module_name] then + -- should_enable_module = false + -- logger.error( + -- ('Doom module "%s.%s" depends on a module that is not enabled "%s.%s". Please enable the %s module.'):format( + -- section_name, + -- module_name, + -- dep_section_name, + -- dep_module_name, + -- dep_module_name + -- ) + -- ) + -- end + -- end + -- end + -- + -- if should_enable_module then + -- -- Import dependencies with packer from module.packages + -- if module.packages then + -- for dependency_name, packer_spec in pairs(module.packages) do + -- -- Set packer_spec to configure function + -- if module.configs and module.configs[dependency_name] then + -- packer_spec.config = module.configs[dependency_name] + -- end + -- + -- local spec = vim.deepcopy(packer_spec) + -- + -- -- Set/unset frozen packer dependencies + -- if type(spec.commit) == "table" then + -- -- Commit can be a table of values, where the keys indicate + -- -- which neovim version is required. + -- spec.commit = utils.pick_compatible_field(spec.commit) + -- end + -- + -- -- Only pin dependencies if doom.freeze_dependencies is true + -- spec.lock = spec.commit and doom.freeze_dependencies + -- + -- -- Save module spec to be initialised later + -- table.insert(doom.packages, spec) + -- end + -- end + -- + -- -- Setup package autogroups + -- if module.autocmds then + -- local autocmds = type(module.autocmds) == "function" and module.autocmds() + -- or module.autocmds + -- for _, autocmd_spec in ipairs(autocmds) do + -- autocmds_service.set(autocmd_spec[1], autocmd_spec[2], autocmd_spec[3], autocmd_spec) + -- end + -- end + -- + -- if module.cmds then + -- for _, cmd_spec in ipairs(module.cmds) do + -- commands_service.set(cmd_spec[1], cmd_spec[2], cmd_spec[3] or cmd_spec.opts) + -- end + -- end + -- + -- if module.binds then + -- keymaps_service.applyKeymaps( + -- type(module.binds) == "function" and module.binds() or module.binds + -- ) + -- end + -- end + -- profiler.stop(profile_msg) + -- end + -- end + -- Handle the Modules - for section_name, _ in pairs(doom.modules) do - for module_name, module in pairs(doom.modules[section_name]) do - if type(module) ~= "table" then - print(("Error on module %s type is %s val is %s"):format(module_name, type(module), module)) - end - local profile_msg = ("modules|init `%s.%s`"):format(section_name, module_name) + require("doom.utils.modules").traverse_loaded(doom.modules, function(node, stack) + if node.type then + local module = node + local t_path = vim.tbl_map(function(stack_node) + return type(stack_node.key) == "string" and stack_node.key + end, stack) + local path_module = table.concat(t_path, ".") + local profile_msg = ("modules|init `%s`"):format(path_module) profiler.start(profile_msg) -- Flag to continue enabling module @@ -53,17 +139,13 @@ modules.load_modules = function() -- Check module has necessary dependencies if module.requires_modules then for _, dependent_module in ipairs(module.requires_modules) do - local dep_section_name, dep_module_name = unpack(vim.split(dependent_module, "%.")) - - if not doom.modules[dep_section_name][dep_module_name] then + if not utils.get_set_table_path(doom.modules, vim.split(dependent_module, "%.")) then should_enable_module = false logger.error( - ('Doom module "%s.%s" depends on a module that is not enabled "%s.%s". Please enable the %s module.'):format( - section_name, - module_name, - dep_section_name, - dep_module_name, - dep_module_name + ('Doom module "%s" depends on a module that is not enabled "%s". Please enable the %s module.'):format( + path_module, + dependent_module, + dependent_module ) ) end @@ -88,8 +170,9 @@ modules.load_modules = function() spec.commit = utils.pick_compatible_field(spec.commit) end - -- Only pin dependencies if doom.freeze_dependencies is true - spec.lock = spec.commit and doom.freeze_dependencies + if not doom.freeze_dependencies then + spec.commit = nil + end -- Save module spec to be initialised later table.insert(doom.packages, spec) @@ -117,9 +200,11 @@ modules.load_modules = function() ) end end + profiler.stop(profile_msg) end - end + end, { debug = doom.logging == "trace" or doom.logging == "debug" }) + end --- Applies user's commands, autocommands, packages from `use_*` helper functions. From 4e3084a645090d8e741cd093f8b65196980ea6c6 Mon Sep 17 00:00:00 2001 From: hjalmar jakobsson Date: Mon, 16 Jan 2023 10:59:03 -0500 Subject: [PATCH 6/9] replaces whichkey module with traverser --- lua/doom/modules/features/whichkey/init.lua | 46 ++++++++++++++++----- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/lua/doom/modules/features/whichkey/init.lua b/lua/doom/modules/features/whichkey/init.lua index 4c0db98f..2f54c805 100644 --- a/lua/doom/modules/features/whichkey/init.lua +++ b/lua/doom/modules/features/whichkey/init.lua @@ -121,18 +121,44 @@ whichkey.configs["which-key.nvim"] = function() local keymaps_service = require("doom.services.keymaps") local whichkey_integration = get_whichkey_integration() - for section_name, _ in pairs(doom.modules) do - for _, module in pairs(doom[section_name]) do - if module and module.binds then - -- table.insert(all_keymaps, type(module.binds) == "function" and module.binds() or module.binds) - keymaps_service.applyKeymaps( - type(module.binds) == "function" and module.binds() or module.binds, - nil, - { whichkey_integration } - ) + -- for section_name, _ in pairs(doom.modules) do + -- for _, module in pairs(doom[section_name]) do + -- if module and module.binds then + -- -- table.insert(all_keymaps, type(module.binds) == "function" and module.binds() or module.binds) + -- keymaps_service.applyKeymaps( + -- type(module.binds) == "function" and module.binds() or module.binds, + -- nil, + -- { whichkey_integration } + -- ) + -- end + -- end + -- end + + require("doom.utils.modules").traverse_loaded( + doom.modules, + function(node, stack) + if node.type then + local module = node + if module.binds then + -- count = count + 1 + -- vim.defer_fn(function() + -- keymaps_service.applyKeymaps( + -- type(module.binds) == "function" and module.binds() or module.binds, + -- nil, + -- { whichkey_integration } + -- ) + -- end, count) + keymaps_service.applyKeymaps( + type(module.binds) == "function" and module.binds() or module.binds, + nil, + { whichkey_integration } + ) + end end end - end + --, { debug = doom.settings.logging == "trace" or doom.settings.logging == "debug" } + ) + -- Add user keymaps to whichkey user keymaps if doom.binds and #doom.binds >= 1 then From ede599370ca95ed650b9d2fd5852d4170f2a0524 Mon Sep 17 00:00:00 2001 From: hjalmar jakobsson Date: Mon, 16 Jan 2023 11:00:50 -0500 Subject: [PATCH 7/9] clean up previous loop versions --- lua/doom/core/config.lua | 36 --------- lua/doom/core/modules.lua | 83 --------------------- lua/doom/modules/features/whichkey/init.lua | 20 ----- 3 files changed, 139 deletions(-) diff --git a/lua/doom/core/config.lua b/lua/doom/core/config.lua index 61042c5a..7d5ce16d 100644 --- a/lua/doom/core/config.lua +++ b/lua/doom/core/config.lua @@ -64,42 +64,6 @@ config.load = function() profiler.start("framework|import modules") - -- -- Iterate over each module and save it to the doom global object - -- for section_name, section_modules in pairs(enabled_modules) do - -- for _, module_name in pairs(section_modules) do - -- -- If the section is `user` resolves from `lua/user/modules` - -- local profiler_message = ("modules|import `%s.%s`"):format(section_name, module_name) - -- profiler.start(profiler_message) - -- local search_paths = { - -- ("user.modules.%s.%s"):format(section_name, module_name), - -- ("doom.modules.%s.%s"):format(section_name, module_name), - -- } - -- - -- local ok, result - -- for _, path in ipairs(search_paths) do - -- ok, result = xpcall(require, debug.traceback, path) - -- if ok then - -- break - -- end - -- end - -- if ok then - -- doom[section_name][module_name] = result - -- else - -- local log = require("doom.utils.logging") - -- log.error( - -- string.format( - -- "There was an error loading module '%s.%s'. Traceback:\n%s", - -- section_name, - -- module_name, - -- result - -- ) - -- ) - -- end - -- profiler.stop(profiler_message) - -- end - -- end - - -- Combine enabled modules (`modules.lua`) with core modules. require("doom.utils.modules").traverse_enabled( enabled_modules, diff --git a/lua/doom/core/modules.lua b/lua/doom/core/modules.lua index 38722c58..f3b4dcaf 100644 --- a/lua/doom/core/modules.lua +++ b/lua/doom/core/modules.lua @@ -39,89 +39,6 @@ local autocmds_service = require("doom.services.autocommands") modules.load_modules = function() local logger = require("doom.utils.logging") - -- -- Handle the Modules - -- for section_name, _ in pairs(doom.modules) do - -- for module_name, module in pairs(doom.modules[section_name]) do - -- if type(module) ~= "table" then - -- print(("Error on module %s type is %s val is %s"):format(module_name, type(module), module)) - -- end - -- local profile_msg = ("modules|init `%s.%s`"):format(section_name, module_name) - -- profiler.start(profile_msg) - -- - -- -- Flag to continue enabling module - -- local should_enable_module = true - -- - -- -- Check module has necessary dependencies - -- if module.requires_modules then - -- for _, dependent_module in ipairs(module.requires_modules) do - -- local dep_section_name, dep_module_name = unpack(vim.split(dependent_module, "%.")) - -- - -- if not doom.modules[dep_section_name][dep_module_name] then - -- should_enable_module = false - -- logger.error( - -- ('Doom module "%s.%s" depends on a module that is not enabled "%s.%s". Please enable the %s module.'):format( - -- section_name, - -- module_name, - -- dep_section_name, - -- dep_module_name, - -- dep_module_name - -- ) - -- ) - -- end - -- end - -- end - -- - -- if should_enable_module then - -- -- Import dependencies with packer from module.packages - -- if module.packages then - -- for dependency_name, packer_spec in pairs(module.packages) do - -- -- Set packer_spec to configure function - -- if module.configs and module.configs[dependency_name] then - -- packer_spec.config = module.configs[dependency_name] - -- end - -- - -- local spec = vim.deepcopy(packer_spec) - -- - -- -- Set/unset frozen packer dependencies - -- if type(spec.commit) == "table" then - -- -- Commit can be a table of values, where the keys indicate - -- -- which neovim version is required. - -- spec.commit = utils.pick_compatible_field(spec.commit) - -- end - -- - -- -- Only pin dependencies if doom.freeze_dependencies is true - -- spec.lock = spec.commit and doom.freeze_dependencies - -- - -- -- Save module spec to be initialised later - -- table.insert(doom.packages, spec) - -- end - -- end - -- - -- -- Setup package autogroups - -- if module.autocmds then - -- local autocmds = type(module.autocmds) == "function" and module.autocmds() - -- or module.autocmds - -- for _, autocmd_spec in ipairs(autocmds) do - -- autocmds_service.set(autocmd_spec[1], autocmd_spec[2], autocmd_spec[3], autocmd_spec) - -- end - -- end - -- - -- if module.cmds then - -- for _, cmd_spec in ipairs(module.cmds) do - -- commands_service.set(cmd_spec[1], cmd_spec[2], cmd_spec[3] or cmd_spec.opts) - -- end - -- end - -- - -- if module.binds then - -- keymaps_service.applyKeymaps( - -- type(module.binds) == "function" and module.binds() or module.binds - -- ) - -- end - -- end - -- profiler.stop(profile_msg) - -- end - -- end - -- Handle the Modules require("doom.utils.modules").traverse_loaded(doom.modules, function(node, stack) if node.type then diff --git a/lua/doom/modules/features/whichkey/init.lua b/lua/doom/modules/features/whichkey/init.lua index 2f54c805..2422763f 100644 --- a/lua/doom/modules/features/whichkey/init.lua +++ b/lua/doom/modules/features/whichkey/init.lua @@ -121,18 +121,6 @@ whichkey.configs["which-key.nvim"] = function() local keymaps_service = require("doom.services.keymaps") local whichkey_integration = get_whichkey_integration() - -- for section_name, _ in pairs(doom.modules) do - -- for _, module in pairs(doom[section_name]) do - -- if module and module.binds then - -- -- table.insert(all_keymaps, type(module.binds) == "function" and module.binds() or module.binds) - -- keymaps_service.applyKeymaps( - -- type(module.binds) == "function" and module.binds() or module.binds, - -- nil, - -- { whichkey_integration } - -- ) - -- end - -- end - -- end require("doom.utils.modules").traverse_loaded( doom.modules, @@ -140,14 +128,6 @@ whichkey.configs["which-key.nvim"] = function() if node.type then local module = node if module.binds then - -- count = count + 1 - -- vim.defer_fn(function() - -- keymaps_service.applyKeymaps( - -- type(module.binds) == "function" and module.binds() or module.binds, - -- nil, - -- { whichkey_integration } - -- ) - -- end, count) keymaps_service.applyKeymaps( type(module.binds) == "function" and module.binds() or module.binds, nil, From 27a9f3253b5ce24cea502aa64e90ccd5d1050341 Mon Sep 17 00:00:00 2001 From: hjalmar jakobsson Date: Mon, 16 Jan 2023 11:03:21 -0500 Subject: [PATCH 8/9] clean up utils/modules --- lua/doom/utils/modules.lua | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/lua/doom/utils/modules.lua b/lua/doom/utils/modules.lua index e41e80df..6a1da52c 100644 --- a/lua/doom/utils/modules.lua +++ b/lua/doom/utils/modules.lua @@ -37,13 +37,6 @@ M.traverse_enabled = traverser.build({ ) -- Traverse back a layer but do not pass this value to the handler function. end end, - -- -- Optional debugging function that can be used to - -- debug_node = function(node, stack) - -- local parent = stack[#stack] - -- local indent_str = string.rep("--", #stack) - -- local indent_cap = type(node) == "table" and "+" or ">" - -- print(("%s%s %s"):format(indent_str, indent_cap, type(node) == "table" and parent.key or node)) - -- end, }) M.traverse_loaded = traverser.build({ @@ -63,14 +56,7 @@ M.traverse_loaded = traverser.build({ -- ) -- ) -- Traverse back a layer but do not pass this value to the handler function. -- end - end, - -- Optional debugging function that can be used to - -- debug_node = function(node, stack) - -- local parent = stack[#stack] - -- local indent_str = string.rep("--", #stack) - -- local indent_cap = type(node) == "table" and "+" or ">" - -- print(("%s%s %s"):format(indent_str, indent_cap, type(node) == "table" and parent.key or node)) - -- end, + end }) return M From f95f966f71fe8ea6de035e9fcf7737323bd8cfc6 Mon Sep 17 00:00:00 2001 From: hjalmar jakobsson Date: Mon, 16 Jan 2023 11:25:42 -0500 Subject: [PATCH 9/9] refactor(settings): doom.settings and update all instances --- init.lua | 2 +- lua/doom/core/config.lua | 32 +- lua/doom/core/doom_global.lua | 327 ++++++++++---------- lua/doom/core/modules.lua | 2 +- lua/doom/core/ui.lua | 22 +- lua/doom/modules/core/doom/init.lua | 16 +- lua/doom/modules/features/explorer/init.lua | 4 +- lua/doom/modules/features/lsp/init.lua | 8 +- lua/doom/modules/features/netrw/init.lua | 2 +- lua/doom/modules/features/whichkey/init.lua | 2 +- lua/doom/services/traverser.lua | 2 +- lua/doom/utils/logging.lua | 2 +- 12 files changed, 211 insertions(+), 210 deletions(-) diff --git a/init.lua b/init.lua index a2775c0e..00b8dda9 100644 --- a/init.lua +++ b/init.lua @@ -41,7 +41,7 @@ require("doom.core") vim.defer_fn(function() -- Check for updates - if doom.check_updates and doom.core.updater then + if doom.settings.check_updates and doom.core.updater then doom.core.updater.check_updates(true) end end, 1) diff --git a/lua/doom/core/config.lua b/lua/doom/core/config.lua index 7d5ce16d..55cf3f9b 100644 --- a/lua/doom/core/config.lua +++ b/lua/doom/core/config.lua @@ -111,7 +111,7 @@ config.load = function() profiler.stop(profiler_message) end end - , { debug = doom.logging == "trace" or doom.logging == "debug" } + , { debug = doom.settings.logging == "trace" or doom.settings.logging == "debug" } ) @@ -128,10 +128,10 @@ config.load = function() profiler.stop("framework|config.lua (user)") -- Apply the necessary `doom.field_name` options - vim.opt.shiftwidth = doom.indent - vim.opt.softtabstop = doom.indent + vim.opt.shiftwidth = doom.settings.indent + vim.opt.softtabstop = doom.settings.indent vim.opt.tabstop = doom.indent - if doom.guicolors then + if doom.settings.guicolors then if vim.fn.exists("+termguicolors") == 1 then vim.opt.termguicolors = true elseif vim.fn.exists("+guicolors") == 1 then @@ -139,49 +139,49 @@ config.load = function() end end - if doom.auto_comment then + if doom.settings.auto_comment then vim.opt.formatoptions:append("croj") end - if doom.movement_wrap then + if doom.settings.movement_wrap then vim.cmd("set whichwrap+=<,>,[,],h,l") end - if doom.undo_dir then + if doom.settings.undo_dir then vim.opt.undofile = true - vim.opt.undodir = doom.undo_dir + vim.opt.undodir = doom.settings.undo_dir else vim.opt.undofile = false vim.opt.undodir = nil end - if doom.global_statusline then + if doom.settings.global_statusline then vim.opt.laststatus = 3 end -- Use system clipboard - if doom.clipboard then + if doom.settings.clipboard then vim.opt.clipboard = "unnamedplus" end - if doom.ignorecase then + if doom.settings.ignorecase then vim.cmd("set ignorecase") else vim.cmd("set noignorecase") end - if doom.smartcase then + if doom.settings.smartcase then vim.cmd("set smartcase") else vim.cmd("set nosmartcase") end -- Color column - vim.opt.colorcolumn = type(doom.max_columns) == "number" and tostring(doom.max_columns) or "" + vim.opt.colorcolumn = type(doom.settings.max_columns) == "number" and tostring(doom.settings.max_columns) or "" -- Number column - vim.opt.number = not doom.disable_numbering - vim.opt.relativenumber = not doom.disable_numbering and doom.relative_num + vim.opt.number = not doom.settings.disable_numbering + vim.opt.relativenumber = not doom.settings.disable_numbering and doom.settings.relative_num - vim.g.mapleader = doom.leader_key + vim.g.mapleader = doom.settings.leader_key end -- Path cases: diff --git a/lua/doom/core/doom_global.lua b/lua/doom/core/doom_global.lua index a42238d5..887c1445 100644 --- a/lua/doom/core/doom_global.lua +++ b/lua/doom/core/doom_global.lua @@ -48,176 +48,177 @@ _G._doom = {} --- Global object doom = { - -- Use the global statusline - -- @default = true - global_statusline = true, - - -- Leader key for keybinds - -- @default = ' ' - leader_key = " ", - - -- Pins plugins to a commit sha to prevent breaking changes - -- @default = true - freeze_dependencies = true, - - -- Autosave - -- false : Disable autosave - -- true : Enable autosave - -- @default = false - autosave = false, - - -- Disable Vim macros - -- false : Enable - -- true : Disable - -- @default = false - disable_macros = false, - - -- Disable ex mode - -- false : Enable - -- true : Disable - -- @default = false - disable_ex = true, - - -- Disable suspension - -- false : Enable - -- true : Disable - -- @default = false - disable_suspension = true, - - -- Set numbering - -- false : Enable number lines - -- true : Disable number lines - -- @default = false - disable_numbering = false, - - -- Set numbering style - -- false : Shows absolute number lines - -- true : Shows relative number lines - -- @default = true - relative_num = true, - - -- h,l, wrap lines - movement_wrap = true, - - -- Undo directory (set to nil to disable) - -- @default = vim.fn.stdpath("data") .. "/undodir/" - undo_dir = vim.fn.stdpath("data") .. "/undodir/", - - -- Set preferred border style across UI - border_style = "single", - - -- Preserve last editing position - -- false : Disable preservation of last editing position - -- true : Enable preservation of last editing position - -- @default = false - preserve_edit_pos = false, - - -- horizontal split on creating a new file (fn) - -- false : doesn't split the window when creating a new file - -- true : horizontal split on creating a new file - -- @default = true - new_file_split = "vertical", - - -- Enable auto comment (current line must be commented) - -- false : disables auto comment - -- true : enables auto comment - -- @default = false - auto_comment = false, - - -- Ignore case in a search pattern - -- false : search is sensitive - -- true : search is insensitive - -- @default = false - ignorecase = false, - - -- Override the 'ignorecase' option if the search pattern contains upper case - -- characters. Only used when the search pattern is typed and 'ignorecase' - -- option is on. - -- false : don't override the 'ignorecase' option - -- true : override the 'ignorecase' option is upper case characters is in search pattern - -- @default = false - smartcase = false, - - -- Enable Highlight on yank - -- false : disables highligh on yank - -- true : enables highlight on yank - -- @default = true - highlight_yank = true, - - -- Use clipboard outside of vim - -- false : won't use third party clipboard - -- true : enables third part clipboard - -- @default = true - clipboard = true, - - -- Enable guicolors - -- Enables gui colors on GUI versions of Neovim - -- @default = true - guicolors = true, - - -- Show hidden files - -- @default = true - show_hidden = true, - - -- Hide files listed in .gitignore from file browsers - -- @default = true - hide_gitignore = true, - - -- Checkupdates on start - -- @default = false - check_updates = false, - - -- sequences used for escaping insert mode - -- @default = { 'jk', 'kj' } - escape_sequences = { "jk", "kj" }, - - -- Use floating windows for plugins manager (packer) operations - -- @default = false - use_floating_win_packer = false, - - -- Set max cols - -- Defines the column to show a vertical marker - -- Set to false to disable - -- @default = 80 - max_columns = 80, - - -- Default indent size - -- @default = 4 - indent = 4, - - -- Logging level - -- Set Doom logging level - -- @default = "info" - --- @type "trace"|"debug"|"info"|"warn"|"error"|"fatal" - logging = "info", - - -- Default colorscheme - -- @default = doom-one - colorscheme = "doom-one", - - -- Doom One colorscheme settings - doom_one = { - -- If the cursor color should be blue + settings = { + -- Use the global statusline + -- @default = true + global_statusline = true, + + -- Leader key for keybinds + -- @default = ' ' + leader_key = " ", + + -- Pins plugins to a commit sha to prevent breaking changes + -- @default = true + freeze_dependencies = true, + + -- Autosave + -- false : Disable autosave + -- true : Enable autosave + -- @default = false + autosave = false, + + -- Disable Vim macros + -- false : Enable + -- true : Disable + -- @default = false + disable_macros = false, + + -- Disable ex mode + -- false : Enable + -- true : Disable + -- @default = false + disable_ex = true, + + -- Disable suspension + -- false : Enable + -- true : Disable + -- @default = false + disable_suspension = true, + + -- Set numbering + -- false : Enable number lines + -- true : Disable number lines -- @default = false - cursor_coloring = false, - -- If TreeSitter highlighting should be enabled + disable_numbering = false, + + -- Set numbering style + -- false : Shows absolute number lines + -- true : Shows relative number lines -- @default = true - enable_treesitter = true, - -- If the comments should be italic + relative_num = true, + + -- h,l, wrap lines + movement_wrap = true, + + -- Undo directory (set to nil to disable) + -- @default = vim.fn.stdpath("data") .. "/undodir/" + undo_dir = vim.fn.stdpath("data") .. "/undodir/", + + -- Set preferred border style across UI + border_style = "single", + + -- Preserve last editing position + -- false : Disable preservation of last editing position + -- true : Enable preservation of last editing position -- @default = false - italic_comments = false, - -- If the telescope plugin window should be colored + preserve_edit_pos = false, + + -- horizontal split on creating a new file (fn) + -- false : doesn't split the window when creating a new file + -- true : horizontal split on creating a new file -- @default = true - telescope_highlights = true, - -- If the built-in Neovim terminal should use the doom-one - -- colorscheme palette + new_file_split = "vertical", + + -- Enable auto comment (current line must be commented) + -- false : disables auto comment + -- true : enables auto comment -- @default = false - terminal_colors = true, - -- If the Neovim instance should be transparent + auto_comment = false, + + -- Ignore case in a search pattern + -- false : search is sensitive + -- true : search is insensitive -- @default = false - transparent_background = false, - }, + ignorecase = false, + + -- Override the 'ignorecase' option if the search pattern contains upper case + -- characters. Only used when the search pattern is typed and 'ignorecase' + -- option is on. + -- false : don't override the 'ignorecase' option + -- true : override the 'ignorecase' option is upper case characters is in search pattern + -- @default = false + smartcase = false, + + -- Enable Highlight on yank + -- false : disables highligh on yank + -- true : enables highlight on yank + -- @default = true + highlight_yank = true, + + -- Use clipboard outside of vim + -- false : won't use third party clipboard + -- true : enables third part clipboard + -- @default = true + clipboard = true, + + -- Enable guicolors + -- Enables gui colors on GUI versions of Neovim + -- @default = true + guicolors = true, + -- Show hidden files + -- @default = true + show_hidden = true, + + -- Hide files listed in .gitignore from file browsers + -- @default = true + hide_gitignore = true, + + -- Checkupdates on start + -- @default = false + check_updates = false, + + -- sequences used for escaping insert mode + -- @default = { 'jk', 'kj' } + escape_sequences = { "jk", "kj" }, + + -- Use floating windows for plugins manager (packer) operations + -- @default = false + use_floating_win_packer = false, + + -- Set max cols + -- Defines the column to show a vertical marker + -- Set to false to disable + -- @default = 80 + max_columns = 80, + + -- Default indent size + -- @default = 4 + indent = 4, + + -- Logging level + -- Set Doom logging level + -- @default = "info" + --- @type "trace"|"debug"|"info"|"warn"|"error"|"fatal" + logging = "info", + + -- Default colorscheme + -- @default = doom-one + colorscheme = "doom-one", + + -- Doom One colorscheme settings + doom_one = { + -- If the cursor color should be blue + -- @default = false + cursor_coloring = false, + -- If TreeSitter highlighting should be enabled + -- @default = true + enable_treesitter = true, + -- If the comments should be italic + -- @default = false + italic_comments = false, + -- If the telescope plugin window should be colored + -- @default = true + telescope_highlights = true, + -- If the built-in Neovim terminal should use the doom-one + -- colorscheme palette + -- @default = false + terminal_colors = true, + -- If the Neovim instance should be transparent + -- @default = false + transparent_background = false, + }, + }, packages = {}, --- Wrapper around packer.nvim `use` function --- diff --git a/lua/doom/core/modules.lua b/lua/doom/core/modules.lua index f3b4dcaf..d7d39240 100644 --- a/lua/doom/core/modules.lua +++ b/lua/doom/core/modules.lua @@ -87,7 +87,7 @@ modules.load_modules = function() spec.commit = utils.pick_compatible_field(spec.commit) end - if not doom.freeze_dependencies then + if not doom.settings.freeze_dependencies then spec.commit = nil end diff --git a/lua/doom/core/ui.lua b/lua/doom/core/ui.lua index 5bfe1933..c421c40e 100644 --- a/lua/doom/core/ui.lua +++ b/lua/doom/core/ui.lua @@ -4,20 +4,20 @@ -- if necessary. local profiler = require("doom.services.profiler") -local profile_message = ("framework|set colorscheme `%s`"):format(doom.colorscheme) +local profile_message = ("framework|set colorscheme `%s`"):format(doom.settings.colorscheme) local utils = require("doom.utils") local log = require("doom.utils.logging") profiler.start(profile_message) -- If the colorscheme was not found then fallback to defaults. -if not utils.is_empty(doom.colorscheme) then +if not utils.is_empty(doom.settings.colorscheme) then local loaded_colorscheme = xpcall(function() - vim.api.nvim_command("colorscheme " .. doom.colorscheme) + vim.api.nvim_command("colorscheme " .. doom.settings.colorscheme) end, debug.traceback) if not loaded_colorscheme then - log.warn("Colorscheme '" .. doom.colorscheme .. "' not found, falling back to doom-one") + log.warn("Colorscheme '" .. doom.settings.colorscheme .. "' not found, falling back to doom-one") vim.api.nvim_command("colorscheme doom-one") end else @@ -26,13 +26,13 @@ else end -- Set doom-one colorscheme settings -if doom.colorscheme == "doom-one" then +if doom.settings.colorscheme == "doom-one" then require("colors.doom-one").setup({ - cursor_coloring = doom.doom_one.cursor_coloring, - terminal_colors = doom.doom_one.terminal_colors, - italic_comments = doom.doom_one.italic_comments, - enable_treesitter = doom.doom_one.enable_treesitter, - transparent_background = doom.doom_one.transparent_background, + cursor_coloring = doom.settings.doom_one.cursor_coloring, + terminal_colors = doom.settings.doom_one.terminal_colors, + italic_comments = doom.settings.doom_one.italic_comments, + enable_treesitter = doom.settings.doom_one.enable_treesitter, + transparent_background = doom.settings.doom_one.transparent_background, pumblend = { enable = true, transparency_amount = doom.complete_transparency, @@ -43,7 +43,7 @@ if doom.colorscheme == "doom-one" then bufferline = true, gitgutter = false, gitsigns = true, - telescope = doom.doom_one.telescope_highlights, + telescope = doom.settings.doom_one.telescope_highlights, neogit = true, nvim_tree = true, dashboard = true, diff --git a/lua/doom/modules/core/doom/init.lua b/lua/doom/modules/core/doom/init.lua index 5e4bf780..ab00167e 100644 --- a/lua/doom/modules/core/doom/init.lua +++ b/lua/doom/modules/core/doom/init.lua @@ -83,20 +83,20 @@ required.binds = function() } -- Conditionally disable macros - if doom.disable_macros then + if doom.settings.disable_macros then table.insert(binds, { "q", "" }) end -- Conditionally disable ex mode - if doom.disable_ex then + if doom.settings.disable_ex then table.insert(binds, { "Q", "" }) end -- Conditionally disable suspension - if doom.disable_suspension then + if doom.settings.disable_suspension then table.insert(binds, { "", "" }) end -- Exit insert mode fast - for _, esc_seq in pairs(doom.escape_sequences) do + for _, esc_seq in pairs(doom.settings.escape_sequences) do table.insert(binds, { esc_seq, "", mode = "i" }) end @@ -105,7 +105,7 @@ required.binds = function() horizontal = "", [false] = "e", } - local split_prefix = split_modes[doom.new_file_split] + local split_prefix = split_modes[doom.settings.new_file_split] table.insert(binds, { "", name = "+prefix", @@ -264,11 +264,11 @@ end required.autocmds = function() local autocmds = {} - if doom.autosave then + if doom.settings.autosave then table.insert(autocmds, { "TextChanged,InsertLeave", "", "silent! write" }) end - if doom.highlight_yank then + if doom.settings.highlight_yank then table.insert(autocmds, { "TextYankPost", "*", @@ -278,7 +278,7 @@ required.autocmds = function() }) end - if doom.preserve_edit_pos then + if doom.settings.preserve_edit_pos then table.insert(autocmds, { "BufReadPost", "*", diff --git a/lua/doom/modules/features/explorer/init.lua b/lua/doom/modules/features/explorer/init.lua index 7ec2a337..961fa284 100644 --- a/lua/doom/modules/features/explorer/init.lua +++ b/lua/doom/modules/features/explorer/init.lua @@ -158,10 +158,10 @@ explorer.configs["nvim-tree.lua"] = function() }, }, filters = { - dotfiles = not doom.show_hidden, + dotfiles = not doom.settings.show_hidden, }, git = { - ignore = doom.hide_gitignore, + ignore = doom.settings.hide_gitignore, }, }, doom.features.explorer.settings, override_table) require("nvim-tree").setup(config) diff --git a/lua/doom/modules/features/lsp/init.lua b/lua/doom/modules/features/lsp/init.lua index b3bb523f..03591e86 100644 --- a/lua/doom/modules/features/lsp/init.lua +++ b/lua/doom/modules/features/lsp/init.lua @@ -175,10 +175,10 @@ lsp.configs["nvim-lspconfig"] = function() }) -- Border for lsp_popups vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { - border = doom.border_style, + border = doom.settings.border_style, }) vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, { - border = doom.border_style, + border = doom.settings.border_style, }) -- symbols for autocomplete local kinds = {} @@ -303,7 +303,7 @@ lsp.configs["lsp_signature.nvim"] = function() require("lsp_signature").setup( vim.tbl_deep_extend("force", doom.features.lsp.settings.signature, { handler_opts = { - border = doom.border_style, + border = doom.settings.border_style, }, }) ) @@ -370,7 +370,7 @@ lsp.binds = function() function() vim.diagnostic.open_float(0, { focusable = false, - border = doom.border_style, + border = doom.settings.border_style, }) end, name = "Line", diff --git a/lua/doom/modules/features/netrw/init.lua b/lua/doom/modules/features/netrw/init.lua index fb3a8a52..d62fa5c6 100644 --- a/lua/doom/modules/features/netrw/init.lua +++ b/lua/doom/modules/features/netrw/init.lua @@ -35,7 +35,7 @@ vim.g.netrw_list_hide = vim.fn["netrw_gitignore#Hide"]() -- 0 : show all files -- 1 : show not-hidden files -- 2 : show hidden files only -vim.g.netrw_hide = not doom.show_hidden +vim.g.netrw_hide = not doom.settings.show_hidden -- Preview files in a vertical split window -- vim.g.netrw_preview = 1 diff --git a/lua/doom/modules/features/whichkey/init.lua b/lua/doom/modules/features/whichkey/init.lua index 2422763f..ec14438f 100644 --- a/lua/doom/modules/features/whichkey/init.lua +++ b/lua/doom/modules/features/whichkey/init.lua @@ -39,7 +39,7 @@ whichkey.settings = { }, window = { padding = { 0, 0, 0, 0 }, - border = doom.border_style, + border = doom.settings.border_style, }, layout = { height = { min = 1, max = 10 }, diff --git a/lua/doom/services/traverser.lua b/lua/doom/services/traverser.lua index 2f04cc86..b544f9a6 100644 --- a/lua/doom/services/traverser.lua +++ b/lua/doom/services/traverser.lua @@ -20,7 +20,7 @@ end -- Default debug levels local default_log_levels = - { debug = doom.logging == "trace" or doom.logging == "debug" } + { debug = doom.settings.logging == "trace" or doom.settings.logging == "debug" } local tree_traverser = { build = function(builder_opts) diff --git a/lua/doom/utils/logging.lua b/lua/doom/utils/logging.lua index 1f2e2948..54f6f459 100644 --- a/lua/doom/utils/logging.lua +++ b/lua/doom/utils/logging.lua @@ -12,7 +12,7 @@ local default_config = { use_console = true, highlights = true, use_file = true, - level = doom.logging, + level = doom.settings.logging, modes = { { name = "trace", hl = "Comment" }, { name = "debug", hl = "Comment" },