From 868e566a5ea482104b1355689fb4df8d445f33f6 Mon Sep 17 00:00:00 2001 From: doylet Date: Sun, 24 Mar 2024 16:55:22 +1100 Subject: [PATCH] Fix LSP in nvim --- Internal/os_nvim_init.vim | 108 +++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 60 deletions(-) diff --git a/Internal/os_nvim_init.vim b/Internal/os_nvim_init.vim index e148843..ac8658c 100644 --- a/Internal/os_nvim_init.vim +++ b/Internal/os_nvim_init.vim @@ -22,60 +22,71 @@ call plug#begin(stdpath('config') . '/plugged') " odin for syntax highlighting Plug 'https://github.com/Tetralux/odin.vim' Plug 'https://github.com/sainnhe/gruvbox-material' - - " Lua cache to speed up load times - Plug 'https://github.com/lewis6991/impatient.nvim' Plug 'https://github.com/ggandor/leap.nvim' " lsp-zero begin " LSP Support - Plug 'neovim/nvim-lspconfig' Plug 'williamboman/mason.nvim' Plug 'williamboman/mason-lspconfig.nvim' - - " Autocompletion + Plug 'neovim/nvim-lspconfig' Plug 'hrsh7th/nvim-cmp' + Plug 'hrsh7th/cmp-nvim-lsp' + Plug 'L3MON4D3/LuaSnip' Plug 'hrsh7th/cmp-buffer' Plug 'hrsh7th/cmp-path' - Plug 'saadparwaiz1/cmp_luasnip' - Plug 'hrsh7th/cmp-nvim-lsp' - Plug 'hrsh7th/cmp-nvim-lua' - - " Snippets - Plug 'L3MON4D3/LuaSnip' - - " Snippet collection (Optional) - Plug 'rafamadriz/friendly-snippets' - Plug 'VonHeikemen/lsp-zero.nvim' + Plug 'VonHeikemen/lsp-zero.nvim', {'branch': 'v3.x'} " lsp-zero end call plug#end() " Lua Setup ======================================================================================== lua <', '(leap-forward-to)') vim.keymap.set({'n', 'x', 'o'}, '', '(leap-backward-to)') -- LSP Setup ===================================================================================== - local lsp = require('lsp-zero') local devenver_root = vim.fn.getenv('devenver_root') - lsp.preset('recommended') - lsp.configure('clangd', { - cmd = { - "clangd", - "-j", - "1", - "--background-index", - "--background-index-priority=background", - "--pch-storage=memory", - "--clang-tidy", - "--header-insertion=iwyu", - "--header-insertion-decorators", - } + local lsp_zero = require('lsp-zero') + lsp_zero.on_attach(function(client, bufnr) + -- see :help lsp-zero-keybindings + -- to learn the available actions + lsp_zero.default_keymaps({buffer = bufnr}) + local opts = {buffer = bufnr} + + vim.keymap.set({'n', 'x'}, 'gq', function() + vim.lsp.buf.format({async = false, timeout_ms = 10000}) + end, opts) + end) + + -- to learn how to use mason.nvim with lsp-zero + -- read this: https://github.com/VonHeikemen/lsp-zero.nvim/blob/v3.x/doc/md/guide/integrate-with-mason-nvim.md + require('mason').setup({}) + require('mason-lspconfig').setup({ + ensure_installed = { "clangd" }, + handlers = { + lsp_zero.default_setup, + }, }) - lsp.setup() + local cmp = require('cmp') + local cmp_action = require('lsp-zero').cmp_action() + local cmp_format = require('lsp-zero').cmp_format({details = true}) + + cmp.setup({ + sources = { + {name = 'nvim_lsp'}, + {name = 'buffer'}, + {name = 'path'}, + + }, + mapping = cmp.mapping.preset.insert({ + [''] = cmp_action.luasnip_supertab(), + [''] = cmp_action.luasnip_shift_supertab(), + [''] = cmp.mapping.confirm({select = false}), + }), + --- (Optional) Show source name in completion menu + formatting = cmp_format, + }) -- Treesitter ==================================================================================== -- TODO: 2022-06-19 Treesitter is too slow on large C++ files @@ -112,7 +123,7 @@ lua <-,trail:■,extends:»,precedes:«') vim.opt.number=true -- Show line numbers vim.opt.relativenumber=true -- Show relative line numbers @@ -131,17 +142,10 @@ lua <cc FzfCommits nnoremap cb FzfBCommits nnoremap b FzfBuffers -function! PadAndTruncateLineFunction() - let line = getline('.') - let line_length = strlen(line) - let padding = 100 - line_length - let padding = max([padding, 0]) - - let existing_space = line_length == 100 || match(line, ' $') != -1 - - " Construct the new line with padding and a space before the padding - let new_line = line . (existing_space ? '' : ' ') . repeat('=', padding - 1) - - call setline(line('.'), new_line) -endfunction -command! PadAndTruncateLine :call PadAndTruncateLineFunction() -nnoremap :PadAndTruncateLine - " Map Ctrl+HJKL to navigate buffer window nmap :wincmd h nmap :wincmd j