From e7db0d487f413542803b2c3559f23fe8424cb7bc Mon Sep 17 00:00:00 2001 From: Sergey Sychugin <40148988+sychugin@users.noreply.github.com> Date: Fri, 19 Jul 2024 22:32:36 +0300 Subject: [PATCH] Keymaps for autocomplete, Setup of Format plugin, Tabs and etc --- init.lua | 140 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 98 insertions(+), 42 deletions(-) diff --git a/init.lua b/init.lua index 7f82265d..b3c49064 100644 --- a/init.lua +++ b/init.lua @@ -154,6 +154,12 @@ vim.opt.cursorline = true -- Minimal number of screen lines to keep above and below the cursor. vim.opt.scrolloff = 10 +-- Глобальные настройки табуляции +vim.o.tabstop = 2 +vim.o.shiftwidth = 2 +vim.o.expandtab = true +vim.o.softtabstop = 2 + -- [[ Basic Keymaps ]] -- See `:help vim.keymap.set()` @@ -161,6 +167,10 @@ vim.opt.scrolloff = 10 vim.opt.hlsearch = true vim.keymap.set('n', '', 'nohlsearch') +-- Move highlighted blocks with J and K +vim.keymap.set('v', 'J', ":m '>+1gv=gv") +vim.keymap.set('v', 'K', ":m '<-2gv=gv") + -- Diagnostic keymaps vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, { desc = 'Go to previous [D]iagnostic message' }) vim.keymap.set('n', ']d', vim.diagnostic.goto_next, { desc = 'Go to next [D]iagnostic message' }) @@ -662,6 +672,9 @@ require('lazy').setup({ end, formatters_by_ft = { lua = { 'stylua' }, + python = { 'isort', 'black' }, + cpp = { 'clang-format' }, + texlab = { 'latexindent' }, -- Conform can also run multiple formatters sequentially -- python = { "isort", "black" }, -- @@ -714,6 +727,12 @@ require('lazy').setup({ local luasnip = require 'luasnip' luasnip.config.setup {} + local has_words_before = function() + unpack = unpack or table.unpack + 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 + cmp.setup { snippet = { expand = function(args) @@ -726,53 +745,90 @@ require('lazy').setup({ -- chosen, you will need to read `:help ins-completion` -- -- No, but seriously. Please read `:help ins-completion`, it is really good! + -- mapping = cmp.mapping.preset.insert { + -- -- Select the [n]ext item + -- [''] = cmp.mapping.select_next_item(), + -- -- Select the [p]revious item + -- [''] = cmp.mapping.select_prev_item(), + -- + -- -- Scroll the documentation window [b]ack / [f]orward + -- [''] = cmp.mapping.scroll_docs(-4), + -- [''] = cmp.mapping.scroll_docs(4), + -- + -- -- Accept ([y]es) the completion. + -- -- This will auto-import if your LSP supports it. + -- -- This will expand snippets if the LSP sent a snippet. + -- [''] = cmp.mapping.confirm { select = true }, + -- + -- -- If you prefer more traditional completion keymaps, + -- -- you can uncomment the following lines + -- --[''] = cmp.mapping.confirm { select = true }, + -- --[''] = cmp.mapping.select_next_item(), + -- --[''] = cmp.mapping.select_prev_item(), + -- + -- -- Manually trigger a completion from nvim-cmp. + -- -- Generally you don't need this, because nvim-cmp will display + -- -- completions whenever it has completion options available. + -- [''] = cmp.mapping.complete {}, + -- + -- -- Think of as moving to the right of your snippet expansion. + -- -- So if you have a snippet that's like: + -- -- function $name($args) + -- -- $body + -- -- end + -- -- + -- -- will move you to the right of each of the expansion locations. + -- -- is similar, except moving you backwards. + -- [''] = cmp.mapping(function() + -- if luasnip.expand_or_locally_jumpable() then + -- luasnip.expand_or_jump() + -- end + -- end, { 'i', 's' }), + -- [''] = cmp.mapping(function() + -- if luasnip.locally_jumpable(-1) then + -- luasnip.jump(-1) + -- end + -- end, { 'i', 's' }), + -- + -- -- For more advanced Luasnip keymaps (e.g. selecting choice nodes, expansion) see: + -- -- https://github.com/L3MON4D3/LuaSnip?tab=readme-ov-file#keymaps + -- }, + + -- Настройки горячих клавиш для автодополнения от Александра Романова mapping = cmp.mapping.preset.insert { - -- Select the [n]ext item - [''] = cmp.mapping.select_next_item(), - -- Select the [p]revious item - [''] = cmp.mapping.select_prev_item(), + + -- Прямой пробег по списку автодополнений + [''] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + elseif has_words_before() then + cmp.complete() + else + fallback() + end + end, { 'i', 's' }), + + -- Обратный пробег по списку автодополнений + [''] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + elseif luasnip.jumpable(-1) then + luasnip.jump(-1) + else + fallback() + end + end, { 'i', 's' }), + + -- Отмена автодополнения + [''] = cmp.mapping.abort(), + -- Подтверждение автодополнения + [''] = cmp.mapping.confirm { select = true }, -- Scroll the documentation window [b]ack / [f]orward [''] = cmp.mapping.scroll_docs(-4), [''] = cmp.mapping.scroll_docs(4), - - -- Accept ([y]es) the completion. - -- This will auto-import if your LSP supports it. - -- This will expand snippets if the LSP sent a snippet. - [''] = cmp.mapping.confirm { select = true }, - - -- If you prefer more traditional completion keymaps, - -- you can uncomment the following lines - --[''] = cmp.mapping.confirm { select = true }, - --[''] = cmp.mapping.select_next_item(), - --[''] = cmp.mapping.select_prev_item(), - - -- Manually trigger a completion from nvim-cmp. - -- Generally you don't need this, because nvim-cmp will display - -- completions whenever it has completion options available. - [''] = cmp.mapping.complete {}, - - -- Think of as moving to the right of your snippet expansion. - -- So if you have a snippet that's like: - -- function $name($args) - -- $body - -- end - -- - -- will move you to the right of each of the expansion locations. - -- is similar, except moving you backwards. - [''] = cmp.mapping(function() - if luasnip.expand_or_locally_jumpable() then - luasnip.expand_or_jump() - end - end, { 'i', 's' }), - [''] = cmp.mapping(function() - if luasnip.locally_jumpable(-1) then - luasnip.jump(-1) - end - end, { 'i', 's' }), - - -- For more advanced Luasnip keymaps (e.g. selecting choice nodes, expansion) see: - -- https://github.com/L3MON4D3/LuaSnip?tab=readme-ov-file#keymaps }, sources = { { name = 'nvim_lsp' },