From ad9c779b205981495bde3f676c7e0fe67493f87e Mon Sep 17 00:00:00 2001 From: pro100ton Date: Sat, 1 Feb 2025 17:02:46 +0300 Subject: [PATCH] Add basic luasnip config --- init.lua | 4 ++-- lua/custom/completion.lua | 22 ++++++++++++++++++++++ lua/custom/luasnip.lua | 33 +++++++++++++++++++++++++++++++++ lua/custom/plugins/luasnip.lua | 9 +++++++++ lua/custom/snippets/lua.lua | 10 ++++++++++ plugin/keymaps.lua | 2 +- 6 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 lua/custom/luasnip.lua create mode 100644 lua/custom/plugins/luasnip.lua create mode 100644 lua/custom/snippets/lua.lua diff --git a/init.lua b/init.lua index 8b6bc7e..f5883da 100644 --- a/init.lua +++ b/init.lua @@ -1,7 +1,7 @@ -- Making leader () key to space -vim.g.mapleader = "," +vim.g.mapleader = "\\" -- Making local leader () key to backslash -vim.g.maplocalleader = "\\" +vim.g.maplocalleader = "" -- Bootstrap lazy.nvim local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim" diff --git a/lua/custom/completion.lua b/lua/custom/completion.lua index 72cb31e..ab16127 100644 --- a/lua/custom/completion.lua +++ b/lua/custom/completion.lua @@ -3,6 +3,10 @@ -- menu: Show menu when completions available (more than 1) -- menuone: Show menu even when there is only 1 option -- noselect: Force user to select convinient option + +-- Load luasnip setup +require "custom.luasnip" + vim.opt.completeopt = { "menu", "menuone", "noselect" } local lspkind = require "lspkind" @@ -25,6 +29,7 @@ local cmp = require "cmp" cmp.setup { sources = { + { name = "luasnip" }, { name = "nvim_lsp" }, { name = "path" }, { name = "buffer" }, @@ -40,4 +45,21 @@ cmp.setup { { "i", "c" } ), }, + + -- Enable luasnip to handle snippet expansion for nvim-cmp + snippet = { + expand = function(args) + vim.snippet.expand(args.body) + end, + }, + + formatting = { + fields = { "abbr", "kind", "menu" }, + expandable_indicator = true, + format = function(entry, vim_item) + -- Lspkind setup for icons + vim_item = kind_formatter(entry, vim_item) + return vim_item + end, + }, } diff --git a/lua/custom/luasnip.lua b/lua/custom/luasnip.lua new file mode 100644 index 0000000..99235c6 --- /dev/null +++ b/lua/custom/luasnip.lua @@ -0,0 +1,33 @@ +local ls = require "luasnip" +local types = require "luasnip.util.types" + +ls.config.set_config { + history = true, + updateevents = "TextChanged, TextChangedI", + enable_autosnippets = true +} + +vim.keymap.set({ "i", "s" }, "", function() + if ls.expand_or_jumpable() then + ls.expand_or_jump() + end +end, { silent = true }) + +vim.keymap.set({ "i", "s" }, "", function() + if ls.jumpable() then + ls.jump(-1) + end +end, { silent = true }) + +vim.keymap.set({ "i", "s" }, "", function() + if ls.choice_active() then + ls.change_choice(1) + end +end, { silent = true }) + +vim.keymap.set("n", "s", "source ~/.config/nvim_dev/lua/custom/luasnip.lua") + +-- Load custom snippets +for _, ft_path in ipairs(vim.api.nvim_get_runtime_file("lua/custom/snippets/*.lua", true)) do + loadfile(ft_path)() +end diff --git a/lua/custom/plugins/luasnip.lua b/lua/custom/plugins/luasnip.lua new file mode 100644 index 0000000..99006fc --- /dev/null +++ b/lua/custom/plugins/luasnip.lua @@ -0,0 +1,9 @@ +return { + { + "L3MON4D3/LuaSnip", + -- follow latest release. + version = "v2.*", -- Replace by the latest released major (first number of latest release) + -- install jsregexp (optional!). + build = "make install_jsregexp" + } +} diff --git a/lua/custom/snippets/lua.lua b/lua/custom/snippets/lua.lua new file mode 100644 index 0000000..166600a --- /dev/null +++ b/lua/custom/snippets/lua.lua @@ -0,0 +1,10 @@ +local ls = require "luasnip" + + +print("hello from lua snips") + +ls.add_snippets("lua", { + ls.parser.parse_snippet("expand", "-- this is what was expanded kek"), + ls.parser.parse_snippet("pipa", "Pipasik"), +}) + diff --git a/plugin/keymaps.lua b/plugin/keymaps.lua index 5af4f86..70d3e45 100644 --- a/plugin/keymaps.lua +++ b/plugin/keymaps.lua @@ -7,5 +7,5 @@ set("n", "", "") set("n", "", "") -- Bindings for reloading LUA files (used when working with lua files) -set("n", "x", ".lua", { desc = "Execute the current lne" }) +-- set("n", "x", ".lua", { desc = "Execute the current lne" }) set("n", "x", "source %", { desc = "Execute the current file" })