diff options
Diffstat (limited to '.config/nvim/lua/core')
-rw-r--r-- | .config/nvim/lua/core/autopairs.lua | 17 | ||||
-rw-r--r-- | .config/nvim/lua/core/cmp.lua | 170 | ||||
-rw-r--r-- | .config/nvim/lua/core/commands.lua | 3 | ||||
-rw-r--r-- | .config/nvim/lua/core/info.lua | 6 | ||||
-rw-r--r-- | .config/nvim/lua/core/nvimtree.lua | 4 | ||||
-rw-r--r-- | .config/nvim/lua/core/telescope.lua | 20 | ||||
-rw-r--r-- | .config/nvim/lua/core/which-key.lua | 3 |
7 files changed, 179 insertions, 44 deletions
diff --git a/.config/nvim/lua/core/autopairs.lua b/.config/nvim/lua/core/autopairs.lua index 0c9ec28..630f3de 100644 --- a/.config/nvim/lua/core/autopairs.lua +++ b/.config/nvim/lua/core/autopairs.lua @@ -4,14 +4,8 @@ function M.config() options.builtin.autopairs = { active = true, on_config_done = nil, - ---@usage map <CR> on insert mode - map_cr = true, ---@usage auto insert after select function or method item map_complete = true, - ---@usage automatically select the first item - auto_select = true, - ---@usage use insert confirm behavior instead of replace - insert = false, ---@usage -- modifies the function or method delimiter by filetypes map_char = { all = "(", @@ -60,12 +54,17 @@ M.setup = function() if package.loaded["cmp"] then require("nvim-autopairs.completion.cmp").setup { - map_cr = options.builtin.autopairs.map_cr, + map_cr = false, map_complete = options.builtin.autopairs.map_complete, - auto_select = options.builtin.autopairs.auto_select, - insert = options.builtin.autopairs.insert, map_char = options.builtin.autopairs.map_char, } + -- we map CR explicitly in cmp.lua but we still need to setup the autopairs CR keymap + vim.api.nvim_set_keymap( + "i", + "<CR>", + "v:lua.MPairs.autopairs_cr()", + { expr = true, noremap = true } + ) end require("nvim-treesitter.configs").setup { autopairs = { enable = true } } diff --git a/.config/nvim/lua/core/cmp.lua b/.config/nvim/lua/core/cmp.lua index f9f2d01..fd9e96c 100644 --- a/.config/nvim/lua/core/cmp.lua +++ b/.config/nvim/lua/core/cmp.lua @@ -29,10 +29,109 @@ M.config = function() if not status_luasnip_ok then return end + local win_get_cursor = vim.api.nvim_win_get_cursor + local get_current_buf = vim.api.nvim_get_current_buf + + local function inside_snippet() + -- for outdated versions of luasnip + if not luasnip.session.current_nodes then + return false + end + + local node = luasnip.session.current_nodes[get_current_buf()] + if not node then + return false + end + + local snip_begin_pos, snip_end_pos = node.parent.snippet.mark:pos_begin_end() + local pos = win_get_cursor(0) + pos[1] = pos[1] - 1 -- LuaSnip is 0-based not 1-based like nvim for rows + return pos[1] >= snip_begin_pos[1] and pos[1] <= snip_end_pos[1] + end + + ---sets the current buffer's luasnip to the one nearest the cursor + ---@return boolean true if a node is found, false otherwise + local function seek_luasnip_cursor_node() + -- for outdated versions of luasnip + if not luasnip.session.current_nodes then + return false + end + + local pos = win_get_cursor(0) + pos[1] = pos[1] - 1 + local node = luasnip.session.current_nodes[get_current_buf()] + if not node then + return false + end + + local snippet = node.parent.snippet + local exit_node = snippet.insert_nodes[0] + + -- exit early if we're past the exit node + if exit_node then + local exit_pos_end = exit_node.mark:pos_end() + if + (pos[1] > exit_pos_end[1]) + or (pos[1] == exit_pos_end[1] and pos[2] > exit_pos_end[2]) + then + snippet:remove_from_jumplist() + luasnip.session.current_nodes[get_current_buf()] = nil + + return false + end + end + + node = snippet.inner_first:jump_into(1, true) + while node ~= nil and node.next ~= nil and node ~= snippet do + local n_next = node.next + local next_pos = n_next and n_next.mark:pos_begin() + local candidate = n_next ~= snippet and next_pos and (pos[1] < next_pos[1]) + or (pos[1] == next_pos[1] and pos[2] < next_pos[2]) + + -- Past unmarked exit node, exit early + if n_next == nil or n_next == snippet.next then + snippet:remove_from_jumplist() + luasnip.session.current_nodes[get_current_buf()] = nil + + return false + end + + if candidate then + luasnip.session.current_nodes[get_current_buf()] = node + return true + end + + local ok + ok, node = pcall(node.jump_from, node, 1, true) -- no_move until last stop + if not ok then + snippet:remove_from_jumplist() + luasnip.session.current_nodes[get_current_buf()] = nil + + return false + end + end + + -- No candidate, but have an exit node + if exit_node then + -- to jump to the exit node, seek to snippet + luasnip.session.current_nodes[get_current_buf()] = snippet + return true + end + + -- No exit node, exit from snippet + snippet:remove_from_jumplist() + luasnip.session.current_nodes[get_current_buf()] = nil + return false + end + options.builtin.cmp = { confirm_opts = { behavior = cmp.ConfirmBehavior.Replace, - select = true, + select = false, + }, + experimental = { + ghost_text = true, + native_menu = false, }, formatting = { kind_icons = { @@ -62,23 +161,28 @@ M.config = function() Value = " ", Variable = " ", }, + source_names = { + nvim_lsp = "(LSP)", + emoji = "(Emoji)", + path = "(Path)", + calc = "(Calc)", + cmp_tabnine = "(Tabnine)", + vsnip = "(Snippet)", + luasnip = "(Snippet)", + buffer = "(Buffer)", + }, + duplicates = { + buffer = 1, + path = 1, + nvim_lsp = 0, + luasnip = 1, + }, + duplicates_default = 0, format = function(entry, vim_item) vim_item.kind = options.builtin.cmp.formatting.kind_icons[vim_item.kind] - vim_item.menu = ({ - nvim_lsp = "(LSP)", - emoji = "(Emoji)", - path = "(Path)", - calc = "(Calc)", - cmp_tabnine = "(Tabnine)", - vsnip = "(Snippet)", - luasnip = "(Snippet)", - buffer = "(Buffer)", - })[entry.source.name] - vim_item.dup = ({ - buffer = 1, - path = 1, - nvim_lsp = 0, - })[entry.source.name] or 0 + vim_item.menu = options.builtin.cmp.formatting.source_names[entry.source.name] + vim_item.dup = options.builtin.cmp.formatting.duplicates[entry.source.name] + or options.builtin.cmp.formatting.duplicates_default return vim_item end, }, @@ -107,10 +211,16 @@ M.config = function() ["<C-f>"] = cmp.mapping.scroll_docs(4), -- TODO: potentially fix emmet nonsense ["<Tab>"] = cmp.mapping(function() - if vim.fn.pumvisible() == 1 then - vim.fn.feedkeys(T "<down>", "n") - elseif luasnip.expand_or_jumpable() then - vim.fn.feedkeys(T "<Plug>luasnip-expand-or-jump", "") + if cmp.visible() then + cmp.select_next_item() + elseif luasnip.expandable() then + luasnip.expand() + elseif + inside_snippet() + and seek_luasnip_cursor_node() + and luasnip.jumpable() + then + luasnip.jump(1) elseif check_backspace() then vim.fn.feedkeys(T "<Tab>", "n") elseif is_emmet_active() then @@ -123,10 +233,10 @@ M.config = function() "s", }), ["<S-Tab>"] = cmp.mapping(function(fallback) - if vim.fn.pumvisible() == 1 then - vim.fn.feedkeys(T "<up>", "n") - elseif luasnip.jumpable(-1) then - vim.fn.feedkeys(T "<Plug>luasnip-jump-prev", "") + if cmp.visible() then + cmp.select_prev_item() + elseif inside_snippet() and luasnip.jumpable(-1) then + luasnip.jump(-1) else fallback() end @@ -138,12 +248,16 @@ M.config = function() ["<C-Space>"] = cmp.mapping.complete(), ["<C-e>"] = cmp.mapping.close(), ["<CR>"] = cmp.mapping(function(fallback) - if not require("cmp").confirm(options.builtin.cmp.confirm_opts) then - if luasnip.jumpable() then - vim.fn.feedkeys(T "<Plug>luasnip-jump-next", "") - else + if cmp.visible() and cmp.confirm(options.builtin.cmp.confirm_opts) then + return + end + + if inside_snippet() and seek_luasnip_cursor_node() and luasnip.jumpable() then + if not luasnip.jump(1) then fallback() end + else + fallback() end end), }, diff --git a/.config/nvim/lua/core/commands.lua b/.config/nvim/lua/core/commands.lua index 34033d4..8a56c40 100644 --- a/.config/nvim/lua/core/commands.lua +++ b/.config/nvim/lua/core/commands.lua @@ -10,7 +10,8 @@ M.defaults = { endif endfunction ]], - [[command! NvimInfo lua require('core.info').toggle_popup(vim.bo.filetype)]], + [[ command! NvimInfo lua require('core.info').toggle_popup(vim.bo.filetype) ]], + [[ command! NvimCacheReset lua require('utils.hooks').reset_cache() ]], } M.load = function(commands) diff --git a/.config/nvim/lua/core/info.lua b/.config/nvim/lua/core/info.lua index bb53644..cf4387e 100644 --- a/.config/nvim/lua/core/info.lua +++ b/.config/nvim/lua/core/info.lua @@ -11,6 +11,7 @@ local M = { local fmt = string.format local text = require "interface.text" +local lsp_utils = require "lsp.utils" local function str_list(list) return fmt("[ %s ]", table.concat(list, ", ")) @@ -75,7 +76,7 @@ local function tbl_set_highlight(terms, highlight_group) end local function make_client_info(client) - local client_enabled_caps = require("lsp.utils").get_ls_capabilities(client.id) + local client_enabled_caps = lsp_utils.get_client_capabilities(client.id) local name = client.name local id = client.id local document_formatting = client.resolved_capabilities.document_formatting @@ -97,8 +98,7 @@ local function make_client_info(client) end function M.toggle_popup(ft) - local lsp_utils = require "lsp.utils" - local clients = lsp_utils.get_active_client_by_ft(ft) + local clients = lsp_utils.get_active_clients_by_ft(ft) local client_names = {} local header = { diff --git a/.config/nvim/lua/core/nvimtree.lua b/.config/nvim/lua/core/nvimtree.lua index ff8db15..86554ab 100644 --- a/.config/nvim/lua/core/nvimtree.lua +++ b/.config/nvim/lua/core/nvimtree.lua @@ -6,9 +6,9 @@ function M.config() active = true, on_config_done = nil, setup = { - auto_open = 0, + open_on_setup = 0, auto_close = 1, - tab_open = 0, + open_on_tab = 0, update_focused_file = { enable = 1, }, diff --git a/.config/nvim/lua/core/telescope.lua b/.config/nvim/lua/core/telescope.lua index d394884..108b6f3 100644 --- a/.config/nvim/lua/core/telescope.lua +++ b/.config/nvim/lua/core/telescope.lua @@ -91,6 +91,26 @@ function M.config() }) end +function M.code_actions() + local opts = { + winblend = 15, + layout_config = { + prompt_position = "top", + width = 80, + height = 12, + }, + borderchars = { + prompt = { "─", "│", " ", "│", "╭", "╮", "│", "│" }, + results = { "─", "│", "─", "│", "├", "┤", "╯", "╰" }, + preview = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" }, + }, + border = {}, + previewer = false, + shorten_path = false, + } + require("telescope.builtin").lsp_code_actions(require("telescope.themes").get_dropdown(opts)) +end + function M.setup() local telescope = require "telescope" diff --git a/.config/nvim/lua/core/which-key.lua b/.config/nvim/lua/core/which-key.lua index db67055..dca1d77 100644 --- a/.config/nvim/lua/core/which-key.lua +++ b/.config/nvim/lua/core/which-key.lua @@ -139,7 +139,7 @@ M.config = function() l = { name = "LSP", - a = { "<cmd>lua vim.lsp.buf.code_action()<cr>", "Code Action" }, + a = { "<cmd>lua require('core.telescope').code_actions()<cr>", "Code Action" }, d = { "<cmd>Telescope lsp_document_diagnostics<cr>", "Document Diagnostics", @@ -159,6 +159,7 @@ M.config = function() "<cmd>lua vim.lsp.diagnostic.goto_prev({popup_opts = {border = options.lsp.popup_border}})<cr>", "Prev Diagnostic", }, + l = { "<cmd>lua vim.lsp.codelens.run()<cr>", "CodeLens Action" }, p = { name = "Peek", d = { "<cmd>lua require('lsp.peek').Peek('definition')<cr>", "Definition" }, |