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