refactored toggle path completion
This commit is contained in:
parent
d5a3c86a85
commit
97ab584fe6
|
@ -30,6 +30,36 @@ return {
|
||||||
local luasnip = require 'luasnip'
|
local luasnip = require 'luasnip'
|
||||||
luasnip.config.setup {} -- Setup luasnip first
|
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
|
-- Configure nvim-cmp
|
||||||
cmp.setup {
|
cmp.setup {
|
||||||
snippet = {
|
snippet = {
|
||||||
|
@ -76,10 +106,7 @@ return {
|
||||||
-- Add other cmp setup options from your old config if any
|
-- Add other cmp setup options from your old config if any
|
||||||
}
|
}
|
||||||
|
|
||||||
-- *** FIX: Require and setup your custom toggle module AFTER cmp is setup ***
|
vim.keymap.set('n', '<leader>tp', toggle_path_completion, { desc = '[T]oggle [p]ath completion' })
|
||||||
-- Ensure the 'custom.toggle-path-cmp' module exists in lua/custom/
|
|
||||||
local completion_toggle = require 'custom.toggle-path-cmp'
|
|
||||||
vim.keymap.set('n', '<leader>tp', completion_toggle.toggle_path_completion, { desc = '[T]oggle [p]ath autocompletion' })
|
|
||||||
end, -- End of config function
|
end, -- End of config function
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
|
Loading…
Reference in New Issue