diff --git a/nvim/lua/plugins/editor-components.lua b/nvim/lua/plugins/editor-components.lua index c306670..61abcbb 100644 --- a/nvim/lua/plugins/editor-components.lua +++ b/nvim/lua/plugins/editor-components.lua @@ -38,57 +38,260 @@ return { }, keys = { { "bs", require("telescope.builtin").buffers, desc = "Buffer Search" }, + { + "sS", + lazyUtils.telescope("lsp_dynamic_workspace_symbols", { + symbols = { + "Class", + "Function", + "Method", + "Constructor", + "Interface", + "Module", + "Struct", + "Trait", + "Field", + }, + }), + }, + { + "tr", + function() + require("telescope.builtin").resume() + end, + }, }, opts = function(_, opts) - local extensions = { - fzf = { - fuzzy = true, -- false will only do exact matching - override_generic_sorter = true, -- override the generic sorter - override_file_sorter = true, -- override the file sorter - case_mode = "smart_case", -- or "ignore_case" or "respect_case" - -- the default case_mode is "smart_case" - }, - } - vim.tbl_extend("force", extensions, opts.extensions or {}) - opts.extensions = extensions - local actions = require("telescope.actions") - if opts.defaults == nil then - opts.defaults = {} - end - if opts.mappings == nil then - opts.mappings = { n = {}, i = {} } - end + local fzf_opts = { + fuzzy = true, -- false will only do exact matching + override_generic_sorter = true, -- override the generic sorter + override_file_sorter = true, -- override the file sorter + case_mode = "smart_case", -- or "ignore_case" or "respect_case" + -- the default case_mode is "smart_case" + } - local mappings = opts.defaults.mappings + local overrides = { + extensions = { + fzf = fzf_opts, + }, + pickers = { + lsp_dynamic_workspace_symbols = { + -- Manually set sorter, for some reason not picked up automatically + sorter = require("telescope").extensions.fzf.native_fzf_sorter(fzf_opts), + }, + }, + defaults = { + mappings = { + i = { + [""] = actions.cycle_history_next, + [""] = actions.cycle_history_prev, + [""] = actions.move_selection_next, + [""] = actions.move_selection_previous, + }, + }, + }, + } - -- @todo revert to defaults - mappings.i[""] = actions.cycle_history_next - mappings.i[""] = actions.cycle_history_prev - mappings.i[""] = actions.move_selection_next - mappings.i[""] = actions.move_selection_previous + return vim.tbl_deep_extend("force", opts, overrides) end, init = function() require("telescope").load_extension("fzf") end, }, { - -- Completions "hrsh7th/nvim-cmp", - --- @param opts cmp.ConfigSchema - opts = function(_, opts) + lazy = true, + event = { "InsertEnter", "CmdlineEnter" }, + dependencies = { + "hrsh7th/nvim-cmp", + "hrsh7th/cmp-nvim-lua", + "hrsh7th/cmp-buffer", + "hrsh7th/cmp-cmdline", + "hrsh7th/cmp-nvim-lsp", + "hrsh7th/cmp-nvim-lsp-document-symbol", + "hrsh7th/cmp-path", + "f3fora/cmp-spell", + "ray-x/cmp-treesitter", + "saadparwaiz1/cmp_luasnip", + "tamago324/cmp-zsh", + "uga-rosa/cmp-dictionary", + "amarakon/nvim-cmp-fonts", + "onsails/lspkind.nvim", + }, + opts = function(_, _) local cmp = require("cmp") - opts.mapping = cmp.mapping.preset.insert({ - [""] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Select }), - [""] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Select }), + local sources = { + { + { name = "nvim_lsp" }, + { name = "nvim_lua" }, + { name = "luasnip" }, + }, + { + { name = "treesitter" }, + { name = "buffer" }, + { name = "path" }, + { name = "spell" }, + { name = "dictionary" }, + { name = "fonts", options = { space_filter = "-" } }, + }, + } + if vim.fn.has("win32") ~= 1 then + table.insert(sources[2], { name = "zsh" }) + end + + local winhighlight = "Normal:Normal,FloatBorder:FloatBorder,CursorLine:CursorLine,Search:Search" + + local luasnip = require("luasnip") + require("luasnip.loaders.from_vscode").load() + + local function has_words_before() + local line, col = unpack(vim.api.nvim_win_get_cursor(0)) + + return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil + end + + -- is used by Copilot, I found the plugin doesn't work + -- if I use for nvim-cmp or any other plugin + -- local mappings for nvim-cmp. + local mapping = { [""] = cmp.mapping.scroll_docs(-4), [""] = cmp.mapping.scroll_docs(4), + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif has_words_before() then + cmp.mapping.complete({}) + else + fallback() + end + end, { "i", "s", "c" }), + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + else + fallback() + end + end, { "i", "s", "c" }), + -- [""] = cmp.mapping(function(fallback) + -- if luasnip.expand_or_jumpable() then + -- luasnip.expand_or_jump() + -- else + -- fallback() + -- end + -- end, { "i", "s" }), + -- [""] = cmp.mapping(function(fallback) + -- if luasnip.jumpable(-1) then + -- luasnip.jump(-1) + -- else + -- fallback() + -- end + -- end, { "i", "s" }), [""] = cmp.mapping.complete({}), [""] = cmp.mapping.abort(), [""] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. + } + + -- cmp plugin + local CMP_SYMBOLS = { + Text = " ", + Method = " ", + Function = "", + Constructor = " ", + Field = " ", + Variable = " ", + Class = " ", + Interface = " ", + Module = " ", + Property = " ", + Unit = "塞", + Value = " ", + Enum = " ", + Keyword = " ", + Snippet = " ", + Color = " ", + File = " ", + Reference = " ", + Folder = " ", + EnumMember = " ", + Constant = " ", + Struct = " ", + Event = "", + Operator = " ", + TypeParameter = " ", + } + + cmp.setup.filetype("gitcommit", { + sources = cmp.config.sources({ + { name = "cmp_git" }, + { name = "nvim_lsp" }, + { name = "nvim_lua" }, + { name = "nvim_lsp_document_symbol" }, + { name = "buffer" }, + { name = "dictionary" }, + { name = "spell" }, + { name = "path" }, + }), }) + + cmp.setup.cmdline("/", { + mapping = cmp.mapping.preset.cmdline(), + sources = { + { name = "nvim_lsp" }, + { name = "nvim_lsp_document_symbol" }, + { name = "dictionary" }, + { name = "buffer" }, + }, + }) + + cmp.setup.cmdline(":", { + mapping = cmp.mapping.preset.cmdline(), + sources = cmp.config.sources({ + { name = "cmdline" }, + { name = "path", options = { trailing_slash = true, label_trailing_slash = true } }, + { name = "dictionary" }, + { name = "buffer" }, + }), + }) + + return { + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end, + }, + window = { + completion = cmp.config.window.bordered({ winhighlight = winhighlight }), + documentation = cmp.config.window.bordered({ winhighlight = winhighlight }), + preview = cmp.config.window.bordered({ winhighlight = winhighlight }), + }, + formatting = { + format = require("lspkind").cmp_format({ + mode = "symbol", + ellipsis_char = "…", + menu = { + nvim_lsp = "lsp", + nvim_lua = "lua", + luasnip = "snip", + buffer = "buf", + path = "path", + calc = "calc", + vsnip = "snip", + nvim_lsp_signature_help = "sign", + treesitter = "ts", + spell = "spel", + dictionary = "dict", + zsh = "zsh", + ["vim-dadbod-completion"] = "db", + }, + symbol_map = CMP_SYMBOLS, + }), + }, + mapping = cmp.mapping.preset.insert(mapping), + sources = cmp.config.sources(sources[1], sources[2]), + } end, }, }