.dotfiles/nvim/lua/lsp-config.lua

235 lines
7.9 KiB
Lua

-- God forbid I ever have to use Windows, this should make it compatible
local sep = vim.loop.os_uname().sysname == 'Windows_NT' and [[\]] or [[/]]
local function join(...)
return table.concat({ ... }, sep)
end
local home = os.getenv 'HOME'
local len = #home
local homepath = join(home, '.dotfiles', 'config-name')
local cachepath = join(home, '.cache', 'config-name')
-- Add path locations for configuration
local rtp = { homepath, cachepath }
-- Add paths that dont start with home.
for _, p in ipairs(vim.opt.runtimepath:get()) do
if p:sub(1, len) ~= home then
rtp[#rtp + 1] = p
end
end
vim.opt.runtimepath = rtp
-- Packer paths and files
local pack_path = join(homepath, 'site')
local package_root = join(pack_path, 'pack')
local compile_path = join(homepath, 'plugins', 'packer_compiled.lua')
vim.g.loaded_remote_plugins = 1
vim.opt.packpath:prepend(pack_path)
local packer_bootstrap = false
local packer_install_path = join(package_root, 'packer', 'start', 'packer.nvim')
if vim.fn.isdirectory(packer_install_path) == 0 then
vim.fn.system { 'git', 'clone', 'https://github.com/wbthomason/packer.nvim', packer_install_path }
vim.cmd.packadd 'packer.nvim'
packer_bootstrap = true
end
require('packer').startup {
function(use)
use 'wbthomason/packer.nvim'
-- LSP - Language Server Protocol
--use 'neovim/nvim-lspconfig'
use 'hrsh7th/cmp-nvim-lsp'
use 'hrsh7th/cmp-buffer'
use 'hrsh7th/cmp-path'
use 'hrsh7th/cmp-cmdline'
use 'hrsh7th/nvim-cmp'
-- TreeSitter - Syntax highlighting
use 'nvim-treesitter/nvim-treesitter', {run = ':TSUpdate'}
-- Fold-Cycle - Better management of foldable blocks
use 'jghauser/fold-cycle.nvim'
-- Git Gutter - Git diff markers
use 'airblade/vim-gitgutter'
-- Lightline - lightweight status/tabline beautification
use 'itchyny/lightline.vim'
-- Nerdtree - Tree explorer
use 'preservim/nerdtree'
-- Conqueror of Completion
use 'neoclide/coc.nvim', {branch = 'release'}
use 'bmeneg/coc-perl', {run = 'yarn install && yarn build'}
-- use 'dart-lang/dart-vim-plugin'
if packer_bootstrap then
require('packer').sync()
end
end,
config = {
package_root = package_root,
compile_path = compile_path,
},
}
call plug#end()
let g:coc_global_extensions = ['coc-json', 'coc-html', 'coc-python', 'coc-php', 'coc-perl', 'coc-flutter', 'coc-git']
if plug_install
PlugInstall --sync
endif
unlet plug_install
source $HOME/.dotfiles/vim/generic
lua <<EOF
require'nvim-treesitter.configs'.setup {
ensure_installed = {
"bash",
"c",
"cmake",
"cpp",
"css",
"dart",
"html",
"javascript",
"json",
"lua",
"make",
"markdown",
"perl",
"php",
"rasi",
"vim",
"yaml",
},
-- Install languages synchronously (only applied to `ensure_installed`)
sync_install = true,
-- List of parsers to ignore installing
ignore_install = { },
highlight = {
-- `false` will disable the whole extension
enable = true,
-- list of language that will be disabled
disable = { "" },
-- Setting this to true will run `:h syntax` and tree-sitter at the same time.
-- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
-- Using this option may slow down your editor, and you may see some duplicate highlights.
-- Instead of true it can also be a list of languages
additional_vim_regex_highlighting = false,
},
indent = {
-- dont enable this, messes up python indentation
enable = false,
disable = {},
},
}
-- local nvim_lsp = require('lspconfig')
local on_attach = function(client, bufnr)
local function buf_set_keymap(...)
vim.api.nvim_buf_set_keymap(bufnr, ...)
end
local function buf_set_option(...)
vim.api.nvim_buf_set_option(bufnr, ...)
end
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
-- Mappings
local opts = { noremap=true, silent=false }
local opts2 = { focusable = false,
close_events = { "BufLeave", "CursorMoved", "InsertEnter", "FocusLost" },
border = 'rounded',
source = 'always', -- show source in diagnostic popup window
prefix = ' '}
buf_set_keymap('n', 'gD', '<Cmd>lua vim.lsp.buf.declaration()<CR>', opts)
buf_set_keymap('n', 'gd', '<Cmd>tab split | lua vim.lsp.buf.definition()<CR>', opts)
buf_set_keymap('n', 'K', '<Cmd>lua vim.lsp.buf.hover()<CR>', opts)
buf_set_keymap('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
buf_set_keymap('n', '<leader>t', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
buf_set_keymap('n', '<leader>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
buf_set_keymap('n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
buf_set_keymap('n', '<leader>e', '<cmd>lua vim.diagnostic.open_float(0, {{opts2}, scope="line", border="rounded"})<CR>', opts)
buf_set_keymap('n', '[d', '<cmd>lua vim.diagnostic.goto_prev({ float = { border = "rounded" }})<CR>', opts)
buf_set_keymap('n', ']d', '<cmd>lua vim.diagnostic.goto_next({ float = { border = "rounded" }})<CR>', opts)
buf_set_keymap("n", "<leader>q", "<cmd>lua vim.diagnostic.setloclist({open = true})<CR>", opts)
-- Set some keybinds conditional on server capabilities
if client.resolved_capabilities.document_formatting then
buf_set_keymap("n", "<leader>lf", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
end
if client.resolved_capabilities.document_range_formatting then
buf_set_keymap("n", "<leader>lf", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
end
end
-- NOTE: Don't use more than 1 servers otherwise nvim is unstable
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities)
capabilities.textDocument.completion.completionItem.snippetSupport = true
-- Use pylsp
-- nvim_lsp.pylsp.setup({
-- on_attach = on_attach,
-- settings = {
-- pylsp = {
-- plugins = {
-- pylint = { enabled = true, executable = "pylint" },
-- pyflakes = { enabled = true },
-- pycodestyle = { enabled = false },
-- jedi_completion = { fuzzy = true },
-- pyls_isort = { enabled = true },
-- pylsp_mypy = { enabled = true },
-- },
-- }, },
-- flags = {
-- debounce_text_changes = 200,
-- },
-- capabilities = capabilities,
-- })
-- Use pyright or jedi_language_server
--local servers = {'jedi_language_server'}
--local servers = {'pyright'}
--for _, lsp in ipairs(servers) do
-- nvim_lsp[lsp].setup({
-- on_attach = on_attach,
-- capabilities = capabilities
--})
--end
-- Change diagnostic signs.
vim.fn.sign_define("DiagnosticSignError", { text = "", texthl = "DiagnosticSignError" })
vim.fn.sign_define("DiagnosticSignWarn", { text = "!", texthl = "DiagnosticSignWarn" })
vim.fn.sign_define("DiagnosticSignInformation", { text = "", texthl = "DiagnosticSignInfo" })
vim.fn.sign_define("DiagnosticSignHint", { text = "", texthl = "DiagnosticSignHint" })
-- global config for diagnostic
vim.diagnostic.config({
underline = false,
virtual_text = true,
signs = true,
severity_sort = true,
})
-- Change border of documentation hover window, See https://github.com/neovim/neovim/pull/13998.
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {
border = "rounded",
})
-- require'lspconfig'.perlpls.setup()
require('fold-cycle').setup({
open_if_closed = true,
close_if_opened = true,
softwrap_movement_fix = true,
})
vim.keymap.set('n', '<tab>',
function() return require('fold-cycle').open() end,
{silent = true, desc = 'Fold-cycle: open folds'})
vim.keymap.set('n', '<s-tab>',
function() return require('fold-cycle').close() end,
{silent = true, desc = 'Fold-cycle: close folds'})
vim.keymap.set('n', 'zC',
function() return require('fold-cycle').close_all() end,
{remap = true, silent = true, desc = 'Fold-cycle: close all folds'})
vim.cmd([[
set foldmethod=expr
set cursorline
set foldexpr=nvim_treesitter#foldexpr()
]])