diff options
| author | Ariel D'Alessandro <dalessandro.ariel@gmail.com> | 2023-01-21 23:05:43 +0100 |
|---|---|---|
| committer | Ariel D'Alessandro <dalessandro.ariel@gmail.com> | 2023-01-21 23:05:43 +0100 |
| commit | e33750c6612e645ab3ca934cc936ccc02f0b4690 (patch) | |
| tree | 6fb5051c45b8ff5aa759e11053db937671f4629f /lua | |
| parent | 221b69d165669371695842d12a85f338ac17ce47 (diff) | |
| download | nvim-conf-e33750c6612e645ab3ca934cc936ccc02f0b4690.tar.gz nvim-conf-e33750c6612e645ab3ca934cc936ccc02f0b4690.zip | |
feat: fix snippets reload, add telescope actions for plugins and config
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/alpha/plugins/snippets.lua | 230 | ||||
| -rw-r--r-- | lua/alpha/scratchs/sample.lua | 37 | ||||
| -rw-r--r-- | lua/alpha/snips/ft/lua.lua | 7 | ||||
| -rw-r--r-- | lua/alpha/telescope/init.lua | 23 | ||||
| -rw-r--r-- | lua/alpha/telescope/mappings.lua | 2 | ||||
| -rw-r--r-- | lua/alpha/utils.lua | 5 |
6 files changed, 179 insertions, 125 deletions
diff --git a/lua/alpha/plugins/snippets.lua b/lua/alpha/plugins/snippets.lua index dee004e..cba0dd1 100644 --- a/lua/alpha/plugins/snippets.lua +++ b/lua/alpha/plugins/snippets.lua @@ -1,135 +1,137 @@ -return { - "L3MON4D3/LuaSnip", - version = "v1.*", - config = function() - if vim.g.snippets ~= "luasnip" then - return - end +local config = function() + if vim.g.snippets ~= "luasnip" then + return + end - local ls = require("luasnip") - local types = require("luasnip.util.types") + local ls = require("luasnip") + local types = require("luasnip.util.types") - ls.config.set_config({ - -- This tells LuaSnip to remember to keep around the last snippet. - -- You can jump back into even if you move outside of the selection - history = true, + ls.config.set_config({ + -- This tells LuaSnip to remember to keep around the last snippet. + -- You can jump back into even if you move outside of the selection + history = true, - -- This one is cool cause if you have dynamic snippets, it updatesas you type! - updateevents = "TextChanged,TextChangedI", + -- This one is cool cause if you have dynamic snippets, it updatesas you type! + updateevents = "TextChanged,TextChangedI", - -- Autosnippets: - enable_autosnippets = true, + -- Autosnippets: + enable_autosnippets = true, - ext_opts = { - [types.choiceNode] = { - active = { - virt_text = { { "<- Choice", "Error" } }, - }, + ext_opts = { + [types.choiceNode] = { + active = { + virt_text = { { "<- Choice", "Error" } }, }, }, - }) - - -- create snippet - -- s(context, nodes, condition, ...) - local snippet = ls.s - - -- TODO: Write about this. - -- Useful for dynamic nodes and choice nodes - -- local snippet_from_nodes = ls.sn - - -- This is the simplest node. - -- Creates a new text node. Places cursor after node by default. - -- t { "this will be inserted" } - -- - -- Multiple lines are by passing a table of strings. - -- t { "line 1", "line 2" } - local t = ls.text_node - - -- Insert Node - -- Creates a location for the cursor to jump to. - -- Possible options to jump to are 1 - N - -- If you use 0, that's the final place to jump to. - -- - -- To create placeholder text, pass it as the second argument - -- i(2, "this is placeholder text") - local i = ls.insert_node - - -- Function Node - -- Takes a function that returns text - -- local f = ls.function_node - - -- This a choice snippet. You can move through with <c-l> (in my config) - -- c(1, { t {"hello"}, t {"world"}, }), - -- - -- The first argument is the jump position - -- The second argument is a table of possible nodes. - -- Note, one thing that's nice is you don't have to include - -- the jump position for nodes that normally require one (can be nil) - -- local c = ls.choice_node - - -- local d = ls.dynamic_node - - -- TODO: Document what I've learned about lambda - -- local l = require("luasnip.extras").lambda - - -- local events = require("luasnip.util.events") - - local shortcut = function(val) - if type(val) == "string" then - return { t({ val }), i(0) } - end + }, + }) + + -- create snippet + -- s(context, nodes, condition, ...) + local snippet = ls.s + + -- TODO: Write about this. + -- Useful for dynamic nodes and choice nodes + -- local snippet_from_nodes = ls.sn + + -- This is the simplest node. + -- Creates a new text node. Places cursor after node by default. + -- t { "this will be inserted" } + -- + -- Multiple lines are by passing a table of strings. + -- t { "line 1", "line 2" } + local t = ls.text_node + + -- Insert Node + -- Creates a location for the cursor to jump to. + -- Possible options to jump to are 1 - N + -- If you use 0, that's the final place to jump to. + -- + -- To create placeholder text, pass it as the second argument + -- i(2, "this is placeholder text") + local i = ls.insert_node + + -- Function Node + -- Takes a function that returns text + -- local f = ls.function_node + + -- This a choice snippet. You can move through with <c-l> (in my config) + -- c(1, { t {"hello"}, t {"world"}, }), + -- + -- The first argument is the jump position + -- The second argument is a table of possible nodes. + -- Note, one thing that's nice is you don't have to include + -- the jump position for nodes that normally require one (can be nil) + -- local c = ls.choice_node + + -- local d = ls.dynamic_node + + -- TODO: Document what I've learned about lambda + -- local l = require("luasnip.extras").lambda + + -- local events = require("luasnip.util.events") + + 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 + if type(val) == "table" then + for k, v in ipairs(val) do + if type(v) == "string" then + val[k] = t({ v }) 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 val + end - return result + 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 - ls.cleanup() - for _, ft_path in ipairs(vim.api.nvim_get_runtime_file("lua/alpha/snips/ft/*.lua", true)) do - local ft = vim.fn.fnamemodify(ft_path, ":t:r") - ls.add_snippets(ft, make(loadfile(ft_path)())) - end + return result + end - -- <c-k> is my expansion key - -- this will expand the current item or jump to the next item within the snippet. - vim.keymap.set({ "i", "s" }, "<c-k>", function() - if ls.expand_or_jumpable() then - ls.expand_or_jump() - end - end, { silent = true }) + ls.cleanup() + for _, ft_path in ipairs(vim.api.nvim_get_runtime_file("lua/alpha/snips/ft/*.lua", true)) do + local ft = vim.fn.fnamemodify(ft_path, ":t:r") + ls.add_snippets(ft, make(loadfile(ft_path)())) + end - -- <c-j> is my jump backwards key. - -- this always moves to the previous item within the snippet - vim.keymap.set({ "i", "s" }, "<c-j>", function() - if ls.jumpable(-1) then - ls.jump(-1) - end - end, { silent = true }) + -- <c-k> is my expansion key + -- this will expand the current item or jump to the next item within the snippet. + vim.keymap.set({ "i", "s" }, "<c-k>", function() + if ls.expand_or_jumpable() then + ls.expand_or_jump() + end + end, { silent = true }) - -- <c-l> is selecting within a list of options. - -- This is useful for choice nodes (introduced in the forthcoming episode 2) - vim.keymap.set("i", "<c-l>", function() - if ls.choice_active() then - ls.change_choice(1) - end - end) + -- <c-j> is my jump backwards key. + -- this always moves to the previous item within the snippet + vim.keymap.set({ "i", "s" }, "<c-j>", function() + if ls.jumpable(-1) then + ls.jump(-1) + end + end, { silent = true }) - -- shorcut to source my luasnips file again, which will reload my snippets - vim.keymap.set("n", "<leader><leader>s", "<cmd>source ~/.config/nvim/after/plugin/luasnip.lua<CR>") + -- <c-l> is selecting within a list of options. + -- This is useful for choice nodes (introduced in the forthcoming episode 2) + vim.keymap.set("i", "<c-l>", function() + if ls.choice_active() then + ls.change_choice(1) + end + end) + +end +return { + "L3MON4D3/LuaSnip", + version = "v1.*", + config = function() + config() + vim.keymap.set("n", "<leader><leader>s", config) end } diff --git a/lua/alpha/scratchs/sample.lua b/lua/alpha/scratchs/sample.lua index 80b0b77..415e9a2 100644 --- a/lua/alpha/scratchs/sample.lua +++ b/lua/alpha/scratchs/sample.lua @@ -1,14 +1,29 @@ -function reverse_current_word() - local line = vim.fn.getline('.') - local current_word = vim.fn.expand('<cword>') - local start_index = vim.fn.col('.') - #current_word + 1 - local reversed_word = current_word:reverse() - - local reversed_line = line:sub(1, start_index -1)..reversed_word..line:sub(start_index+#current_word) - P(reversed_line) - vim.fn.setline('.', reversed_line) +local M = {} + +local action_state = require("telescope.actions.state") +local actions = require('telescope.actions') + +M.find_config = function() + require("telescope.builtin").find_files({ + prompt_title = "< Neovim >", + cwd = "$HOME/.config/nvim/", + }) end -vim.api.nvim_command('command! -range=% ReverseWord :<line1>,<line2>lua reverse_current_word()') +M.find_plugin = function() + require("telescope.builtin").find_files({ + prompt_title = "< Plugins >", + cwd = "$HOME/.config/nvim/lua/alpha/plugins/", + attach_mappings = function(_, map) + map("i", "<C-t>", function(prompt_bufnr) + local new_plugin = action_state.get_current_line() + actions.close(prompt_bufnr) + vim.cmd(string.format("edit ~/.config/nvim/lua/alpha/plugins/%s.lua", new_plugin)) + end) + + return true + end, + }) +end --- testing +M.find_plugin() diff --git a/lua/alpha/snips/ft/lua.lua b/lua/alpha/snips/ft/lua.lua index 5ea789e..9f6b6fa 100644 --- a/lua/alpha/snips/ft/lua.lua +++ b/lua/alpha/snips/ft/lua.lua @@ -57,4 +57,11 @@ end]], i(1) } ), + + plugin = fmt( +[[return {{ + "{}" +}}]], { + i(1, "plugin") + }), } diff --git a/lua/alpha/telescope/init.lua b/lua/alpha/telescope/init.lua index ccb1b9e..2b85cac 100644 --- a/lua/alpha/telescope/init.lua +++ b/lua/alpha/telescope/init.lua @@ -40,6 +40,29 @@ function M.search_config() }) end +M.find_nvim_config = function() + require("telescope.builtin").find_files({ + prompt_title = "< Neovim >", + cwd = "$HOME/.config/nvim/", + }) +end + +M.find_nvim_plugin = function() + require("telescope.builtin").find_files({ + prompt_title = "< Plugins >", + cwd = "$HOME/.config/nvim/lua/alpha/plugins/", + attach_mappings = function(_, map) + map("i", "<C-t>", function(prompt_bufnr) + local new_plugin = action_state.get_current_line() + actions.close(prompt_bufnr) + vim.cmd(string.format("edit ~/.config/nvim/lua/alpha/plugins/%s.lua", new_plugin)) + end) + + return true + end, + }) +end + function M.grep_string() vim.ui.input({ prompt = "Grep for > " }, function(input) if input == nil then diff --git a/lua/alpha/telescope/mappings.lua b/lua/alpha/telescope/mappings.lua index 42d2c4b..0845110 100644 --- a/lua/alpha/telescope/mappings.lua +++ b/lua/alpha/telescope/mappings.lua @@ -27,6 +27,8 @@ map_tele("<leader>pw", "grep_word") map_tele("<leader>ps", "grep_string") map_tele("<leader>pf", "projectionist") map_tele("<leader>rc", "search_config") +map_tele("<leader>rn", "find_nvim_config") +map_tele("<leader>rp", "find_nvim_plugin") map_tele("<leader>ph", "help_tags") map_tele("<leader>pe", "buffers") map_tele("<leader>pr", "treesitter") diff --git a/lua/alpha/utils.lua b/lua/alpha/utils.lua index b1911bc..cd7d2bb 100644 --- a/lua/alpha/utils.lua +++ b/lua/alpha/utils.lua @@ -1,4 +1,9 @@ local utils = {} +-- TODO check to add to the notify configuration +local ok, notify = pcall(require, "notify") +if ok then + vim.notify = notify.notify +end utils.save_and_exec = function() local source_commands = { |