fixed lsp keybindings.

They now work on more than just the first buffer you open of a given
filetype.
This commit is contained in:
Gabe Venberg 2021-12-15 16:23:07 -06:00
parent 83aea3c147
commit 09042586ec
4 changed files with 37 additions and 22 deletions

View file

@ -109,3 +109,5 @@ vim.cmd 'colorscheme moonfly'
--tabline stuff (gt and gT are prev/next tab in stock vim) --tabline stuff (gt and gT are prev/next tab in stock vim)
map('n', 'gf', ':TablineBufferNext<CR>', opts) map('n', 'gf', ':TablineBufferNext<CR>', opts)
map('n', 'gF', ':TablineBufferPrevious<CR>', opts) map('n', 'gF', ':TablineBufferPrevious<CR>', opts)
--gitsigns
map('n', '<leader>hl', ':Gitsigns toggle_current_line_blame<CR>', opts)

View file

@ -2,33 +2,33 @@ local nvim_lsp = require('lspconfig')
-- Use an on_attach function to only map the following keys -- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer -- after the language server attaches to the current buffer
local on_attach = function(client, bufnr) local on_attach = function()
local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end local function set_keymap(...) vim.api.nvim_set_keymap(...) end
local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end local function set_option(...) vim.api.nvim_set_option(...) end
-- Enable completion triggered by <c-x><c-o> -- Enable completion triggered by <c-x><c-o>
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc') set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
-- Mappings. -- Mappings.
local opts = { noremap=true, silent=true } local opts = { noremap=true, silent=true }
-- See `:help vim.lsp.*` for documentation on any of the below functions -- See `:help vim.lsp.*` for documentation on any of the below functions
buf_set_keymap('', ';lc', '<cmd>lua vim.lsp.buf.declaration()<CR>', opts) set_keymap('', ';lc', '<cmd>lua vim.lsp.buf.declaration()<CR>', opts)
buf_set_keymap('', ';lf', '<cmd>lua vim.lsp.buf.definition()<CR>', opts) set_keymap('', ';lf', '<cmd>lua vim.lsp.buf.definition()<CR>', opts)
buf_set_keymap('', ';lh', '<cmd>lua vim.lsp.buf.hover()<CR>', opts) set_keymap('', ';lh', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
buf_set_keymap('', ';li', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts) set_keymap('', ';li', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
buf_set_keymap('', ';ls', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts) set_keymap('', ';ls', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
buf_set_keymap('', ';lwa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts) set_keymap('', ';lwa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts)
buf_set_keymap('', ';lwr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts) set_keymap('', ';lwr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts)
buf_set_keymap('', ';lw', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts) set_keymap('', ';lw', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts)
buf_set_keymap('', ';lt', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts) set_keymap('', ';lt', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
buf_set_keymap('', ';lr', '<cmd>lua vim.lsp.buf.rename()<CR>', opts) set_keymap('', ';lr', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
buf_set_keymap('', ';la', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts) set_keymap('', ';la', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
buf_set_keymap('', ';lc', '<cmd>lua vim.lsp.buf.references()<CR>', opts) set_keymap('', ';lc', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
buf_set_keymap('', ';lo', '<cmd>lua vim.diagnostic.open_float()<CR>', opts) set_keymap('', ';lo', '<cmd>lua vim.diagnostic.open_float()<CR>', opts)
buf_set_keymap('', ';ln', '<cmd>lua vim.diagnostic.goto_prev()<CR>', opts) set_keymap('', ';ln', '<cmd>lua vim.diagnostic.goto_prev()<CR>', opts)
buf_set_keymap('', ';lp', '<cmd>lua vim.diagnostic.goto_next()<CR>', opts) set_keymap('', ';lp', '<cmd>lua vim.diagnostic.goto_next()<CR>', opts)
buf_set_keymap('', ';lm', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts) set_keymap('', ';lm', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts)
end end
@ -36,7 +36,6 @@ end
-- map buffer local keybindings when the language server attaches -- map buffer local keybindings when the language server attaches
local servers = { 'pyright', 'rust_analyzer', 'texlab'} local servers = { 'pyright', 'rust_analyzer', 'texlab'}
for _, lsp in ipairs(servers) do for _, lsp in ipairs(servers) do
on_attach()
nvim_lsp[lsp].setup { nvim_lsp[lsp].setup {
flags = { flags = {
debounce_text_changes = 150, debounce_text_changes = 150,
@ -48,7 +47,6 @@ end
local runtime_path = vim.split(package.path, ';') local runtime_path = vim.split(package.path, ';')
table.insert(runtime_path, "lua/?.lua") table.insert(runtime_path, "lua/?.lua")
table.insert(runtime_path, "lua/?/init.lua") table.insert(runtime_path, "lua/?/init.lua")
on_attach()
require'lspconfig'.sumneko_lua.setup { require'lspconfig'.sumneko_lua.setup {
settings = { settings = {
Lua = { Lua = {
@ -73,3 +71,4 @@ require'lspconfig'.sumneko_lua.setup {
}, },
}, },
} }
on_attach()

View file

@ -5,6 +5,7 @@
local capabilities = vim.lsp.protocol.make_client_capabilities() local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities) capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities)
local luasnip = require 'luasnip'
local lspconfig = require('lspconfig') local lspconfig = require('lspconfig')
@ -23,6 +24,11 @@ vim.o.completeopt = 'menuone,noselect'
-- nvim-cmp setup -- nvim-cmp setup
local cmp = require 'cmp' local cmp = require 'cmp'
cmp.setup { cmp.setup {
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body)
end,
},
mapping = { mapping = {
['<C-p>'] = cmp.mapping.select_prev_item(), ['<C-p>'] = cmp.mapping.select_prev_item(),
['<C-n>'] = cmp.mapping.select_next_item(), ['<C-n>'] = cmp.mapping.select_next_item(),
@ -37,6 +43,8 @@ cmp.setup {
['<Tab>'] = function(fallback) ['<Tab>'] = function(fallback)
if cmp.visible() then if cmp.visible() then
cmp.select_next_item() cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
else else
fallback() fallback()
end end
@ -44,6 +52,8 @@ cmp.setup {
['<S-Tab>'] = function(fallback) ['<S-Tab>'] = function(fallback)
if cmp.visible() then if cmp.visible() then
cmp.select_prev_item() cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else else
fallback() fallback()
end end
@ -51,5 +61,7 @@ cmp.setup {
}, },
sources = { sources = {
{ name = 'nvim_lsp' }, { name = 'nvim_lsp' },
{ name = 'luasnip' },
{ name = 'buffer' }
}, },
} }

View file

@ -90,6 +90,8 @@ return require('packer').startup(function(use)
requires = { requires = {
'neovim/nvim-lspconfig', 'neovim/nvim-lspconfig',
'hrsh7th/cmp-nvim-lsp', 'hrsh7th/cmp-nvim-lsp',
'saadparwaiz1/cmp_luasnip',
'L3MON4D3/LuaSnip'
} }
} }