From 97ab584fe64f73387aa0b5467bcfd1a149d2c942 Mon Sep 17 00:00:00 2001 From: dlond Date: Mon, 26 May 2025 04:03:37 +1200 Subject: [PATCH] refactored toggle path completion --- lua/custom/plugins/completion.lua | 35 ++++++++++++++++++++--- lua/custom/toggle-path-cmp.lua | 47 ------------------------------- 2 files changed, 31 insertions(+), 51 deletions(-) delete mode 100644 lua/custom/toggle-path-cmp.lua diff --git a/lua/custom/plugins/completion.lua b/lua/custom/plugins/completion.lua index f0e7d270..81a23b17 100644 --- a/lua/custom/plugins/completion.lua +++ b/lua/custom/plugins/completion.lua @@ -30,6 +30,36 @@ return { local luasnip = require 'luasnip' luasnip.config.setup {} -- Setup luasnip first + local function toggle_path_completion() + local config = cmp.get_config() + local snippet = config.snippet + local completion = config.completion + local mapping = config.mapping + + local path_enabled = vim.tbl_any(function(src) + return src.name == 'path' + end, config.sources) + + local new_sources = { + { name = 'lazydev', group_index = 0 }, + { name = 'nvim_lsp' }, + { name = 'luasnip' }, + } + + if not path_enabled then + table.insert(new_sources, { name = 'path' }) + end + + cmp.setup { + snippet = snippet, + completion = completion, + mapping = mapping, + sources = new_sources, + } + + print('Path completion ' .. (path_enabled and 'disabled' or 'enabled')) + end + -- Configure nvim-cmp cmp.setup { snippet = { @@ -76,10 +106,7 @@ return { -- Add other cmp setup options from your old config if any } - -- *** FIX: Require and setup your custom toggle module AFTER cmp is setup *** - -- Ensure the 'custom.toggle-path-cmp' module exists in lua/custom/ - local completion_toggle = require 'custom.toggle-path-cmp' - vim.keymap.set('n', 'tp', completion_toggle.toggle_path_completion, { desc = '[T]oggle [p]ath autocompletion' }) + vim.keymap.set('n', 'tp', toggle_path_completion, { desc = '[T]oggle [p]ath completion' }) end, -- End of config function }, } diff --git a/lua/custom/toggle-path-cmp.lua b/lua/custom/toggle-path-cmp.lua deleted file mode 100644 index 71e90ebd..00000000 --- a/lua/custom/toggle-path-cmp.lua +++ /dev/null @@ -1,47 +0,0 @@ -local cmp = require 'cmp' - -local M = {} - -function M.toggle_path_completion() - local snippet = cmp.get_config().snippet - local completion = cmp.get_config().completion - local mapping = cmp.get_config().mapping - local sources = cmp.get_config().sources - local path_enabled = false - - for _, source in ipairs(sources) do - if source.name == 'path' then - path_enabled = true - break - end - end - - if path_enabled then - cmp.setup { - snippet, - completion, - mapping, - sources = { - { name = 'lazydev', group_index = 0 }, - { name = 'nvim_lsp' }, - { name = 'luasnip' }, - }, - } - print 'Path completion disabled' - else - cmp.setup { - snippet, - completion, - mapping, - sources = { - { name = 'lazydev', group_index = 0 }, - { name = 'nvim_lsp' }, - { name = 'luasnip' }, - { name = 'path' }, - }, - } - print 'Path completion enabled' - end -end - -return M