feat: merge some changes from kickstart.nvim

This commit is contained in:
Matej Janezic 2024-12-29 13:49:05 +01:00
parent af404f2329
commit d97244ca10
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
5 changed files with 198 additions and 124 deletions

View File

@ -1,41 +1,65 @@
function Leave_snippet() -- function Leave_snippet()
if -- if
((vim.v.event.old_mode == 's' and vim.v.event.new_mode == 'n') or vim.v.event.old_mode == 'i') -- ((vim.v.event.old_mode == 's' and vim.v.event.new_mode == 'n') or vim.v.event.old_mode == 'i')
and require('luasnip').session.current_nodes[vim.api.nvim_get_current_buf()] -- and require('luasnip').session.current_nodes[vim.api.nvim_get_current_buf()]
and not require('luasnip').session.jump_active -- and not require('luasnip').session.jump_active
then -- then
require('luasnip').unlink_current() -- require('luasnip').unlink_current()
end -- end
end -- end
--
vim.api.nvim_command [[ -- vim.api.nvim_command [[
autocmd ModeChanged * lua Leave_snippet() -- autocmd ModeChanged * lua Leave_snippet()
]] -- ]]
--
vim.cmd 'highlight! link CmpPmenu Pmenu' -- vim.cmd 'highlight! link CmpPmenu Pmenu'
vim.cmd 'highlight! link CmpPmenuBorder Pmenu' -- vim.cmd 'highlight! link CmpPmenuBorder Pmenu'
vim.cmd 'highlight! CmpPmenu guibg=#282828' -- vim.cmd 'highlight! CmpPmenu guibg=#282828'
vim.cmd 'highlight! CmpPmenuBorder guifg=#615750' -- vim.cmd 'highlight! CmpPmenuBorder guifg=#615750'
return { return {
{ {
'hrsh7th/nvim-cmp', 'hrsh7th/nvim-cmp',
event = 'InsertEnter',
dependencies = { dependencies = {
'hrsh7th/cmp-nvim-lsp', -- Snippet Engine & its associated nvim-cmp source
{
'L3MON4D3/LuaSnip', 'L3MON4D3/LuaSnip',
'saadparwaiz1/cmp_luasnip', build = (function()
-- Build Step is needed for regex support in snippets.
-- This step is not supported in many windows environments.
-- Remove the below condition to re-enable on windows.
if vim.fn.has 'win32' == 1 or vim.fn.executable 'make' == 0 then
return
end
return 'make install_jsregexp'
end)(),
dependencies = {
-- `friendly-snippets` contains a variety of premade snippets.
-- See the README about individual language/framework/plugin snippets:
-- https://github.com/rafamadriz/friendly-snippets
{
'rafamadriz/friendly-snippets', 'rafamadriz/friendly-snippets',
config = function()
require('luasnip.loaders.from_vscode').lazy_load()
end,
},
},
},
'saadparwaiz1/cmp_luasnip',
-- Adds other completion capabilities.
-- nvim-cmp does not ship with all sources by default. They are split
-- into multiple repos for maintenance purposes.
'hrsh7th/cmp-nvim-lsp',
'hrsh7th/cmp-path',
}, },
config = function() config = function()
-- nvim-cmp setup
local cmp = require 'cmp' local cmp = require 'cmp'
local luasnip = require 'luasnip' local luasnip = require 'luasnip'
require('luasnip.loaders.from_vscode').lazy_load()
local border = require('utils').nvim_open_win_border
luasnip.config.setup {} luasnip.config.setup {}
local border = require('utils').nvim_open_win_border
local highlight_opts = 'Normal:CmpPmenu,FloatBorder:CmpPmenuBorder,CursorLine:PmenuSel,Search:None' local highlight_opts = 'Normal:CmpPmenu,FloatBorder:CmpPmenuBorder,CursorLine:PmenuSel,Search:None'
cmp.setup { cmp.setup {
@ -44,40 +68,37 @@ return {
luasnip.lsp_expand(args.body) luasnip.lsp_expand(args.body)
end, end,
}, },
completion = { completion = { completeopt = 'menu,menuone,noinsert' },
completeopt = 'menu,menuone,noinsert',
},
mapping = cmp.mapping.preset.insert { mapping = cmp.mapping.preset.insert {
['<C-n>'] = cmp.mapping.select_next_item(), ['<C-n>'] = cmp.mapping.select_next_item(),
['<C-p>'] = cmp.mapping.select_prev_item(), ['<C-p>'] = cmp.mapping.select_prev_item(),
['<C-d>'] = cmp.mapping.scroll_docs(-4), ['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4), ['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-y>'] = cmp.mapping.confirm { select = true },
['<C-Space>'] = cmp.mapping.complete {}, ['<C-Space>'] = cmp.mapping.complete {},
['<CR>'] = cmp.mapping.confirm { select = true },
['<C-e>'] = cmp.mapping.close(), ['<C-l>'] = cmp.mapping(function()
['<Tab>'] = cmp.mapping(function(fallback) if luasnip.expand_or_locally_jumpable() then
if cmp.visible() then luasnip.expand_or_jump()
cmp.select_next_item()
elseif luasnip.locally_jumpable() then
luasnip.jump(1)
else
fallback()
end end
end, { 'i', 's' }), end, { 'i', 's' }),
['<S-Tab>'] = cmp.mapping(function(fallback) ['<C-h>'] = cmp.mapping(function()
if cmp.visible() then if luasnip.locally_jumpable(-1) then
cmp.select_prev_item()
elseif luasnip.locally_jumpable(-1) then
luasnip.jump(-1) luasnip.jump(-1)
else
fallback()
end end
end, { 'i', 's' }), end, { 'i', 's' }),
}, },
sources = { sources = {
{
name = 'lazydev',
-- set group index to 0 to skip loading LuaLS completions as lazydev recommends it
group_index = 0,
},
{ name = 'nvim_lsp' }, { name = 'nvim_lsp' },
{ name = 'luasnip' }, { name = 'luasnip' },
{ name = 'crates' }, { name = 'path' },
-- { name = 'crates' },
}, },
window = { window = {
completion = { completion = {

View File

@ -1,109 +1,141 @@
local on_attach = function(_, bufnr) vim.api.nvim_create_autocmd('LspAttach', {
local nmap = function(keys, func, desc) group = vim.api.nvim_create_augroup('kickstart-lsp-attach', { clear = true }),
if desc then callback = function(event)
desc = 'LSP: ' .. desc local map = function(keys, func, desc, mode)
end mode = mode or 'n'
vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc }) vim.keymap.set(mode, keys, func, { buffer = event.buf, desc = 'LSP: ' .. desc })
end end
map('<leader>D', require('telescope.builtin').lsp_type_definitions, 'Type [D]efinition')
map('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction', { 'n', 'x' })
map('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
map('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
map('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')
map('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
map('gI', require('telescope.builtin').lsp_implementations, '[G]oto [I]mplementation')
map('gd', require('telescope.builtin').lsp_definitions, '[G]oto [D]efinition')
map('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
local delayed_format = function() local delayed_format = function()
vim.lsp.buf.format { timeout_ms = 2000 } vim.lsp.buf.format { timeout_ms = 2000 }
end end
nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame') map('<leader>ff', delayed_format, '[F]ormat')
nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction') --
nmap('<leader>ff', delayed_format, '[F]ormat') -- -- See `:help K` for why this keymap
-- nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
-- nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation')
nmap('gd', vim.lsp.buf.definition, '[G]oto [D]efinition') local client = vim.lsp.get_client_by_id(event.data.client_id)
nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences') if client and client.supports_method(vim.lsp.protocol.Methods.textDocument_documentHighlight) then
nmap('gI', vim.lsp.buf.implementation, '[G]oto [I]mplementation') local highlight_augroup = vim.api.nvim_create_augroup('kickstart-lsp-highlight', { clear = false })
nmap('<leader>D', vim.lsp.buf.type_definition, 'Type [D]efinition') vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols') buffer = event.buf,
nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols') group = highlight_augroup,
callback = vim.lsp.buf.document_highlight,
})
-- See `:help K` for why this keymap vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
nmap('K', vim.lsp.buf.hover, 'Hover Documentation') buffer = event.buf,
nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation') group = highlight_augroup,
callback = vim.lsp.buf.clear_references,
})
-- Lesser used LSP functionality vim.api.nvim_create_autocmd('LspDetach', {
nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration') group = vim.api.nvim_create_augroup('kickstart-lsp-detach', { clear = true }),
nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder') callback = function(event2)
nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder') vim.lsp.buf.clear_references()
nmap('<leader>wl', function() vim.api.nvim_clear_autocmds { group = 'kickstart-lsp-highlight', buffer = event2.buf }
print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end,
end, '[W]orkspace [L]ist Folders') })
-- Create a command `:Format` local to the LSP buffer
vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
vim.lsp.buf.format()
end, { desc = 'Format current buffer with LSP' })
vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, { command = 'lua pcall(vim.lsp.buf.document_highlight)', buffer = bufnr })
vim.api.nvim_create_autocmd({ 'CursorMoved' }, { command = 'lua pcall(vim.lsp.buf.clear_references)', buffer = bufnr })
end end
if client and client.supports_method(vim.lsp.protocol.Methods.textDocument_inlayHint) then
map('<leader>th', function()
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled { bufnr = event.buf })
end, '[T]oggle Inlay [H]ints')
end
end,
})
return { return {
{ {
-- LSP Configuration & Plugins -- LSP Configuration & Plugins
'neovim/nvim-lspconfig', 'neovim/nvim-lspconfig',
dependencies = { dependencies = {
-- Automatically install LSPs to stdpath for neovim
'williamboman/mason.nvim',
'williamboman/mason-lspconfig.nvim',
-- adds extra functionality over rust_analyzer -- Automatically install LSPs and related tools to stdpath for Neovim
{ 'williamboman/mason.nvim', config = true }, -- NOTE: Must be loaded before dependants
'williamboman/mason-lspconfig.nvim',
'WhoIsSethDaniel/mason-tool-installer.nvim',
-- Useful status updates for LSP.
{ 'j-hui/fidget.nvim', opts = {} },
-- Allows extra capabilities provided by nvim-cmp
'hrsh7th/cmp-nvim-lsp',
{ {
'mrcjkb/rustaceanvim', 'mrcjkb/rustaceanvim',
version = '^4', version = '^4',
ft = { 'rust' }, ft = { 'rust' },
}, },
-- Useful status updates for LSP {
{ 'j-hui/fidget.nvim', opts = {} }, -- `lazydev` configures Lua LSP for your Neovim config, runtime and plugins
-- used for completion, annotations and signatures of Neovim apis
-- Additional lua configuration, makes nvim stuff amazing! 'folke/lazydev.nvim',
'folke/neodev.nvim', ft = 'lua',
opts = {
library = {
-- Load luvit types when the `vim.uv` word is found
{ path = 'luvit-meta/library', words = { 'vim%.uv' } },
},
},
},
{ 'Bilal2453/luvit-meta', lazy = true },
}, },
config = function() config = function()
local servers = { local servers = {
clangd = {}, clangd = {},
gopls = {},
marksman = {}, marksman = {},
pyright = {}, gopls = {},
tsserver = {}, ruff = {},
pyright = {
pyright = {
disableOrganizeImports = true,
},
},
ts_ls = {},
lua_ls = { lua_ls = {
Lua = { Lua = {
workspace = { checkThirdParty = false }, workspace = { checkThirdParty = false },
telemetry = { enable = false }, telemetry = { enable = false },
}, },
}, },
stylua = {},
} }
-- Setup neovim lua configuration
require('neodev').setup()
-- nvim-cmp supports additional completion capabilities, so broadcast that to servers
local capabilities = vim.lsp.protocol.make_client_capabilities() local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities) capabilities = vim.tbl_deep_extend('force', capabilities, require('cmp_nvim_lsp').default_capabilities())
-- Setup mason so it can manage external tooling -- Setup mason so it can manage external tooling
require('mason').setup() require('mason').setup()
-- Ensure the servers above are installed -- Ensure the servers above are installed
local mason_lspconfig = require 'mason-lspconfig' require('mason-tool-installer').setup { ensure_installed = vim.tbl_keys(servers) }
mason_lspconfig.setup { require('mason-lspconfig').setup {
ensure_installed = vim.tbl_keys(servers), handlers = {
}
mason_lspconfig.setup_handlers {
function(server_name) function(server_name)
require('lspconfig')[server_name].setup { local server = servers[server_name] or {}
capabilities = capabilities, -- This handles overriding only values explicitly passed
on_attach = on_attach, -- by the server configuration above. Useful when disabling
settings = servers[server_name], -- certain features of an LSP (for example, turning off formatting for ts_ls)
} server.capabilities = vim.tbl_deep_extend('force', {}, capabilities, server.capabilities or {})
require('lspconfig')[server_name].setup(server)
end, end,
},
} }
vim.g.rustaceanvim = { vim.g.rustaceanvim = {
@ -113,7 +145,6 @@ return {
}, },
}, },
server = { server = {
on_attach = on_attach,
settings = { settings = {
['rust-analyzer'] = { ['rust-analyzer'] = {
cargo = { cargo = {
@ -124,8 +155,13 @@ return {
checkOnSave = { checkOnSave = {
command = 'clippy', command = 'clippy',
}, },
-- leave rainbow higlightifg to plugin -- leave rainbow higlight to plugin
rainbowHighlightOn = false, rainbowHighlightOn = false,
useLibraryCodeForTypes = true,
autoSearchPaths = true,
autoImportCompletions = false,
reportMissingImports = true,
followImportForHints = true,
}, },
}, },
}, },

6
lua/plugins/neogen.lua Normal file
View File

@ -0,0 +1,6 @@
return {
'danymat/neogen',
config = true,
-- Uncomment next line if you want to follow only stable versions
-- version = "*"
}

View File

@ -1,23 +1,22 @@
return { return {
-- inject lsp formatting, diagonstics etc -- inject lsp formatting, diagonstics etc
'nvimtools/none-ls.nvim', 'nvimtools/none-ls.nvim',
dependencies = { 'nvim-lua/plenary.nvim' }, dependencies = {
'nvim-lua/plenary.nvim',
},
config = function() config = function()
local null_ls = require 'null-ls' local none_ls = require 'null-ls'
null_ls.setup { none_ls.setup {
sources = { sources = {
-- general -- general
null_ls.builtins.code_actions.gitsigns, none_ls.builtins.code_actions.gitsigns,
-- python -- python
-- null_ls.builtins.diagnostics.pylint, -- none_ls.builtins.formatting.black,
null_ls.builtins.formatting.black,
-- null_ls.builtins.formatting.isort,
null_ls.builtins.diagnostics.ruff,
-- typescript -- typescript
null_ls.builtins.formatting.prettier, none_ls.builtins.formatting.prettier,
-- lua -- lua
null_ls.builtins.formatting.stylua, none_ls.builtins.formatting.stylua,
}, },
} }
end, end,

View File

@ -10,7 +10,19 @@ return {
vim.defer_fn(function() vim.defer_fn(function()
require('nvim-treesitter.configs').setup { require('nvim-treesitter.configs').setup {
-- Add languages to be installed here that you want installed for treesitter -- Add languages to be installed here that you want installed for treesitter
ensure_installed = { 'c', 'cpp', 'go', 'lua', 'python', 'rust', 'tsx', 'typescript', 'vimdoc', 'vim', 'markdown' }, ensure_installed = {
'c',
'cpp',
'go',
'lua',
'python',
'rust',
'tsx',
'typescript',
'vimdoc',
'vim',
'markdown',
},
-- Autoinstall languages that are not installed. Defaults to false (but you can change for yourself!) -- Autoinstall languages that are not installed. Defaults to false (but you can change for yourself!)
auto_install = true, auto_install = true,