]> git.rmz.io Git - dotfiles.git/blob - nvim/lua/plugins/coding.lua
131fc4acd874948c4d19f94aeeaa5948e7c7f66b
[dotfiles.git] / nvim / lua / plugins / coding.lua
1 ---@type LazyPluginSpec
2 return {
3 -- snippets
4 {
5 "L3MON4D3/LuaSnip",
6 -- disable luasnip bindings for <tab> and <s-tab>
7 dependencies = {
8 {
9 "honza/vim-snippets",
10 config = function()
11 require("luasnip.loaders.from_snipmate").lazy_load()
12 require("luasnip.loaders.from_lua").load({ paths = vim.fn.stdpath("config") .. "/lua/snippets" })
13 end,
14 },
15 },
16 keys = function()
17 return {}
18 end,
19 opts = {
20 store_selection_keys = "<Tab>",
21 },
22 },
23
24 -- auto completion
25 {
26 "hrsh7th/nvim-cmp",
27 version = false, -- last release is way too old
28 event = "InsertEnter",
29 dependencies = {
30 "hrsh7th/cmp-nvim-lsp",
31 "hrsh7th/cmp-buffer",
32 "hrsh7th/cmp-path",
33 "saadparwaiz1/cmp_luasnip",
34 },
35 ---@param opts cmp.ConfigSchema
36 opts = function(_, opts)
37 local has_words_before = function()
38 unpack = unpack or table.unpack
39 local line, col = unpack(vim.api.nvim_win_get_cursor(0))
40 return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
41 end
42
43 local cmp = require("cmp")
44 local luasnip = require("luasnip")
45
46 local upstream_format = opts.formatting.format
47 opts.formatting.format = function(entry, vim_item)
48 vim_item = upstream_format(entry, vim_item)
49 local menu = {
50 nvim_lsp = "[lsp]",
51 luasnip = "[snip]",
52 buffer = "[buf]",
53 path = "[path]",
54 }
55 vim_item.menu = menu[entry.source.name]
56 return vim_item
57 end
58
59 opts.completion = vim.tbl_extend("force", opts.completion, {
60 completeopt = "menu,menuone,noselect",
61 })
62 -- TODO: review if I want to keep any of LazyVim's mappings
63 opts.mapping = {
64 -- lazyvims
65 ["<C-n>"] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }),
66 ["<C-p>"] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }),
67 ["<C-b>"] = cmp.mapping.scroll_docs(-4),
68 ["<C-f>"] = cmp.mapping.scroll_docs(4),
69 ["<C-e>"] = cmp.mapping.abort(),
70 ["<C-Space>"] = cmp.mapping.complete(),
71 -- mine
72 ["<Tab>"] = cmp.mapping(function(fallback)
73 if cmp.visible() then
74 cmp.select_next_item()
75 elseif luasnip.expand_or_jumpable() then
76 luasnip.expand_or_jump()
77 elseif has_words_before() then
78 cmp.complete()
79 else
80 fallback()
81 end
82 end, { "i", "s" }),
83 ["<S-Tab>"] = cmp.mapping(function(fallback)
84 if cmp.visible() then
85 cmp.select_prev_item()
86 elseif luasnip.jumpable(-1) then
87 luasnip.jump(-1)
88 else
89 fallback()
90 end
91 end, { "i", "s" }),
92 }
93 end,
94 },
95
96 -- auto pairs
97 {
98 "echasnovski/mini.pairs",
99 },
100
101 -- surround
102 {
103 "echasnovski/mini.surround",
104 keys = function()
105 -- HACK: use function to disable merging with LazyVim's keys definition
106 return {
107 { "ys", desc = "Add surrounding", "n" },
108 { "S", desc = "Add surrounding", "x" },
109 { "ds", desc = "Delete surrounding" },
110 { "cs", desc = "Change surrounding" },
111 { "yss", "ys_", remap = true },
112 }
113 end,
114 opts = {
115 mappings = {
116 -- TODO: this is tpope/surround like, but should consider using vim-sandwich mappings
117 -- see: :h MiniSurround-vim-surround-config
118 add = "ys",
119 delete = "ds",
120 find = "",
121 find_left = "",
122 highlight = "",
123 replace = "cs",
124 update_n_lines = "",
125 },
126 },
127 config = function(_, opts)
128 require("mini.surround").setup(opts)
129 -- remap visual
130 vim.keymap.del("x", "ys", { silent = true })
131 vim.keymap.set("x", "S", [[:<C-u>lua MiniSurround.add('visual')<CR>]], { silent = true })
132 end,
133 },
134 -- comments
135 {
136 "numToStr/Comment.nvim",
137 opts = {
138 toggler = {
139 line = "gcc",
140 block = "gbb",
141 },
142 mappings = {
143 basic = true,
144 extra = true,
145 },
146 },
147 },
148 { "JoosepAlviste/nvim-ts-context-commentstring", enabled = false },
149 { "echasnovski/mini.comment", enabled = false },
150 }