diff --git a/after/plugin/completion.lua b/after/plugin/completion.lua index be012a8..70c5756 100644 --- a/after/plugin/completion.lua +++ b/after/plugin/completion.lua @@ -1,19 +1,12 @@ -local cmp_status_ok, cmp = pcall(require, "cmp") -if not cmp_status_ok then - return -end - -local snip_status_ok, luasnip = pcall(require, "luasnip") -if not snip_status_ok then - return -end +local cmp = require "cmp" +local ls = require "luasnip" vim.opt.shortmess:append "c" cmp.setup { snippet = { expand = function(args) - luasnip.lsp_expand(args.body) + ls.lsp_expand(args.body) end, }, @@ -21,14 +14,14 @@ cmp.setup { [""] = cmp.mapping.close(), [""] = cmp.mapping(function() - if luasnip.locally_jumpable(-1) then - luasnip.jump(-1) + if ls.locally_jumpable(-1) then + ls.jump(-1) end end, { "i", "s" }), [""] = cmp.mapping(function() - if luasnip.expand_or_locally_jumpable() then - luasnip.expand_or_jump() + if ls.expand_or_locally_jumpable() then + ls.expand_or_jump() end end, { "i", "s" }), @@ -87,3 +80,80 @@ cmp.setup.filetype({ "sql" }, { { name = "buffer" }, }, }) + +local snippet = ls.snippet +local i = ls.insert_node +local t = ls.text_node + +local shortcut = function(val) + if type(val) == "string" then + return { t { val }, i(0) } + end + + if type(val) == "table" then + for k, v in ipairs(val) do + if type(v) == "string" then + val[k] = t { v } + end + end + end + + return val +end + +local make = function(tbl) + local result = {} + for k, v in pairs(tbl) do + table.insert(result, (snippet({ trig = k, desc = v.desc }, shortcut(v)))) + end + + return result +end + +local snippets = {} + +for _, ft_path in ipairs(vim.api.nvim_get_runtime_file("lua/opdavies/snippets/ft/*.lua", true)) do + local ft = vim.fn.fnamemodify(ft_path, ":t:r") + snippets[ft] = make(loadfile(ft_path)()) + + ls.add_snippets(ft, snippets[ft]) +end + +ls.add_snippets("js", snippets.javascript) +ls.add_snippets("typescript", snippets.javascript) +ls.add_snippets("vue", snippets.javascript) + +require("luasnip.loaders.from_vscode").lazy_load() + +ls.config.set_config { + enable_autosnippets = true, + history = true, + updateevents = "TextChanged,TextChangedI", +} + +-- Expand the current item or just to the next item within the snippet. +vim.keymap.set({ "i", "s" }, "", function() + if ls.expand_or_jumpable() then + ls.expand_or_jump() + end +end, { silent = true }) + +-- Jump backwards. +vim.keymap.set({ "i", "s" }, "", function() + if ls.jumpable(-1) then + ls.jump(-1) + end +end, { silent = true }) + +-- Select within a list of options. +vim.keymap.set("i", "", function() + if ls.choice_active() then + ls.change_choice(1) + end +end) + +vim.keymap.set( + "n", + "s", + "source ~/Code/personal/opdavies.nvim/after/plugin/luasnip.lua" +) diff --git a/after/plugin/luasnip.lua b/after/plugin/luasnip.lua deleted file mode 100644 index 701c51a..0000000 --- a/after/plugin/luasnip.lua +++ /dev/null @@ -1,78 +0,0 @@ -local ls = require "luasnip" - -local snippet = ls.snippet -local i = ls.insert_node -local t = ls.text_node - -local shortcut = function(val) - if type(val) == "string" then - return { t { val }, i(0) } - end - - if type(val) == "table" then - for k, v in ipairs(val) do - if type(v) == "string" then - val[k] = t { v } - end - end - end - - return val -end - -local make = function(tbl) - local result = {} - for k, v in pairs(tbl) do - table.insert(result, (snippet({ trig = k, desc = v.desc }, shortcut(v)))) - end - - return result -end - -local snippets = {} - -for _, ft_path in ipairs(vim.api.nvim_get_runtime_file("lua/opdavies/snippets/ft/*.lua", true)) do - local ft = vim.fn.fnamemodify(ft_path, ":t:r") - snippets[ft] = make(loadfile(ft_path)()) - - ls.add_snippets(ft, snippets[ft]) -end - -ls.add_snippets("js", snippets.javascript) -ls.add_snippets("typescript", snippets.javascript) -ls.add_snippets("vue", snippets.javascript) - -require("luasnip.loaders.from_vscode").lazy_load() - -ls.config.set_config { - enable_autosnippets = true, - history = true, - updateevents = "TextChanged,TextChangedI", -} - --- Expand the current item or just to the next item within the snippet. -vim.keymap.set({ "i", "s" }, "", function() - if ls.expand_or_jumpable() then - ls.expand_or_jump() - end -end, { silent = true }) - --- Jump backwards. -vim.keymap.set({ "i", "s" }, "", function() - if ls.jumpable(-1) then - ls.jump(-1) - end -end, { silent = true }) - --- Select within a list of options. -vim.keymap.set("i", "", function() - if ls.choice_active() then - ls.change_choice(1) - end -end) - -vim.keymap.set( - "n", - "s", - "source ~/Code/github.com/opdavies/dotfiles/config/neovim/after/plugin/luasnip.lua" -)